Skip to content

Commit a8173a1

Browse files
[libc][wchar] implement wcslen
Add internal helper, which may be reusable when implementing wmemchr, wcspbrk, wcsrchr, wcsstr. Link: #121183 Link: #124027 Co-authored-by: Nick Desaulniers <[email protected]>
1 parent 4bd0440 commit a8173a1

File tree

12 files changed

+141
-1
lines changed

12 files changed

+141
-1
lines changed

libc/config/gpu/amdgpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
261261
libc.src.time.nanosleep
262262

263263
# wchar.h entrypoints
264+
libc.src.wchar.wcslen
264265
libc.src.wchar.wctob
265266

266267
# locale.h entrypoints

libc/config/gpu/nvptx/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
261261
libc.src.time.nanosleep
262262

263263
# wchar.h entrypoints
264+
libc.src.wchar.wcslen
264265
libc.src.wchar.wctob
265266

266267
# locale.h entrypoints

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ set(TARGET_LIBC_ENTRYPOINTS
350350
libc.src.unistd.write
351351

352352
# wchar.h entrypoints
353+
libc.src.wchar.wcslen
353354
libc.src.wchar.wctob
354355

355356
# sys/uio.h entrypoints

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
346346
libc.src.unistd.write
347347

348348
# wchar.h entrypoints
349+
libc.src.wchar.wcslen
349350
libc.src.wchar.wctob
350351
)
351352

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ set(TARGET_LIBC_ENTRYPOINTS
349349
libc.src.unistd.write
350350

351351
# wchar.h entrypoints
352-
libc.src.wchar.wctob
353352
libc.src.wchar.btowc
353+
libc.src.wchar.wcslen
354+
libc.src.wchar.wctob
354355

355356
# sys/uio.h entrypoints
356357
libc.src.sys.uio.writev

libc/include/wchar.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ types:
99
enums: []
1010
objects: []
1111
functions:
12+
- name: wcslen
13+
standards:
14+
- stdc
15+
return_type: size_t
16+
arguments:
17+
- type: const wchar_t *
1218
- name: wctob
1319
standards:
1420
- stdc

libc/src/wchar/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
add_header_library(
2+
wide_string_utils
3+
HDRS
4+
wide_string_utils.h
5+
)
6+
7+
add_entrypoint_object(
8+
wcslen
9+
SRCS
10+
wcslen.cpp
11+
HDRS
12+
wcslen.h
13+
DEPENDS
14+
.wide_string_utils
15+
libc.hdr.types.size_t
16+
libc.hdr.types.wchar_t
17+
)
118

219
add_entrypoint_object(
320
wctob

libc/src/wchar/wcslen.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation of wcslen ------------------------------------------===//
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+
#include "src/wchar/wcslen.h"
10+
11+
#include "hdr/types/size_t.h"
12+
#include "hdr/types/wchar_t.h"
13+
#include "src/__support/common.h"
14+
#include "src/__support/macros/config.h"
15+
#include "src/wchar/wide_string_utils.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
LLVM_LIBC_FUNCTION(size_t, wcslen, (const wchar_t *src)) {
20+
return internal::wide_string_length(src);
21+
}
22+
23+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcslen.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for wcslen ----------------------------------===//
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_WCSLEN_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSLEN_H
11+
12+
#include "hdr/types/size_t.h"
13+
#include "hdr/types/wchar_t.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
size_t wcslen (const wchar_t *src);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_WCHAR_WCSLEN_H

libc/src/wchar/wide_string_utils.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Wide String utils -------------------------------------------------===//
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_WIDE_STRING_UTILS_H
10+
#define LLVM_LIBC_SRC_WCHAR_WIDE_STRING_UTILS_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "hdr/types/size_t.h"
14+
#include "hdr/types/wchar_t.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
namespace internal {
18+
19+
LIBC_INLINE size_t wide_string_length(const wchar_t *src) {
20+
const wchar_t *cpy = src;
21+
while (*cpy)
22+
++cpy;
23+
return cpy - src;
24+
}
25+
26+
} // namespace internal
27+
} // namespace LIBC_NAMESPACE_DECL
28+
29+
#endif // LLVM_LIBC_SRC_WCHAR_WIDE_STRING_UTILS_H

0 commit comments

Comments
 (0)