NRP Core  1.4.1
fixed_string.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef FIXED_STRING_H
23 #define FIXED_STRING_H
24 
25 #include <string>
26 #include <string_view>
27 
28 template<std::size_t N>
30 {
31  static constexpr auto Length = N;
32 
33  constexpr FixedString(const char(&str)[N])
34  {
35  for(unsigned i = 0; i != N; ++i)
36  m_data[i] = str[i];
37  }
38 
39  constexpr FixedString(const FixedString<N> &str) = default;
40 
41  template<class T>
42  constexpr FixedString(T str)
44  {
45  static_assert(N==T::Length, "String mismatch");
46  }
47 
48  constexpr FixedString()
49  {}
50 
51  //auto operator<=>(const basic_fixed_string &, const basic_fixed_string &) = default;
52 
53  char m_data[N]{};
54  constexpr operator auto() const
55  { return m_data; }
56 
60  constexpr operator int() const
61  { return Length; }
62 
63  constexpr operator std::string_view() const
64  { return std::string_view(m_data); }
65 
66  constexpr operator std::string() const
67  { return std::string(m_data); }
68 
69  constexpr const char *data() const
70  { return m_data; }
71 
72  template<std::size_t M>
73  constexpr bool compare(const char(&other)[M]) const
74  {
75  const auto l = N<M ? N : M;
76  for(std::size_t i = 0; i < l; ++i)
77  {
78  const auto cmp = other[i] - m_data[i];
79  if(cmp)
80  return cmp;
81  }
82 
83  return M-N;
84  }
85 
86  constexpr bool compare(const char *const str) const
87  {
88  std::size_t i = 0;
89  for(; i < N; ++i)
90  {
91  const auto cmp = str[i] - m_data[i];
92  if(cmp)
93  return cmp;
94 
95  if(str == 0)
96  break;
97  }
98 
99  return i-N;
100  }
101 
102  constexpr bool compare(const std::string_view &str) const
103  { return this->compare(str.data()); }
104 };
105 
106 template<std::size_t N>
107 FixedString(const char (&str)[N]) -> FixedString<N>;
108 
109 template<std::size_t N>
111 
113 
114 template<class T>
116 
117 #endif // FIXED_STRING_H
FixedString::Length
static constexpr auto Length
Definition: fixed_string.h:31
FixedString::compare
constexpr bool compare(const char(&other)[M]) const
Definition: fixed_string.h:73
FixedString::data
constexpr const char * data() const
Definition: fixed_string.h:69
FixedString::FixedString
constexpr FixedString(const char(&str)[N])
Definition: fixed_string.h:33
FixedString::FixedString
constexpr FixedString()
Definition: fixed_string.h:48
FixedString::compare
constexpr bool compare(const std::string_view &str) const
Definition: fixed_string.h:102
FixedString::FixedString
constexpr FixedString(T str)
Definition: fixed_string.h:42
FixedString
Definition: fixed_string.h:29
python_grpc_engine.str
str
Definition: python_grpc_engine.py:63
FixedString::compare
constexpr bool compare(const char *const str) const
Definition: fixed_string.h:86
FixedString::m_data
char m_data[N]
Definition: fixed_string.h:53
FixedString
FixedString(const char(&str)[N]) -> FixedString< N >