-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Implemented wctomb #145554
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
+162
−0
Merged
[libc] Implemented wctomb #145554
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,35 @@ | ||
//===-- Implementation of wctomb ------------------------------------------===// | ||
// | ||
// 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/wctomb.h" | ||
|
||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/libc_errno.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/__support/wchar/mbstate.h" | ||
#include "src/__support/wchar/wcrtomb.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(int, wctomb, (char *s, wchar_t wc)) { | ||
static internal::mbstate internal_mbstate; | ||
if (s == nullptr) | ||
return 0; | ||
|
||
auto result = internal::wcrtomb(s, wc, &internal_mbstate); | ||
|
||
if (!result.has_value()) { // invalid wide character | ||
libc_errno = EILSEQ; | ||
return -1; | ||
} | ||
|
||
return static_cast<int>(result.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 wctomb ------------------------*- C++ -*-===// | ||
// | ||
// 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_WCTOMB_H | ||
#define LLVM_LIBC_SRC_WCHAR_WCTOMB_H | ||
|
||
#include "hdr/types/mbstate_t.h" | ||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int wctomb(char *s, wchar_t wc); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_WCTOMB_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,73 @@ | ||
//===-- Unittests for wctomb ----------------------------------------------===// | ||
// | ||
// 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/wchar_t.h" | ||
#include "src/__support/libc_errno.h" | ||
#include "src/wchar/wctomb.h" | ||
#include "test/UnitTest/ErrnoCheckingTest.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
using LlvmLibcWCToMBTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; | ||
|
||
TEST(LlvmLibcWCToMBTest, OneByte) { | ||
wchar_t wc = L'U'; | ||
char mb[4]; | ||
int cnt = LIBC_NAMESPACE::wctomb(mb, wc); | ||
ASSERT_EQ(cnt, 1); | ||
ASSERT_EQ(mb[0], 'U'); | ||
} | ||
|
||
TEST(LlvmLibcWCToMBTest, TwoByte) { | ||
// testing utf32: 0xff -> utf8: 0xc3 0xbf | ||
wchar_t wc = 0xff; | ||
char mb[4]; | ||
int cnt = LIBC_NAMESPACE::wctomb(mb, wc); | ||
ASSERT_EQ(cnt, 2); | ||
ASSERT_EQ(mb[0], static_cast<char>(0xc3)); | ||
ASSERT_EQ(mb[1], static_cast<char>(0xbf)); | ||
} | ||
|
||
TEST(LlvmLibcWCToMBTest, ThreeByte) { | ||
// testing utf32: 0xac15 -> utf8: 0xea 0xb0 0x95 | ||
wchar_t wc = 0xac15; | ||
char mb[4]; | ||
int cnt = LIBC_NAMESPACE::wctomb(mb, wc); | ||
ASSERT_EQ(cnt, 3); | ||
ASSERT_EQ(mb[0], static_cast<char>(0xea)); | ||
ASSERT_EQ(mb[1], static_cast<char>(0xb0)); | ||
ASSERT_EQ(mb[2], static_cast<char>(0x95)); | ||
} | ||
|
||
TEST(LlvmLibcWCToMBTest, FourByte) { | ||
// testing utf32: 0x1f921 -> utf8: 0xf0 0x9f 0xa4 0xa1 | ||
wchar_t wc = 0x1f921; | ||
char mb[4]; | ||
int cnt = LIBC_NAMESPACE::wctomb(mb, wc); | ||
ASSERT_EQ(cnt, 4); | ||
ASSERT_EQ(mb[0], static_cast<char>(0xf0)); | ||
ASSERT_EQ(mb[1], static_cast<char>(0x9f)); | ||
ASSERT_EQ(mb[2], static_cast<char>(0xa4)); | ||
ASSERT_EQ(mb[3], static_cast<char>(0xa1)); | ||
} | ||
|
||
TEST(LlvmLibcWCToMBTest, NullString) { | ||
wchar_t wc = L'A'; | ||
|
||
int cnt = LIBC_NAMESPACE::wctomb(nullptr, wc); | ||
|
||
// no state-dependent encoding | ||
ASSERT_EQ(cnt, 0); | ||
} | ||
|
||
TEST(LlvmLibcWCToMBTest, InvalidWchar) { | ||
wchar_t wc = 0x12ffff; | ||
char mb[4]; | ||
int cnt = LIBC_NAMESPACE::wctomb(mb, wc); | ||
ASSERT_EQ(cnt, -1); | ||
ASSERT_ERRNO_EQ(EILSEQ); | ||
} |
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.