Skip to content

[libc] wcslcpy implementation #146571

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wcsstr
libc.src.wchar.wcsncat
libc.src.wchar.wcscpy
libc.src.wchar.wcslcpy
libc.src.wchar.wmemchr
libc.src.wchar.wcpcpy
libc.src.wchar.wcpncpy
Expand Down
8 changes: 8 additions & 0 deletions libc/include/wchar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ functions:
arguments:
- type: wchar_t *__restrict
- type: const wchar_t *__restrict
- name: wcslcpy
standards:
- stdc
return_type: size_t
arguments:
- type: wchar_t *__restrict
- type: const wchar_t *__restrict
- type: size_t
- name: wcstok
standards:
- stdc
Expand Down
11 changes: 11 additions & 0 deletions libc/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,14 @@ add_entrypoint_object(
libc.hdr.wchar_macros
libc.src.string.string_utils
)

add_entrypoint_object(
wcslcpy
SRCS
wcslcpy.cpp
HDRS
wcslcpy.h
DEPENDS
libc.hdr.types.size_t
libc.hdr.wchar_macros
)
32 changes: 32 additions & 0 deletions libc/src/wchar/wcslcpy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===-- Implementation of wcslcpy -----------------------------------------===//
//
// 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/wcslcpy.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/string/string_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(size_t, wcslcpy,
(wchar_t *__restrict dst, const wchar_t *__restrict src,
size_t dstsize)) {
size_t len = internal::string_length(src);
if (dstsize == 0)
return len;
size_t i = 0;
for (; i < dstsize - 1 && src[i] != L'\0'; ++i)
dst[i] = src[i];
dst[i] = L'\0';
return len;
}

} // namespace LIBC_NAMESPACE_DECL
23 changes: 23 additions & 0 deletions libc/src/wchar/wcslcpy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation header for wcslcpy ---------------------------------===//
//
// 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_WCSLCPY_H
#define LLVM_LIBC_SRC_WCHAR_WCSLCPY_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 wcslcpy(wchar_t *__restrict dst, const wchar_t *__restrict src,
size_t dstsize);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_WCHAR_WCSLCPY_H
11 changes: 11 additions & 0 deletions libc/test/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,17 @@ add_libc_test(
libc.src.wchar.wcpcpy
)

add_libc_test(
wcslcpy_test
SUITE
libc_wchar_unittests
SRCS
wcslcpy_test.cpp
DEPENDS
libc.hdr.types.size_t
libc.src.wchar.wcslcpy
)

add_libc_test(
wcpncpy_test
SUITE
Expand Down
51 changes: 51 additions & 0 deletions libc/test/src/wchar/wcslcpy_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===-- Unittests for wcslcpy ---------------------------------------------===//
//
// 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 "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/wchar/wcslcpy.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcWCSLCpyTest, BiggerSource) {
const wchar_t *src = L"abcde";
wchar_t dst[3];
size_t res = LIBC_NAMESPACE::wcslcpy(dst, src, 3);
ASSERT_TRUE(dst[0] == L'a');
ASSERT_TRUE(dst[1] == L'b');
// Should append null terminator
ASSERT_TRUE(dst[2] == L'\0');
// Should still return length of src
ASSERT_EQ(res, size_t(5));
}

TEST(LlvmLibcWCSLCpyTest, CopyZero) {
const wchar_t *src = L"abcde";
wchar_t dst = L'f';
// Copying zero should not change destination
size_t res = LIBC_NAMESPACE::wcslcpy(&dst, src, 0);
ASSERT_TRUE(dst == L'f');
// Should still return length of src
ASSERT_EQ(res, size_t(5));
}

TEST(LlvmLibcWCSLCpyTest, SmallerSource) {
const wchar_t *src = L"abc";
wchar_t dst[7]{L"111111"};
size_t res = LIBC_NAMESPACE::wcslcpy(dst, src, 7);
ASSERT_TRUE(dst[0] == L'a');
ASSERT_TRUE(dst[1] == L'b');
ASSERT_TRUE(dst[2] == L'c');
// Should append null terminator after copying source
ASSERT_TRUE(dst[3] == L'\0');
// Should not change following characters
ASSERT_TRUE(dst[4] == L'1');
ASSERT_TRUE(dst[5] == L'1');
ASSERT_TRUE(dst[6] == L'\0');
// Should still return length of src
ASSERT_EQ(res, size_t(3));
}
13 changes: 13 additions & 0 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5827,6 +5827,19 @@ libc_function(
],
)

libc_function(
name = "wcslcpy",
srcs = ["src/wchar/wcslcpy.cpp"],
hdrs = ["src/wchar/wcslcpy.h"],
deps = [
":__support_common",
":__support_macros_config",
":string_utils",
":types_size_t",
":types_wchar_t",
],
)

libc_function(
name = "wcslen",
srcs = ["src/wchar/wcslen.cpp"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ libc_test(
],
)

libc_test(
name = "wcslcpy_test",
srcs = ["wcslcpy_test.cpp"],
deps = [
"//libc:types_size_t",
"//libc:types_wchar_t",
"//libc:wcslcpy",
],
)

libc_test(
name = "wcslen_test",
srcs = ["wcslen_test.cpp"],
Expand Down
Loading