roar
Loading...
Searching...
No Matches
fixed_string.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace Roar::Detail
4{
10 template <unsigned Size>
12 {
13 public:
14 constexpr FixedString()
15 {}
16 constexpr FixedString(char const* s)
17 {
18 for (unsigned i = 0; i != Size; ++i)
19 m_buffer[i] = s[i];
20 }
21 constexpr operator char const*() const
22 {
23 return m_buffer;
24 }
25
29 template <unsigned OtherSize>
30 constexpr int compare(FixedString<OtherSize> const& other) const
31 {
32 auto const* s1 = &m_buffer[0];
33 auto const* s2 = &other.m_buffer[0];
34 unsigned char c1, c2;
35 do
36 {
37 c1 = static_cast<unsigned char>(*s1++);
38 c2 = static_cast<unsigned char>(*s2++);
39 if (c1 == '\0')
40 {
41 return c1 - c2;
42 }
43 } while (c1 == c2);
44 return c1 - c2;
45 }
46
47 constexpr static auto m_size = Size;
48 char m_buffer[Size + 1]{};
49 };
50 template <unsigned N>
51 FixedString(char const (&)[N]) -> FixedString<N - 1>;
52
56 template <unsigned... Length>
57 constexpr auto FixConcat(const char (&... strings)[Length])
58 {
59 constexpr unsigned Count = (... + Length) - sizeof...(Length);
60 FixedString<Count + 1> result = {};
61 result.m_buffer[Count] = '\0';
62
63 char* dst = result.m_buffer;
64 for (const char* src : {strings...})
65 {
66 for (; *src != '\0'; ++src, ++dst)
67 {
68 *dst = *src;
69 }
70 }
71 return result;
72 }
73}
Utilitarian class to store and modify strings in compile time.
Definition fixed_string.hpp:12
constexpr FixedString(char const *s)
Definition fixed_string.hpp:16
constexpr int compare(FixedString< OtherSize > const &other) const
glibc strcmp implementation.
Definition fixed_string.hpp:30
static constexpr auto m_size
Definition fixed_string.hpp:47
char m_buffer[Size+1]
Definition fixed_string.hpp:48
constexpr FixedString()
Definition fixed_string.hpp:14
Definition range_file_body.hpp:18
constexpr auto FixConcat(const char(&... strings)[Length])
Allows for compile-time string concatenation. Use very sparingly.
Definition fixed_string.hpp:57