Skip to content

Commit 45d0750

Browse files
sribee8Sriya Pratipati
andauthored
[libc] Cleaned up wcsspn and wcscspn (#147408)
created internal wcsspn to avoid duplicated code --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent 15715f4 commit 45d0750

File tree

5 files changed

+82
-26
lines changed

5 files changed

+82
-26
lines changed

libc/src/wchar/CMakeLists.txt

Lines changed: 14 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
@@ -255,6 +265,8 @@ add_entrypoint_object(
255265
DEPENDS
256266
libc.hdr.wchar_macros
257267
libc.hdr.types.size_t
268+
libc.src.wchar.wchar_utils
269+
libc.src.__support.macros.null_check
258270
)
259271

260272
add_entrypoint_object(
@@ -266,6 +278,8 @@ add_entrypoint_object(
266278
DEPENDS
267279
libc.hdr.wchar_macros
268280
libc.hdr.types.size_t
281+
libc.src.wchar.wchar_utils
282+
libc.src.__support.macros.null_check
269283
)
270284

271285
add_entrypoint_object(

libc/src/wchar/wchar_utils.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
// 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;
27+
}
28+
29+
// bool should be true for wcscspn for complimentary span
30+
// should be false for wcsspn since we want it to span
31+
LIBC_INLINE static size_t wcsspn(const wchar_t *s1, const wchar_t *s2,
32+
bool not_match_set) {
33+
size_t i = 0;
34+
for (; s1[i]; ++i) {
35+
bool in_set = wcschr(s1[i], s2);
36+
if (in_set == not_match_set)
37+
return i;
38+
}
39+
return i;
40+
}
41+
42+
} // namespace internal
43+
} // namespace LIBC_NAMESPACE_DECL
44+
45+
#endif // LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H

libc/src/wchar/wcscspn.cpp

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

1618
namespace LIBC_NAMESPACE_DECL {
1719

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;
22-
}
23-
return true;
24-
}
2520
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;
21+
LIBC_CRASH_ON_NULLPTR(s1);
22+
LIBC_CRASH_ON_NULLPTR(s2);
23+
return internal::wcsspn(s1, s2, /*not_match_set=*/true);
3224
}
3325

3426
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcsspn.cpp

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

1618
namespace LIBC_NAMESPACE_DECL {
1719

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-
}
2520
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;
21+
LIBC_CRASH_ON_NULLPTR(s1);
22+
LIBC_CRASH_ON_NULLPTR(s2);
23+
return internal::wcsspn(s1, s2, /*not_match_set=*/false);
3224
}
3325

3426
} // 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
@@ -6083,6 +6083,17 @@ libc_function(
60836083

60846084
############################## wchar targets ###############################
60856085

6086+
libc_support_library(
6087+
name = "wchar_utils",
6088+
hdrs = ["src/wchar/wchar_utils.h"],
6089+
deps = [
6090+
":__support_common",
6091+
":__support_macros_attributes",
6092+
":types_size_t",
6093+
":types_wchar_t",
6094+
],
6095+
)
6096+
60866097
libc_function(
60876098
name = "btowc",
60886099
srcs = ["src/wchar/btowc.cpp"],
@@ -6180,6 +6191,7 @@ libc_function(
61806191
":__support_macros_config",
61816192
":types_size_t",
61826193
":types_wchar_t",
6194+
":wchar_utils",
61836195
],
61846196
)
61856197

@@ -6291,6 +6303,7 @@ libc_function(
62916303
":__support_macros_config",
62926304
":types_size_t",
62936305
":types_wchar_t",
6306+
":wchar_utils",
62946307
],
62956308
)
62966309

0 commit comments

Comments
 (0)