AsmGrader 0.0.0
Loading...
Searching...
No Matches
static_string.hpp
Go to the documentation of this file.
1// Heavily inspired by: https://blog.ganets.ky/StaticString/
2// In accordance with above mentioned site's licensing:
3//
4// The MIT License (MIT)
5// Copyright (c) 2013-2018 Blackrock Digital LLC
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include <fmt/base.h>
28#include <fmt/compile.h>
29#include <fmt/format.h>
30#include <range/v3/algorithm/copy.hpp>
31#include <range/v3/algorithm/copy_n.hpp>
32#include <range/v3/algorithm/find.hpp>
33#include <range/v3/range/access.hpp>
34#include <range/v3/range/concepts.hpp>
35
36#include <array>
37#include <cstddef>
38#include <iterator>
39#include <string>
40#include <string_view>
41#include <utility>
42
43namespace asmgrader {
44
47template <std::size_t N>
49{
50public:
51 consteval StaticString() = default;
52
53 // NOLINTNEXTLINE(google-explicit-constructor,*-avoid-c-arrays)
54 consteval /*implicit*/ StaticString(const char (&input)[N + 1]) {
55 if (input[N] != '\0') {
56 throw;
57 }
58 ranges::copy_n(std::data(input), N + 1, data.begin());
59 }
60
61 // NOLINTNEXTLINE(google-explicit-constructor)
62 consteval /*implicit*/ StaticString(const ranges::forward_range auto& rng) {
63 ranges::copy_n(ranges::begin(rng), N + 1, data.begin());
64
65 if (data.back() != '\0') {
66 throw;
67 }
68 }
69
70 std::array<char, N + 1> data{};
71
72 consteval std::size_t size() const { return data.size(); }
73
74 // NOLINTNEXTLINE(google-explicit-constructor)
75 consteval /*implicit*/ operator std::string_view() const { return data.data(); }
76
77 std::string str() const { return data.data(); }
78
79 template <std::size_t OtherSize>
80 consteval auto operator<=>(const StaticString<OtherSize>& rhs) const {
81 return std::string_view{*this} <=> std::string_view{rhs};
82 }
83
84 // Support comparison with string_view-convertable objects
85 constexpr auto operator<=>(std::string_view rhs) const { return std::string_view{*this} <=> rhs; }
86
87 template <std::size_t OtherSize>
88 consteval bool operator==(const StaticString<OtherSize>& rhs) const {
89 return std::string_view{*this} == std::string_view{rhs};
90 }
91
92 // Support comparison with string_view-convertable objects
93 consteval bool operator==(std::string_view rhs) const { return std::string_view{*this} == rhs; }
94};
95
96// Deduction guide
97template <std::size_t N>
98// NOLINTNEXTLINE(*-avoid-c-arrays)
99StaticString(const char (&input)[N]) -> StaticString<N - 1>;
100
101static_assert(StaticString("abc") == "abc");
102static_assert("abc" == StaticString("abc"));
103static_assert("abc" == StaticString<3>(std::string_view{"abc"}));
104
105// GCC Bug until around version 14
106// See: https://godbolt.org/z/vKEPY1qWT
107#if !defined(__GNUC__) || __GNUC__ >= 14
108static_assert("abc" == std::string_view{StaticString("abc")});
109#endif
110
111template <StaticString Fmt, std::size_t MaxSz = 10 * 1'024, fmt::formattable... Args>
112consteval auto static_format(Args&&... args) {
113 constexpr auto COMPILED_FMT = FMT_COMPILE(Fmt.data.begin());
114
115 StaticString<MaxSz> result;
116
117 fmt::format_to(result.data.begin(), COMPILED_FMT, std::forward<Args>(args)...);
118
119 return result;
120}
121
122static_assert(static_format<"{0} + {0} = {1}">(1, 2) == "1 + 1 = 2");
123static_assert(static_format<"">(1, 2) == "");
124
125template <StaticString String>
126consteval auto operator""_static() {
127 return String;
128}
129
130} // namespace asmgrader
A fully compile-time capable string type Guaranteed to be null-terminated.
Definition static_string.hpp:49
consteval StaticString()=default
consteval StaticString(const ranges::forward_range auto &rng)
Definition static_string.hpp:62
constexpr auto operator<=>(std::string_view rhs) const
Definition static_string.hpp:85
consteval auto operator<=>(const StaticString< OtherSize > &rhs) const
Definition static_string.hpp:80
consteval StaticString(const char(&input)[N+1])
Definition static_string.hpp:54
consteval bool operator==(const StaticString< OtherSize > &rhs) const
Definition static_string.hpp:88
consteval std::size_t size() const
Definition static_string.hpp:72
std::string str() const
Definition static_string.hpp:77
consteval bool operator==(std::string_view rhs) const
Definition static_string.hpp:93
std::array< char, N+1 > data
Definition static_string.hpp:70
Definition asm_buffer.hpp:19
consteval auto static_format(Args &&... args)
Definition static_string.hpp:112
StaticString(const char(&input)[N]) -> StaticString< N - 1 >