nlib
Cstring.h
[詳解]
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 #if defined(_MSC_VER)
25 #if defined(n_EXPORTS)
26 #undef NLIB_VIS_PUBLIC
27 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
28 #elif defined(nx_misc_EXPORTS)
29 #undef NLIB_VIS_PUBLIC
30 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
31 #endif
32 #endif
33 
34 NLIB_NAMESPACE_BEGIN
35 
36 inline size_t StrLen(const char* str) NLIB_NOEXCEPT {
37  return nlib_strlen(str);
38 }
39 inline size_t StrLen(const nlib_utf16_t* str) NLIB_NOEXCEPT {
40  return nlib_utf16len(str);
41 }
42 inline size_t StrLen(const nlib_utf32_t* str) NLIB_NOEXCEPT {
43  return nlib_utf32len(str);
44 }
45 inline size_t StrLen(const wchar_t* str) NLIB_NOEXCEPT {
46  return nlib_wcslen(str);
47 }
48 
49 inline errno_t CodePointCount(const char* str, size_t* count) NLIB_NOEXCEPT {
50  return nlib_strcplen(count, nullptr, nullptr, str);
51 }
52 inline errno_t CodePointCount(const nlib_utf16_t* str, size_t* count) NLIB_NOEXCEPT {
53  return nlib_utf16cplen(count, str);
54 }
55 inline errno_t CodePointCount(const nlib_utf32_t* str, size_t* count) NLIB_NOEXCEPT {
56  return nlib_utf32cplen(count, str);
57 }
58 inline errno_t CodePointCount(const wchar_t* str, size_t* count) NLIB_NOEXCEPT {
59  return nlib_wcscplen(count, str);
60 }
61 
62 inline int StrCmp(const char* s1, const char* s2) NLIB_NOEXCEPT {
63  return strcmp(s1, s2);
64 }
65 inline int StrCmp(const wchar_t* s1, const wchar_t* s2) NLIB_NOEXCEPT {
66  return wcscmp(s1, s2);
67 }
68 inline int StrCmp(const nlib_utf16_t* s1, const nlib_utf16_t* s2) NLIB_NOEXCEPT {
69  for (; *s1 == *s2; ++s1, ++s2) {
70  if (NLIB_UNLIKELY(*s1 == '\0')) return 0;
71  }
72  return *s1 < *s2 ? -1 : 1;
73 }
74 inline int StrCmp(const nlib_utf32_t* s1, const nlib_utf32_t* s2) NLIB_NOEXCEPT {
75  for (; *s1 == *s2; ++s1, ++s2) {
76  if (NLIB_UNLIKELY(*s1 == '\0')) return 0;
77  }
78  return *s1 < *s2 ? -1 : 1;
79 }
80 
81 inline int StrNcmp(const char* s1, const char* s2, size_t n) NLIB_NOEXCEPT {
82  return strncmp(s1, s2, n);
83 }
84 inline int StrNcmp(const wchar_t* s1, const wchar_t* s2, size_t n) NLIB_NOEXCEPT {
85  return wcsncmp(s1, s2, n);
86 }
87 inline int StrNcmp(const nlib_utf16_t* s1, const nlib_utf16_t* s2, size_t n) NLIB_NOEXCEPT {
88  for (; n > 0; ++s1, ++s2, --n) {
89  if (NLIB_UNLIKELY(*s1 != *s2)) {
90  return *s1 < *s2 ? -1 : 1;
91  } else if (NLIB_UNLIKELY(*s1 == '\0')) {
92  return 0;
93  }
94  }
95  return 0;
96 }
97 inline int StrNcmp(const nlib_utf32_t* s1, const nlib_utf32_t* s2, size_t n) NLIB_NOEXCEPT {
98  for (; n > 0; ++s1, ++s2, --n) {
99  if (NLIB_UNLIKELY(*s1 != *s2)) {
100  return *s1 < *s2 ? -1 : 1;
101  } else if (NLIB_UNLIKELY(*s1 == '\0')) {
102  return 0;
103  }
104  }
105  return 0;
106 }
107 
108 inline errno_t StrCpy(char* dest, size_t dest_size, const char* src) NLIB_NOEXCEPT {
109  return nlib_strcpy(dest, dest_size, src);
110 }
111 inline errno_t StrCpy(nlib_utf16_t* dest, size_t dest_size, const nlib_utf16_t* src) NLIB_NOEXCEPT {
112  return nlib_utf16cpy(dest, dest_size, src);
113 }
114 inline errno_t StrCpy(nlib_utf32_t* dest, size_t dest_size, const nlib_utf32_t* src) NLIB_NOEXCEPT {
115  return nlib_utf32cpy(dest, dest_size, src);
116 }
117 inline errno_t StrCpy(wchar_t* dest, size_t dest_size, const wchar_t* src) NLIB_NOEXCEPT {
118  return nlib_wcscpy(dest, dest_size, src);
119 }
120 
121 template<class T, size_t N>
122 NLIB_ALWAYS_INLINE errno_t StrCpy(T (&dest)[N], const T* src) NLIB_NOEXCEPT {
123  return StrCpy(&dest[0], N, src);
124 }
125 
126 inline errno_t StrNcpy(char* dest, size_t dest_size, const char* src, size_t n) NLIB_NOEXCEPT {
127  return nlib_strncpy(dest, dest_size, src, n);
128 }
129 inline errno_t
130 StrNcpy(nlib_utf16_t* dest, size_t dest_size, const nlib_utf16_t* src, size_t n) NLIB_NOEXCEPT {
131  return nlib_utf16ncpy(dest, dest_size, src, n);
132 }
133 inline errno_t
134 StrNcpy(nlib_utf32_t* dest, size_t dest_size, const nlib_utf32_t* src, size_t n) NLIB_NOEXCEPT {
135  return nlib_utf32ncpy(dest, dest_size, src, n);
136 }
137 inline errno_t
138 StrNcpy(wchar_t* dest, size_t dest_size, const wchar_t* src, size_t n) NLIB_NOEXCEPT {
139  return nlib_wcsncpy(dest, dest_size, src, n);
140 }
141 
142 template<class T, size_t N>
143 NLIB_ALWAYS_INLINE errno_t StrNcpy(T (&dest)[N], const T* src, size_t n) NLIB_NOEXCEPT {
144  return StrNcpy(&dest[0], N, src, n);
145 }
146 
147 template<class T, size_t N>
148 NLIB_ALWAYS_INLINE errno_t StrCat(T (&dest)[N], const T* src) NLIB_NOEXCEPT {
149  return StrCat(&dest[0], N, src);
150 }
151 
152 template<class T, size_t N>
153 NLIB_ALWAYS_INLINE errno_t StrNcat(T (&dest)[N], const T* src, size_t n) NLIB_NOEXCEPT {
154  return StrNcat(&dest[0], N, src, n);
155 }
156 
157 // NOTE: It is possible that NLIB_VSNPRINTF is VsnPrintfFallback.
158 NLIB_VIS_PUBLIC int
159 VsnPrintfFallback(char* buf, size_t size, _Printf_format_string_ const char* fmt,
160  va_list args) NLIB_NOEXCEPT;
161 
162 NLIB_VIS_PUBLIC int
163 VsnPrintfFallback(wchar_t* buf, size_t size, _Printf_format_string_ const wchar_t* fmt,
164  va_list args) NLIB_NOEXCEPT;
165 
166 NLIB_VIS_PUBLIC int
167 VsnPrintfFallback(nlib_utf16_t* buf, size_t size, _Printf_format_string_ const nlib_utf16_t* fmt,
168  va_list args) NLIB_NOEXCEPT;
169 
170 NLIB_VIS_PUBLIC int
171 VsnPrintfFallback(nlib_utf32_t* buf, size_t size, _Printf_format_string_ const nlib_utf32_t* fmt,
172  va_list args) NLIB_NOEXCEPT;
173 
174 template<size_t N>
175 NLIB_ALWAYS_INLINE int VsnPrintfFallback(char (&buf)[N], _Printf_format_string_ const char* fmt,
176  va_list args) NLIB_NOEXCEPT {
177  return VsnPrintfFallback(buf, N, fmt, args);
178 }
179 
180 inline int SnPrintfFallback(char* buf, size_t size, _Printf_format_string_ const char* fmt,
181  ...) NLIB_NOEXCEPT {
182  va_list args;
183  va_start(args, fmt);
184  int rval = VsnPrintfFallback(buf, size, fmt, args);
185  va_end(args);
186  return rval;
187 }
188 
189 template<size_t N>
190 inline int
191 SnPrintfFallback(char (&buf)[N], _Printf_format_string_ const char* fmt, ...) NLIB_NOEXCEPT {
192  va_list args;
193  va_start(args, fmt);
194  int rval = VsnPrintfFallback(buf, N, fmt, args);
195  va_end(args);
196  return rval;
197 }
198 
199 template<size_t N>
201 VsnPrintfFallback(wchar_t (&buf)[N], _Printf_format_string_ const wchar_t* fmt,
202  va_list args) NLIB_NOEXCEPT {
203  return VsnPrintfFallback(buf, N, fmt, args);
204 }
205 
206 inline int SnPrintfFallback(wchar_t* buf, size_t size, _Printf_format_string_ const wchar_t* fmt,
207  ...) NLIB_NOEXCEPT {
208  va_list args;
209  va_start(args, fmt);
210  int rval = VsnPrintfFallback(buf, size, fmt, args);
211  va_end(args);
212  return rval;
213 }
214 
215 template<size_t N>
216 inline int
217 SnPrintfFallback(wchar_t (&buf)[N], _Printf_format_string_ const wchar_t* fmt, ...) NLIB_NOEXCEPT {
218  va_list args;
219  va_start(args, fmt);
220  int rval = VsnPrintfFallback(buf, N, fmt, args);
221  va_end(args);
222  return rval;
223 }
224 
225 template<size_t N>
227 VsnPrintfFallback(nlib_utf16_t (&buf)[N], _Printf_format_string_ const nlib_utf16_t* fmt,
228  va_list args) NLIB_NOEXCEPT {
229  return VsnPrintfFallback(buf, N, fmt, args);
230 }
231 
232 inline int SnPrintfFallback(nlib_utf16_t* buf, size_t size,
233  _Printf_format_string_ const nlib_utf16_t* fmt, ...) NLIB_NOEXCEPT {
234  va_list args;
235  va_start(args, fmt);
236  int rval = VsnPrintfFallback(buf, size, fmt, args);
237  va_end(args);
238  return rval;
239 }
240 
241 template<size_t N>
242 inline int SnPrintfFallback(nlib_utf16_t (&buf)[N], _Printf_format_string_ const nlib_utf16_t* fmt,
243  ...) NLIB_NOEXCEPT {
244  va_list args;
245  va_start(args, fmt);
246  int rval = VsnPrintfFallback(buf, N, fmt, args);
247  va_end(args);
248  return rval;
249 }
250 
251 template<size_t N>
253 VsnPrintfFallback(nlib_utf32_t (&buf)[N], _Printf_format_string_ const nlib_utf32_t* fmt,
254  va_list args) NLIB_NOEXCEPT {
255  return VsnPrintfFallback(buf, N, fmt, args);
256 }
257 
258 inline int SnPrintfFallback(nlib_utf32_t* buf, size_t size,
259  _Printf_format_string_ const nlib_utf32_t* fmt, ...) NLIB_NOEXCEPT {
260  va_list args;
261  va_start(args, fmt);
262  int rval = VsnPrintfFallback(buf, size, fmt, args);
263  va_end(args);
264  return rval;
265 }
266 
267 template<size_t N>
268 inline int SnPrintfFallback(nlib_utf32_t (&buf)[N], _Printf_format_string_ const nlib_utf32_t* fmt,
269  ...) NLIB_NOEXCEPT {
270  va_list args;
271  va_start(args, fmt);
272  int rval = VsnPrintfFallback(buf, N, fmt, args);
273  va_end(args);
274  return rval;
275 }
276 
277 inline int VsnPrintf(char* buf, size_t size, _Printf_format_string_ const char* fmt,
278  va_list args) NLIB_NOEXCEPT {
279  size_t count;
280  errno_t e = nlib_vsnprintf(&count, buf, size, fmt, args);
281  if (e != 0) errno = e;
282  return (e == 0) ? static_cast<int>(count) : -1;
283 }
284 
285 inline int VsnPrintf(wchar_t* buf, size_t size, _Printf_format_string_ const wchar_t* fmt,
286  va_list args) NLIB_NOEXCEPT {
287  size_t count;
288  errno_t e = nlib_vsnwprintf(&count, buf, size, fmt, args);
289  if (e != 0) errno = e;
290  return (e == 0) ? static_cast<int>(count) : -1;
291 }
292 
293 inline int VsnPrintf(nlib_utf16_t* buf, size_t size, _Printf_format_string_ const nlib_utf16_t* fmt,
294  va_list args) NLIB_NOEXCEPT {
295 #if NLIB_WCHAR_SIZE == 2
296  size_t count;
297  errno_t e = nlib_vsnwprintf(&count, reinterpret_cast<wchar_t*>(buf), size,
298  reinterpret_cast<const wchar_t*>(fmt), args);
299  if (e != 0) errno = e;
300  return (e == 0) ? static_cast<int>(count) : -1;
301 #else
302  return VsnPrintfFallback(buf, size, fmt, args);
303 #endif
304 }
305 
306 inline int VsnPrintf(nlib_utf32_t* buf, size_t size, _Printf_format_string_ const nlib_utf32_t* fmt,
307  va_list args) NLIB_NOEXCEPT {
308 #if NLIB_WCHAR_SIZE == 4
309  size_t count;
310  errno_t e = nlib_vsnwprintf(&count, reinterpret_cast<wchar_t*>(buf), size,
311  reinterpret_cast<const wchar_t*>(fmt), args);
312  if (e != 0) errno = e;
313  return (e == 0) ? static_cast<int>(count) : -1;
314 #else
315  return VsnPrintfFallback(buf, size, fmt, args);
316 #endif
317 }
318 
319 template<size_t N, class CH>
321 VsnPrintf(CH (&buf)[N], _Printf_format_string_ const CH* fmt, va_list args) NLIB_NOEXCEPT {
322  return VsnPrintf(buf, N, fmt, args);
323 }
324 
325 template<class CH>
326 inline int SnPrintf(CH* buf, size_t size, _Printf_format_string_ const CH* fmt, ...) NLIB_NOEXCEPT {
327  va_list args;
328  va_start(args, fmt);
329  int rval = VsnPrintf(buf, size, fmt, args);
330  va_end(args);
331  return rval;
332 }
333 
334 template<size_t N, class CH>
335 inline int SnPrintf(CH (&buf)[N], _Printf_format_string_ const CH* fmt, ...) NLIB_NOEXCEPT {
336  va_list args;
337  va_start(args, fmt);
338  int rval = VsnPrintf(buf, N, fmt, args);
339  va_end(args);
340  return rval;
341 }
342 
343 NLIB_NAMESPACE_END
344 
345 #if defined(_MSC_VER)
346 #if defined(n_EXPORTS)
347 #undef NLIB_VIS_PUBLIC
348 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
349 #elif defined(nx_misc_EXPORTS)
350 #undef NLIB_VIS_PUBLIC
351 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
352 #endif
353 #endif
354 
355 #endif // INCLUDE_NN_NLIB_CSTRING_H_
static errno_t nlib_utf16ncpy(nlib_utf16_t *s1, size_t s1max, const nlib_utf16_t *s2, size_t n)
nlib_strcpy()のUTF-16版です。
Definition: Platform.h:2248
static size_t nlib_utf16len(const nlib_utf16_t *str)
ヌル文字を含まないnlib_utf16_tの数を数えます。
Definition: Platform.h:2236
errno_t nlib_wcscplen(size_t *count, const wchar_t *str)
文字列中のコードポイントの数を取得します。
errno_t StrCpy(T(&dest)[N], const T *src) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Cstring.h:122
#define NLIB_ALWAYS_INLINE
コンパイラに関数をインライン展開するように強く示します。
Definition: Platform_unix.h:95
errno_t nlib_vsnwprintf(size_t *count, wchar_t(&buf)[N], const wchar_t *fmt, va_list args) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Config.h:490
errno_t nlib_strncpy(char(&s1)[N], const char *s2, size_t n) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Config.h:555
#define NLIB_UNLIKELY(x)
条件xが偽になる傾向が高いことをコンパイラに示します。
Definition: Platform_unix.h:98
int VsnPrintf(CH(&buf)[N], const CH *fmt, va_list args) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Cstring.h:321
int VsnPrintfFallback(char *buf, size_t size, const char *fmt, va_list args) noexcept
VsnPrintfのフォールバックです。
static size_t nlib_utf32len(const nlib_utf32_t *str)
ヌル文字を含まないnlib_utf32_tの数を数えます。
Definition: Platform.h:2261
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:87
static errno_t nlib_utf32cpy(nlib_utf32_t *s1, size_t s1max, const nlib_utf32_t *s2)
nlib_strcpy()のUTF-32版です。
Definition: Platform.h:2269
errno_t nlib_strcplen(size_t *codepoint_count, size_t *supplementary_codepoint_count, size_t *len, const nlib_utf8_t *str)
文字列中のコードポイントの数と補助文字の数と文字列長を取得します。 EILSEQを返す場合は、その場所までの...
int StrCmp(const nlib_utf32_t *s1, const nlib_utf32_t *s2) noexcept
UTF-32文字列をstrcmp()と同様に比較します。
Definition: Cstring.h:74
uint32_t nlib_utf32_t
char32_tが利用できる場合はchar32_tに、そうでない場合はuint32_tにtypedefされます。 ...
Definition: Platform.h:289
static errno_t nlib_utf16cpy(nlib_utf16_t *s1, size_t s1max, const nlib_utf16_t *s2)
nlib_strcpy()のUTF-16版です。
Definition: Platform.h:2244
int StrNcmp(const nlib_utf32_t *s1, const nlib_utf32_t *s2, size_t n) noexcept
UTF-32文字列をstrncmp()と同様に比較します。
Definition: Cstring.h:97
errno_t nlib_utf32cplen(size_t *count, const nlib_utf32_t *str)
文字列中のコードポイントの数を取得します。
uint16_t nlib_utf16_t
char16_tが利用できる場合はchar16_tに、そうでない場合はuint16_tにtypedefされます。 ...
Definition: Platform.h:288
static errno_t nlib_utf32ncpy(nlib_utf32_t *s1, size_t s1max, const nlib_utf32_t *s2, size_t n)
nlib_strcpy()のUTF-32版です。
Definition: Platform.h:2273
int SnPrintf(CH(&buf)[N], const CH *fmt,...) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Cstring.h:335
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:109
size_t nlib_wcslen(const wchar_t *s)
内部でwcslen()を呼び出します。独自の実装が動作する場合もあります。
開発環境別の設定が書かれるファイルです。
errno_t CodePointCount(const wchar_t *str, size_t *count) noexcept
nlib_wcscplen()をラップします。
Definition: Cstring.h:58
static errno_t nlib_utf16cplen(size_t *count, const nlib_utf16_t *str)
文字列中のコードポイントの数を取得します。
Definition: Platform.h:2281
errno_t nlib_vsnprintf(size_t *count, char(&buf)[N], const char *fmt, va_list args) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Config.h:472
size_t nlib_strlen(const char *s)
内部でstrlen()を呼び出します。独自の実装が動作する場合もあります。
errno_t nlib_wcscpy(wchar_t(&s1)[N], const wchar_t *s2) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Config.h:560
errno_t StrNcat(T(&dest)[N], const T *src, size_t n) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Cstring.h:153
size_t StrLen(const wchar_t *str) noexcept
nlib_wcslen()をラップします。
Definition: Cstring.h:45
errno_t StrNcpy(T(&dest)[N], const T *src, size_t n) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Cstring.h:143
errno_t nlib_wcsncpy(wchar_t(&s1)[N], const wchar_t *s2, size_t n) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Config.h:565
errno_t nlib_strcpy(char(&s1)[N], const char *s2) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Config.h:544
errno_t StrCat(T(&dest)[N], const T *src) noexcept
上記関数のテンプレートオーバーロードです。
Definition: Cstring.h:148
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37