nlib
Cstring.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_CSTRING_H_
17 #define INCLUDE_NN_NLIB_CSTRING_H_
18 
19 #include <stdarg.h>
20 #include <string.h>
21 
22 #include "nn/nlib/Config.h"
23 
24 NLIB_NAMESPACE_BEGIN
25 
26 inline size_t StrLen(const char* str) NLIB_NOEXCEPT { return nlib_strlen(str); }
27 inline size_t StrLen(const nlib_utf16_t* str) NLIB_NOEXCEPT { return nlib_utf16len(str); }
28 inline size_t StrLen(const nlib_utf32_t* str) NLIB_NOEXCEPT { return nlib_utf32len(str); }
29 inline size_t StrLen(const wchar_t* str) NLIB_NOEXCEPT { return nlib_wcslen(str); }
30 
32  const char* str,
33  size_t* count) NLIB_NOEXCEPT {
34  return nlib_strcplen(count, nullptr, nullptr, str);
35 }
37  const nlib_utf16_t* str,
38  size_t* count) NLIB_NOEXCEPT {
39  return nlib_utf16cplen(count, str);
40 }
42  const nlib_utf32_t* str,
43  size_t* count) NLIB_NOEXCEPT {
44  return nlib_utf32cplen(count, str);
45 }
47  const wchar_t* str,
48  size_t* count) NLIB_NOEXCEPT {
49  return nlib_wcscplen(count, str);
50 }
51 
52 inline int StrCmp(const char* s1, const char* s2) NLIB_NOEXCEPT {
53  return strcmp(s1, s2);
54 }
55 inline int StrCmp(const wchar_t* s1, const wchar_t* s2) NLIB_NOEXCEPT {
56  return wcscmp(s1, s2);
57 }
58 inline int StrCmp(const nlib_utf16_t* s1, const nlib_utf16_t* s2) NLIB_NOEXCEPT {
59  for (; *s1 == *s2; ++s1, ++s2) {
60  if (NLIB_UNLIKELY(*s1 == '\0')) return 0;
61  }
62  return *s1 < *s2 ? -1 : 1;
63 }
64 inline int StrCmp(const nlib_utf32_t* s1, const nlib_utf32_t* s2) NLIB_NOEXCEPT {
65  for (; *s1 == *s2; ++s1, ++s2) {
66  if (NLIB_UNLIKELY(*s1 == '\0')) return 0;
67  }
68  return *s1 < *s2 ? -1 : 1;
69 }
70 
71 inline int StrNcmp(const char* s1,
72  const char* s2,
73  size_t n) NLIB_NOEXCEPT {
74  return strncmp(s1, s2, n);
75 }
76 inline int StrNcmp(const wchar_t* s1,
77  const wchar_t* s2,
78  size_t n) NLIB_NOEXCEPT {
79  return wcsncmp(s1, s2, n);
80 }
81 inline int StrNcmp(const nlib_utf16_t* s1,
82  const nlib_utf16_t* s2,
83  size_t n) NLIB_NOEXCEPT {
84  for (; n > 0; ++s1, ++s2, --n) {
85  if (NLIB_UNLIKELY(*s1 != *s2)) {
86  return *s1 < *s2 ? -1 : 1;
87  } else if (NLIB_UNLIKELY(*s1 == '\0')) {
88  return 0;
89  }
90  }
91  return 0;
92 }
93 inline int StrNcmp(const nlib_utf32_t* s1,
94  const nlib_utf32_t* s2,
95  size_t n) NLIB_NOEXCEPT {
96  for (; n > 0; ++s1, ++s2, --n) {
97  if (NLIB_UNLIKELY(*s1 != *s2)) {
98  return *s1 < *s2 ? -1 : 1;
99  } else if (NLIB_UNLIKELY(*s1 == '\0')) {
100  return 0;
101  }
102  }
103  return 0;
104 }
105 
106 inline errno_t StrCpy(char* dest,
107  size_t dest_size,
108  const char* src) NLIB_NOEXCEPT {
109  return nlib_strcpy(dest, dest_size, src);
110 }
112  size_t dest_size,
113  const nlib_utf16_t* src) NLIB_NOEXCEPT {
114  return nlib_utf16cpy(dest, dest_size, src);
115 }
117  size_t dest_size,
118  const nlib_utf32_t* src) NLIB_NOEXCEPT {
119  return nlib_utf32cpy(dest, dest_size, src);
120 }
121 inline errno_t StrCpy(wchar_t* dest,
122  size_t dest_size,
123  const wchar_t* src) NLIB_NOEXCEPT {
124  return nlib_wcscpy(dest, dest_size, src);
125 }
126 
127 template<class T, size_t N>
129  const T* src) NLIB_NOEXCEPT {
130  return StrCpy(&dest[0], N, src);
131 }
132 
133 inline errno_t StrNcpy(char* dest,
134  size_t dest_size,
135  const char* src,
136  size_t n) NLIB_NOEXCEPT {
137  return nlib_strncpy(dest, dest_size, src, n);
138 }
140  size_t dest_size,
141  const nlib_utf16_t* src,
142  size_t n) NLIB_NOEXCEPT {
143  return nlib_utf16ncpy(dest, dest_size, src, n);
144 }
146  size_t dest_size,
147  const nlib_utf32_t* src,
148  size_t n) NLIB_NOEXCEPT {
149  return nlib_utf32ncpy(dest, dest_size, src, n);
150 }
151 inline errno_t StrNcpy(wchar_t* dest,
152  size_t dest_size,
153  const wchar_t* src,
154  size_t n) NLIB_NOEXCEPT {
155  return nlib_wcsncpy(dest, dest_size, src, n);
156 }
157 
158 template<class T, size_t N>
160  const T* src,
161  size_t n) NLIB_NOEXCEPT {
162  return StrNcpy(&dest[0], N, src, n);
163 }
164 
165 template<class T, size_t N>
167  const T* src) NLIB_NOEXCEPT {
168  return StrCat(&dest[0], N, src);
169 }
170 
171 template<class T, size_t N>
173  const T* src,
174  size_t n) NLIB_NOEXCEPT {
175  return StrNcat(&dest[0], N, src, n);
176 }
177 
178 // NOTE: It is possible that NLIB_VSNPRINTF is VsnPrintfFallback.
180  char* buf,
181  size_t size,
182  _Printf_format_string_ const char* fmt,
183  va_list args) NLIB_NOEXCEPT;
184 
185 inline int VsnPrintf(
186  char* buf,
187  size_t size,
188  _Printf_format_string_ const char* fmt,
189  va_list args) NLIB_NOEXCEPT {
190  size_t count;
191  errno_t e = nlib_vsnprintf(&count, buf, size, fmt, args);
192  if (e != 0) errno = e;
193  return (e == 0) ? static_cast<int>(count) : -1;
194 }
195 
196 template <size_t N>
198  char (&buf)[N],
199  _Printf_format_string_ const char* fmt,
200  va_list args) NLIB_NOEXCEPT {
201  return VsnPrintf(buf, N, fmt, args);
202 }
203 
204 inline int SnPrintf(
205  char* buf,
206  size_t size,
207  _Printf_format_string_ const char* fmt,
208  ...) NLIB_NOEXCEPT {
209  va_list args;
210  va_start(args, fmt);
211  int rval = VsnPrintf(buf, size, 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  ...) NLIB_NOEXCEPT {
221  // gcc cannot inline vaargs functions
222  va_list args;
223  va_start(args, fmt);
224  int rval = VsnPrintf(buf, N, fmt, args);
225  va_end(args);
226  return rval;
227 }
228 
229 template <size_t N>
231  char (&buf)[N],
232  _Printf_format_string_ const char* fmt,
233  va_list args) NLIB_NOEXCEPT {
234  return VsnPrintfFallback(buf, N, fmt, args);
235 }
236 
237 inline int SnPrintfFallback(
238  char* buf,
239  size_t size,
240  _Printf_format_string_ const char* fmt,
241  ...) NLIB_NOEXCEPT {
242  va_list args;
243  va_start(args, fmt);
244  int rval = VsnPrintfFallback(buf, size, fmt, args);
245  va_end(args);
246  return rval;
247 }
248 
249 template <size_t N>
250 inline NLIB_VIS_HIDDEN int SnPrintfFallback(
251  char (&buf)[N],
252  _Printf_format_string_ const char* fmt,
253  ...) NLIB_NOEXCEPT {
254  va_list args;
255  va_start(args, fmt);
256  int rval = VsnPrintfFallback(buf, N, fmt, args);
257  va_end(args);
258  return rval;
259 }
260 
262  wchar_t* buf,
263  size_t size,
264  _Printf_format_string_ const wchar_t* fmt,
265  va_list args) NLIB_NOEXCEPT;
266 
267 inline int VsnPrintf(
268  wchar_t* buf,
269  size_t size,
270  _Printf_format_string_ const wchar_t* fmt,
271  va_list args) NLIB_NOEXCEPT {
272  size_t count;
273  errno_t e = nlib_vsnwprintf(&count, buf, size, fmt, args);
274  if (e != 0) errno = e;
275  return (e == 0) ? static_cast<int>(count) : -1;
276 }
277 
278 template <size_t N>
280  wchar_t (&buf)[N],
281  _Printf_format_string_ const wchar_t* fmt,
282  va_list args) NLIB_NOEXCEPT {
283  return VsnPrintf(buf, N, fmt, args);
284 }
285 
286 inline int SnPrintf(
287  wchar_t* buf,
288  size_t size,
289  _Printf_format_string_ const wchar_t* fmt,
290  ...) NLIB_NOEXCEPT {
291  va_list args;
292  va_start(args, fmt);
293  int rval = VsnPrintf(buf, size, fmt, args);
294  va_end(args);
295  return rval;
296 }
297 
298 template <size_t N>
300  wchar_t (&buf)[N],
301  _Printf_format_string_ const wchar_t* fmt,
302  ...) NLIB_NOEXCEPT {
303  va_list args;
304  va_start(args, fmt);
305  int rval = VsnPrintf(buf, N, fmt, args);
306  va_end(args);
307  return rval;
308 }
309 
310 template <size_t N>
312  wchar_t (&buf)[N],
313  _Printf_format_string_ const wchar_t* fmt,
314  va_list args) NLIB_NOEXCEPT {
315  return VsnPrintfFallback(buf, N, fmt, args);
316 }
317 
318 inline int SnPrintfFallback(
319  wchar_t* buf,
320  size_t size,
321  _Printf_format_string_ const wchar_t* fmt,
322  ...) NLIB_NOEXCEPT {
323  va_list args;
324  va_start(args, fmt);
325  int rval = VsnPrintfFallback(buf, size, fmt, args);
326  va_end(args);
327  return rval;
328 }
329 
330 template <size_t N>
331 inline NLIB_VIS_HIDDEN int SnPrintfFallback(
332  wchar_t (&buf)[N],
333  _Printf_format_string_ const wchar_t* fmt,
334  ...) NLIB_NOEXCEPT {
335  va_list args;
336  va_start(args, fmt);
337  int rval = VsnPrintfFallback(buf, N, fmt, args);
338  va_end(args);
339  return rval;
340 }
341 
342 NLIB_NAMESPACE_END
343 
344 #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:279
static errno_t nlib_utf16ncpy(nlib_utf16_t *s1, size_t s1max, const nlib_utf16_t *s2, size_t n)
The UTF-16 version of the nlib_strcpy function.
Definition: Platform.h:2268
static size_t nlib_utf16len(const nlib_utf16_t *str)
Counts the number of nlib_utf16_t-type characters, not including the null character.
Definition: Platform.h:2256
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:128
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:97
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:493
errno_t nlib_strncpy(char(&s1)[N], const char *s2, size_t n) noexcept
The function template version of nlib_strncpy.
Definition: Config.h:572
int SnPrintf(wchar_t(&buf)[N], const wchar_t *fmt,...) noexcept
Internally runs the VsnPrintf function.
Definition: Cstring.h:299
#define NLIB_UNLIKELY(x)
Indicates to the compiler that condition x is likely to be false.
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.
static size_t nlib_utf32len(const nlib_utf32_t *str)
Counts the number of nlib_utf32_t-type characters, not including the null character.
Definition: Platform.h:2281
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:88
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
static errno_t nlib_utf32cpy(nlib_utf32_t *s1, size_t s1max, const nlib_utf32_t *s2)
The UTF-32 version of the nlib_strcpy function.
Definition: Platform.h:2289
errno_t nlib_strcplen(size_t *codepoint_count, size_t *supplementary_codepoint_count, size_t *len, const nlib_utf8_t *str)
Gets the number of code points contained in the string, the number of supplementary characters contai...
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:64
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: Platform.h:294
static errno_t nlib_utf16cpy(nlib_utf16_t *s1, size_t s1max, const nlib_utf16_t *s2)
The UTF-16 version of the nlib_strcpy function.
Definition: Platform.h:2264
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:93
errno_t nlib_utf32cplen(size_t *count, const nlib_utf32_t *str)
Gets the number of code points in the string.
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: Platform.h:293
static errno_t nlib_utf32ncpy(nlib_utf32_t *s1, size_t s1max, const nlib_utf32_t *s2, size_t n)
The UTF-32 version of the nlib_strcpy function.
Definition: Platform.h:2293
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
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 CodePointCount(const wchar_t *str, size_t *count) noexcept
Wraps the nlib_wcscplen function.
Definition: Cstring.h:46
static errno_t nlib_utf16cplen(size_t *count, const nlib_utf16_t *str)
Gets the number of code points in the string.
Definition: Platform.h:2301
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:471
size_t nlib_strlen(const char *s)
Internally calls strlen(). In some cases, it may operate as an independent implementation.
errno_t nlib_wcscpy(wchar_t(&s1)[N], const wchar_t *s2) noexcept
The function template version of nlib_wcscpy.
Definition: Config.h:580
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:172
size_t StrLen(const wchar_t *str) noexcept
Wraps the nlib_wcslen function.
Definition: Cstring.h:29
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:159
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:587
errno_t nlib_strcpy(char(&s1)[N], const char *s2) noexcept
The function template version of nlib_strcpy.
Definition: Config.h:559
errno_t StrCat(T(&dest)[N], const T *src) noexcept
Makes a call to StrCat(&dest[0], N, src).
Definition: Cstring.h:166
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37