Skip to content

Commit ab07c55

Browse files
author
Sriya Pratipati
committed
[libc] Cleaned up wcsspn and wcscspn
1 parent 424abcb commit ab07c55

File tree

5 files changed

+87
-25
lines changed

5 files changed

+87
-25
lines changed

libc/src/wchar/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
add_header_library(
2+
wchar_utils
3+
HDRS
4+
wchar_utils.h
5+
DEPENDS
6+
libc.hdr.types.size_t
7+
libc.hdr.types.wchar_t
8+
libc.src.__support.common
9+
)
10+
111
add_entrypoint_object(
212
wcslen
313
SRCS
@@ -211,6 +221,7 @@ add_entrypoint_object(
211221
DEPENDS
212222
libc.hdr.wchar_macros
213223
libc.hdr.types.size_t
224+
libc.src.wchar.wchar_utils
214225
)
215226

216227
add_entrypoint_object(
@@ -222,6 +233,7 @@ add_entrypoint_object(
222233
DEPENDS
223234
libc.hdr.wchar_macros
224235
libc.hdr.types.size_t
236+
libc.src.wchar.wchar_utils
225237
)
226238

227239
add_entrypoint_object(

libc/src/wchar/wchar_utils.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- wchar utils ---------------------------------------------*- C++ -*-===//
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+
9+
#ifndef LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H
11+
12+
#include "hdr/types/size_t.h"
13+
#include "hdr/types/wchar_t.h"
14+
#include "src/__support/common.h"
15+
#include "src/__support/macros/attributes.h" // LIBC_INLINE
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
namespace internal {
19+
20+
template <typename Check>
21+
LIBC_INLINE size_t inline_wcsspn(const wchar_t *s1, Check check) {
22+
size_t i = 0;
23+
for (; s1[i]; ++i) {
24+
if (!check(s1[i]))
25+
return i;
26+
}
27+
return i;
28+
}
29+
30+
} // namespace internal
31+
} // namespace LIBC_NAMESPACE_DECL
32+
33+
#endif // LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H

libc/src/wchar/wcscspn.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@
1212
#include "hdr/types/wchar_t.h"
1313
#include "src/__support/common.h"
1414
#include "src/__support/macros/config.h"
15+
#include "wchar_utils.h"
1516

1617
namespace LIBC_NAMESPACE_DECL {
1718

18-
bool check(wchar_t c, const wchar_t *s2) {
19-
for (int n = 0; s2[n]; ++n) {
20-
if (s2[n] == c)
21-
return false;
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;
2228
}
23-
return true;
24-
}
29+
};
30+
2531
LLVM_LIBC_FUNCTION(size_t, wcscspn, (const wchar_t *s1, const wchar_t *s2)) {
26-
size_t i = 0;
27-
for (; s1[i]; ++i) {
28-
if (!check(s1[i], s2))
29-
return i;
30-
}
31-
return i;
32+
CheckCSpan check(s2);
33+
return internal::inline_wcsspn(s1, check);
3234
}
3335

3436
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcsspn.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@
1212
#include "hdr/types/wchar_t.h"
1313
#include "src/__support/common.h"
1414
#include "src/__support/macros/config.h"
15+
#include "wchar_utils.h"
16+
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+
};
1528

1629
namespace LIBC_NAMESPACE_DECL {
1730

18-
bool check(wchar_t c, const wchar_t *s2) {
19-
for (int n = 0; s2[n]; ++n) {
20-
if (s2[n] == c)
21-
return true;
22-
}
23-
return false;
24-
}
2531
LLVM_LIBC_FUNCTION(size_t, wcsspn, (const wchar_t *s1, const wchar_t *s2)) {
26-
size_t i = 0;
27-
for (; s1[i]; ++i) {
28-
if (!check(s1[i], s2))
29-
return i;
30-
}
31-
return i;
32+
CheckSpan check(s2);
33+
return internal::inline_wcsspn(s1, check);
3234
}
3335

3436
} // namespace LIBC_NAMESPACE_DECL

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5729,6 +5729,17 @@ libc_function(
57295729

57305730
############################## wchar targets ###############################
57315731

5732+
libc_support_library(
5733+
name = "wchar_utils",
5734+
hdrs = ["src/wchar/wchar_utils.h"],
5735+
deps = [
5736+
":__support_common",
5737+
":__support_macros_attributes",
5738+
":types_size_t",
5739+
":types_wchar_t",
5740+
],
5741+
)
5742+
57325743
libc_function(
57335744
name = "btowc",
57345745
srcs = ["src/wchar/btowc.cpp"],
@@ -5826,6 +5837,7 @@ libc_function(
58265837
":__support_macros_config",
58275838
":types_size_t",
58285839
":types_wchar_t",
5840+
":wchar_utils",
58295841
],
58305842
)
58315843

@@ -5911,6 +5923,7 @@ libc_function(
59115923
":__support_macros_config",
59125924
":types_size_t",
59135925
":types_wchar_t",
5926+
":wchar_utils",
59145927
],
59155928
)
59165929

0 commit comments

Comments
 (0)