-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[libc] Implemented mblen functions #150141
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
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
69768eb
implemented mblen
692505f
implemented mbrlen
ed00cc0
Merge branch 'main' into mblen-implementation
1eab8de
[libc] Implemented mblen functions
4c591a2
removed restrict from parameter
e52ee89
fixed formatting
2d6506b
Merge branch 'main' into mblen-implementation
sribee8 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,35 @@ | ||
| //===-- Implementation of mblen -------------------------------------------===// | ||
| // | ||
| // 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/mblen.h" | ||
|
|
||
| #include "hdr/types/size_t.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/libc_errno.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/wchar/mbrtowc.h" | ||
| #include "src/__support/wchar/mbstate.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, mblen, (const char *__restrict s, size_t n)) { | ||
| // returns 0 since UTF-8 encoding is not state-dependent | ||
| if (s == nullptr) | ||
| return 0; | ||
| internal::mbstate internal_mbstate; | ||
| auto ret = internal::mbrtowc(nullptr, s, n, &internal_mbstate); | ||
| if (!ret.has_value() || static_cast<int>(ret.value()) == -2) { | ||
| // Encoding failure | ||
| if (!ret.has_value()) | ||
| libc_errno = EILSEQ; | ||
| return -1; | ||
| } | ||
| return static_cast<int>(ret.value()); | ||
| } | ||
|
|
||
| } // 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,21 @@ | ||
| //===-- Implementation header for mblen -----------------------------------===// | ||
| // | ||
| // 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_MBLEN_H | ||
| #define LLVM_LIBC_SRC_WCHAR_MBLEN_H | ||
|
|
||
| #include "hdr/types/size_t.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| int mblen(const char *__restrict s, size_t n); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_WCHAR_MBLEN_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| //===-- Implementation of mbrlen ------------------------------------------===// | ||
| // | ||
| // 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/mbrlen.h" | ||
|
|
||
| #include "hdr/types/mbstate_t.h" | ||
| #include "hdr/types/size_t.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/libc_errno.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/wchar/mbrtowc.h" | ||
| #include "src/__support/wchar/mbstate.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(size_t, mbrlen, | ||
| (const char *__restrict s, size_t n, | ||
| mbstate_t *__restrict ps)) { | ||
| static internal::mbstate internal_mbstate; | ||
| auto ret = internal::mbrtowc(nullptr, s, n, | ||
| ps == nullptr | ||
| ? &internal_mbstate | ||
| : reinterpret_cast<internal::mbstate *>(ps)); | ||
| if (!ret.has_value()) { | ||
| // Encoding failure | ||
| libc_errno = ret.error(); | ||
| return -1; | ||
| } | ||
| return ret.value(); | ||
| } | ||
|
|
||
| } // 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,22 @@ | ||
| //===-- Implementation header for mbrlen ----------------------------------===// | ||
| // | ||
| // 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_MBRLEN_H | ||
| #define LLVM_LIBC_SRC_WCHAR_MBRLEN_H | ||
|
|
||
| #include "hdr/types/mbstate_t.h" | ||
| #include "hdr/types/size_t.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| size_t mbrlen(const char *__restrict s, size_t n, mbstate_t *__restrict ps); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_WCHAR_MBRLEN_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,104 @@ | ||
| //===-- Unittests for mblen -----------------------------------------------===// | ||
| // | ||
| // 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/__support/libc_errno.h" | ||
| #include "src/wchar/mblen.h" | ||
| #include "test/UnitTest/ErrnoCheckingTest.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| using LlvmLibcMBLenTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; | ||
|
|
||
| TEST_F(LlvmLibcMBLenTest, OneByte) { | ||
| const char *ch = "A"; | ||
| int n = LIBC_NAMESPACE::mblen(ch, 1); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| ASSERT_EQ(n, 1); | ||
|
|
||
| // Should fail since we have not read enough | ||
| n = LIBC_NAMESPACE::mblen(ch, 0); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| ASSERT_EQ(n, -1); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcMBLenTest, TwoByte) { | ||
| const char ch[2] = {static_cast<char>(0xC2), | ||
| static_cast<char>(0x8E)}; // car symbol | ||
| int n = LIBC_NAMESPACE::mblen(ch, 4); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| ASSERT_EQ(n, 2); | ||
|
|
||
| // Should fail since we have not read enough | ||
| n = LIBC_NAMESPACE::mblen(ch, 1); | ||
| ASSERT_EQ(n, -1); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| // Should fail after trying to read next byte too | ||
| n = LIBC_NAMESPACE::mblen(ch + 1, 1); | ||
| ASSERT_EQ(n, -1); | ||
| // This one should be an invalid starting byte so should set errno | ||
| ASSERT_ERRNO_EQ(EILSEQ); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcMBLenTest, ThreeByte) { | ||
| const char ch[3] = {static_cast<char>(0xE2), static_cast<char>(0x88), | ||
| static_cast<char>(0x91)}; // ∑ sigma symbol | ||
| int n = LIBC_NAMESPACE::mblen(ch, 3); | ||
| ASSERT_EQ(n, 3); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
|
|
||
| // Should fail since we have not read enough | ||
| n = LIBC_NAMESPACE::mblen(ch, 2); | ||
| ASSERT_EQ(n, -1); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcMBLenTest, FourByte) { | ||
| const char ch[4] = {static_cast<char>(0xF0), static_cast<char>(0x9F), | ||
| static_cast<char>(0xA4), | ||
| static_cast<char>(0xA1)}; // 🤡 clown emoji | ||
| int n = LIBC_NAMESPACE::mblen(ch, 4); | ||
| ASSERT_EQ(n, 4); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
|
|
||
| // Should fail since we have not read enough | ||
| n = LIBC_NAMESPACE::mblen(ch, 2); | ||
| ASSERT_EQ(n, -1); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcMBLenTest, InvalidByte) { | ||
| const char ch[1] = {static_cast<char>(0x80)}; | ||
| int n = LIBC_NAMESPACE::mblen(ch, 1); | ||
| ASSERT_EQ(n, -1); | ||
| ASSERT_ERRNO_EQ(EILSEQ); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcMBLenTest, InvalidMultiByte) { | ||
| const char ch[4] = {static_cast<char>(0x80), static_cast<char>(0x00), | ||
| static_cast<char>(0x80), | ||
| static_cast<char>(0x00)}; // invalid sequence of bytes | ||
| // Trying to push all 4 should error | ||
| int n = LIBC_NAMESPACE::mblen(ch, 4); | ||
| ASSERT_EQ(n, -1); | ||
| ASSERT_ERRNO_EQ(EILSEQ); | ||
|
|
||
| // Trying to push the second and third should correspond to null wc | ||
| n = LIBC_NAMESPACE::mblen(ch + 1, 2); | ||
| ASSERT_EQ(n, 0); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcMBLenTest, NullString) { | ||
| // reading on nullptr should return 0 | ||
| int n = LIBC_NAMESPACE::mblen(nullptr, 2); | ||
| ASSERT_EQ(n, 0); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| // reading a null terminator should return 0 | ||
| const char *ch = "\0"; | ||
| n = LIBC_NAMESPACE::mblen(ch, 1); | ||
| ASSERT_EQ(n, 0); | ||
| } |
Oops, something went wrong.
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.