[WebAssembly] Fix asan issue from https://reviews.llvm.org/D121349
[llvm-project.git] / libcxx / test / support / locale_helpers.h
blob782e0e4e1e84345a141b9c20f9b80e228b3013da
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #ifndef LIBCXX_TEST_SUPPORT_LOCALE_HELPERS_H
9 #define LIBCXX_TEST_SUPPORT_LOCALE_HELPERS_H
11 #include <string>
12 #include "platform_support.h"
13 #include "test_macros.h"
14 #include "make_string.h"
16 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
18 #include <cwctype>
20 #endif // TEST_HAS_NO_WIDE_CHARACTERS
22 namespace LocaleHelpers {
24 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
26 std::wstring convert_thousands_sep(std::wstring const& in, wchar_t sep) {
27 std::wstring out;
28 bool seen_num_start = false;
29 bool seen_decimal = false;
30 for (unsigned i = 0; i < in.size(); ++i) {
31 seen_decimal |= in[i] == L',';
32 seen_num_start |= in[i] == L'-' || std::iswdigit(in[i]);
33 if (seen_decimal || !seen_num_start || in[i] != L' ') {
34 out.push_back(in[i]);
35 continue;
37 assert(in[i] == L' ');
38 out.push_back(sep);
40 return out;
43 // GLIBC 2.27 and newer use U+202F NARROW NO-BREAK SPACE as a thousands separator.
44 // This function converts the spaces in string inputs to U+202F if need
45 // be. FreeBSD's locale data also uses U+202F, since 2018.
46 // Windows uses U+00A0 NO-BREAK SPACE.
47 std::wstring convert_thousands_sep_fr_FR(std::wstring const& in) {
48 #if defined(_CS_GNU_LIBC_VERSION)
49 if (glibc_version_less_than("2.27"))
50 return in;
51 else
52 return convert_thousands_sep(in, L'\u202F');
53 #elif defined(__FreeBSD__)
54 return convert_thousands_sep(in, L'\u202F');
55 #elif defined(_WIN32)
56 return convert_thousands_sep(in, L'\u00A0');
57 #else
58 return in;
59 #endif
62 // GLIBC 2.27 uses U+202F NARROW NO-BREAK SPACE as a thousands separator.
63 // FreeBSD and Windows use U+00A0 NO-BREAK SPACE.
64 std::wstring convert_thousands_sep_ru_RU(std::wstring const& in) {
65 #if defined(TEST_HAS_GLIBC)
66 return convert_thousands_sep(in, L'\u202F');
67 #elif defined(__FreeBSD__) || defined(_WIN32)
68 return convert_thousands_sep(in, L'\u00A0');
69 #else
70 return in;
71 #endif
74 std::wstring negate_en_US(std::wstring s) {
75 #if defined(_WIN32)
76 return L"(" + s + L")";
77 #else
78 return L"-" + s;
79 #endif
82 #endif // TEST_HAS_NO_WIDE_CHARACTERS
84 std::string negate_en_US(std::string s) {
85 #if defined(_WIN32)
86 return "(" + s + ")";
87 #else
88 return "-" + s;
89 #endif
92 MultiStringType currency_symbol_ru_RU() {
93 #if defined(_CS_GNU_LIBC_VERSION)
94 if (glibc_version_less_than("2.24"))
95 return MKSTR("\u0440\u0443\u0431");
96 else
97 return MKSTR("\u20BD"); // U+20BD RUBLE SIGN
98 #elif defined(_WIN32) || defined(__FreeBSD__)
99 return MKSTR("\u20BD"); // U+20BD RUBLE SIGN
100 #else
101 return MKSTR("\u0440\u0443\u0431.");
102 #endif
105 MultiStringType currency_symbol_zh_CN() {
106 #if defined(_WIN32)
107 return MKSTR("\u00A5"); // U+00A5 YEN SIGN
108 #else
109 return MKSTR("\uFFE5"); // U+FFE5 FULLWIDTH YEN SIGN
110 #endif
113 } // namespace LocaleHelpers
115 #endif // LIBCXX_TEST_SUPPORT_LOCALE_HELPERS_H