-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Implemented wcstrombs internal and public function #145794
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
10
commits into
llvm:main
Choose a base branch
from
uzairnawaz:wchar-wcsrtombs
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
10 commits
Select commit
Hold shift + click to select a range
a58f040
added internal wcsrtombs function
uzairnawaz 4bb40cd
began implementing public function
uzairnawaz 0de8381
build files
uzairnawaz 23d9216
fixed edge case with null dest; added tests
uzairnawaz 1da6360
refactored internal wcrtomb to work with wcsrtombs
uzairnawaz 72a87b0
Merge branch 'main' into wchar-wcsrtombs
uzairnawaz fe88a13
add test for invalid state
uzairnawaz 9049c06
Merge branch 'main' into wchar-wcsrtombs
uzairnawaz 04a67fd
added death test
uzairnawaz b078334
added errno checks + 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
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,55 @@ | ||
//===-- 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/__support/wchar/wcsrtombs.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/error_or.h" | ||
#include "src/__support/libc_assert.h" | ||
#include "src/__support/wchar/character_converter.h" | ||
#include "src/__support/wchar/mbstate.h" | ||
#include "src/__support/wchar/wcrtomb.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace internal { | ||
|
||
ErrorOr<size_t> wcsrtombs(char *__restrict dst, const wchar_t **__restrict src, | ||
size_t len, mbstate *__restrict ps) { | ||
Comment on lines
+23
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is |
||
// ignore len parameter when theres no destination string | ||
if (dst == nullptr) | ||
len = SIZE_MAX; | ||
|
||
size_t bytes_written = 0; | ||
while (bytes_written < len) { | ||
auto result = | ||
internal::wcrtomb(dst == nullptr ? nullptr : dst + bytes_written, **src, | ||
ps, len - bytes_written); | ||
if (!result.has_value()) | ||
return result; // forward the error | ||
|
||
// couldn't complete conversion | ||
if (result.value() == static_cast<size_t>(-1)) | ||
return len; | ||
|
||
// terminate the loop after converting the null wide character | ||
if (**src == L'\0') { | ||
*src = nullptr; | ||
return bytes_written; | ||
} | ||
|
||
bytes_written += result.value(); | ||
(*src)++; | ||
} | ||
|
||
return bytes_written; | ||
} | ||
|
||
} // 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,27 @@ | ||
//===-- 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__SUPPORT_WCHAR_WCSRTOMBS_H | ||
#define LLVM_LIBC_SRC__SUPPORT_WCHAR_WCSRTOMBS_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> wcsrtombs(char *__restrict dst, const wchar_t **__restrict src, | ||
size_t len, mbstate *__restrict ps); | ||
|
||
} // namespace internal | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC__SUPPORT_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
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,42 @@ | ||
//===-- 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/mbstate_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/wcsrtombs.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(size_t, wcsrtombs, | ||
(char *__restrict dst, const wchar_t **__restrict src, | ||
size_t len, mbstate_t *__restrict ps)) { | ||
static internal::mbstate internal_mbstate; | ||
|
||
LIBC_CRASH_ON_NULLPTR(src); | ||
|
||
auto result = internal::wcsrtombs( | ||
dst, src, 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 wcsrtombs ---------------------*- 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_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 dst, const wchar_t **__restrict src, | ||
size_t len, mbstate_t *__restrict 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're calling this function
wcrtomb
it should match that interface. If you want to have a different interface, then that's fine but it should have a different name to avoid confusion.