nlib
Cstring.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_CSTRING_H_
4 #define INCLUDE_NN_NLIB_CSTRING_H_
5 
6 #include <stdarg.h>
7 #include <string.h>
8 
9 #include "nn/nlib/Config.h"
10 
11 NLIB_NAMESPACE_BEGIN
12 
13 inline size_t StrLen(const char* str) NLIB_NOEXCEPT { return nlib_strlen(str); }
14 inline size_t StrLen(const nlib_utf16_t* str) NLIB_NOEXCEPT { return nlib_utf16len(str); }
15 inline size_t StrLen(const nlib_utf32_t* str) NLIB_NOEXCEPT { return nlib_utf32len(str); }
16 inline size_t StrLen(const wchar_t* str) NLIB_NOEXCEPT { return nlib_wcslen(str); }
17 
19  const char* str,
20  size_t* count) NLIB_NOEXCEPT {
21  return nlib_strcplen(count, NULL, NULL, str);
22 }
24  const nlib_utf16_t* str,
25  size_t* count) NLIB_NOEXCEPT {
26  return nlib_utf16cplen(count, str);
27 }
29  const nlib_utf32_t* str,
30  size_t* count) NLIB_NOEXCEPT {
31  return nlib_utf32cplen(count, str);
32 }
34  const wchar_t* str,
35  size_t* count) NLIB_NOEXCEPT {
36  return nlib_wcscplen(count, str);
37 }
38 
39 inline int StrCmp(const char* s1, const char* s2) NLIB_NOEXCEPT {
40  return strcmp(s1, s2);
41 }
42 inline int StrCmp(const wchar_t* s1, const wchar_t* s2) NLIB_NOEXCEPT {
43  return wcscmp(s1, s2);
44 }
45 inline int StrCmp(const nlib_utf16_t* s1, const nlib_utf16_t* s2) NLIB_NOEXCEPT {
46  for (; *s1 == *s2; ++s1, ++s2) {
47  if (NLIB_UNLIKELY(*s1 == '\0')) return 0;
48  }
49  return *s1 < *s2 ? -1 : 1;
50 }
51 inline int StrCmp(const nlib_utf32_t* s1, const nlib_utf32_t* s2) NLIB_NOEXCEPT {
52  for (; *s1 == *s2; ++s1, ++s2) {
53  if (NLIB_UNLIKELY(*s1 == '\0')) return 0;
54  }
55  return *s1 < *s2 ? -1 : 1;
56 }
57 
58 inline int StrNcmp(const char* s1,
59  const char* s2,
60  size_t n) NLIB_NOEXCEPT {
61  return strncmp(s1, s2, n);
62 }
63 inline int StrNcmp(const wchar_t* s1,
64  const wchar_t* s2,
65  size_t n) NLIB_NOEXCEPT {
66  return wcsncmp(s1, s2, n);
67 }
68 inline int StrNcmp(const nlib_utf16_t* s1,
69  const nlib_utf16_t* s2,
70  size_t n) NLIB_NOEXCEPT {
71  for (; n > 0; ++s1, ++s2, --n) {
72  if (NLIB_UNLIKELY(*s1 != *s2)) {
73  return *s1 < *s2 ? -1 : 1;
74  } else if (NLIB_UNLIKELY(*s1 == '\0')) {
75  return 0;
76  }
77  }
78  return 0;
79 }
80 inline int StrNcmp(const nlib_utf32_t* s1,
81  const nlib_utf32_t* s2,
82  size_t n) NLIB_NOEXCEPT {
83  for (; n > 0; ++s1, ++s2, --n) {
84  if (NLIB_UNLIKELY(*s1 != *s2)) {
85  return *s1 < *s2 ? -1 : 1;
86  } else if (NLIB_UNLIKELY(*s1 == '\0')) {
87  return 0;
88  }
89  }
90  return 0;
91 }
92 
93 inline errno_t StrCpy(char* dest,
94  size_t dest_size,
95  const char* src) NLIB_NOEXCEPT {
96  return nlib_strcpy(dest, dest_size, src);
97 }
99  size_t dest_size,
100  const nlib_utf16_t* src) NLIB_NOEXCEPT {
101  return nlib_utf16cpy(dest, dest_size, src);
102 }
104  size_t dest_size,
105  const nlib_utf32_t* src) NLIB_NOEXCEPT {
106  return nlib_utf32cpy(dest, dest_size, src);
107 }
108 inline errno_t StrCpy(wchar_t* dest,
109  size_t dest_size,
110  const wchar_t* src) NLIB_NOEXCEPT {
111  return nlib_wcscpy(dest, dest_size, src);
112 }
113 
114 template<class T, size_t N>
116  const T* src) NLIB_NOEXCEPT {
117  return StrCpy(&dest[0], N, src);
118 }
119 
120 inline errno_t StrNcpy(char* dest,
121  size_t dest_size,
122  const char* src,
123  size_t n) NLIB_NOEXCEPT {
124  return nlib_strncpy(dest, dest_size, src, n);
125 }
127  size_t dest_size,
128  const nlib_utf16_t* src,
129  size_t n) NLIB_NOEXCEPT {
130  return nlib_utf16ncpy(dest, dest_size, src, n);
131 }
133  size_t dest_size,
134  const nlib_utf32_t* src,
135  size_t n) NLIB_NOEXCEPT {
136  return nlib_utf32ncpy(dest, dest_size, src, n);
137 }
138 inline errno_t StrNcpy(wchar_t* dest,
139  size_t dest_size,
140  const wchar_t* src,
141  size_t n) NLIB_NOEXCEPT {
142  return nlib_wcsncpy(dest, dest_size, src, n);
143 }
144 
145 template<class T, size_t N>
147  const T* src,
148  size_t n) NLIB_NOEXCEPT {
149  return StrNcpy(&dest[0], N, src, n);
150 }
151 
152 template<class T, size_t N>
154  const T* src) NLIB_NOEXCEPT {
155  return StrCat(&dest[0], N, src);
156 }
157 
158 template<class T, size_t N>
160  const T* src,
161  size_t n) NLIB_NOEXCEPT {
162  return StrNcat(&dest[0], N, src, n);
163 }
164 
165 // NOTE: It is possible that NLIB_VSNPRINTF is VsnPrintfFallback.
167  char* buf,
168  size_t size,
169  _Printf_format_string_ const char* fmt,
170  va_list args) NLIB_NOEXCEPT;
171 
172 inline int VsnPrintf(
173  char* buf,
174  size_t size,
175  _Printf_format_string_ const char* fmt,
176  va_list args) NLIB_NOEXCEPT {
177  size_t count;
178  errno_t e = nlib_vsnprintf(&count, buf, size, fmt, args);
179  if (e != 0) errno = e;
180  return (e == 0) ? static_cast<int>(count) : -1;
181 }
182 
183 template <size_t N>
185  char (&buf)[N],
186  _Printf_format_string_ const char* fmt,
187  va_list args) NLIB_NOEXCEPT {
188  return VsnPrintf(buf, N, fmt, args);
189 }
190 
191 inline int SnPrintf(
192  char* buf,
193  size_t size,
194  _Printf_format_string_ const char* fmt,
195  ...) NLIB_NOEXCEPT {
196  va_list args;
197  va_start(args, fmt);
198  int rval = VsnPrintf(buf, size, fmt, args);
199  va_end(args);
200  return rval;
201 }
202 
203 template <size_t N>
205  char (&buf)[N],
206  _Printf_format_string_ const char* fmt,
207  ...) NLIB_NOEXCEPT {
208  // gcc cannot inline vaargs functions
209  va_list args;
210  va_start(args, fmt);
211  int rval = VsnPrintf(buf, N, fmt, args);
212  va_end(args);
213  return rval;
214 }
215 
216 template <size_t N>
218  char (&buf)[N],
219  _Printf_format_string_ const char* fmt,
220  va_list args) NLIB_NOEXCEPT {
221  return VsnPrintfFallback(buf, N, fmt, args);
222 }
223 
224 inline int SnPrintfFallback(
225  char* buf,
226  size_t size,
227  _Printf_format_string_ const char* fmt,
228  ...) NLIB_NOEXCEPT {
229  va_list args;
230  va_start(args, fmt);
231  int rval = VsnPrintfFallback(buf, size, fmt, args);
232  va_end(args);
233  return rval;
234 }
235 
236 template <size_t N>
237 inline NLIB_VIS_HIDDEN int SnPrintfFallback(
238  char (&buf)[N],
239  _Printf_format_string_ const char* fmt,
240  ...) NLIB_NOEXCEPT {
241  va_list args;
242  va_start(args, fmt);
243  int rval = VsnPrintfFallback(buf, N, fmt, args);
244  va_end(args);
245  return rval;
246 }
247 
249  wchar_t* buf,
250  size_t size,
251  _Printf_format_string_ const wchar_t* fmt,
252  va_list args) NLIB_NOEXCEPT;
253 
254 inline int VsnPrintf(
255  wchar_t* buf,
256  size_t size,
257  _Printf_format_string_ const wchar_t* fmt,
258  va_list args) NLIB_NOEXCEPT {
259  size_t count;
260  errno_t e = nlib_vsnwprintf(&count, buf, size, fmt, args);
261  if (e != 0) errno = e;
262  return (e == 0) ? static_cast<int>(count) : -1;
263 }
264 
265 template <size_t N>
267  wchar_t (&buf)[N],
268  _Printf_format_string_ const wchar_t* fmt,
269  va_list args) NLIB_NOEXCEPT {
270  return VsnPrintf(buf, N, fmt, args);
271 }
272 
273 inline int SnPrintf(
274  wchar_t* buf,
275  size_t size,
276  _Printf_format_string_ const wchar_t* fmt,
277  ...) NLIB_NOEXCEPT {
278  va_list args;
279  va_start(args, fmt);
280  int rval = VsnPrintf(buf, size, fmt, args);
281  va_end(args);
282  return rval;
283 }
284 
285 template <size_t N>
287  wchar_t (&buf)[N],
288  _Printf_format_string_ const wchar_t* fmt,
289  ...) NLIB_NOEXCEPT {
290  va_list args;
291  va_start(args, fmt);
292  int rval = VsnPrintf(buf, N, fmt, args);
293  va_end(args);
294  return rval;
295 }
296 
297 template <size_t N>
299  wchar_t (&buf)[N],
300  _Printf_format_string_ const wchar_t* fmt,
301  va_list args) NLIB_NOEXCEPT {
302  return VsnPrintfFallback(buf, N, fmt, args);
303 }
304 
305 inline int SnPrintfFallback(
306  wchar_t* buf,
307  size_t size,
308  _Printf_format_string_ const wchar_t* fmt,
309  ...) NLIB_NOEXCEPT {
310  va_list args;
311  va_start(args, fmt);
312  int rval = VsnPrintfFallback(buf, size, fmt, args);
313  va_end(args);
314  return rval;
315 }
316 
317 template <size_t N>
318 inline NLIB_VIS_HIDDEN int SnPrintfFallback(
319  wchar_t (&buf)[N],
320  _Printf_format_string_ const wchar_t* fmt,
321  ...) NLIB_NOEXCEPT {
322  va_list args;
323  va_start(args, fmt);
324  int rval = VsnPrintfFallback(buf, N, fmt, args);
325  va_end(args);
326  return rval;
327 }
328 
329 NLIB_NAMESPACE_END
330 
331 #endif // INCLUDE_NN_NLIB_CSTRING_H_
int VsnPrintf(wchar_t(&buf)[N], const wchar_t *fmt, va_list args) noexcept
Internally runs VsnPrintf(buf, N, fmt, args).
Definition: Cstring.h:266
NLIB_CHECK_RESULT errno_t nlib_wcscplen(size_t *count, const wchar_t *str)
Gets the number of code points in the string.
errno_t StrCpy(T(&dest)[N], const T *src) noexcept
Makes a call to StrCpy(&dest[0], N, src).
Definition: Cstring.h:115
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:69
errno_t nlib_vsnwprintf(size_t *count, wchar_t(&buf)[N], const wchar_t *fmt, va_list args) noexcept
The function template version of nlib_vsnwprintf.
Definition: Config.h:733
errno_t nlib_strncpy(char(&s1)[N], const char *s2, size_t n) noexcept
The function template version of nlib_strncpy.
Definition: Config.h:812
int SnPrintf(wchar_t(&buf)[N], const wchar_t *fmt,...) noexcept
Internally runs the VsnPrintf function.
Definition: Cstring.h:286
#define NLIB_UNLIKELY(x)
Indicates to the compiler that condition x is likely to be false.
Definition: Platform_unix.h:72
int VsnPrintfFallback(char *buf, size_t size, const char *fmt, va_list args) noexcept
The fallback for VsnPrintf. Also implemented in a wide-character version.
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:60
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
int StrCmp(const nlib_utf32_t *s1, const nlib_utf32_t *s2) noexcept
Compares UTF-32 strings in the same way as the strcmp function.
Definition: Cstring.h:51
uint32_t nlib_utf32_t
Uses typedef to define as char32_t if that can be used. If not, it uses typedef to define as uint32_t...
Definition: Config.h:539
int StrNcmp(const nlib_utf32_t *s1, const nlib_utf32_t *s2, size_t n) noexcept
Compares UTF-32 strings in the same way as the strncmp function.
Definition: Cstring.h:80
errno_t nlib_utf16ncpy(nlib_utf16_t *s1, size_t s1max, const nlib_utf16_t *s2, size_t n) noexcept
The UTF-16 version of the nlib_strcpy function.
Definition: Config.h:640
uint16_t nlib_utf16_t
Uses typedef to define as char16_t if that can be used. If not, it uses typedef to define as uint16_t...
Definition: Config.h:538
errno_t nlib_utf16cplen(size_t *count, const nlib_utf16_t *str) noexcept
Gets the number of code points in the string.
Definition: Config.h:693
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
size_t nlib_wcslen(const wchar_t *s)
Makes a call to thewcslen function. In some cases, it may operate as an independent implementation...
A file that contains the configuration information for each development environment.
errno_t nlib_utf32ncpy(nlib_utf32_t *s1, size_t s1max, const nlib_utf32_t *s2, size_t n) noexcept
The UTF-32 version of the nlib_strcpy function.
Definition: Config.h:679
errno_t CodePointCount(const wchar_t *str, size_t *count) noexcept
Wraps the nlib_wcscplen function.
Definition: Cstring.h:33
errno_t nlib_vsnprintf(size_t *count, char(&buf)[N], const char *fmt, va_list args) noexcept
The function template version of nlib_vsnprintf.
Definition: Config.h:711
size_t nlib_strlen(const char *s)
Internally calls strlen(). In some cases, it may operate as an independent implementation.
errno_t nlib_utf16cpy(nlib_utf16_t *s1, size_t s1max, const nlib_utf16_t *s2) noexcept
The UTF-16 version of the nlib_strcpy function.
Definition: Config.h:633
errno_t nlib_wcscpy(wchar_t(&s1)[N], const wchar_t *s2) noexcept
The function template version of nlib_wcscpy.
Definition: Config.h:820
NLIB_CHECK_RESULT size_t nlib_utf16len(const nlib_utf16_t *str) noexcept
Counts the number of nlib_utf16_t-type characters, not including the null character.
Definition: Config.h:624
errno_t StrNcat(T(&dest)[N], const T *src, size_t n) noexcept
Makes an internal call to StrNcat(&dest[0], N, src, n).
Definition: Cstring.h:159
size_t StrLen(const wchar_t *str) noexcept
Wraps the nlib_wcslen function.
Definition: Cstring.h:16
NLIB_CHECK_RESULT size_t nlib_utf32len(const nlib_utf32_t *str) noexcept
Counts the number of nlib_utf32_t-type characters, not including the null character.
Definition: Config.h:663
errno_t nlib_utf32cpy(nlib_utf32_t *s1, size_t s1max, const nlib_utf32_t *s2) noexcept
The UTF-32 version of the nlib_strcpy function.
Definition: Config.h:672
errno_t StrNcpy(T(&dest)[N], const T *src, size_t n) noexcept
Makes an internal call to StrNcpy(&dest[0], N, src, n).
Definition: Cstring.h:146
errno_t nlib_wcsncpy(wchar_t(&s1)[N], const wchar_t *s2, size_t n) noexcept
The function template version of nlib_wcsncpy.
Definition: Config.h:827
errno_t nlib_strcpy(char(&s1)[N], const char *s2) noexcept
The function template version of nlib_strcpy.
Definition: Config.h:799
NLIB_CHECK_RESULT errno_t nlib_utf32cplen(size_t *count, const nlib_utf32_t *str) noexcept
Gets the number of code points in the string.
errno_t StrCat(T(&dest)[N], const T *src) noexcept
Makes a call to StrCat(&dest[0], N, src).
Definition: Cstring.h:153
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24