17 using locale_t = _locale_t;
19 using locale_t = ::locale_t;
26 typedef wchar_t utf16_t;
28 typedef char16_t utf16_t;
36 inline bool is_high_surrogate(_In_ utf16_t chr)
38 return 0xd800 < chr && chr < 0xdc00;
46 inline bool is_low_surrogate(_In_ utf16_t chr)
48 return 0xdc00 < chr && chr < 0xe000;
56 inline bool is_surrogate_pair(_In_reads_(2)
const utf16_t* str)
58 return is_high_surrogate(str[0]) && is_low_surrogate(str[1]);
66 inline char32_t surrogate_pair_to_ucs4(_In_reads_(2)
const utf16_t* str)
68 assert(is_surrogate_pair(str));
70 ((
char32_t)(str[0] - 0xd800) << 10) +
71 (char32_t)(str[1] - 0xdc00) +
80 inline void ucs4_to_surrogate_pair(_Out_writes_(2) utf16_t* str, _In_
char32_t chr)
82 assert(chr >= 0x10000);
84 str[0] = 0xd800 + (char32_t)((chr >> 10) & 0x3ff);
85 str[1] = 0xdc00 + (char32_t)(chr & 0x3ff);
93 inline bool iscombining(_In_
char32_t chr)
96 0x0300 <= chr && chr < 0x0370 ||
97 0x1dc0 <= chr && chr < 0x1e00 ||
98 0x20d0 <= chr && chr < 0x2100 ||
99 0xfe20 <= chr && chr < 0xfe30;
108 inline size_t islbreak(_In_ T chr)
110 return chr ==
'\n' || chr ==
'\r';
120 inline size_t islbreak(_In_reads_or_z_opt_(count)
const T* chr, _In_
size_t count)
122 _Analysis_assume_(chr || !count);
123 if (count >= 2 && (chr[0] ==
'\r' && chr[1] ==
'\n' || chr[0] ==
'\n' && chr[1] ==
'\r'))
125 if (count > 1 && (chr[0] ==
'\n' || chr[0] ==
'\r'))
136 inline size_t glyphlen(_In_reads_or_z_opt_(count)
const wchar_t* glyph,
size_t count)
138 _Analysis_assume_(glyph || !count);
141 size_t i = count < 2 || !is_surrogate_pair(glyph) ? 1 : 2;
145 for (; i < count && iscombining(glyph[i]); ++i);
159 inline size_t strlen(_In_z_
const T* str)
163 for (i = 0; str[i]; ++i);
176 inline size_t strnlen(_In_reads_or_z_opt_(count)
const T* str, _In_
size_t count)
180 for (i = 0; i < count && str[i]; ++i);
184 constexpr auto npos{
static_cast<size_t>(-1) };
196 inline size_t strnchr(
197 _In_reads_or_z_opt_(count)
const T* str,
201 assert(str || !count);
202 for (
size_t i = 0; i < count && str[i]; ++i)
203 if (str[i] == chr)
return i;
217 inline size_t strrnchr(
218 _In_reads_or_z_opt_(count)
const T* str,
222 assert(str || !count);
224 for (
size_t i = 0; i < count && str[i]; ++i)
225 if (str[i] == chr) z = i;
239 inline size_t strnichr(
240 _In_reads_or_z_opt_(count)
const T* str,
243 _In_
const std::locale& locale)
245 assert(str || !count);
246 const auto& ctype = std::use_facet<std::ctype<T>>(locale);
247 chr = ctype.tolower(chr);
248 for (
size_t i = 0; i < count && str[i]; ++i)
249 if (ctype.tolower(str[i]) == chr)
return i;
263 inline size_t strrnichr(
264 _In_reads_or_z_opt_(count)
const T* str,
267 _In_
const std::locale& locale)
269 assert(str || !count);
270 const auto& ctype = std::use_facet<std::ctype<T>>(locale);
271 chr = ctype.tolower(chr);
273 for (
size_t i = 0; i < count && str[i]; ++i)
274 if (ctype.tolower(str[i]) == chr) z = i;
288 template <
class T1,
class T2>
290 _In_reads_or_z_opt_(count1)
const T1* str1, _In_
size_t count1,
291 _In_reads_or_z_opt_(count2)
const T2* str2, _In_
size_t count2)
293 assert(str1 || !count1);
294 assert(str2 || !count2);
295 size_t i; T1 a; T2 b;
296 for (i = 0; i < count1 && i < count2 && ((a = str1[i]) | (b = str2[i])); ++i) {
297 if (a > b)
return +1;
298 if (a < b)
return -1;
300 if (i < count1 && str1[i])
return +1;
301 if (i < count2 && str2[i])
return -1;
317 _In_reads_or_z_opt_(count1)
const T* str1, _In_
size_t count1,
318 _In_reads_or_z_opt_(count2)
const T* str2, _In_
size_t count2,
319 _In_
const std::locale& locale)
321 assert(str1 || !count1);
322 assert(str2 || !count2);
323 auto& collate = std::use_facet<std::collate<T>>(locale);
324 return collate.compare(str1, str1 + count1, str2, str2 + count2);
337 template <
class T1,
class T2>
339 _In_reads_or_z_opt_(count1)
const T1* str1, _In_
size_t count1,
340 _In_reads_or_z_opt_(count2)
const T2* str2, _In_
size_t count2,
341 _In_
const std::locale& locale)
343 assert(str1 || !count1);
344 assert(str2 || !count2);
345 size_t i; T1 a; T2 b;
346 const auto& ctype1 = std::use_facet<std::ctype<T1>>(locale);
347 const auto& ctype2 = std::use_facet<std::ctype<T2>>(locale);
348 for (i = 0; i < count1 && i < count2 && ((a = ctype1.tolower(str1[i])) | (b = ctype2.tolower(str2[i]))); i++) {
349 if (a > b)
return +1;
350 if (a < b)
return -1;
352 if (i < count1 && str1[i])
return +1;
353 if (i < count2 && str2[i])
return -1;
366 template <
class T1,
class T2>
367 inline size_t strnstr(
368 _In_reads_or_z_opt_(count)
const T1* str,
370 _In_z_
const T2* sample)
372 assert(str || !count);
374 for (
size_t offset = 0;; ++offset) {
375 for (
size_t i = offset, j = 0;; ++i, ++j) {
378 if (i >= count || !str[i])
380 if (str[i] != sample[j])
395 template <
class T1,
class T2>
396 inline size_t strnistr(
397 _In_reads_or_z_opt_(count)
const T1* str,
399 _In_z_
const T2* sample,
400 _In_
const std::locale& locale)
402 assert(str || !count);
404 const auto& ctype1 = std::use_facet<std::ctype<T1>>(locale);
405 const auto& ctype2 = std::use_facet<std::ctype<T2>>(locale);
406 for (
size_t offset = 0;; ++offset) {
407 for (
size_t i = offset, j = 0;; ++i, ++j) {
410 if (i >= count || !str[i])
412 if (ctype1.tolower(str[i]) != ctype2.tolower(sample[j]))
427 template <
class T1,
class T2>
428 inline size_t strncpy(
429 _Out_writes_(count) _Post_maybez_ T1* dst,
430 _In_reads_or_z_opt_(count)
const T2* src, _In_
size_t count)
432 assert(dst && src || !count);
433 for (
size_t i = 0; ; ++i) {
436 if ((dst[i] = src[i]) == 0)
451 template <
class T1,
class T2>
452 inline size_t strncpy(
453 _Out_writes_(count_dst) _Post_maybez_ T1* dst, _In_
size_t count_dst,
454 _In_reads_or_z_opt_(count_src)
const T2* src, _In_
size_t count_src)
456 assert(dst || !count_dst);
457 assert(src || !count_src);
458 for (
size_t i = 0; ; ++i)
466 if ((dst[i] = src[i]) == 0)
481 inline size_t crlf2nl(_Out_writes_z_(strlen(src)) T* dst, _In_z_
const T* src)
486 for (i = j = 0; src[j];) {
487 if (src[j] !=
'\r' || src[j + 1] !=
'\n')
499 template <
class T,
class T_bin>
500 inline T_bin strtoint(
501 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
502 _Out_opt_
size_t* end,
504 _Out_ uint8_t& flags)
506 assert(str || !count);
507 assert(radix == 0 || 2 <= radix && radix <= 36);
510 T_bin value = 0, digit,
512 max_ui_pre1, max_ui_pre2;
518 if (i >= count || !str[i])
goto error;
519 if (!isspace(str[i]))
break;
526 if (i >= count || !str[i])
goto error;
528 else if (str[i] ==
'-') {
531 if (i >= count || !str[i])
goto error;
536 if (str[i] ==
'0' && i + 1 < count && (str[i + 1] ==
'x' || str[i + 1] ==
'X')) {
538 if (i >= count || !str[i])
goto error;
545 if (i >= count || !str[i])
goto error;
546 if (str[i] ==
'x' || str[i] ==
'X') {
549 if (i >= count || !str[i])
goto error;
559 max_ui_pre1 = max_ui / (T_bin)radix;
560 max_ui_pre2 = max_ui % (T_bin)radix;
562 if (
'0' <= str[i] && str[i] <=
'9')
563 digit = (T_bin)str[i] -
'0';
564 else if (
'A' <= str[i] && str[i] <=
'Z')
565 digit = (T_bin)str[i] -
'A' +
'\x0a';
566 else if (
'a' <= str[i] && str[i] <=
'z')
567 digit = (T_bin)str[i] -
'a' +
'\x0a';
570 if (digit >= (T_bin)radix)
573 if (value < max_ui_pre1 ||
574 value == max_ui_pre1 && digit <= max_ui_pre2)
575 value = value * (T_bin)radix + digit;
582 if (i >= count || !str[i])
602 template <
class T,
class T_bin>
604 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
605 _Out_opt_
size_t* end,
611 switch (
sizeof(T_bin)) {
613 value = (T_bin)strtoint<T, uint8_t>(str, count, end, radix, flags);
614 if ((flags & 0x01) && (value & 0x80)) {
618 return (flags & 0x02) ?
619 (flags & 0x01) ? (T_bin)0x80 : (T_bin)0x7f :
620 (flags & 0x01) ? -value : value;
623 value = (T_bin)strtoint<T, T_U2>(str, count, end, radix, flags);
624 if ((flags & 0x01) && (value & 0x8000)) {
628 return (flags & 0x02) ?
629 (flags & 0x01) ? (T_bin)0x8000 : (T_bin)0x7fff :
630 (flags & 0x01) ? -value : value;
633 value = (T_bin)strtoint<T, uint32_t>(str, count, end, radix, flags);
634 if ((flags & 0x01) && (value & 0x80000000)) {
638 return (flags & 0x02) ?
639 (flags & 0x01) ? (T_bin)0x80000000 : (T_bin)0x7fffffff :
640 (flags & 0x01) ? -value : value;
643 value = (T_bin)strtoint<T, uint64_t>(str, count, end, radix, flags);
644 if ((flags & 0x01) && (value & 0x8000000000000000)) {
648 return (flags & 0x02) ?
649 (flags & 0x01) ? (T_bin)0x8000000000000000 : (T_bin)0x7fffffffffffffff :
650 (flags & 0x01) ? -value : value;
653 throw std::invalid_argument(
"Unsupported bit length");
667 template <
class T,
class T_bin>
668 inline T_bin strtouint(
669 _In_reads_or_z_opt_(count)
const T* str,
671 _Out_opt_
size_t* end,
677 switch (
sizeof(T_bin)) {
678 case 1: value = (T_bin)strtoint<T, uint8_t>(str, count, end, radix, flags);
break;
679 case 2: value = (T_bin)strtoint<T, uint16_t>(str, count, end, radix, flags);
break;
680 case 4: value = (T_bin)strtoint<T, uint32_t>(str, count, end, radix, flags);
break;
681 case 8: value = (T_bin)strtoint<T, uint64_t>(str, count, end, radix, flags);
break;
682 default:
throw std::invalid_argument(
"Unsupported bit length");
685 return (flags & 0x02) ?
686 (flags & 0x01) ? (T_bin)0 : (T_bin)-1 :
687 (flags & 0x01) ? ~value : value;
701 inline int32_t strto32(
702 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
703 _Out_opt_
size_t* end,
706 return strtoint<T, int32_t>(str, count, end, radix);
720 inline int64_t strto64(
721 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
722 _Out_opt_
size_t* end,
725 return strtoint<T, int64_t>(str, count, end, radix);
740 inline intptr_t strtoi(
741 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
742 _Out_opt_
size_t* end,
745#if defined(_WIN64) || defined(__LP64__)
746 return (intptr_t)strto64(str, count, end, radix);
748 return (intptr_t)strto32(str, count, end, radix);
763 inline uint32_t strtou32(
764 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
765 _Out_opt_
size_t* end,
768 return strtouint<T, uint32_t>(str, count, end, radix);
782 inline uint64_t strtou64(
783 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
784 _Out_opt_
size_t* end,
787 return strtouint<T, uint64_t>(str, count, end, radix);
802 inline size_t strtoui(
803 _In_reads_or_z_opt_(count)
const T* str, _In_
size_t count,
804 _Out_opt_
size_t* end,
807#if defined(_WIN64) || defined(__LP64__)
808 return (
size_t)strtou64(str, count, end, radix);
810 return (
size_t)strtou32(str, count, end, radix);
815 inline int vsnprintf(_Out_z_cap_(capacity)
char *str, _In_
size_t capacity, _In_z_ _Printf_format_string_
const char *format, _In_opt_ locale_t locale, _In_ va_list arg)
820#pragma warning(suppress: 4996)
821 r = _vsnprintf_l(str, capacity, format, locale, arg);
823 r = vsnprintf(str, capacity, format, arg);
825 if (r == -1 && strnlen(str, capacity) == capacity) {
827 capacity += std::max<size_t>(capacity / 8, 0x80);
828 if (capacity > INT_MAX)
829 throw std::invalid_argument(
"string too big");
830 return (
int)capacity;
835 inline int vsnprintf(_Out_z_cap_(capacity)
wchar_t *str, _In_
size_t capacity, _In_z_ _Printf_format_string_
const wchar_t *format, _In_opt_ locale_t locale, _In_ va_list arg)
841#pragma warning(suppress: 4996)
842 r = _vsnwprintf_l(str, capacity, format, locale, arg);
844 r = vswprintf(str, capacity, format, arg);
846 if (r == -1 && strnlen(str, capacity) == capacity) {
848 capacity += std::max<size_t>(capacity / 8, 0x80);
849 if (capacity > INT_MAX)
850 throw std::invalid_argument(
"string too big");
851 return (
int)capacity;
865 template<
class _Elem,
class _Traits,
class _Ax>
866 inline void vappendf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, _In_opt_ locale_t locale, _In_ va_list arg)
868 _Elem buf[1024/
sizeof(_Elem)];
871 int count = vsnprintf(buf, _countof(buf) - 1, format, locale, arg);
874 str.append(buf, count);
876 for (
size_t capacity = 2*1024/
sizeof(_Elem);; capacity *= 2) {
878 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
879 count = vsnprintf(buf_dyn.get(), capacity - 1, format, locale, arg);
881 str.append(buf_dyn.get(), count);
895 template<
class _Elem,
class _Traits,
class _Ax>
896 inline void appendf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, _In_opt_ locale_t locale, ...)
899 va_start(arg, locale);
900 vappendf(str, format, locale, arg);
912 template<
class _Elem,
class _Traits,
class _Ax>
913 inline void vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, _In_opt_ locale_t locale, _In_ va_list arg)
916 appendf(str, format, locale, arg);
926 template<
class _Elem,
class _Traits,
class _Ax>
927 inline void sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, _In_opt_ locale_t locale, ...)
930 va_start(arg, locale);
931 vsprintf(str, format, locale, arg);