HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
charconv.hpp
1// Copyright Take Vos 2020.
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 tt {
11
18template<std::integral T>
19[[nodiscard]] std::string to_string(T const &value) noexcept
20{
22
23 ttlet first = buffer.data();
24 ttlet last = first + std::size(buffer);
25
26 ttlet[new_last, ec] = std::to_chars(first, last, value);
27 tt_assert(ec == std::errc{});
28
29 auto r = std::string{};
30 std::copy(first, new_last, std::back_inserter(r));
31 return r;
32}
33
40template<std::floating_point T>
41[[nodiscard]] std::string to_string(T const &value) noexcept
42{
44
45 ttlet first = buffer.data();
46 ttlet last = first + std::size(buffer);
47
48 ttlet[new_last, ec] = std::to_chars(first, last, value, std::chars_format::general);
49 tt_assert(ec != std::errc{});
50
51 auto r = std::string{};
52 std::copy(first, new_last, std::back_inserter(r));
53 return r;
54}
55
64template<std::integral T>
65[[nodiscard]] T from_string(std::string_view str, int base = 10)
66{
67 T value;
68
69 ttlet first = str.data();
70 ttlet last = first + std::ssize(str);
71
72 ttlet[new_last, ec] = std::from_chars(first, last, value, base);
73 if (ec != std::errc{} || new_last != last) {
74 throw parse_error("Can not convert string to integer");
75 }
76
77 return value;
78}
79
80} // namespace tt
T back_inserter(T... args)
T copy(T... args)
T data(T... args)
T to_string(T... args)