CsString  1.4.0
cs_char.h
1 /***********************************************************************
2 *
3 * Copyright (c) 2017-2024 Barbara Geller
4 * Copyright (c) 2017-2024 Ansel Sermersheim
5 *
6 * This file is part of CsString.
7 *
8 * CsString is free software, released under the BSD 2-Clause license.
9 * For license details refer to LICENSE provided with this project.
10 *
11 * CsString is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 *
15 * https://opensource.org/licenses/BSD-2-Clause
16 *
17 ***********************************************************************/
18 
19 #ifndef LIB_CS_CHAR_H
20 #define LIB_CS_CHAR_H
21 
22 #include <stdint.h>
23 
24 #include <functional>
25 #include <memory>
26 
27 namespace CsString {
28 
29 #ifdef _WIN32
30 
31 #ifdef BUILDING_LIB_CS_STRING
32 # define LIB_CS_STRING_EXPORT __declspec(dllexport)
33 #else
34 # define LIB_CS_STRING_EXPORT __declspec(dllimport)
35 #endif
36 
37 #else
38 # define LIB_CS_STRING_EXPORT
39 
40 #endif
41 
42 template <typename E, typename A = std::allocator<typename E::storage_unit>>
43 class CsBasicString;
44 
45 class CsChar
46 {
47  public:
48  CsChar()
49  : m_char(0)
50  {
51  }
52 
53  template <typename T = int>
54  CsChar(char c)
55  : m_char(static_cast<unsigned char>(c))
56  {
57 #ifndef CS_STRING_ALLOW_UNSAFE
58  static_assert(! std::is_same<T, T>::value, "Unsafe operations not allowed, unknown encoding for this operation");
59 #endif
60  }
61 
62  CsChar(char32_t c)
63  : m_char(c)
64  {
65  }
66 
67  CsChar(int c)
68  : m_char(c)
69  {
70  }
71 
72  CsChar(const CsChar &other)
73  : m_char(other.m_char)
74  {
75  }
76 
77 #ifdef __cpp_char8_t
78  // support new data type added in C++20
79 
80  CsChar(char8_t c)
81  : m_char(c)
82  {
83  }
84 #endif
85 
86  inline bool operator!=(const CsChar &other) const;
87  inline bool operator==(const CsChar &other) const;
88 
89  inline bool operator<(const CsChar &other) const;
90  inline bool operator<=(const CsChar &other) const;
91  inline bool operator>(const CsChar &other) const;
92  inline bool operator>=(const CsChar &other) const;
93 
94  inline CsChar &operator=(char c) &;
95  inline CsChar &operator=(char32_t c) &;
96  inline CsChar &operator=(CsChar c) &;
97 
98  inline uint32_t unicode() const;
99 
100  private:
101  uint32_t m_char;
102 };
103 
104 // comparisons
105 inline bool CsChar::operator!=(const CsChar &other) const
106 {
107  return m_char != other.m_char;
108 }
109 
110 inline bool CsChar::operator==(const CsChar &other) const
111 {
112  return m_char == other.m_char;
113 }
114 
115 inline bool CsChar::operator<(const CsChar &other) const
116 {
117  return m_char < other.m_char;
118 }
119 
120 inline bool CsChar::operator<=(const CsChar &other) const
121 {
122  return m_char <= other.m_char;
123 }
124 
125 inline bool CsChar::operator>(const CsChar &other) const
126 {
127  return m_char > other.m_char;
128 }
129 
130 inline bool CsChar::operator>=(const CsChar &other) const
131 {
132  return m_char >= other.m_char;
133 }
134 
135 inline CsChar &CsChar::operator=(char c) &
136 {
137  m_char = c;
138  return *this;
139 }
140 
141 inline CsChar &CsChar::operator=(char32_t c) &
142 {
143  m_char = c;
144  return *this;
145 }
146 
147 inline CsChar &CsChar::operator=(CsChar c) &
148 {
149  m_char = c.m_char;
150  return *this;
151 }
152 
153 inline uint32_t CsChar::unicode() const
154 {
155  return m_char;
156 }
157 
158 } // namespace
159 
160 namespace std {
161  template<>
162  struct hash<CsString::CsChar>
163  {
164  inline size_t operator()(const CsString::CsChar &key) const
165  {
166  return key.unicode();
167  }
168  };
169 }
170 
171 #endif