Skip to content

Commit 701de35

Browse files
authored
[libc] Stop duplicating wcschr(). (#150661)
Three implementations of wcschr() is two too many.
1 parent f0c90df commit 701de35

File tree

4 files changed

+19
-37
lines changed

4 files changed

+19
-37
lines changed

libc/src/wchar/wchar_utils.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
namespace LIBC_NAMESPACE_DECL {
1818
namespace internal {
1919

20-
// returns true if the character exists in the string
21-
LIBC_INLINE static bool wcschr(wchar_t c, const wchar_t *str) {
22-
for (int n = 0; str[n]; ++n) {
23-
if (str[n] == c)
24-
return true;
25-
}
26-
return false;
20+
LIBC_INLINE static const wchar_t *wcschr(const wchar_t *s, wchar_t c) {
21+
for (; *s && *s != c; ++s)
22+
;
23+
return (*s == c) ? s : nullptr;
2724
}
2825

2926
// bool should be true for wcscspn for complimentary span
@@ -32,7 +29,7 @@ LIBC_INLINE static size_t wcsspn(const wchar_t *s1, const wchar_t *s2,
3229
bool not_match_set) {
3330
size_t i = 0;
3431
for (; s1[i]; ++i) {
35-
bool in_set = wcschr(s1[i], s2);
32+
bool in_set = internal::wcschr(s2, s1[i]);
3633
if (in_set == not_match_set)
3734
return i;
3835
}

libc/src/wchar/wcschr.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
#include "hdr/types/wchar_t.h"
1212
#include "src/__support/common.h"
1313
#include "src/__support/macros/config.h"
14+
#include "src/__support/macros/null_check.h"
15+
#include "wchar_utils.h"
1416

1517
namespace LIBC_NAMESPACE_DECL {
1618

1719
LLVM_LIBC_FUNCTION(const wchar_t *, wcschr, (const wchar_t *s, wchar_t c)) {
18-
for (; *s && *s != c; ++s)
19-
;
20-
if (*s == c)
21-
return s;
22-
return nullptr;
20+
LIBC_CRASH_ON_NULLPTR(s);
21+
return internal::wcschr(s, c);
2322
}
2423

2524
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcspbrk.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,18 @@
1111
#include "hdr/types/wchar_t.h"
1212
#include "src/__support/common.h"
1313
#include "src/__support/macros/null_check.h"
14+
#include "wchar_utils.h"
1415

1516
namespace LIBC_NAMESPACE_DECL {
1617

17-
bool contains_char(const wchar_t *str, wchar_t target) {
18-
for (; *str != L'\0'; str++)
19-
if (*str == target)
20-
return true;
21-
22-
return false;
23-
}
24-
2518
LLVM_LIBC_FUNCTION(const wchar_t *, wcspbrk,
2619
(const wchar_t *src, const wchar_t *breakset)) {
2720
LIBC_CRASH_ON_NULLPTR(src);
2821
LIBC_CRASH_ON_NULLPTR(breakset);
2922

3023
// currently O(n * m), can be further optimized to O(n + m) with a hash set
3124
for (int src_idx = 0; src[src_idx] != 0; src_idx++)
32-
if (contains_char(breakset, src[src_idx]))
25+
if (internal::wcschr(breakset, src[src_idx]))
3326
return src + src_idx;
3427

3528
return nullptr;

libc/src/wchar/wcstok.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@
1010

1111
#include "hdr/types/wchar_t.h"
1212
#include "src/__support/common.h"
13+
#include "wchar_utils.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

16-
bool isADelimeter(wchar_t wc, const wchar_t *delimiters) {
17-
for (const wchar_t *delim_ptr = delimiters; *delim_ptr != L'\0'; ++delim_ptr)
18-
if (wc == *delim_ptr)
19-
return true;
20-
return false;
21-
}
22-
2317
LLVM_LIBC_FUNCTION(wchar_t *, wcstok,
24-
(wchar_t *__restrict str, const wchar_t *__restrict delim,
18+
(wchar_t *__restrict str, const wchar_t *__restrict delims,
2519
wchar_t **__restrict context)) {
2620
if (str == nullptr) {
2721
if (*context == nullptr)
@@ -30,14 +24,13 @@ LLVM_LIBC_FUNCTION(wchar_t *, wcstok,
3024
str = *context;
3125
}
3226

33-
wchar_t *tok_start, *tok_end;
34-
for (tok_start = str; *tok_start != L'\0' && isADelimeter(*tok_start, delim);
35-
++tok_start)
36-
;
27+
wchar_t *tok_start = str;
28+
while (*tok_start != L'\0' && internal::wcschr(delims, *tok_start))
29+
++tok_start;
3730

38-
for (tok_end = tok_start; *tok_end != L'\0' && !isADelimeter(*tok_end, delim);
39-
++tok_end)
40-
;
31+
wchar_t *tok_end = tok_start;
32+
while (*tok_end != L'\0' && !internal::wcschr(delims, *tok_end))
33+
++tok_end;
4134

4235
if (*tok_end != L'\0') {
4336
*tok_end = L'\0';

0 commit comments

Comments
 (0)