20 typedef wchar_t utf16_t;
22 typedef char16_t utf16_t;
30 inline bool is_high_surrogate(_In_ utf16_t chr)
32 return 0xd800 < chr && chr < 0xdc00;
40 inline bool is_low_surrogate(_In_ utf16_t chr)
42 return 0xdc00 < chr && chr < 0xe000;
50 inline bool is_surrogate_pair(_In_reads_(2)
const utf16_t* str)
52 return is_high_surrogate(str[0]) && is_low_surrogate(str[1]);
60 inline char32_t surrogate_pair_to_ucs4(_In_reads_(2)
const utf16_t* str)
62 assert(is_surrogate_pair(str));
64 ((
char32_t)(str[0] - 0xd800) << 10) +
65 (char32_t)(str[1] - 0xdc00) +
74 inline void ucs4_to_surrogate_pair(_Out_writes_(2) utf16_t* str, _In_
char32_t chr)
76 assert(chr >= 0x10000);
78 str[0] = 0xd800 + (char32_t)((chr >> 10) & 0x3ff);
79 str[1] = 0xdc00 + (char32_t)(chr & 0x3ff);
87 inline bool iscombining(_In_
char32_t chr)
90 0x0300 <= chr && chr < 0x0370 ||
91 0x1dc0 <= chr && chr < 0x1e00 ||
92 0x20d0 <= chr && chr < 0x2100 ||
93 0xfe20 <= chr && chr < 0xfe30;
102 inline size_t islbreak(_In_ T chr)
104 return chr ==
'\n' || chr ==
'\r';
114 inline size_t islbreak(_In_reads_or_z_opt_(count)
const T* chr, _In_
size_t count)
116 if (count >= 2 && (chr[0] ==
'\r' && chr[1] ==
'\n' || chr[0] ==
'\n' && chr[1] ==
'\r'))
118 if (count > 1 && (chr[0] ==
'\n' || chr[0] ==
'\r'))
129 inline size_t glyphlen(_In_reads_or_z_opt_(count)
const wchar_t* glyph,
size_t count)
133 size_t i = count < 2 || !is_surrogate_pair(glyph) ? 1 : 2;
137 for (; i < count && iscombining(glyph[i]); ++i);
151 inline size_t strlen(_In_z_
const T* str)
155 for (i = 0; str[i]; ++i);
168 inline size_t strnlen(_In_reads_or_z_opt_(count)
const T* str, _In_
size_t count)
172 for (i = 0; i < count && str[i]; ++i);
176 constexpr auto npos{
static_cast<size_t>(-1) };
188 inline size_t strnchr(
189 _In_reads_or_z_opt_(count)
const T* str,
193 assert(str || !count);
194 for (
size_t i = 0; i < count && str[i]; ++i)
195 if (str[i] == chr)
return i;
209 inline size_t strrnchr(
210 _In_reads_or_z_opt_(count)
const T* str,
214 assert(str || !count);
216 for (
size_t i = 0; i < count && str[i]; ++i)
217 if (str[i] == chr) z = i;
231 inline size_t strnichr(
232 _In_reads_or_z_opt_(count)
const T* str,
235 _In_
const std::locale& locale)
237 assert(str || !count);
238 const auto& ctype = std::use_facet<std::ctype<T>>(locale);
239 chr = ctype.tolower(chr);
240 for (
size_t i = 0; i < count && str[i]; ++i)
241 if (ctype.tolower(str[i]) == chr)
return i;
255 inline size_t strrnichr(
256 _In_reads_or_z_opt_(count)
const T* str,
259 _In_
const std::locale& locale)
261 assert(str || !count);
262 const auto& ctype = std::use_facet<std::ctype<T>>(locale);
263 chr = ctype.tolower(chr);
265 for (
size_t i = 0; i < count && str[i]; ++i)
266 if (ctype.tolower(str[i]) == chr) z = i;
280 template <
class T1,
class T2>
282 _In_reads_or_z_opt_(count1)
const T1* str1, _In_
size_t count1,
283 _In_reads_or_z_opt_(count2)
const T2* str2, _In_
size_t count2)
285 assert(str1 || !count1);
286 assert(str2 || !count2);
287 size_t i; T1 a; T2 b;
288 for (i = 0; i < count1 && i < count2 && ((a = str1[i]) | (b = str2[i])); ++i) {
289 if (a > b)
return +1;
290 if (a < b)
return -1;
292 if (i < count1 && str1[i])
return +1;
293 if (i < count2 && str2[i])
return -1;
309 _In_reads_or_z_opt_(count1)
const T* str1, _In_
size_t count1,
310 _In_reads_or_z_opt_(count2)
const T* str2, _In_
size_t count2,
311 _In_
const std::locale& locale)
313 assert(str1 || !count1);
314 assert(str2 || !count2);
315 auto& collate = std::use_facet<std::collate<T>>(locale);
316 return collate.compare(str1, str1 + count1, str2, str2 + count2);
329 template <
class T1,
class T2>
331 _In_reads_or_z_opt_(count1)
const T1* str1, _In_
size_t count1,
332 _In_reads_or_z_opt_(count2)
const T2* str2, _In_
size_t count2,
333 _In_
const std::locale& locale)
335 assert(str1 || !count1);
336 assert(str2 || !count2);
337 size_t i; T1 a; T2 b;
338 const auto& ctype1 = std::use_facet<std::ctype<T1>>(locale);
339 const auto& ctype2 = std::use_facet<std::ctype<T2>>(locale);
340 for (i = 0; i < count1 && i < count2 && ((a = ctype1.tolower(str1[i])) | (b = ctype2.tolower(str2[i]))); i++) {
341 if (a > b)
return +1;
342 if (a < b)
return -1;
344 if (i < count1 && str1[i])
return +1;
345 if (i < count2 && str2[i])
return -1;
358 template <
class T1,
class T2>
359 inline size_t strnstr(
360 _In_reads_or_z_opt_(count)
const T1* str,
362 _In_z_
const T2* sample)
364 assert(str || !count);
366 for (
size_t offset = 0;; ++offset) {
367 for (
size_t i = offset, j = 0;; ++i, ++j) {
370 if (i >= count || !str[i])
372 if (str[i] != sample[j])
387 template <
class T1,
class T2>
388 inline size_t strnistr(
389 _In_reads_or_z_opt_(count)
const T1* str,
391 _In_z_
const T2* sample,
392 _In_
const std::locale& locale)
394 assert(str || !count);
396 const auto& ctype1 = std::use_facet<std::ctype<T1>>(locale);
397 const auto& ctype2 = std::use_facet<std::ctype<T2>>(locale);
398 for (
size_t offset = 0;; ++offset) {
399 for (
size_t i = offset, j = 0;; ++i, ++j) {
402 if (i >= count || !str[i])
404 if (ctype1.tolower(str[i]) != ctype2.tolower(sample[j]))
419 template <
class T1,
class T2>
420 inline size_t strncpy(
421 _Out_writes_(count) _Post_maybez_ T1* dst,
422 _In_reads_or_z_opt_(count)
const T2* src, _In_
size_t count)
424 assert(dst && src || !count);
425 for (
size_t i = 0; ; ++i) {
428 if ((dst[i] = src[i]) == 0)
443 template <
class T1,
class T2>
444 inline size_t strncpy(
445 _Out_writes_(count_dst) _Post_maybez_ T1* dst, _In_
size_t count_dst,
446 _In_reads_or_z_opt_(count_src)
const T2* src, _In_
size_t count_src)
448 assert(dst || !count_dst);
449 assert(src || !count_src);
450 for (
size_t i = 0; ; ++i)
458 if ((dst[i] = src[i]) == 0)
473 inline size_t crlf2nl(_Out_writes_z_(strlen(src)) T* dst, _In_z_
const T* src)
478 for (i = j = 0; src[j];) {
479 if (src[j] !=
'\r' || src[j + 1] !=
'\n')
491 template <
class T,
class T_bin>
492 inline T_bin strtoint(
493 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
494 _Out_opt_
size_t* end,
496 _Out_ uint8_t& flags)
498 assert(str || !count);
499 assert(radix == 0 || 2 <= radix && radix <= 36);
502 T_bin value = 0, digit,
504 max_ui_pre1, max_ui_pre2;
510 if (i >= count || !str[i])
goto error;
511 if (!isspace(str[i]))
break;
518 if (i >= count || !str[i])
goto error;
520 else if (str[i] ==
'-') {
523 if (i >= count || !str[i])
goto error;
528 if (str[i] ==
'0' && i + 1 < count && (str[i + 1] ==
'x' || str[i + 1] ==
'X')) {
530 if (i >= count || !str[i])
goto error;
537 if (i >= count || !str[i])
goto error;
538 if (str[i] ==
'x' || str[i] ==
'X') {
541 if (i >= count || !str[i])
goto error;
551 max_ui_pre1 = max_ui / (T_bin)radix;
552 max_ui_pre2 = max_ui % (T_bin)radix;
554 if (
'0' <= str[i] && str[i] <=
'9')
555 digit = (T_bin)str[i] -
'0';
556 else if (
'A' <= str[i] && str[i] <=
'Z')
557 digit = (T_bin)str[i] -
'A' +
'\x0a';
558 else if (
'a' <= str[i] && str[i] <=
'z')
559 digit = (T_bin)str[i] -
'a' +
'\x0a';
562 if (digit >= (T_bin)radix)
565 if (value < max_ui_pre1 ||
566 value == max_ui_pre1 && digit <= max_ui_pre2)
567 value = value * (T_bin)radix + digit;
574 if (i >= count || !str[i])
594 template <
class T,
class T_bin>
596 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
597 _Out_opt_
size_t* end,
603 switch (
sizeof(T_bin)) {
605 value = (T_bin)strtoint<T, uint8_t>(str, count, end, radix, flags);
606 if ((flags & 0x01) && (value & 0x80)) {
610 return (flags & 0x02) ?
611 (flags & 0x01) ? (T_bin)0x80 : (T_bin)0x7f :
612 (flags & 0x01) ? -value : value;
615 value = (T_bin)strtoint<T, T_U2>(str, count, end, radix, flags);
616 if ((flags & 0x01) && (value & 0x8000)) {
620 return (flags & 0x02) ?
621 (flags & 0x01) ? (T_bin)0x8000 : (T_bin)0x7fff :
622 (flags & 0x01) ? -value : value;
625 value = (T_bin)strtoint<T, uint32_t>(str, count, end, radix, flags);
626 if ((flags & 0x01) && (value & 0x80000000)) {
630 return (flags & 0x02) ?
631 (flags & 0x01) ? (T_bin)0x80000000 : (T_bin)0x7fffffff :
632 (flags & 0x01) ? -value : value;
635 value = (T_bin)strtoint<T, uint64_t>(str, count, end, radix, flags);
636 if ((flags & 0x01) && (value & 0x8000000000000000)) {
640 return (flags & 0x02) ?
641 (flags & 0x01) ? (T_bin)0x8000000000000000 : (T_bin)0x7fffffffffffffff :
642 (flags & 0x01) ? -value : value;
645 throw std::invalid_argument(
"Unsupported bit length");
659 template <
class T,
class T_bin>
660 inline T_bin strtouint(
661 _In_reads_or_z_opt_(count)
const T* str,
663 _Out_opt_
size_t* end,
669 switch (
sizeof(T_bin)) {
670 case 1: value = (T_bin)strtoint<T, uint8_t>(str, count, end, radix, flags);
break;
671 case 2: value = (T_bin)strtoint<T, uint16_t>(str, count, end, radix, flags);
break;
672 case 4: value = (T_bin)strtoint<T, uint32_t>(str, count, end, radix, flags);
break;
673 case 8: value = (T_bin)strtoint<T, uint64_t>(str, count, end, radix, flags);
break;
674 default:
throw std::invalid_argument(
"Unsupported bit length");
677 return (flags & 0x02) ?
678 (flags & 0x01) ? (T_bin)0 : (T_bin)-1 :
679 (flags & 0x01) ? ~value : value;
693 inline int32_t strto32(
694 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
695 _Out_opt_
size_t* end,
698 return strtoint<T, int32_t>(str, count, end, radix);
712 inline int64_t strto64(
713 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
714 _Out_opt_
size_t* end,
717 return strtoint<T, int64_t>(str, count, end, radix);
732 inline intptr_t strtoi(
733 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
734 _Out_opt_
size_t* end,
737#if defined(_WIN64) || defined(__LP64__)
738 return (intptr_t)strto64(str, count, end, radix);
740 return (intptr_t)strto32(str, count, end, radix);
755 inline uint32_t strtou32(
756 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
757 _Out_opt_
size_t* end,
760 return strtouint<T, uint32_t>(str, count, end, radix);
774 inline uint64_t strtou64(
775 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
776 _Out_opt_
size_t* end,
779 return strtouint<T, uint64_t>(str, count, end, radix);
794 inline size_t strtoui(
795 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
796 _Out_opt_
size_t* end,
799#if defined(_WIN64) || defined(__LP64__)
800 return (
size_t)strtou64(str, count, end, radix);
802 return (
size_t)strtou32(str, count, end, radix);