20 using locale_t = _locale_t;
22 inline locale_t create_locale(_In_
int category, _In_z_
const char* locale) {
return _create_locale(category, locale); }
23 inline locale_t create_locale(_In_
int category, _In_z_
const wchar_t* locale) {
return _wcreate_locale(category, locale); }
24 inline void free_locale(_In_opt_ _locale_t locale) { _free_locale(locale); }
26 using locale_t = ::locale_t;
46 using locale = std::unique_ptr<__crt_locale_pointers, free_locale_delete>;
51 static locale locale_C(_create_locale(LC_ALL,
"C"));
57 typedef wchar_t utf16_t;
59 typedef char16_t utf16_t;
67 inline bool is_high_surrogate(_In_ utf16_t chr)
69 return 0xd800 < chr && chr < 0xdc00;
77 inline bool is_low_surrogate(_In_ utf16_t chr)
79 return 0xdc00 < chr && chr < 0xe000;
87 inline bool is_surrogate_pair(_In_reads_(2)
const utf16_t* str)
89 return is_high_surrogate(str[0]) && is_low_surrogate(str[1]);
97 inline char32_t surrogate_pair_to_ucs4(_In_reads_(2)
const utf16_t* str)
99 assert(is_surrogate_pair(str));
101 ((
char32_t)(str[0] - 0xd800) << 10) +
102 (char32_t)(str[1] - 0xdc00) +
111 inline void ucs4_to_surrogate_pair(_Out_writes_(2) utf16_t* str, _In_
char32_t chr)
113 assert(chr >= 0x10000);
115 str[0] = 0xd800 + (char32_t)((chr >> 10) & 0x3ff);
116 str[1] = 0xdc00 + (char32_t)(chr & 0x3ff);
124 inline bool iscombining(_In_
char32_t chr)
127 0x0300 <= chr && chr < 0x0370 ||
128 0x1dc0 <= chr && chr < 0x1e00 ||
129 0x20d0 <= chr && chr < 0x2100 ||
130 0xfe20 <= chr && chr < 0xfe30;
139 inline size_t islbreak(_In_ T chr)
141 return chr ==
'\n' || chr ==
'\r';
151 inline size_t islbreak(_In_reads_or_z_opt_(count)
const T* chr, _In_
size_t count)
153 _Analysis_assume_(chr || !count);
154 if (count >= 2 && (chr[0] ==
'\r' && chr[1] ==
'\n' || chr[0] ==
'\n' && chr[1] ==
'\r'))
156 if (count > 1 && (chr[0] ==
'\n' || chr[0] ==
'\r'))
167 inline size_t glyphlen(_In_reads_or_z_opt_(count)
const wchar_t* glyph, _In_
size_t count)
169 _Analysis_assume_(glyph || !count);
172 size_t i = count < 2 || !is_surrogate_pair(glyph) ? 1 : 2;
176 for (; i < count && iscombining(glyph[i]); ++i);
190 inline size_t strlen(_In_z_
const T* str)
194 for (i = 0; str[i]; ++i);
207 inline size_t strnlen(_In_reads_or_z_opt_(count)
const T* str, _In_
size_t count)
209 assert(str || !count);
211 for (i = 0; i < count && str[i]; ++i);
215 constexpr auto npos{
static_cast<size_t>(-1) };
227 inline size_t strnchr(
228 _In_reads_or_z_opt_(count)
const T* str,
232 assert(str || !count);
233 for (
size_t i = 0; i < count && str[i]; ++i)
234 if (str[i] == chr)
return i;
248 inline size_t strrnchr(
249 _In_reads_or_z_opt_(count)
const T* str,
253 assert(str || !count);
255 for (
size_t i = 0; i < count && str[i]; ++i)
256 if (str[i] == chr) z = i;
270 inline size_t strnichr(
271 _In_reads_or_z_opt_(count)
const T* str,
274 _In_
const std::locale& locale)
276 assert(str || !count);
277 const auto& ctype = std::use_facet<std::ctype<T>>(locale);
278 chr = ctype.tolower(chr);
279 for (
size_t i = 0; i < count && str[i]; ++i)
280 if (ctype.tolower(str[i]) == chr)
return i;
294 inline size_t strrnichr(
295 _In_reads_or_z_opt_(count)
const T* str,
298 _In_
const std::locale& locale)
300 assert(str || !count);
301 const auto& ctype = std::use_facet<std::ctype<T>>(locale);
302 chr = ctype.tolower(chr);
304 for (
size_t i = 0; i < count && str[i]; ++i)
305 if (ctype.tolower(str[i]) == chr) z = i;
319 template <
class T1,
class T2>
321 _In_reads_or_z_opt_(count1)
const T1* str1, _In_
size_t count1,
322 _In_reads_or_z_opt_(count2)
const T2* str2, _In_
size_t count2)
324 assert(str1 || !count1);
325 assert(str2 || !count2);
326 size_t i; T1 a; T2 b;
327 for (i = 0; i < count1 && i < count2 && ((a = str1[i]) | (b = str2[i])); ++i) {
328 if (a > b)
return +1;
329 if (a < b)
return -1;
331 if (i < count1 && str1[i])
return +1;
332 if (i < count2 && str2[i])
return -1;
348 _In_reads_or_z_opt_(count1)
const T* str1, _In_
size_t count1,
349 _In_reads_or_z_opt_(count2)
const T* str2, _In_
size_t count2,
350 _In_
const std::locale& locale)
352 assert(str1 || !count1);
353 assert(str2 || !count2);
354 auto& collate = std::use_facet<std::collate<T>>(locale);
355 return collate.compare(str1, str1 + count1, str2, str2 + count2);
368 template <
class T1,
class T2>
370 _In_reads_or_z_opt_(count1)
const T1* str1, _In_
size_t count1,
371 _In_reads_or_z_opt_(count2)
const T2* str2, _In_
size_t count2,
372 _In_
const std::locale& locale)
374 assert(str1 || !count1);
375 assert(str2 || !count2);
376 size_t i; T1 a; T2 b;
377 const auto& ctype1 = std::use_facet<std::ctype<T1>>(locale);
378 const auto& ctype2 = std::use_facet<std::ctype<T2>>(locale);
379 for (i = 0; i < count1 && i < count2 && ((a = ctype1.tolower(str1[i])) | (b = ctype2.tolower(str2[i]))); i++) {
380 if (a > b)
return +1;
381 if (a < b)
return -1;
383 if (i < count1 && str1[i])
return +1;
384 if (i < count2 && str2[i])
return -1;
397 template <
class T1,
class T2>
398 inline size_t strnstr(
399 _In_reads_or_z_opt_(count)
const T1* str,
401 _In_z_
const T2* sample)
403 assert(str || !count);
405 for (
size_t offset = 0;; ++offset) {
406 for (
size_t i = offset, j = 0;; ++i, ++j) {
409 if (i >= count || !str[i])
411 if (str[i] != sample[j])
426 template <
class T1,
class T2>
427 inline size_t strnistr(
428 _In_reads_or_z_opt_(count)
const T1* str,
430 _In_z_
const T2* sample,
431 _In_
const std::locale& locale)
433 assert(str || !count);
435 const auto& ctype1 = std::use_facet<std::ctype<T1>>(locale);
436 const auto& ctype2 = std::use_facet<std::ctype<T2>>(locale);
437 for (
size_t offset = 0;; ++offset) {
438 for (
size_t i = offset, j = 0;; ++i, ++j) {
441 if (i >= count || !str[i])
443 if (ctype1.tolower(str[i]) != ctype2.tolower(sample[j]))
457 template <
class T1,
class T2>
458 inline size_t strcpy(
459 _Out_writes_z_(_String_length_(src) + 1) T1* dst,
460 _In_z_
const T2* src)
463 for (
size_t i = 0; ; ++i) {
464 if ((dst[i] = src[i]) == 0)
478 template <
class T1,
class T2>
479 inline size_t strncpy(
480 _Out_writes_(count) _Post_maybez_ T1* dst,
481 _In_reads_or_z_opt_(count)
const T2* src, _In_
size_t count)
483 assert(dst && src || !count);
484 for (
size_t i = 0; ; ++i) {
487 if ((dst[i] = src[i]) == 0)
502 template <
class T1,
class T2>
503 inline size_t strncpy(
504 _Out_writes_(count_dst) _Post_maybez_ T1* dst, _In_
size_t count_dst,
505 _In_reads_or_z_opt_(count_src)
const T2* src, _In_
size_t count_src)
507 assert(dst || !count_dst);
508 assert(src || !count_src);
509 for (
size_t i = 0; ; ++i)
517 if ((dst[i] = src[i]) == 0)
530 template <
class T1,
class T2>
531 inline size_t strcat(
532 _In_z_ _Out_writes_z_(_String_length_(dst) + _String_length_(src) + 1) T1* dst,
533 _In_z_
const T2* src)
536 for (
size_t i = 0, j = stdex::strlen<T1>(dst); ; ++i, ++j) {
537 if ((dst[j] = src[i]) == 0)
551 template <
class T1,
class T2>
552 inline size_t strncat(
553 _Out_writes_(count) _Post_maybez_ T1* dst,
554 _In_reads_or_z_opt_(count)
const T2* src, _In_
size_t count)
556 assert(dst && src || !count);
557 for (
size_t i = 0, j = stdex::strlen<T1>(dst); ; ++i, ++j) {
560 if ((dst[j] = src[i]) == 0)
575 template <
class T1,
class T2>
576 inline size_t strncat(
577 _Out_writes_(count_dst) _Post_maybez_ T1* dst, _In_
size_t count_dst,
578 _In_reads_or_z_opt_(count_src)
const T2* src, _In_
size_t count_src)
580 assert(dst || !count_dst);
581 assert(src || !count_src);
582 for (
size_t i = 0, j = stdex::strnlen<T1>(dst, count_dst); ; ++i, ++j)
590 if ((dst[j] = src[i]) == 0)
606 inline _Check_return_ _Ret_maybenull_z_ T* strdup(_In_opt_z_
const T* str)
610 size_t count = strlen(str) + 1;
611 T* dst =
new T[count];
612 strncpy(dst, count, str, SIZE_MAX);
628 inline _Ret_z_ T* strndup(
629 _In_reads_or_z_opt_(count)
const T* str,
632 T* dst =
new T[count];
633 strncpy(dst, count, str, SIZE_MAX);
647 inline size_t crlf2nl(_Out_writes_z_(strlen(src)) T* dst, _In_z_
const T* src)
652 for (i = j = 0; src[j];) {
653 if (src[j] !=
'\r' || src[j + 1] !=
'\n')
665 template <
class T,
class T_bin>
666 inline T_bin strtoint(
667 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
668 _Out_opt_
size_t* end,
670 _Out_ uint8_t& flags)
672 assert(str || !count);
673 assert(radix == 0 || 2 <= radix && radix <= 36);
676 T_bin value = 0, digit,
678 max_ui_pre1, max_ui_pre2;
684 if (i >= count || !str[i])
goto error;
685 if (!isspace(str[i]))
break;
692 if (i >= count || !str[i])
goto error;
694 else if (str[i] ==
'-') {
697 if (i >= count || !str[i])
goto error;
702 if (str[i] ==
'0' && i + 1 < count && (str[i + 1] ==
'x' || str[i + 1] ==
'X')) {
704 if (i >= count || !str[i])
goto error;
711 if (i >= count || !str[i])
goto error;
712 if (str[i] ==
'x' || str[i] ==
'X') {
715 if (i >= count || !str[i])
goto error;
725 max_ui_pre1 = max_ui / (T_bin)radix;
726 max_ui_pre2 = max_ui % (T_bin)radix;
728 if (
'0' <= str[i] && str[i] <=
'9')
729 digit = (T_bin)str[i] -
'0';
730 else if (
'A' <= str[i] && str[i] <=
'Z')
731 digit = (T_bin)str[i] -
'A' +
'\x0a';
732 else if (
'a' <= str[i] && str[i] <=
'z')
733 digit = (T_bin)str[i] -
'a' +
'\x0a';
736 if (digit >= (T_bin)radix)
739 if (value < max_ui_pre1 ||
740 value == max_ui_pre1 && digit <= max_ui_pre2)
741 value = value * (T_bin)radix + digit;
748 if (i >= count || !str[i])
768 template <
class T,
class T_bin>
770 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
771 _Out_opt_
size_t* end,
777 switch (
sizeof(T_bin)) {
779 value = (T_bin)strtoint<T, uint8_t>(str, count, end, radix, flags);
780 if ((flags & 0x01) && (value & 0x80)) {
784 return (flags & 0x02) ?
785 (flags & 0x01) ? (T_bin)0x80 : (T_bin)0x7f :
786 (flags & 0x01) ? -value : value;
789 value = (T_bin)strtoint<T, uint16_t>(str, count, end, radix, flags);
790 if ((flags & 0x01) && (value & 0x8000)) {
794 return (flags & 0x02) ?
795 (flags & 0x01) ? (T_bin)0x8000 : (T_bin)0x7fff :
796 (flags & 0x01) ? -value : value;
799 value = (T_bin)strtoint<T, uint32_t>(str, count, end, radix, flags);
800 if ((flags & 0x01) && (value & 0x80000000)) {
804 return (flags & 0x02) ?
805 (flags & 0x01) ? (T_bin)0x80000000 : (T_bin)0x7fffffff :
806 (flags & 0x01) ? -value : value;
809 value = (T_bin)strtoint<T, uint64_t>(str, count, end, radix, flags);
810 if ((flags & 0x01) && (value & 0x8000000000000000)) {
814 return (flags & 0x02) ?
815 (flags & 0x01) ? (T_bin)0x8000000000000000 : (T_bin)0x7fffffffffffffff :
816 (flags & 0x01) ? -value : value;
819 throw std::invalid_argument(
"Unsupported bit length");
833 template <
class T,
class T_bin>
834 inline T_bin strtouint(
835 _In_reads_or_z_opt_(count)
const T* str,
837 _Out_opt_
size_t* end,
843 switch (
sizeof(T_bin)) {
844 case 1: value = (T_bin)strtoint<T, uint8_t>(str, count, end, radix, flags);
break;
845 case 2: value = (T_bin)strtoint<T, uint16_t>(str, count, end, radix, flags);
break;
846 case 4: value = (T_bin)strtoint<T, uint32_t>(str, count, end, radix, flags);
break;
847 case 8: value = (T_bin)strtoint<T, uint64_t>(str, count, end, radix, flags);
break;
848 default:
throw std::invalid_argument(
"Unsupported bit length");
851 return (flags & 0x02) ?
852 (flags & 0x01) ? (T_bin)0 : (T_bin)-1 :
853 (flags & 0x01) ? ~value : value;
867 inline int32_t strto32(
868 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
869 _Out_opt_
size_t* end,
872 return strtoint<T, int32_t>(str, count, end, radix);
886 inline int64_t strto64(
887 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
888 _Out_opt_
size_t* end,
891 return strtoint<T, int64_t>(str, count, end, radix);
906 inline intptr_t strtoi(
907 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
908 _Out_opt_
size_t* end,
911#if defined(_WIN64) || defined(__LP64__)
912 return (intptr_t)strto64(str, count, end, radix);
914 return (intptr_t)strto32(str, count, end, radix);
929 inline uint32_t strtou32(
930 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
931 _Out_opt_
size_t* end,
934 return strtouint<T, uint32_t>(str, count, end, radix);
948 inline uint64_t strtou64(
949 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
950 _Out_opt_
size_t* end,
953 return strtouint<T, uint64_t>(str, count, end, radix);
968 inline size_t strtoui(
969 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
970 _Out_opt_
size_t* end,
973#if defined(_WIN64) || defined(__LP64__)
974 return (
size_t)strtou64(str, count, end, radix);
976 return (
size_t)strtou32(str, count, end, radix);
981 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)
986#pragma warning(suppress: 4996)
987 r = _vsnprintf_l(str, capacity, format, locale, arg);
989 r = vsnprintf(str, capacity, format, arg);
991 if (r == -1 && strnlen(str, capacity) == capacity) {
993 capacity += std::max<size_t>(capacity / 8, 0x80);
994 if (capacity > INT_MAX)
995 throw std::invalid_argument(
"string too big");
996 return (
int)capacity;
1001 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)
1007#pragma warning(suppress: 4996)
1008 r = _vsnwprintf_l(str, capacity, format, locale, arg);
1010 r = vswprintf(str, capacity, format, arg);
1012 if (r == -1 && strnlen(str, capacity) == capacity) {
1014 capacity += std::max<size_t>(capacity / 8, 0x80);
1015 if (capacity > INT_MAX)
1016 throw std::invalid_argument(
"string too big");
1017 return (
int)capacity;
1031 template<
class _Elem,
class _Traits,
class _Ax>
1032 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)
1034 _Elem buf[1024/
sizeof(_Elem)];
1037 int count = vsnprintf(buf, _countof(buf) - 1, format, locale, arg);
1040 str.append(buf, count);
1042 for (
size_t capacity = 2*1024/
sizeof(_Elem);; capacity *= 2) {
1044 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
1045 count = vsnprintf(buf_dyn.get(), capacity - 1, format, locale, arg);
1047 str.append(buf_dyn.get(), count);
1061 template<
class _Elem,
class _Traits,
class _Ax>
1062 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, ...)
1065 va_start(arg, locale);
1066 vappendf(str, format, locale, arg);
1078 template<
class _Elem,
class _Traits,
class _Ax>
1079 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)
1082 vappendf(str, format, locale, arg);
1092 template<
class _Elem,
class _Traits,
class _Ax>
1093 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, ...)
1096 va_start(arg, locale);
1097 vsprintf(str, format, locale, arg);
1110 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
1111 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)
1113 std::basic_string<_Elem, _Traits, _Ax> str;
1114 vappendf(str, format, locale, arg);
1126 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
1127 inline std::basic_string<_Elem, _Traits, _Ax> sprintf(_In_z_ _Printf_format_string_params_(2)
const _Elem *format, _In_opt_ locale_t locale, ...)
1130 va_start(arg, locale);
1131 auto str = vsprintf(format, locale, arg);
Deleter for unique_ptr using free_locale.
Definition string.hpp:33
void operator()(locale_t locale) const
Delete a pointer.
Definition string.hpp:37