20 using locale_t = _locale_t;
25 struct free_locale_delete
30 void operator()(_In_ locale_t locale)
const
36 static std::unique_ptr<__crt_locale_pointers, free_locale_delete> locale_C(_create_locale(LC_ALL,
"C"));
38 using locale_t = ::locale_t;
45 typedef wchar_t utf16_t;
47 typedef char16_t utf16_t;
55 inline bool is_high_surrogate(_In_ utf16_t chr)
57 return 0xd800 < chr && chr < 0xdc00;
65 inline bool is_low_surrogate(_In_ utf16_t chr)
67 return 0xdc00 < chr && chr < 0xe000;
75 inline bool is_surrogate_pair(_In_reads_(2)
const utf16_t* str)
77 return is_high_surrogate(str[0]) && is_low_surrogate(str[1]);
85 inline char32_t surrogate_pair_to_ucs4(_In_reads_(2)
const utf16_t* str)
87 assert(is_surrogate_pair(str));
89 ((
char32_t)(str[0] - 0xd800) << 10) +
90 (char32_t)(str[1] - 0xdc00) +
99 inline void ucs4_to_surrogate_pair(_Out_writes_(2) utf16_t* str, _In_
char32_t chr)
101 assert(chr >= 0x10000);
103 str[0] = 0xd800 + (char32_t)((chr >> 10) & 0x3ff);
104 str[1] = 0xdc00 + (char32_t)(chr & 0x3ff);
112 inline bool iscombining(_In_
char32_t chr)
115 0x0300 <= chr && chr < 0x0370 ||
116 0x1dc0 <= chr && chr < 0x1e00 ||
117 0x20d0 <= chr && chr < 0x2100 ||
118 0xfe20 <= chr && chr < 0xfe30;
127 inline size_t islbreak(_In_ T chr)
129 return chr ==
'\n' || chr ==
'\r';
139 inline size_t islbreak(_In_reads_or_z_opt_(count)
const T* chr, _In_
size_t count)
141 _Analysis_assume_(chr || !count);
142 if (count >= 2 && (chr[0] ==
'\r' && chr[1] ==
'\n' || chr[0] ==
'\n' && chr[1] ==
'\r'))
144 if (count > 1 && (chr[0] ==
'\n' || chr[0] ==
'\r'))
155 inline size_t glyphlen(_In_reads_or_z_opt_(count)
const wchar_t* glyph, _In_
size_t count)
157 _Analysis_assume_(glyph || !count);
160 size_t i = count < 2 || !is_surrogate_pair(glyph) ? 1 : 2;
164 for (; i < count && iscombining(glyph[i]); ++i);
178 inline size_t strlen(_In_z_
const T* str)
182 for (i = 0; str[i]; ++i);
195 inline size_t strnlen(_In_reads_or_z_opt_(count)
const T* str, _In_
size_t count)
199 for (i = 0; i < count && str[i]; ++i);
203 constexpr auto npos{
static_cast<size_t>(-1) };
215 inline size_t strnchr(
216 _In_reads_or_z_opt_(count)
const T* str,
220 assert(str || !count);
221 for (
size_t i = 0; i < count && str[i]; ++i)
222 if (str[i] == chr)
return i;
236 inline size_t strrnchr(
237 _In_reads_or_z_opt_(count)
const T* str,
241 assert(str || !count);
243 for (
size_t i = 0; i < count && str[i]; ++i)
244 if (str[i] == chr) z = i;
258 inline size_t strnichr(
259 _In_reads_or_z_opt_(count)
const T* str,
262 _In_
const std::locale& locale)
264 assert(str || !count);
265 const auto& ctype = std::use_facet<std::ctype<T>>(locale);
266 chr = ctype.tolower(chr);
267 for (
size_t i = 0; i < count && str[i]; ++i)
268 if (ctype.tolower(str[i]) == chr)
return i;
282 inline size_t strrnichr(
283 _In_reads_or_z_opt_(count)
const T* str,
286 _In_
const std::locale& locale)
288 assert(str || !count);
289 const auto& ctype = std::use_facet<std::ctype<T>>(locale);
290 chr = ctype.tolower(chr);
292 for (
size_t i = 0; i < count && str[i]; ++i)
293 if (ctype.tolower(str[i]) == chr) z = i;
307 template <
class T1,
class T2>
309 _In_reads_or_z_opt_(count1)
const T1* str1, _In_
size_t count1,
310 _In_reads_or_z_opt_(count2)
const T2* str2, _In_
size_t count2)
312 assert(str1 || !count1);
313 assert(str2 || !count2);
314 size_t i; T1 a; T2 b;
315 for (i = 0; i < count1 && i < count2 && ((a = str1[i]) | (b = str2[i])); ++i) {
316 if (a > b)
return +1;
317 if (a < b)
return -1;
319 if (i < count1 && str1[i])
return +1;
320 if (i < count2 && str2[i])
return -1;
336 _In_reads_or_z_opt_(count1)
const T* str1, _In_
size_t count1,
337 _In_reads_or_z_opt_(count2)
const T* str2, _In_
size_t count2,
338 _In_
const std::locale& locale)
340 assert(str1 || !count1);
341 assert(str2 || !count2);
342 auto& collate = std::use_facet<std::collate<T>>(locale);
343 return collate.compare(str1, str1 + count1, str2, str2 + count2);
356 template <
class T1,
class T2>
358 _In_reads_or_z_opt_(count1)
const T1* str1, _In_
size_t count1,
359 _In_reads_or_z_opt_(count2)
const T2* str2, _In_
size_t count2,
360 _In_
const std::locale& locale)
362 assert(str1 || !count1);
363 assert(str2 || !count2);
364 size_t i; T1 a; T2 b;
365 const auto& ctype1 = std::use_facet<std::ctype<T1>>(locale);
366 const auto& ctype2 = std::use_facet<std::ctype<T2>>(locale);
367 for (i = 0; i < count1 && i < count2 && ((a = ctype1.tolower(str1[i])) | (b = ctype2.tolower(str2[i]))); i++) {
368 if (a > b)
return +1;
369 if (a < b)
return -1;
371 if (i < count1 && str1[i])
return +1;
372 if (i < count2 && str2[i])
return -1;
385 template <
class T1,
class T2>
386 inline size_t strnstr(
387 _In_reads_or_z_opt_(count)
const T1* str,
389 _In_z_
const T2* sample)
391 assert(str || !count);
393 for (
size_t offset = 0;; ++offset) {
394 for (
size_t i = offset, j = 0;; ++i, ++j) {
397 if (i >= count || !str[i])
399 if (str[i] != sample[j])
414 template <
class T1,
class T2>
415 inline size_t strnistr(
416 _In_reads_or_z_opt_(count)
const T1* str,
418 _In_z_
const T2* sample,
419 _In_
const std::locale& locale)
421 assert(str || !count);
423 const auto& ctype1 = std::use_facet<std::ctype<T1>>(locale);
424 const auto& ctype2 = std::use_facet<std::ctype<T2>>(locale);
425 for (
size_t offset = 0;; ++offset) {
426 for (
size_t i = offset, j = 0;; ++i, ++j) {
429 if (i >= count || !str[i])
431 if (ctype1.tolower(str[i]) != ctype2.tolower(sample[j]))
445 template <
class T1,
class T2>
446 inline size_t strcpy(
447 _Out_writes_z_(_String_length_(src) + 1) T1* dst,
448 _In_z_
const T2* src)
451 for (
size_t i = 0; ; ++i) {
452 if ((dst[i] = src[i]) == 0)
466 template <
class T1,
class T2>
467 inline size_t strncpy(
468 _Out_writes_(count) _Post_maybez_ T1* dst,
469 _In_reads_or_z_opt_(count)
const T2* src, _In_
size_t count)
471 assert(dst && src || !count);
472 for (
size_t i = 0; ; ++i) {
475 if ((dst[i] = src[i]) == 0)
490 template <
class T1,
class T2>
491 inline size_t strncpy(
492 _Out_writes_(count_dst) _Post_maybez_ T1* dst, _In_
size_t count_dst,
493 _In_reads_or_z_opt_(count_src)
const T2* src, _In_
size_t count_src)
495 assert(dst || !count_dst);
496 assert(src || !count_src);
497 for (
size_t i = 0; ; ++i)
505 if ((dst[i] = src[i]) == 0)
518 template <
class T1,
class T2>
519 inline size_t strcat(
520 _In_z_ _Out_writes_z_(_String_length_(dst) + _String_length_(src) + 1) T1* dst,
521 _In_z_
const T2* src)
524 for (
size_t i = 0, j = stdex::strlen<T1>(dst); ; ++i, ++j) {
525 if ((dst[j] = src[i]) == 0)
539 template <
class T1,
class T2>
540 inline size_t strncat(
541 _Out_writes_(count) _Post_maybez_ T1* dst,
542 _In_reads_or_z_opt_(count)
const T2* src, _In_
size_t count)
544 assert(dst && src || !count);
545 for (
size_t i = 0, j = stdex::strlen<T1>(dst); ; ++i, ++j) {
548 if ((dst[j] = src[i]) == 0)
563 template <
class T1,
class T2>
564 inline size_t strncat(
565 _Out_writes_(count_dst) _Post_maybez_ T1* dst, _In_
size_t count_dst,
566 _In_reads_or_z_opt_(count_src)
const T2* src, _In_
size_t count_src)
568 assert(dst || !count_dst);
569 assert(src || !count_src);
570 for (
size_t i = 0, j = stdex::strnlen<T1>(dst, count_dst); ; ++i, ++j)
578 if ((dst[j] = src[i]) == 0)
593 inline size_t crlf2nl(_Out_writes_z_(strlen(src)) T* dst, _In_z_
const T* src)
598 for (i = j = 0; src[j];) {
599 if (src[j] !=
'\r' || src[j + 1] !=
'\n')
611 template <
class T,
class T_bin>
612 inline T_bin strtoint(
613 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
614 _Out_opt_
size_t* end,
616 _Out_ uint8_t& flags)
618 assert(str || !count);
619 assert(radix == 0 || 2 <= radix && radix <= 36);
622 T_bin value = 0, digit,
624 max_ui_pre1, max_ui_pre2;
630 if (i >= count || !str[i])
goto error;
631 if (!isspace(str[i]))
break;
638 if (i >= count || !str[i])
goto error;
640 else if (str[i] ==
'-') {
643 if (i >= count || !str[i])
goto error;
648 if (str[i] ==
'0' && i + 1 < count && (str[i + 1] ==
'x' || str[i + 1] ==
'X')) {
650 if (i >= count || !str[i])
goto error;
657 if (i >= count || !str[i])
goto error;
658 if (str[i] ==
'x' || str[i] ==
'X') {
661 if (i >= count || !str[i])
goto error;
671 max_ui_pre1 = max_ui / (T_bin)radix;
672 max_ui_pre2 = max_ui % (T_bin)radix;
674 if (
'0' <= str[i] && str[i] <=
'9')
675 digit = (T_bin)str[i] -
'0';
676 else if (
'A' <= str[i] && str[i] <=
'Z')
677 digit = (T_bin)str[i] -
'A' +
'\x0a';
678 else if (
'a' <= str[i] && str[i] <=
'z')
679 digit = (T_bin)str[i] -
'a' +
'\x0a';
682 if (digit >= (T_bin)radix)
685 if (value < max_ui_pre1 ||
686 value == max_ui_pre1 && digit <= max_ui_pre2)
687 value = value * (T_bin)radix + digit;
694 if (i >= count || !str[i])
714 template <
class T,
class T_bin>
716 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
717 _Out_opt_
size_t* end,
723 switch (
sizeof(T_bin)) {
725 value = (T_bin)strtoint<T, uint8_t>(str, count, end, radix, flags);
726 if ((flags & 0x01) && (value & 0x80)) {
730 return (flags & 0x02) ?
731 (flags & 0x01) ? (T_bin)0x80 : (T_bin)0x7f :
732 (flags & 0x01) ? -value : value;
735 value = (T_bin)strtoint<T, uint16_t>(str, count, end, radix, flags);
736 if ((flags & 0x01) && (value & 0x8000)) {
740 return (flags & 0x02) ?
741 (flags & 0x01) ? (T_bin)0x8000 : (T_bin)0x7fff :
742 (flags & 0x01) ? -value : value;
745 value = (T_bin)strtoint<T, uint32_t>(str, count, end, radix, flags);
746 if ((flags & 0x01) && (value & 0x80000000)) {
750 return (flags & 0x02) ?
751 (flags & 0x01) ? (T_bin)0x80000000 : (T_bin)0x7fffffff :
752 (flags & 0x01) ? -value : value;
755 value = (T_bin)strtoint<T, uint64_t>(str, count, end, radix, flags);
756 if ((flags & 0x01) && (value & 0x8000000000000000)) {
760 return (flags & 0x02) ?
761 (flags & 0x01) ? (T_bin)0x8000000000000000 : (T_bin)0x7fffffffffffffff :
762 (flags & 0x01) ? -value : value;
765 throw std::invalid_argument(
"Unsupported bit length");
779 template <
class T,
class T_bin>
780 inline T_bin strtouint(
781 _In_reads_or_z_opt_(count)
const T* str,
783 _Out_opt_
size_t* end,
789 switch (
sizeof(T_bin)) {
790 case 1: value = (T_bin)strtoint<T, uint8_t>(str, count, end, radix, flags);
break;
791 case 2: value = (T_bin)strtoint<T, uint16_t>(str, count, end, radix, flags);
break;
792 case 4: value = (T_bin)strtoint<T, uint32_t>(str, count, end, radix, flags);
break;
793 case 8: value = (T_bin)strtoint<T, uint64_t>(str, count, end, radix, flags);
break;
794 default:
throw std::invalid_argument(
"Unsupported bit length");
797 return (flags & 0x02) ?
798 (flags & 0x01) ? (T_bin)0 : (T_bin)-1 :
799 (flags & 0x01) ? ~value : value;
813 inline int32_t strto32(
814 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
815 _Out_opt_
size_t* end,
818 return strtoint<T, int32_t>(str, count, end, radix);
832 inline int64_t strto64(
833 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
834 _Out_opt_
size_t* end,
837 return strtoint<T, int64_t>(str, count, end, radix);
852 inline intptr_t strtoi(
853 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
854 _Out_opt_
size_t* end,
857#if defined(_WIN64) || defined(__LP64__)
858 return (intptr_t)strto64(str, count, end, radix);
860 return (intptr_t)strto32(str, count, end, radix);
875 inline uint32_t strtou32(
876 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
877 _Out_opt_
size_t* end,
880 return strtouint<T, uint32_t>(str, count, end, radix);
894 inline uint64_t strtou64(
895 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
896 _Out_opt_
size_t* end,
899 return strtouint<T, uint64_t>(str, count, end, radix);
914 inline size_t strtoui(
915 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
916 _Out_opt_
size_t* end,
919#if defined(_WIN64) || defined(__LP64__)
920 return (
size_t)strtou64(str, count, end, radix);
922 return (
size_t)strtou32(str, count, end, radix);
927 inline int vsnprintf(_Out_z_cap_(capacity)
char *str, _In_
size_t capacity, _In_z_ _Printf_format_string_params_(2)
const char *format, _In_opt_ locale_t locale, _In_ va_list arg)
932#pragma warning(suppress: 4996)
933 r = _vsnprintf_l(str, capacity, format, locale, arg);
935 r = vsnprintf(str, capacity, format, arg);
937 if (r == -1 && strnlen(str, capacity) == capacity) {
939 capacity += std::max<size_t>(capacity / 8, 0x80);
940 if (capacity > INT_MAX)
941 throw std::invalid_argument(
"string too big");
942 return (
int)capacity;
947 inline int vsnprintf(_Out_z_cap_(capacity)
wchar_t *str, _In_
size_t capacity, _In_z_ _Printf_format_string_params_(2)
const wchar_t *format, _In_opt_ locale_t locale, _In_ va_list arg)
953#pragma warning(suppress: 4996)
954 r = _vsnwprintf_l(str, capacity, format, locale, arg);
956 r = vswprintf(str, capacity, format, arg);
958 if (r == -1 && strnlen(str, capacity) == capacity) {
960 capacity += std::max<size_t>(capacity / 8, 0x80);
961 if (capacity > INT_MAX)
962 throw std::invalid_argument(
"string too big");
963 return (
int)capacity;
977 template<
class _Elem,
class _Traits,
class _Ax>
978 inline void vappendf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_params_(2)
const _Elem *format, _In_opt_ locale_t locale, _In_ va_list arg)
980 _Elem buf[1024/
sizeof(_Elem)];
983 int count = vsnprintf(buf, _countof(buf) - 1, format, locale, arg);
986 str.append(buf, count);
988 for (
size_t capacity = 2*1024/
sizeof(_Elem);; capacity *= 2) {
990 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
991 count = vsnprintf(buf_dyn.get(), capacity - 1, format, locale, arg);
993 str.append(buf_dyn.get(), count);
1007 template<
class _Elem,
class _Traits,
class _Ax>
1008 inline void appendf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_params_(2)
const _Elem *format, _In_opt_ locale_t locale, ...)
1011 va_start(arg, locale);
1012 vappendf(str, format, locale, arg);
1024 template<
class _Elem,
class _Traits,
class _Ax>
1025 inline void vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_params_(2)
const _Elem *format, _In_opt_ locale_t locale, _In_ va_list arg)
1028 vappendf(str, format, locale, arg);
1038 template<
class _Elem,
class _Traits,
class _Ax>
1039 inline void sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_params_(2)
const _Elem *format, _In_opt_ locale_t locale, ...)
1042 va_start(arg, locale);
1043 vsprintf(str, format, locale, arg);
1056 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
1057 inline std::basic_string<_Elem, _Traits, _Ax> vsprintf(_In_z_ _Printf_format_string_params_(2)
const _Elem *format, _In_opt_ locale_t locale, _In_ va_list arg)
1059 std::basic_string<_Elem, _Traits, _Ax> str;
1060 vappendf(str, format, locale, arg);
1072 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
1073 inline std::basic_string<_Elem, _Traits, _Ax> sprintf(_In_z_ _Printf_format_string_params_(2)
const _Elem *format, _In_opt_ locale_t locale, ...)
1076 va_start(arg, locale);
1077 auto str = vsprintf(format, locale, arg);