Nui
fixed_string.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 namespace Nui
4 {
6  {};
7 
13  template <unsigned Size>
15  {
16  public:
17  constexpr FixedString()
18  {}
19  constexpr FixedString(char const* s)
20  {
21  for (unsigned i = 0; i != Size; ++i)
22  m_buffer[i] = s[i];
23  }
24  constexpr FixedString(char const* s, FixToLowerFlag)
25  {
26  for (unsigned i = 0; i != Size; ++i)
27  {
28  if (s[i] >= 'A' && s[i] <= 'Z')
29  m_buffer[i] = s[i] - 'A' + 'a';
30  else
31  m_buffer[i] = s[i];
32  }
33  }
34  constexpr operator char const*() const
35  {
36  return m_buffer;
37  }
38 
42  template <unsigned OtherSize>
43  constexpr int compare(FixedString<OtherSize> const& other) const
44  {
45  auto const* s1 = &m_buffer[0];
46  auto const* s2 = &other.m_buffer[0];
47  unsigned char c1, c2;
48  do
49  {
50  c1 = static_cast<unsigned char>(*s1++);
51  c2 = static_cast<unsigned char>(*s2++);
52  if (c1 == '\0')
53  {
54  return c1 - c2;
55  }
56  } while (c1 == c2);
57  return c1 - c2;
58  }
59 
60  constexpr static auto m_size = Size;
61  char m_buffer[Size + 1]{};
62  };
63  template <unsigned N>
64  FixedString(char const (&)[N]) -> FixedString<N - 1>;
65 
69  template <unsigned... Length>
70  constexpr auto fixConcat(const char (&... strings)[Length])
71  {
72  constexpr unsigned Count = (... + Length) - sizeof...(Length);
73  FixedString<Count + 1> result = {};
74  result.m_buffer[Count] = '\0';
75 
76  char* dst = result.m_buffer;
77  for (const char* src : {strings...})
78  {
79  for (; *src != '\0'; ++src, ++dst)
80  {
81  *dst = *src;
82  }
83  }
84  return result;
85  }
86 
87  template <unsigned N>
88  constexpr auto fixToLower(FixedString<N> base)
89  {
90  FixedString<N> result;
91  for (unsigned i = 0; i != N; ++i)
92  {
93  if (base.m_buffer[i] >= 'A' && base.m_buffer[i] <= 'Z')
94  result.m_buffer[i] = base.m_buffer[i] - 'A' + 'a';
95  else
96  result.m_buffer[i] = base.m_buffer[i];
97  }
98  return result;
99  }
100 
101  template <unsigned N>
102  constexpr auto fixToLower(char const (&str)[N])
103  {
104  return FixedString<N>{str, FixToLowerFlag{}};
105  }
106 }
Utilitarian class to store and modify strings in compile time.
Definition: fixed_string.hpp:15
constexpr FixedString(char const *s, FixToLowerFlag)
Definition: fixed_string.hpp:24
constexpr FixedString(char const *s)
Definition: fixed_string.hpp:19
char m_buffer[Size+1]
Definition: fixed_string.hpp:61
constexpr static auto m_size
Definition: fixed_string.hpp:60
constexpr int compare(FixedString< OtherSize > const &other) const
glibc strcmp implementation.
Definition: fixed_string.hpp:43
constexpr FixedString()
Definition: fixed_string.hpp:17
Definition: file_dialog.hpp:6
constexpr auto fixConcat(const char(&... strings)[Length])
Allows for compile-time string concatenation.
Definition: fixed_string.hpp:70
FixedString(char const (&)[N]) -> FixedString< N - 1 >
constexpr auto fixToLower(FixedString< N > base)
Definition: fixed_string.hpp:88
Definition: fixed_string.hpp:6