9namespace Stroika::Foundation::Characters::CString {
16 template <IPossibleCharacterRepresentation T>
20 if constexpr (
sizeof (T) == 1) {
21 return ::strlen (
reinterpret_cast<const char*
> (p));
23 else if constexpr (
sizeof (T) ==
sizeof (
wchar_t)) {
24 return ::wcslen (
reinterpret_cast<const wchar_t*
> (p));
41 inline bool Equals (
const char* lhs,
const char* rhs)
45 return ::strcmp (lhs, rhs) == 0;
48 inline bool Equals (
const char8_t* lhs,
const char8_t* rhs)
52 return ::strcmp (
reinterpret_cast<const char*
> (lhs),
reinterpret_cast<const char*
> (rhs)) == 0;
55 inline bool Equals (
const char16_t* lhs,
const char16_t* rhs)
59 const char16_t* li = lhs;
60 const char16_t* ri = rhs;
71 inline bool Equals (
const char32_t* lhs,
const char32_t* rhs)
75 const char32_t* li = lhs;
76 const char32_t* ri = rhs;
87 inline bool Equals (
const wchar_t* lhs,
const wchar_t* rhs)
91 return ::wcscmp (lhs, rhs) == 0;
100 void Copy (T* dest,
size_t nEltsInDest,
const T* src)
108 inline void Copy (
char* dest,
size_t nEltsInDest,
const char* src)
112 Require (nEltsInDest >= 1);
113 char* destEnd = dest + nEltsInDest;
115 const char* si = src;
116 while ((*di++ = *si++) !=
'\0') {
117 Assert (di <= destEnd);
123 Ensure (Length (dest) < nEltsInDest);
126 inline void Copy (
wchar_t* dest,
size_t nEltsInDest,
const wchar_t* src)
130 Require (nEltsInDest >= 1);
131 wchar_t* destEnd = dest + nEltsInDest;
133 const wchar_t* si = src;
134 while ((*di++ = *si++) !=
'\0') {
135 Assert (di <= destEnd);
141 Ensure (Length (dest) < nEltsInDest);
149 template <
typename T>
150 void Cat (T* dest,
size_t nEltsInDest,
const T* src)
158 inline void Cat (
char* dest,
size_t nEltsInDest,
const char* src)
162 Require (nEltsInDest >= 1);
163 char* destEnd = dest + nEltsInDest;
164 char* di = dest + strlen (dest);
165 const char* si = src;
166 while ((*di++ = *si++) !=
'\0') {
167 Assert (di <= destEnd);
173 Ensure (Length (dest) < nEltsInDest);
176 inline void Cat (
wchar_t* dest,
size_t nEltsInDest,
const wchar_t* src)
180 Require (nEltsInDest >= 1);
181 wchar_t* destEnd = dest + nEltsInDest;
182 wchar_t* di = dest + ::wcslen (dest);
183 const wchar_t* si = src;
184 while ((*di++ = *si++) !=
'\0') {
185 Assert (di <= destEnd);
191 Ensure (Length (dest) < nEltsInDest);
199 template <
typename TCHAR>
200 basic_string<TCHAR>
LTrim (
const basic_string<TCHAR>& text)
203 const ctype<TCHAR>& ct = use_facet<ctype<TCHAR>> (loc1);
204 typename basic_string<TCHAR>::const_iterator i = text.begin ();
205 for (; i != text.end () and ct.is (ctype<TCHAR>::space, *i); ++i)
207 return basic_string<TCHAR> (i, text.end ());
209 template <
typename TCHAR>
210 basic_string<TCHAR> RTrim (
const basic_string<TCHAR>& text)
213 const ctype<TCHAR>& ct = use_facet<ctype<TCHAR>> (loc1);
214 typename basic_string<TCHAR>::const_iterator i = text.end ();
215 for (; i != text.begin () and ct.is (ctype<TCHAR>::space, *(i - 1)); --i)
217 return basic_string<TCHAR> (text.begin (), i);
219 template <
typename TCHAR>
220 inline basic_string<TCHAR> Trim (
const basic_string<TCHAR>& text)
222 return LTrim (RTrim (text));
231 long long int String2Int_ (
const string& s);
232 long long int String2Int_ (
const char* s);
233 long long int String2Int_ (
const wchar_t* s);
234 long long int String2Int_ (
const wstring& s);
235 unsigned long long int String2UInt_ (
const string& s);
236 unsigned long long int String2UInt_ (
const char* s);
237 unsigned long long int String2UInt_ (
const wchar_t* s);
238 unsigned long long int String2UInt_ (
const wstring& s);
239 inline long long int String2Int_ (
const char* s)
241 return String2Int_ (
string{s});
243 inline long long int String2Int_ (
const wchar_t* s)
245 return String2Int_ (wstring (s));
247 inline unsigned long long int String2UInt_ (
const char* s)
249 return String2UInt_ (
string{s});
251 inline unsigned long long int String2UInt_ (
const wchar_t* s)
253 return String2UInt_ (wstring{s});
256 template <typename T, typename STRING_ARG>
257 T String2IntOrUInt_ (STRING_ARG s)
259 if (numeric_limits<T>::is_signed) {
260 long long int l = String2Int_ (s);
261 if (l <= numeric_limits<T>::min ()) {
262 return numeric_limits<T>::min ();
264 if (l >= numeric_limits<T>::max ()) {
265 return numeric_limits<T>::max ();
267 return static_cast<T
> (l);
270 unsigned long long int l = String2UInt_ (s);
271 if (l >= numeric_limits<T>::max ()) {
272 return numeric_limits<T>::max ();
274 return static_cast<T
> (l);
278 DISABLE_COMPILER_MSC_WARNING_END (4018)
280 template <typename T>
281 inline T String2Int (const
string& s)
283 return Private_::String2IntOrUInt_<T, const string&> (s);
285 template <
typename T>
286 inline T String2Int (
const wchar_t* s)
288 return Private_::String2IntOrUInt_<T, const wchar_t*> (s);
290 template <
typename T>
291 inline T String2Int (
const wstring& s)
293 return Private_::String2IntOrUInt_<T, const wstring&> (s);
#define RequireNotNull(p)
void Cat(T *dest, size_t nEltsInDest, const T *src2Append)
Safe variant of strncat() - which always NUL-terminates the string. DIFFERNT arguments however,...
basic_string< TCHAR > LTrim(const basic_string< TCHAR > &text)
size_t Length(const T *p)
Measure the length of the argument c-string (NUL-terminated string).
void Copy(T *dest, size_t nEltsInDest, const T *src)
Safe variant of strncpy() - which always NUL-terminates the string.
bool Equals(const T *lhs, const T *rhs)
strcmp or wsccmp() as appropriate == 0
DISABLE_COMPILER_MSC_WARNING_START(4996)