-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[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
sribee8
wants to merge
3
commits into
llvm:main
Choose a base branch
from
sribee8:wcslcpy-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−0
Open
[libc] wcslcpy implementation #146571
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//===-- 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"123456"}; | ||
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'5'); | ||
ASSERT_TRUE(dst[5] == L'6'); | ||
ASSERT_TRUE(dst[6] == L'\0'); | ||
// Should still return length of src | ||
ASSERT_EQ(res, size_t(3)); | ||
} | ||
|
||
TEST(LlvmLibcWCSLCpyTest, DoesNotCopyAfterNull) { | ||
const wchar_t src[5] = {L'a', L'b', L'\0', L'c', L'd'}; | ||
wchar_t dst[5]{L"1234"}; | ||
size_t res = LIBC_NAMESPACE::wcslcpy(dst, src, 5); | ||
ASSERT_TRUE(dst[0] == L'a'); | ||
ASSERT_TRUE(dst[1] == L'b'); | ||
ASSERT_TRUE(dst[2] == L'\0'); | ||
// Should not change following characters | ||
ASSERT_TRUE(dst[3] == L'4'); | ||
ASSERT_TRUE(dst[4] == L'\0'); | ||
// Should still return length of src | ||
ASSERT_EQ(res, size_t(2)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.