Skip to content

Commit 259f776

Browse files
author
Sriya Pratipati
committed
moved check function to wchar_utils
1 parent ab07c55 commit 259f776

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

libc/src/wchar/wchar_utils.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@
1414
#include "src/__support/common.h"
1515
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1616

17+
bool check_span(wchar_t c, const wchar_t *str) {
18+
for (int n = 0; str[n]; ++n) {
19+
if (str[n] == c)
20+
return true;
21+
}
22+
return false;
23+
}
24+
1725
namespace LIBC_NAMESPACE_DECL {
1826
namespace internal {
1927

20-
template <typename Check>
21-
LIBC_INLINE size_t inline_wcsspn(const wchar_t *s1, Check check) {
28+
// To avoid duplicated code, call this with true for wcscspn and call with false
29+
// for wcsspn
30+
LIBC_INLINE size_t inline_wcsspn(const wchar_t *s1, const wchar_t *s2,
31+
bool invert) {
2232
size_t i = 0;
2333
for (; s1[i]; ++i) {
24-
if (!check(s1[i]))
34+
bool check = check_span(s1[i], s2);
35+
check = invert ? !check : check;
36+
if (!check)
2537
return i;
2638
}
2739
return i;

libc/src/wchar/wcscspn.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,8 @@
1616

1717
namespace LIBC_NAMESPACE_DECL {
1818

19-
struct CheckCSpan {
20-
const wchar_t *str;
21-
CheckCSpan(const wchar_t *w) { str = w; }
22-
bool operator()(wchar_t c) {
23-
for (int n = 0; str[n]; ++n) {
24-
if (str[n] == c)
25-
return false;
26-
}
27-
return true;
28-
}
29-
};
30-
3119
LLVM_LIBC_FUNCTION(size_t, wcscspn, (const wchar_t *s1, const wchar_t *s2)) {
32-
CheckCSpan check(s2);
33-
return internal::inline_wcsspn(s1, check);
20+
return internal::inline_wcsspn(s1, s2, true);
3421
}
3522

3623
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcsspn.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,10 @@
1414
#include "src/__support/macros/config.h"
1515
#include "wchar_utils.h"
1616

17-
struct CheckSpan {
18-
const wchar_t *str;
19-
CheckSpan(const wchar_t *w) { str = w; }
20-
bool operator()(wchar_t c) {
21-
for (int n = 0; str[n]; ++n) {
22-
if (str[n] == c)
23-
return true;
24-
}
25-
return false;
26-
}
27-
};
28-
2917
namespace LIBC_NAMESPACE_DECL {
3018

3119
LLVM_LIBC_FUNCTION(size_t, wcsspn, (const wchar_t *s1, const wchar_t *s2)) {
32-
CheckSpan check(s2);
33-
return internal::inline_wcsspn(s1, check);
20+
return internal::inline_wcsspn(s1, s2, false);
3421
}
3522

3623
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)