refactor: update C++ from 17 to 23

This commit is contained in:
2025-07-25 09:35:26 +08:00
parent f014e54604
commit 4f0b3d19d1
23 changed files with 335 additions and 1139 deletions

View File

@ -1,12 +1,11 @@
#pragma once
#include "../string.hpp"
#include "../string/reinterpret.hpp"
#include <string>
#include <array>
#include <type_traits>
#include <charconv>
#include <stdexcept>
#include <type_traits>
#define NS_YYCC_STRING ::yycc::string
#define NS_YYCC_STRING_REINTERPRET ::yycc::string::reinterpret
/**
@ -25,7 +24,7 @@ namespace yycc::num::stringify {
inline constexpr size_t STRINGIFY_BUFFER_SIZE = 64u;
/// @private
/// @brief Type alias for the buffer used in string conversion.
using StringifyBuffer = std::array<NS_YYCC_STRING::u8char, STRINGIFY_BUFFER_SIZE>;
using StringifyBuffer = std::array<char8_t, STRINGIFY_BUFFER_SIZE>;
/**
* @brief Return the string representation of given floating point value.
@ -35,10 +34,9 @@ namespace yycc::num::stringify {
* @param[in] precision The floating point precision used when getting string representation.
* @return The string representation of given value.
*/
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
NS_YYCC_STRING::u8string stringify(T num,
std::chars_format fmt = std::chars_format::general,
int precision = 6) {
template<typename T>
requires(std::is_floating_point_v<T>)
std::u8string stringify(T num, std::chars_format fmt = std::chars_format::general, int precision = 6) {
namespace reinterpret = NS_YYCC_STRING_REINTERPRET;
StringifyBuffer buffer;
@ -48,8 +46,7 @@ namespace yycc::num::stringify {
fmt,
precision);
if (ec == std::errc()) {
return NS_YYCC_STRING::u8string(buffer.data(),
reinterpret::as_utf8(ptr) - buffer.data());
return std::u8string(buffer.data(), reinterpret::as_utf8(ptr) - buffer.data());
} else if (ec == std::errc::value_too_large) {
// Too short buffer. This should not happen.
throw std::out_of_range("stringify() buffer is not sufficient.");
@ -65,18 +62,18 @@ namespace yycc::num::stringify {
* @param[in] base Integer base used when getting string representation: a value between 2 and 36 (inclusive).
* @return The string representation of given value.
*/
template<typename T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>, int> = 0>
NS_YYCC_STRING::u8string stringify(T num, int base = 10) {
template<typename T>
requires(std::is_integral_v<T> && !std::is_same_v<T, bool>)
std::u8string stringify(T num, int base = 10) {
namespace reinterpret = NS_YYCC_STRING_REINTERPRET;
StringifyBuffer buffer;
auto [ptr, ec] = std::to_chars(reinterpret::as_ordinary(buffer.data()),
reinterpret::as_ordinary(buffer.data() + buffer.size()),
num,
base);
if (ec == std::errc()) {
return NS_YYCC_STRING::u8string(buffer.data(),
reinterpret::as_utf8(ptr) - buffer.data());
return std::u8string(buffer.data(), reinterpret::as_utf8(ptr) - buffer.data());
} else if (ec == std::errc::value_too_large) {
// Too short buffer. This should not happen.
throw std::out_of_range("stringify() buffer is not sufficient.");
@ -91,13 +88,13 @@ namespace yycc::num::stringify {
* @param[in] num The value need to get string representation.
* @return The string representation of given value ("true" or "false").
*/
template<typename T, std::enable_if_t<std::is_same_v<T, bool>, int> = 0>
NS_YYCC_STRING::u8string stringify(T num) {
if (num) return NS_YYCC_STRING::u8string(YYCC_U8("true"));
else return NS_YYCC_STRING::u8string(YYCC_U8("false"));
template<typename T>
requires(std::is_same_v<T, bool>)
std::u8string stringify(T num) {
if (num) return std::u8string(u8"true");
else return std::u8string(u8"false");
}
} // namespace yycc::string::stringify
} // namespace yycc::num::stringify
#undef NS_YYCC_STRING_REINTERPRET
#undef NS_YYCC_STRING