diff options
Diffstat (limited to 'server/src/util.cppm')
| -rw-r--r-- | server/src/util.cppm | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/server/src/util.cppm b/server/src/util.cppm index 2857fe5..986e350 100644 --- a/server/src/util.cppm +++ b/server/src/util.cppm | |||
| @@ -20,14 +20,9 @@ constexpr auto parse_double( | |||
| 20 | { | 20 | { |
| 21 | auto x = 0.0; | 21 | auto x = 0.0; |
| 22 | auto [_, ec] = std::from_chars(s.data(), s.data() + s.size(), x, fmt); | 22 | auto [_, ec] = std::from_chars(s.data(), s.data() + s.size(), x, fmt); |
| 23 | if (ec == std::errc{}) | 23 | if (ec != std::errc{}) |
| 24 | { | ||
| 25 | return x; | ||
| 26 | } | ||
| 27 | else | ||
| 28 | { | ||
| 29 | return std::nullopt; | 24 | return std::nullopt; |
| 30 | } | 25 | return x; |
| 31 | } | 26 | } |
| 32 | 27 | ||
| 33 | constexpr auto parse_float( | 28 | constexpr auto parse_float( |
| @@ -37,14 +32,20 @@ constexpr auto parse_float( | |||
| 37 | { | 32 | { |
| 38 | auto x = 0.0; | 33 | auto x = 0.0; |
| 39 | auto [_, ec] = std::from_chars(s.data(), s.data() + s.size(), x, fmt); | 34 | auto [_, ec] = std::from_chars(s.data(), s.data() + s.size(), x, fmt); |
| 40 | if (ec == std::errc{}) | 35 | if (ec != std::errc{}) |
| 41 | { | ||
| 42 | return x; | ||
| 43 | } | ||
| 44 | else | ||
| 45 | { | ||
| 46 | return std::nullopt; | 36 | return std::nullopt; |
| 47 | } | 37 | return x; |
| 38 | } | ||
| 39 | |||
| 40 | export template <std::integral T> | ||
| 41 | constexpr auto parse_int(std::string_view s, int base = 10) noexcept | ||
| 42 | -> std::optional<T> | ||
| 43 | { | ||
| 44 | auto x = T{}; | ||
| 45 | auto [_, ec] = std::from_chars(s.data(), s.data() + s.size(), x, base); | ||
| 46 | if (ec != std::errc{}) | ||
| 47 | return std::nullopt; | ||
| 48 | return x; | ||
| 48 | } | 49 | } |
| 49 | 50 | ||
| 50 | template <class T> | 51 | template <class T> |
| @@ -107,10 +108,14 @@ public: | |||
| 107 | { | 108 | { |
| 108 | } | 109 | } |
| 109 | 110 | ||
| 111 | constexpr zstring_view(std::string s) : zstring_view{s.c_str(), s.length()} {} | ||
| 112 | |||
| 110 | constexpr auto length() const -> std::size_t { return length_; } | 113 | constexpr auto length() const -> std::size_t { return length_; } |
| 111 | 114 | ||
| 112 | constexpr auto c_str() const -> char const* { return s_; } | 115 | constexpr auto c_str() const -> char const* { return s_; } |
| 113 | 116 | ||
| 117 | constexpr auto empty() const -> bool { return length() == 0; } | ||
| 118 | |||
| 114 | constexpr operator std::string_view() const | 119 | constexpr operator std::string_view() const |
| 115 | { | 120 | { |
| 116 | return std::string_view{s_, length_}; | 121 | return std::string_view{s_, length_}; |
| @@ -239,3 +244,26 @@ public: | |||
| 239 | explicit not_null(lazy_zstring_view s) -> not_null<lazy_zstring_view>; | 244 | explicit not_null(lazy_zstring_view s) -> not_null<lazy_zstring_view>; |
| 240 | 245 | ||
| 241 | } // namespace routemon::util | 246 | } // namespace routemon::util |
| 247 | |||
| 248 | template <> | ||
| 249 | struct std::formatter<routemon::util::zstring_view, char> | ||
| 250 | { | ||
| 251 | template <class ParseContext> | ||
| 252 | constexpr auto parse(ParseContext& ctx) -> ParseContext::iterator | ||
| 253 | { | ||
| 254 | auto it = ctx.begin(); | ||
| 255 | if (it != ctx.end() && *it != '}') | ||
| 256 | throw std::format_error{ | ||
| 257 | "unexpected formatting arguments for util::zstring_view" | ||
| 258 | }; | ||
| 259 | return it; | ||
| 260 | } | ||
| 261 | |||
| 262 | template <class FmtContext> | ||
| 263 | auto format(routemon::util::zstring_view s, FmtContext& ctx) const | ||
| 264 | -> FmtContext::iterator | ||
| 265 | { | ||
| 266 | return std::ranges::copy(std::string_view{s.c_str(), s.length()}, ctx.out()) | ||
| 267 | .out; | ||
| 268 | } | ||
| 269 | }; | ||