My Project
char_traits.hpp
Go to the documentation of this file.
1 
28 #ifndef BSL_CHAR_TRAITS_HPP
29 #define BSL_CHAR_TRAITS_HPP
30 
31 #include "char_type.hpp"
32 #include "cstdint.hpp"
33 #include "cstring.hpp"
34 
35 namespace bsl
36 {
38 
49  template<typename CharT>
50  class char_traits final
51  {};
52 
54 
69  template<>
70  class char_traits<char_type> final
71  {
72  public:
82  [[nodiscard]] static constexpr bool
83  eq(char_type const a, char_type const b) noexcept
84  {
85  return a == b;
86  }
87 
97  [[nodiscard]] static constexpr bool
98  lt(char_type const a, char_type const b) noexcept
99  {
100  return a < b;
101  }
102 
124  [[nodiscard]] static constexpr bsl::int32
125  compare( // --
126  char_type const *const s1, // --
127  char_type const *const s2, // --
128  bsl::uintmax const count) noexcept
129  {
130  return bsl::builtin_strncmp(s1, s2, count);
131  }
132 
145  [[nodiscard]] static constexpr bsl::uintmax
146  length(char_type const *const s) noexcept
147  {
148  return bsl::builtin_strlen(s);
149  }
150 
166  [[nodiscard]] static constexpr char_type const *
167  find(char_type const *const p, bsl::uintmax const count, char_type const &ch) noexcept
168  {
169  return bsl::builtin_strnchr(p, ch, count);
170  }
171 
182  [[nodiscard]] static constexpr char_type
183  to_char_type(bsl::intmax const c) noexcept
184  {
185  return static_cast<char_type>(c);
186  }
187 
196  [[nodiscard]] static constexpr bsl::intmax
197  to_int_type(char_type const c) noexcept
198  {
199  return static_cast<bsl::intmax>(c);
200  }
201 
213  [[nodiscard]] static constexpr bool
214  eq_int_type(bsl::intmax const c1, bsl::intmax const c2) noexcept
215  {
216  if ((eof() == c1) && (eof() == c2)) {
217  return true;
218  }
219 
220  return eq(to_char_type(c1), to_char_type(c2));
221  }
222 
230  [[nodiscard]] static constexpr bsl::intmax
231  eof() noexcept
232  {
233  constexpr bsl::intmax value_of_eof{-1};
234  return value_of_eof;
235  }
236 
245  [[nodiscard]] static constexpr bsl::intmax
246  not_eof(bsl::intmax const e) noexcept
247  {
248  if (!eq_int_type(e, eof())) {
249  return e;
250  }
251 
252  return 0;
253  }
254  };
255 }
256 
257 #endif
static constexpr char_type const * find(char_type const *const p, bsl::uintmax const count, char_type const &ch) noexcept
Returns a pointer to the first occurrence of "ch" in "p".
Definition: char_traits.hpp:167
static constexpr bool lt(char_type const a, char_type const b) noexcept
Returns true if "a" < "b".
Definition: char_traits.hpp:98
static constexpr bsl::uintmax length(char_type const *const s) noexcept
Returns the length of the provided string.
Definition: char_traits.hpp:146
static constexpr bool eq_int_type(bsl::intmax const c1, bsl::intmax const c2) noexcept
Checks whether two values of type int_type are equal.
Definition: char_traits.hpp:214
static constexpr bsl::intmax not_eof(bsl::intmax const e) noexcept
Returns e if e is not EOF, otherwise returns 0.
Definition: char_traits.hpp:246
::intmax_t intmax
defines a signed integer with the maximum possible size
Definition: cstdint.hpp:95
constexpr bsl::int32 builtin_strncmp(cstr_type const lhs, cstr_type const rhs, bsl::uintmax const count) noexcept
Same as std::strncmp with parameter checks. If lhs, rhs are a nullptr, or count is 0,...
Definition: cstring.hpp:132
static constexpr bsl::intmax to_int_type(char_type const c) noexcept
Converts a value of char_type to bsl::intmax.
Definition: char_traits.hpp:197
static constexpr char_type to_char_type(bsl::intmax const c) noexcept
Converts a value of bsl::intmax to char_type. If there is no equivalent value (such as when c is a co...
Definition: char_traits.hpp:183
static constexpr bsl::int32 compare(char_type const *const s1, char_type const *const s2, bsl::uintmax const count) noexcept
Compares two strings. Returns negative value if s1 appears before s2 in lexicographical order....
Definition: char_traits.hpp:125
::int32_t int32
defines an 32bit signed integer
Definition: cstdint.hpp:40
char char_type
Standard Char Type.
Definition: char_type.hpp:41
static constexpr bsl::intmax eof() noexcept
Returns the value of EOF.
Definition: char_traits.hpp:231
constexpr cstr_type builtin_strnchr(cstr_type const str, char_type const ch, bsl::uintmax const count) noexcept
Same as std::strnchr with parameter checks. If str is a nullptr, or count is 0, this function returns...
Definition: cstring.hpp:176
constexpr bsl::uintmax builtin_strlen(cstr_type const str) noexcept
Same as std::strlen with parameter checks. If str is a nullptr, this returns 0.
Definition: cstring.hpp:154
static constexpr bool eq(char_type const a, char_type const b) noexcept
Returns true if "a" == "b".
Definition: char_traits.hpp:83
::uintmax_t uintmax
defines a unsigned integer with the maximum possible size
Definition: cstdint.hpp:97