Skip to content

[libc][wchar] implement wcslen #124150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/gpu/amdgpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.nanosleep

# wchar.h entrypoints
libc.src.wchar.wcslen
libc.src.wchar.wctob

# locale.h entrypoints
Expand Down
1 change: 1 addition & 0 deletions libc/config/gpu/nvptx/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.nanosleep

# wchar.h entrypoints
libc.src.wchar.wcslen
libc.src.wchar.wctob

# locale.h entrypoints
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.write

# wchar.h entrypoints
libc.src.wchar.wcslen
libc.src.wchar.wctob

# sys/uio.h entrypoints
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.write

# wchar.h entrypoints
libc.src.wchar.wcslen
libc.src.wchar.wctob
)

Expand Down
3 changes: 2 additions & 1 deletion libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.write

# wchar.h entrypoints
libc.src.wchar.wctob
libc.src.wchar.btowc
libc.src.wchar.wcslen
libc.src.wchar.wctob

# sys/uio.h entrypoints
libc.src.sys.uio.writev
Expand Down
6 changes: 6 additions & 0 deletions libc/include/wchar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ types:
enums: []
objects: []
functions:
- name: wcslen
standards:
- stdc
return_type: size_t
arguments:
- type: const wchar_t *
- name: wctob
standards:
- stdc
Expand Down
17 changes: 17 additions & 0 deletions libc/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
add_header_library(
wide_string_utils
HDRS
wide_string_utils.h
)

add_entrypoint_object(
wcslen
SRCS
wcslen.cpp
HDRS
wcslen.h
DEPENDS
.wide_string_utils
libc.hdr.types.size_t
libc.hdr.types.wchar_t
)

add_entrypoint_object(
wctob
Expand Down
23 changes: 23 additions & 0 deletions libc/src/wchar/wcslen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation of wcslen ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/wchar/wcslen.h"

#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/wchar/wide_string_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(size_t, wcslen, (const wchar_t *src)) {
return internal::wide_string_length(src);
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/wchar/wcslen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for wcslen ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_WCHAR_WCSLEN_H
#define LLVM_LIBC_SRC_WCHAR_WCSLEN_H

#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

size_t wcslen (const wchar_t *src);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_WCHAR_WCSLEN_H
29 changes: 29 additions & 0 deletions libc/src/wchar/wide_string_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===-- Wide String utils -------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_WCHAR_WIDE_STRING_UTILS_H
#define LLVM_LIBC_SRC_WCHAR_WIDE_STRING_UTILS_H

#include "src/__support/macros/config.h"
#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"

namespace LIBC_NAMESPACE_DECL {
namespace internal {

LIBC_INLINE size_t wide_string_length(const wchar_t *src) {
const wchar_t *cpy = src;
while (*cpy)
++cpy;
return cpy - src;
}

} // namespace internal
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_WCHAR_WIDE_STRING_UTILS_H
12 changes: 12 additions & 0 deletions libc/test/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
add_custom_target(libc_wchar_unittests)

add_libc_test(
wcslen_test
SUITE
libc_wchar_unittests
SRCS
wcslen_test.cpp
DEPENDS
libc.hdr.types.size_t
libc.hdr.types.wchar_t
libc.src.wchar.wcslen
)

add_libc_test(
btowc_test
SUITE
Expand Down
26 changes: 26 additions & 0 deletions libc/test/src/wchar/wcslen_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Unittests for wcslen ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/wchar/wcslen.h"
#include "hdr/types/wchar_t.h"
#include "hdr/types/size_t.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcWCSLenTest, EmptyString) {
const wchar_t *empty = L"";

size_t result = LIBC_NAMESPACE::wcslen(empty);
ASSERT_EQ(size_t{0}, result);
}

TEST(LlvmLibcWCSLenTest, AnyString) {
const wchar_t *any = L"Hello World!";

size_t result = LIBC_NAMESPACE::wcslen(any);
ASSERT_EQ(size_t{12}, result);
}
Loading