Skip to content

Commit e7f7973

Browse files
authored
[libc] Migrate wctype_utils to use wchar_t where applicable. (#166234)
This is a counterpart of llvm/llvm-project#166225 but for wctype_utils (which are not yet widely used). For now, I'm just changing the types from wint_t to wchar_t to match the regular ctype_utils change. The next change may rename most of the functions to match the name of ctype_utils variants, so that we could be calling them from the templated code operating on "const char*" and "const wchar_t*" strings, and the right function signature would be picked up.
1 parent 120689e commit e7f7973

File tree

5 files changed

+29
-24
lines changed

5 files changed

+29
-24
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ add_header_library(
161161
HDRS
162162
wctype_utils.h
163163
DEPENDS
164+
libc.hdr.types.wchar_t
164165
libc.hdr.types.wint_t
165166
)
166167

libc/src/__support/wctype_utils.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
1010
#define LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
1111

12+
#include "hdr/types/wchar_t.h"
1213
#include "hdr/types/wint_t.h"
1314
#include "src/__support/CPP/optional.h"
1415
#include "src/__support/macros/attributes.h" // LIBC_INLINE
@@ -30,7 +31,7 @@ namespace internal {
3031

3132
// Similarly, do not change these fumarks to show your new solution is faster,
3233
// as well as a way to support non-Anctions to use case ranges. e.g.
33-
// bool iswlower(wint_t ch) {
34+
// bool iswlower(wchar_t ch) {
3435
// switch(ch) {
3536
// case L'a'...L'z':
3637
// return true;
@@ -40,7 +41,7 @@ namespace internal {
4041
// EBCDIC. Technically we could use some smaller ranges, but that's even harder
4142
// to read.
4243

43-
LIBC_INLINE static constexpr bool iswlower(wint_t wch) {
44+
LIBC_INLINE static constexpr bool iswlower(wchar_t wch) {
4445
switch (wch) {
4546
case L'a':
4647
case L'b':
@@ -74,7 +75,7 @@ LIBC_INLINE static constexpr bool iswlower(wint_t wch) {
7475
}
7576
}
7677

77-
LIBC_INLINE static constexpr bool iswupper(wint_t wch) {
78+
LIBC_INLINE static constexpr bool iswupper(wchar_t wch) {
7879
switch (wch) {
7980
case L'A':
8081
case L'B':
@@ -108,7 +109,7 @@ LIBC_INLINE static constexpr bool iswupper(wint_t wch) {
108109
}
109110
}
110111

111-
LIBC_INLINE static constexpr bool iswdigit(wint_t wch) {
112+
LIBC_INLINE static constexpr bool iswdigit(wchar_t wch) {
112113
switch (wch) {
113114
case L'0':
114115
case L'1':
@@ -126,7 +127,7 @@ LIBC_INLINE static constexpr bool iswdigit(wint_t wch) {
126127
}
127128
}
128129

129-
LIBC_INLINE static constexpr wint_t towlower(wint_t wch) {
130+
LIBC_INLINE static constexpr wchar_t towlower(wchar_t wch) {
130131
switch (wch) {
131132
case L'A':
132133
return L'a';
@@ -185,7 +186,7 @@ LIBC_INLINE static constexpr wint_t towlower(wint_t wch) {
185186
}
186187
}
187188

188-
LIBC_INLINE static constexpr wint_t towupper(wint_t wch) {
189+
LIBC_INLINE static constexpr wchar_t towupper(wchar_t wch) {
189190
switch (wch) {
190191
case L'a':
191192
return L'A';
@@ -244,7 +245,7 @@ LIBC_INLINE static constexpr wint_t towupper(wint_t wch) {
244245
}
245246
}
246247

247-
LIBC_INLINE static constexpr bool iswalpha(wint_t wch) {
248+
LIBC_INLINE static constexpr bool iswalpha(wchar_t wch) {
248249
switch (wch) {
249250
case L'a':
250251
case L'b':
@@ -304,7 +305,7 @@ LIBC_INLINE static constexpr bool iswalpha(wint_t wch) {
304305
}
305306
}
306307

307-
LIBC_INLINE static constexpr bool iswalnum(wint_t wch) {
308+
LIBC_INLINE static constexpr bool iswalnum(wchar_t wch) {
308309
switch (wch) {
309310
case L'a':
310311
case L'b':
@@ -374,7 +375,7 @@ LIBC_INLINE static constexpr bool iswalnum(wint_t wch) {
374375
}
375376
}
376377

377-
LIBC_INLINE static constexpr int b36_wchar_to_int(wint_t wch) {
378+
LIBC_INLINE static constexpr int b36_wchar_to_int(wchar_t wch) {
378379
switch (wch) {
379380
case L'0':
380381
return 0;
@@ -479,7 +480,7 @@ LIBC_INLINE static constexpr int b36_wchar_to_int(wint_t wch) {
479480
}
480481
}
481482

482-
LIBC_INLINE static constexpr wint_t int_to_b36_wchar(int num) {
483+
LIBC_INLINE static constexpr wchar_t int_to_b36_wchar(int num) {
483484
// Can't actually use LIBC_ASSERT here because it depends on integer_to_string
484485
// which depends on this.
485486

@@ -562,7 +563,7 @@ LIBC_INLINE static constexpr wint_t int_to_b36_wchar(int num) {
562563
}
563564
}
564565

565-
LIBC_INLINE static constexpr bool iswspace(wint_t wch) {
566+
LIBC_INLINE static constexpr bool iswspace(wchar_t wch) {
566567
switch (wch) {
567568
case L' ':
568569
case L'\t':

libc/src/wctype/iswalpha.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

17-
LLVM_LIBC_FUNCTION(int, iswalpha, (wint_t c)) { return internal::iswalpha(c); }
17+
LLVM_LIBC_FUNCTION(int, iswalpha, (wint_t c)) {
18+
return internal::iswalpha(static_cast<wchar_t>(c));
19+
}
1820

1921
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/wchar/WcstolTest.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ struct WcstoTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
178178
wchar_t small_string[4] = {L'\0', L'\0', L'\0', L'\0'};
179179
for (int base = 2; base <= 36; ++base) {
180180
for (int first_digit = 0; first_digit <= 36; ++first_digit) {
181-
small_string[0] = static_cast<wchar_t>(
182-
LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit));
181+
small_string[0] =
182+
LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit);
183183
if (first_digit < base) {
184184
ASSERT_EQ(func(small_string, nullptr, base),
185185
static_cast<ReturnT>(first_digit));
@@ -193,11 +193,11 @@ struct WcstoTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
193193

194194
for (int base = 2; base <= 36; ++base) {
195195
for (int first_digit = 0; first_digit <= 36; ++first_digit) {
196-
small_string[0] = static_cast<wchar_t>(
197-
LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit));
196+
small_string[0] =
197+
LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit);
198198
for (int second_digit = 0; second_digit <= 36; ++second_digit) {
199-
small_string[1] = static_cast<wchar_t>(
200-
LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit));
199+
small_string[1] =
200+
LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit);
201201
if (first_digit < base && second_digit < base) {
202202
ASSERT_EQ(
203203
func(small_string, nullptr, base),
@@ -217,14 +217,14 @@ struct WcstoTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
217217

218218
for (int base = 2; base <= 36; ++base) {
219219
for (int first_digit = 0; first_digit <= 36; ++first_digit) {
220-
small_string[0] = static_cast<wchar_t>(
221-
LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit));
220+
small_string[0] =
221+
LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit);
222222
for (int second_digit = 0; second_digit <= 36; ++second_digit) {
223-
small_string[1] = static_cast<wchar_t>(
224-
LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit));
223+
small_string[1] =
224+
LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit);
225225
for (int third_digit = 0; third_digit <= limit; ++third_digit) {
226-
small_string[2] = static_cast<wchar_t>(
227-
LIBC_NAMESPACE::internal::int_to_b36_wchar(third_digit));
226+
small_string[2] =
227+
LIBC_NAMESPACE::internal::int_to_b36_wchar(third_digit);
228228

229229
if (first_digit < base && second_digit < base &&
230230
third_digit < base) {

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,7 @@ libc_support_library(
18051805
":__support_cpp_optional",
18061806
":__support_macros_attributes",
18071807
":__support_macros_config",
1808+
":types_wchar_t",
18081809
":types_wint_t",
18091810
],
18101811
)

0 commit comments

Comments
 (0)