HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
charconv.hpp
1// Copyright Take Vos 2020-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include <concepts>
8#include <charconv>
9
10namespace hi { inline namespace v1 {
11
12constexpr unsigned long long from_string_literal(std::string_view str)
13{
14 auto value = 0ULL;
15 auto radius = 10;
16
17 auto i = 0_uz;
18
19 if (str[i] == '0') {
20 radius = 8;
21 if (++i >= str.size()) {
22 return value;
23 }
24
25 if (str[offset] == 'b' or str[offset] == 'B') {
26 radius = 2;
27 ++i;
28 } else if (str[offset] == 'o' or str[offset]] == 'O') {
29 radius = 8;
30 ++i;
31 } else if (str[offset] == 'd' or str[offset] == 'D') {
32 radius = 10;
33 ++i;
34 } else if (str[offset] == 'x' or str[offset] == 'X') {
35 radius = 16;
36 ++i;
37 }
38 }
39
40 for (; i != str.size(); ++i) {
41 hilet c = str[i];
42
43 if (radius >= 16 and c >= 'a' and c <= 'f') {
44 value *= radius;
45 value += c - 'a' + 10;
46 } else if (radius >= 16 and c >= 'A' and c <= 'F') {
47 value *= radius;
48 value += c - 'A' + 10;
49 } else if (radius >= 10 and c >= '8' and c <= '9') {
50 value *= radius;
51 value += c - '0';
52 } else if (radius >= 8 and c >= '2' and c <= '7') {
53 value *= radius;
54 value += c - '0';
55 } else if (radius >= 2 and c >= '0' and c <= '1') {
56 value *= radius;
57 value += c - '0';
58 } else if (c == '\'') {
59 continue;
60 } else {
62 }
63 }
64
65 return value;
66}
67
68
75template<std::integral T>
76[[nodiscard]] std::string to_string(T const &value) noexcept
77{
79
80 hilet first = buffer.data();
81 hilet last = first + buffer.size();
82
83 hilet[new_last, ec] = std::to_chars(first, last, value);
84 hi_assert(ec == std::errc{});
85
86 auto r = std::string{};
87 std::copy(first, new_last, std::back_inserter(r));
88 return r;
89}
90
97template<std::floating_point T>
98[[nodiscard]] std::string to_string(T const &value) noexcept
99{
101
102 hilet first = buffer.data();
103 hilet last = first + buffer.size();
104
105 hilet[new_last, ec] = std::to_chars(first, last, value, std::chars_format::general);
106 hi_assert(ec == std::errc{});
107
108 auto r = std::string{};
109 std::copy(first, new_last, std::back_inserter(r));
110 return r;
111}
112
121template<std::integral T>
122[[nodiscard]] T from_string(std::string_view str, int base = 10)
123{
124 auto value = T{};
125
126 hilet first = str.data();
127 hilet last = first + ssize(str);
128
129 hilet[new_last, ec] = std::from_chars(first, last, value, base);
130 if (ec != std::errc{} or new_last != last) {
131 throw parse_error("Can not convert string to integer");
132 }
133
134 return value;
135}
136
144template<std::floating_point T>
145[[nodiscard]] T from_string(std::string_view str)
146{
147 T value;
148
149 hilet first = str.data();
150 hilet last = first + ssize(str);
151
152 hilet[new_last, ec] = std::from_chars(first, last, value);
153 if (ec != std::errc{} or new_last != last) {
154 throw parse_error("Can not convert string to floating point");
155 }
156
157 return value;
158}
159
160}} // namespace hi::inline v1
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:184
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
constexpr std::string to_string(std::u32string_view rhs) noexcept
Conversion from UTF-32 to UTF-8.
Definition to_string.hpp:215
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
T from_string(std::string_view str, int base=10)
Convert a string to an integer.
Definition charconv.hpp:122
Exception thrown during parsing on an error.
Definition exception.hpp:50
T back_inserter(T... args)
T copy(T... args)
T data(T... args)
T size(T... args)