2025-06-23 16:22:55 +08:00
|
|
|
#pragma once
|
|
|
|
#include "../macro/feature_probe.hpp"
|
|
|
|
#include "../string/parse.hpp"
|
|
|
|
#include "panic.hpp"
|
|
|
|
#include "result.hpp"
|
|
|
|
|
2025-06-26 10:27:33 +08:00
|
|
|
#define NS_YYCC_STRING ::yycc::string
|
2025-06-23 16:22:55 +08:00
|
|
|
#define NS_YYCC_STRING_PARSE ::yycc::string::parse
|
2025-06-26 10:27:33 +08:00
|
|
|
#define NS_YYCC_RUST_RESULT ::yycc::rust::result
|
2025-06-23 16:22:55 +08:00
|
|
|
|
2025-06-26 10:27:33 +08:00
|
|
|
/**
|
|
|
|
* @namespace yycc::rust::parse
|
|
|
|
* @brief Provides Rust-inspired parsing utilities for converting strings to various types.
|
|
|
|
* @details
|
|
|
|
* This namespace contains template functions for parsing strings into different types
|
|
|
|
* (floating-point, integral, boolean) with Rust-like Result error handling.
|
|
|
|
*/
|
2025-06-23 16:22:55 +08:00
|
|
|
namespace yycc::rust::parse {
|
|
|
|
|
|
|
|
#if defined(YYCC_CPPFEAT_EXPECTED)
|
|
|
|
|
2025-06-26 10:27:33 +08:00
|
|
|
/// @brief The error type of parsing.
|
2025-06-23 16:22:55 +08:00
|
|
|
using Error = NS_YYCC_STRING_PARSE::ParseError;
|
|
|
|
|
2025-06-26 10:27:33 +08:00
|
|
|
/// @brief The result type of parsing.
|
|
|
|
/// @tparam T The expected value type in result.
|
|
|
|
template<typename T>
|
|
|
|
using Result = NS_YYCC_RUST_RESULT::Result<T, Error>;
|
2025-06-23 16:22:55 +08:00
|
|
|
|
2025-06-26 10:27:33 +08:00
|
|
|
/**
|
|
|
|
* @brief Parses a string into a floating-point value.
|
|
|
|
* @tparam T Floating-point type to parse into (float, double, etc.)
|
|
|
|
* @param strl String view to parse
|
|
|
|
* @param fmt Formatting flags for parsing (default: general)
|
|
|
|
* @return Result<T> containing either the parsed value or an error
|
|
|
|
*/
|
|
|
|
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
|
|
|
|
Result<T> parse(const NS_YYCC_STRING::u8string_view& strl,
|
|
|
|
std::chars_format fmt = std::chars_format::general) {
|
|
|
|
auto rv = NS_YYCC_STRING_PARSE::priv_parse<T>(strl, fmt);
|
2025-06-23 16:22:55 +08:00
|
|
|
|
2025-06-26 10:27:33 +08:00
|
|
|
if (const auto* ptr = std::get_if<T>(&rv)) {
|
|
|
|
return NS_YYCC_RUST_RESULT::Ok<Result<T>>(*ptr);
|
|
|
|
} else if (const auto* ptr = std::get_if<Error>(&rv)) {
|
|
|
|
return NS_YYCC_RUST_RESULT::Err<Result<T>>(*ptr);
|
|
|
|
} else {
|
|
|
|
// Unreachable
|
|
|
|
RS_PANIC("unreachable code.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parses a string into an integral value (excluding bool).
|
|
|
|
* @tparam T Integral type to parse into (int, long, etc.)
|
|
|
|
* @param strl String view to parse
|
|
|
|
* @param base Numeric base for parsing (default: 10)
|
|
|
|
* @return Result<T> containing either the parsed value or an error
|
|
|
|
*/
|
|
|
|
template<typename T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>, int> = 0>
|
|
|
|
Result<T> parse(const NS_YYCC_STRING::u8string_view& strl, int base = 10) {
|
|
|
|
auto rv = NS_YYCC_STRING_PARSE::priv_parse<T>(strl, base);
|
|
|
|
|
|
|
|
if (const auto* ptr = std::get_if<T>(&rv)) {
|
|
|
|
return NS_YYCC_RUST_RESULT::Ok<Result<T>>(*ptr);
|
|
|
|
} else if (const auto* ptr = std::get_if<Error>(&rv)) {
|
|
|
|
return NS_YYCC_RUST_RESULT::Err<Result<T>>(*ptr);
|
|
|
|
} else {
|
|
|
|
// Unreachable
|
|
|
|
RS_PANIC("unreachable code.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parses a string into a boolean value.
|
|
|
|
* @tparam T Must be bool type
|
|
|
|
* @param strl String view to parse
|
|
|
|
* @return Result<bool> containing either the parsed value or an error
|
|
|
|
*/
|
|
|
|
template<typename T, std::enable_if_t<std::is_same_v<T, bool>, int> = 0>
|
|
|
|
Result<T> parse(const NS_YYCC_STRING::u8string_view& strl) {
|
|
|
|
auto rv = NS_YYCC_STRING_PARSE::priv_parse<T>(strl);
|
|
|
|
|
|
|
|
if (const auto* ptr = std::get_if<T>(&rv)) {
|
|
|
|
return NS_YYCC_RUST_RESULT::Ok<Result<T>>(*ptr);
|
|
|
|
} else if (const auto* ptr = std::get_if<Error>(&rv)) {
|
|
|
|
return NS_YYCC_RUST_RESULT::Err<Result<T>>(*ptr);
|
|
|
|
} else {
|
|
|
|
// Unreachable
|
|
|
|
RS_PANIC("unreachable code.");
|
|
|
|
}
|
|
|
|
}
|
2025-06-23 16:22:55 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-06-26 10:27:33 +08:00
|
|
|
#undef NS_YYCC_RUST_RESULT
|
2025-06-23 16:22:55 +08:00
|
|
|
#undef NS_YYCC_STRING_PARSE
|
2025-06-26 10:27:33 +08:00
|
|
|
#undef NS_YYCC_STRING
|