- Refactored fennec::format to use to_chars for ints. floats TODO but will also use to_chars.
This commit is contained in:
147
source/format/charconv.cpp
Normal file
147
source/format/charconv.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
// =====================================================================================================================
|
||||
// fennec, a free and open source game engine
|
||||
// Copyright © 2025 Medusa Slockbower
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
// =====================================================================================================================
|
||||
|
||||
///
|
||||
/// \file charconv.h
|
||||
/// \brief
|
||||
///
|
||||
///
|
||||
/// \details
|
||||
/// \author Medusa Slockbower
|
||||
///
|
||||
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
|
||||
///
|
||||
///
|
||||
|
||||
#define __cpp_lib_to_chars
|
||||
#include <charconv>
|
||||
#include <system_error>
|
||||
#include <fennec/format/charconv.h>
|
||||
#include <fennec/lang/assert.h>
|
||||
|
||||
namespace fennec
|
||||
{
|
||||
|
||||
char* to_chars(char* first, char* last, char x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
char* to_chars(char* first, char* last, signed char x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
char* to_chars(char* first, char* last, unsigned char x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
char* to_chars(char* first, char* last, signed short x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
char* to_chars(char* first, char* last, unsigned short x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
char* to_chars(char* first, char* last, signed int x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
char* to_chars(char* first, char* last, unsigned int x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
char* to_chars(char* first, char* last, signed long x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
char* to_chars(char* first, char* last, unsigned long x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
char* to_chars(char* first, char* last, signed long long x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
char* to_chars(char* first, char* last, unsigned long long x, int base) {
|
||||
auto res = std::to_chars(first, last, x, base);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
|
||||
std::chars_format ctofmt(char c) {
|
||||
switch (c) {
|
||||
default: return std::chars_format::general;
|
||||
case 'a': case 'A': return std::chars_format::hex;
|
||||
case 'f': case 'F': return std::chars_format::fixed;
|
||||
case 'e': case 'E': return std::chars_format::scientific;
|
||||
}
|
||||
}
|
||||
|
||||
char* to_chars(char* first, char* last, float x) {
|
||||
auto res = std::to_chars(first, last, x);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
char* to_chars(char* first, char* last, float x, char fmt) {
|
||||
auto res = std::to_chars(first, last, x, ctofmt(fmt));
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
char* to_chars(char* first, char* last, float x, char fmt, int precision) {
|
||||
auto res = std::to_chars(first, last, x, ctofmt(fmt), precision);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
|
||||
char* to_chars(char* first, char* last, double x) {
|
||||
auto res = std::to_chars(first, last, x);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
char* to_chars(char* first, char* last, double x, char fmt) {
|
||||
auto res = std::to_chars(first, last, x, ctofmt(fmt));
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
char* to_chars(char* first, char* last, double x, char fmt, int precision) {
|
||||
auto res = std::to_chars(first, last, x, ctofmt(fmt), precision);
|
||||
assertf(res.ec == std::errc(), std::make_error_code(res.ec).message().c_str());
|
||||
return res.ptr;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user