-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[libc] Implemented wcrtomb internal function and public libc function #144596
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
uzairnawaz
wants to merge
7
commits into
llvm:main
Choose a base branch
from
uzairnawaz:wchar-wcrtomb
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc5e83f
Implemented wcrtomb internal and public function
uzairnawaz bb072df
formatting
uzairnawaz a16caf9
formatting
uzairnawaz fc563be
fix mbstate_t in overlay mode
uzairnawaz 094d2c4
moved libc error code to public function for consistency
uzairnawaz 591882f
use internal mbstate in internal function; used explicit casts
uzairnawaz babc998
formatting
uzairnawaz 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-- Definition of macros from mbstate_t.h -----------------------------===// | ||
// | ||
// 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_HDR_TYPES_MBSTATE_T_H | ||
#define LLVM_LIBC_HDR_TYPES_MBSTATE_T_H | ||
|
||
#ifdef LIBC_FULL_BUILD | ||
|
||
#include "include/llvm-libc-types/mbstate_t.h" | ||
|
||
#else // Overlay mode | ||
|
||
#include "hdr/wchar_overlay.h" | ||
|
||
#endif // LLVM_LIBC_FULL_BUILD | ||
|
||
#endif // LLVM_LIBC_HDR_TYPES_MBSTATE_T_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
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,50 @@ | ||
//===-- Implementation of wcrtomb -----------------------------------------===// | ||
// | ||
// 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/wchar/wcrtomb.h" | ||
#include "src/__support/error_or.h" | ||
#include "src/__support/wchar/character_converter.h" | ||
#include "src/__support/wchar/mbstate.h" | ||
|
||
#include "hdr/types/char32_t.h" | ||
#include "hdr/types/size_t.h" | ||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/common.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace internal { | ||
|
||
ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc, | ||
mbstate *__restrict ps) { | ||
static_assert(sizeof(wchar_t) == 4); | ||
|
||
CharacterConverter cr(ps); | ||
|
||
// when s is nullptr, this is equivalent to wcrtomb(buf, L'\0', ps) | ||
char buf[sizeof(wchar_t) / sizeof(char)]; | ||
uzairnawaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (s == nullptr) { | ||
s = buf; | ||
wc = L'\0'; | ||
} | ||
|
||
int status = cr.push(static_cast<char32_t>(wc)); | ||
if (status != 0) | ||
return Error(status); | ||
|
||
size_t count = 0; | ||
while (!cr.isComplete()) { | ||
auto utf8 = cr.pop_utf8(); // can never fail as long as the push succeeded | ||
*s = utf8.value(); | ||
s++; | ||
count++; | ||
} | ||
return count; | ||
} | ||
|
||
} // namespace internal | ||
} // 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,26 @@ | ||
//===-- Implementation header for wcrtomb ---------------------------------===// | ||
// | ||
// 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__SUPPORT_WCHAR_WCRTOMB_H | ||
#define LLVM_LIBC_SRC__SUPPORT_WCHAR_WCRTOMB_H | ||
|
||
#include "hdr/types/size_t.h" | ||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/error_or.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/__support/wchar/mbstate.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace internal { | ||
|
||
ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc, mbstate *__restrict ps); | ||
|
||
} // namespace internal | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC__SUPPORT_WCHAR_WCRTOMB_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,37 @@ | ||
//===-- Implementation of wcrtomb -----------------------------------------===// | ||
// | ||
// 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/wcrtomb.h" | ||
|
||
#include "hdr/types/mbstate_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(size_t, wcrtomb, | ||
(char *__restrict s, wchar_t wc, mbstate_t *__restrict ps)) { | ||
static internal::mbstate internal_mbstate; | ||
|
||
auto result = internal::wcrtomb( | ||
s, wc, | ||
ps == nullptr ? &internal_mbstate | ||
: reinterpret_cast<internal::mbstate *>(ps)); | ||
|
||
if (!result.has_value()) { | ||
libc_errno = EILSEQ; | ||
return -1; | ||
} | ||
|
||
return 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,23 @@ | ||
//===-- Implementation header for wcrtomb -----------------------*- 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_WCRTOMB_H | ||
#define LLVM_LIBC_SRC_WCHAR_WCRTOMB_H | ||
|
||
#include "hdr/types/mbstate_t.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 wcrtomb(char *__restrict s, wchar_t wc, mbstate_t *__restrict ps); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_WCRTOMB_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,93 @@ | ||
//===-- Unittests for wcrtomb --------------------------------------------===// | ||
// | ||
// 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/mbstate_t.h" | ||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/libc_errno.h" | ||
#include "src/string/memset.h" | ||
#include "src/wchar/wcrtomb.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
TEST(LlvmLibcWCRToMBTest, OneByte) { | ||
mbstate_t state; | ||
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t)); | ||
wchar_t wc = L'U'; | ||
char mb[4]; | ||
size_t cnt = LIBC_NAMESPACE::wcrtomb(mb, wc, &state); | ||
ASSERT_EQ(cnt, static_cast<size_t>(1)); | ||
ASSERT_EQ(mb[0], 'U'); | ||
} | ||
|
||
TEST(LlvmLibcWCRToMBTest, TwoByte) { | ||
mbstate_t state; | ||
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t)); | ||
// testing utf32: 0xff -> utf8: 0xc3 0xbf | ||
wchar_t wc = 0xff; | ||
char mb[4]; | ||
size_t cnt = LIBC_NAMESPACE::wcrtomb(mb, wc, &state); | ||
ASSERT_EQ(cnt, static_cast<size_t>(2)); | ||
ASSERT_EQ(mb[0], static_cast<char>(0xc3)); | ||
ASSERT_EQ(mb[1], static_cast<char>(0xbf)); | ||
} | ||
|
||
TEST(LlvmLibcWCRToMBTest, ThreeByte) { | ||
mbstate_t state; | ||
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t)); | ||
// testing utf32: 0xac15 -> utf8: 0xea 0xb0 0x95 | ||
wchar_t wc = 0xac15; | ||
char mb[4]; | ||
size_t cnt = LIBC_NAMESPACE::wcrtomb(mb, wc, &state); | ||
ASSERT_EQ(cnt, static_cast<size_t>(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(LlvmLibcWCRToMBTest, FourByte) { | ||
mbstate_t state; | ||
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t)); | ||
// testing utf32: 0x1f921 -> utf8: 0xf0 0x9f 0xa4 0xa1 | ||
wchar_t wc = 0x1f921; | ||
char mb[4]; | ||
size_t cnt = LIBC_NAMESPACE::wcrtomb(mb, wc, &state); | ||
ASSERT_EQ(cnt, static_cast<size_t>(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(LlvmLibcWCRToMBTest, NullString) { | ||
mbstate_t state; | ||
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t)); | ||
wchar_t wc = L'A'; | ||
char mb[4]; | ||
|
||
// should be equivalent to the call wcrtomb(buf, L'\0', state) | ||
size_t cnt1 = LIBC_NAMESPACE::wcrtomb(nullptr, wc, &state); | ||
size_t cnt2 = LIBC_NAMESPACE::wcrtomb(mb, L'\0', &state); | ||
|
||
ASSERT_EQ(cnt1, cnt2); | ||
} | ||
|
||
TEST(LlvmLibcWCRToMBTest, NullState) { | ||
wchar_t wc = L'A'; | ||
char mb[4]; | ||
size_t cnt = LIBC_NAMESPACE::wcrtomb(mb, wc, nullptr); | ||
ASSERT_EQ(cnt, static_cast<size_t>(1)); | ||
} | ||
|
||
TEST(LlvmLibcWCRToMBTest, InvalidWchar) { | ||
mbstate_t state; | ||
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t)); | ||
wchar_t wc = 0x12ffff; | ||
char mb[4]; | ||
size_t cnt = LIBC_NAMESPACE::wcrtomb(mb, wc, &state); | ||
ASSERT_EQ(cnt, static_cast<size_t>(-1)); | ||
ASSERT_EQ(static_cast<int>(libc_errno), 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.