-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[libc] Implement wcs to mbs family of functions #149421
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
18
commits into
llvm:main
Choose a base branch
from
uzairnawaz:wcstombs-functions
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.
+1,051
−0
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
953eca5
implemented public functions
uzairnawaz d4a86b2
wcstombs test
uzairnawaz 586497d
wcsrtombs tests
uzairnawaz 7445bf7
created internal function
uzairnawaz efac57f
Merge branch 'main' into wcstombs-functions
uzairnawaz e2be69a
Merge branch 'main' into wcstombs-functions
uzairnawaz 843c79a
add invalid state test
uzairnawaz 69ed44c
public string functions + tests
uzairnawaz f01702a
yaml typo
uzairnawaz ee62e62
typo
uzairnawaz 1c1a7f9
fixed behavior when dest=null (shouldnt update src pointer)
uzairnawaz 61be17f
made internal function header only
uzairnawaz 2af9001
formatting
uzairnawaz f60a0ae
format
uzairnawaz 924f32f
added tests for src == null and mbstate == null
uzairnawaz af85c95
mssing headers for death tests
uzairnawaz eb0741a
added macro for death tests
uzairnawaz fd70ded
exclude windows
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
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,69 @@ | ||
//===-- Implementation header for wcsnrtombs ------------------------------===// | ||
// | ||
// 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_WCSNRTOMBS_H | ||
#define LLVM_LIBC_SRC__SUPPORT_WCHAR_WCSNRTOMBS_H | ||
|
||
#include "hdr/types/char32_t.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/macros/null_check.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> wcsnrtombs(char *__restrict s, | ||
const wchar_t **__restrict pwcs, | ||
size_t nwc, size_t len, | ||
mbstate *ps) { | ||
LIBC_CRASH_ON_NULLPTR(pwcs); | ||
LIBC_CRASH_ON_NULLPTR(ps); | ||
|
||
CharacterConverter cr(ps); | ||
if (!cr.isValidState()) | ||
return Error(EINVAL); | ||
|
||
if (s == nullptr) | ||
len = SIZE_MAX; | ||
|
||
StringConverter<char32_t> str_conv(reinterpret_cast<const char32_t *>(*pwcs), | ||
uzairnawaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ps, len, nwc); | ||
size_t dst_idx = 0; | ||
ErrorOr<char8_t> converted = str_conv.popUTF8(); | ||
while (converted.has_value()) { | ||
if (s != nullptr) | ||
s[dst_idx] = converted.value(); | ||
|
||
if (converted.value() == '\0') { | ||
if (s != nullptr) | ||
*pwcs = nullptr; | ||
return dst_idx; | ||
} | ||
|
||
dst_idx++; | ||
converted = str_conv.popUTF8(); | ||
} | ||
|
||
if (s != nullptr) | ||
*pwcs += str_conv.getSourceIndex(); | ||
|
||
if (converted.error() == -1) // if we hit conversion limit | ||
return dst_idx; | ||
|
||
return Error(converted.error()); | ||
} | ||
|
||
} // namespace internal | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC__SUPPORT_WCHAR_WCSNRTOMBS_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,40 @@ | ||
//===-- Implementation of wcsnrtombs --------------------------------------===// | ||
// | ||
// 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/wcsnrtombs.h" | ||
|
||
#include "hdr/types/char32_t.h" | ||
#include "hdr/types/mbstate_t.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/mbstate.h" | ||
#include "src/__support/wchar/wcsnrtombs.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(size_t, wcsnrtombs, | ||
(char *__restrict s, const wchar_t **__restrict pwcs, | ||
size_t nwc, size_t len, mbstate_t *ps)) { | ||
LIBC_CRASH_ON_NULLPTR(pwcs); | ||
static internal::mbstate internal_mbstate; | ||
auto result = internal::wcsnrtombs( | ||
s, pwcs, nwc, len, | ||
ps == nullptr ? &internal_mbstate | ||
: reinterpret_cast<internal::mbstate *>(ps)); | ||
if (!result.has_value()) { | ||
libc_errno = result.error(); | ||
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,24 @@ | ||
//===-- Implementation header for wcsnrtombs ------------------------------===// | ||
// | ||
// 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_WCSNRTOMBS_H | ||
#define LLVM_LIBC_SRC_WCHAR_WCSNRTOMBS_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 wcsnrtombs(char *__restrict s, const wchar_t **__restrict pwcs, | ||
size_t nwc, size_t len, mbstate_t *ps); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_WCSNRTOMBS_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,40 @@ | ||
//===-- Implementation of wcsrtombs ---------------------------------------===// | ||
// | ||
// 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/wcsrtombs.h" | ||
|
||
#include "hdr/types/char32_t.h" | ||
#include "hdr/types/mbstate_t.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/mbstate.h" | ||
#include "src/__support/wchar/wcsnrtombs.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(size_t, wcsrtombs, | ||
(char *__restrict s, const wchar_t **__restrict pwcs, | ||
size_t n, mbstate_t *ps)) { | ||
LIBC_CRASH_ON_NULLPTR(pwcs); | ||
static internal::mbstate internal_mbstate; | ||
auto result = internal::wcsnrtombs( | ||
s, pwcs, SIZE_MAX, n, | ||
ps == nullptr ? &internal_mbstate | ||
: reinterpret_cast<internal::mbstate *>(ps)); | ||
if (!result.has_value()) { | ||
libc_errno = result.error(); | ||
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,24 @@ | ||
//===-- Implementation header for wcsrtombs -------------------------------===// | ||
// | ||
// 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_WCSRTOMBS_H | ||
#define LLVM_LIBC_SRC_WCHAR_WCSRTOMBS_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 wcsrtombs(char *__restrict s, const wchar_t **__restrict pwcs, size_t n, | ||
mbstate_t *ps); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_WCSRTOMBS_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 wcstombs ----------------------------------------===// | ||
// | ||
// 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/wcstombs.h" | ||
|
||
#include "hdr/types/char32_t.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/mbstate.h" | ||
#include "src/__support/wchar/wcsnrtombs.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(size_t, wcstombs, | ||
(char *__restrict s, const wchar_t *__restrict wcs, | ||
size_t n)) { | ||
LIBC_CRASH_ON_NULLPTR(wcs); | ||
static internal::mbstate internal_mbstate; | ||
const wchar_t *wcs_ptr_copy = wcs; | ||
auto result = | ||
internal::wcsnrtombs(s, &wcs_ptr_copy, SIZE_MAX, n, &internal_mbstate); | ||
if (!result.has_value()) { | ||
libc_errno = result.error(); | ||
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,22 @@ | ||
//===-- Implementation header for wcstombs --------------------------------===// | ||
// | ||
// 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_WCSTOMBS_H | ||
#define LLVM_LIBC_SRC_WCHAR_WCSTOMBS_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 wcstombs(char *__restrict s, const wchar_t *__restrict pwcs, size_t n); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_WCSTOMBS_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
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.