-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[libc] wchar string conversion functions mb to wc #149423
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 all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5757dce
started mbstowcs implementation
c9d06fc
finished mbstowcs implementation
26a6741
implemented mbsrtowcs
0694e91
added tests for mbstowcs
1921356
added final test
6bc7497
added internal mbsnrtowcs
fbd6453
Merge branch 'main' into mbstowcs-implementation
40aa6ee
implemented mbsnrtowcs and tests
b3b7b2a
fixed formatting
d8f2d6f
added crash on nullptr for src and moved internal implementation to h…
18e2f26
fixed formatting
84ad6a9
header file changes
6595702
fixed spacing in cmake file
6a55826
fixed typo in cmake
eacce44
alphabetized yaml
e116c79
Merge branch 'main' into mbstowcs-implementation
sribee8 6bc272a
Merge branch 'main' into mbstowcs-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,66 @@ | ||
//===-- Implementation for mbsnrtowcs function ------------------*- 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___SUPPORT_WCHAR_MBSNRTOWCS_H | ||
#define LLVM_LIBC_SRC___SUPPORT_WCHAR_MBSNRTOWCS_H | ||
|
||
#include "hdr/errno_macros.h" | ||
#include "hdr/types/size_t.h" | ||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/error_or.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/__support/macros/null_check.h" | ||
#include "src/__support/wchar/character_converter.h" | ||
#include "src/__support/wchar/mbstate.h" | ||
#include "src/__support/wchar/string_converter.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace internal { | ||
|
||
LIBC_INLINE static ErrorOr<size_t> mbsnrtowcs(wchar_t *__restrict dst, | ||
const char **__restrict src, | ||
size_t nmc, size_t len, | ||
mbstate *__restrict ps) { | ||
LIBC_CRASH_ON_NULLPTR(src); | ||
// Checking if mbstate is valid | ||
CharacterConverter char_conv(ps); | ||
if (!char_conv.isValidState()) | ||
return Error(EINVAL); | ||
|
||
StringConverter<char8_t> str_conv(reinterpret_cast<const char8_t *>(*src), ps, | ||
len, nmc); | ||
size_t dst_idx = 0; | ||
ErrorOr<char32_t> converted = str_conv.popUTF32(); | ||
while (converted.has_value()) { | ||
if (dst != nullptr) | ||
dst[dst_idx] = converted.value(); | ||
// null terminator should not be counted in return value | ||
if (converted.value() == L'\0') { | ||
if (dst != nullptr) | ||
*src = nullptr; | ||
return dst_idx; | ||
} | ||
dst_idx++; | ||
converted = str_conv.popUTF32(); | ||
} | ||
|
||
if (converted.error() == -1) { // if we hit conversion limit | ||
if (dst != nullptr) | ||
*src += str_conv.getSourceIndex(); | ||
return dst_idx; | ||
} | ||
|
||
return Error(converted.error()); | ||
} | ||
|
||
} // namespace internal | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC___SUPPORT_WCHAR_MBSNRTOWCS_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,39 @@ | ||
//===-- Implementation of mbsnrtowcs --------------------------------------===// | ||
// | ||
// 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/mbsnrtowcs.h" | ||
|
||
#include "hdr/types/size_t.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/mbsnrtowcs.h" | ||
#include "src/__support/wchar/mbstate.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(size_t, mbsnrtowcs, | ||
(wchar_t *__restrict dst, const char **__restrict src, | ||
size_t nmc, size_t len, mbstate_t *__restrict ps)) { | ||
static internal::mbstate internal_mbstate; | ||
// If destination is null, ignore len | ||
len = dst == nullptr ? SIZE_MAX : len; | ||
auto ret = internal::mbsnrtowcs( | ||
dst, src, nmc, len, | ||
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,24 @@ | ||
//===-- Implementation header for mbsnrtowcs ------------------------------===// | ||
// | ||
// 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_MBSNRTOWCS_H | ||
#define LLVM_LIBC_SRC_WCHAR_MBSNRTOWCS_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 mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src, | ||
size_t nmc, size_t len, mbstate_t *__restrict ps); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_MBSNRTOWCS_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,39 @@ | ||
//===-- Implementation of mbsrtowcs ---------------------------------------===// | ||
// | ||
// 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/mbsrtowcs.h" | ||
|
||
#include "hdr/types/size_t.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/mbsnrtowcs.h" | ||
#include "src/__support/wchar/mbstate.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(size_t, mbsrtowcs, | ||
(wchar_t *__restrict dst, const char **__restrict src, | ||
size_t len, mbstate_t *__restrict ps)) { | ||
static internal::mbstate internal_mbstate; | ||
// If destination is null, ignore len | ||
len = dst == nullptr ? SIZE_MAX : len; | ||
auto ret = internal::mbsnrtowcs( | ||
dst, src, SIZE_MAX, len, | ||
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,24 @@ | ||
//===-- Implementation header for mbsrtowcs -------------------------------===// | ||
// | ||
// 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_MBSRTOWCS_H | ||
#define LLVM_LIBC_SRC_WCHAR_MBSRTOWCS_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 mbsrtowcs(wchar_t *__restrict dst, const char **__restrict src, | ||
size_t len, mbstate_t *__restrict ps); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_MBSRTOWCS_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,38 @@ | ||
//===-- Implementation of mbstowcs ----------------------------------------===// | ||
// | ||
// 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/mbstowcs.h" | ||
|
||
#include "hdr/types/size_t.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/mbsnrtowcs.h" | ||
#include "src/__support/wchar/mbstate.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(size_t, mbstowcs, | ||
(wchar_t *__restrict pwcs, const char *__restrict s, | ||
size_t n)) { | ||
// If destination is null, ignore n | ||
n = pwcs == nullptr ? SIZE_MAX : n; | ||
static internal::mbstate internal_mbstate; | ||
const char *temp = s; | ||
auto ret = internal::mbsnrtowcs(pwcs, &temp, SIZE_MAX, n, &internal_mbstate); | ||
|
||
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 mbstowcs --------------------------------===// | ||
// | ||
// 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_MBSTOWCS_H | ||
#define LLVM_LIBC_SRC_WCHAR_MBSTOWCS_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 mbstowcs(wchar_t *__restrict pwcs, const char *__restrict s, size_t n); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_MBSTOWCS_H |
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.