/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/common/fixed_string.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/common/fixed_string.hh Source File
DiFfRG
fixed_string.hh
Go to the documentation of this file.
1#pragma once
2
3#include <initializer_list>
4
5namespace DiFfRG
6{
12 template <unsigned N> struct FixedString {
13 char buf[N + 1]{};
14
21 constexpr FixedString(char const *s)
22 {
23 for (unsigned i = 0; i != N; ++i)
24 buf[i] = s[i];
25 }
26
27 constexpr FixedString(std::initializer_list<char> s)
28 {
29 unsigned i = 0;
30 for (auto c : s) {
31 buf[i] = c;
32 ++i;
33 }
34 }
35
36 constexpr FixedString(char const (&arr)[N])
37 {
38 for (unsigned i = 0; i < N; ++i)
39 buf[i] = arr[i];
40 }
41
42 constexpr operator char const *() const { return buf; }
43
44 auto operator<=>(const FixedString &) const = default;
45 };
46
47 template <unsigned N> FixedString(char const (&)[N]) -> FixedString<N - 1>;
48
49 template <unsigned N1, unsigned N2> consteval bool strings_equal(FixedString<N1> s1, FixedString<N2> s2)
50 {
51 if (N1 != N2) return false;
52 for (unsigned i = 0; i < N1; ++i)
53 if (s1.buf[i] != s2.buf[i]) return false;
54 return true;
55 }
56} // namespace DiFfRG
Definition complex_math.hh:10
consteval bool strings_equal(FixedString< N1 > s1, FixedString< N2 > s2)
Definition fixed_string.hh:49
FixedString(char const (&)[N]) -> FixedString< N - 1 >
A fixed size compile-time string.
Definition fixed_string.hh:12
char buf[N+1]
Definition fixed_string.hh:13
constexpr FixedString(std::initializer_list< char > s)
Definition fixed_string.hh:27
auto operator<=>(const FixedString &) const =default
constexpr FixedString(char const (&arr)[N])
Definition fixed_string.hh:36
constexpr FixedString(char const *s)
Construct a new Fixed String object.
Definition fixed_string.hh:21