-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc] mbrtowc implementation #144760
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
[libc] mbrtowc implementation #144760
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1ac2bff
created initial files and internal function
98ea7ae
[libc] mbrtowc implementation
a34d1ca
mbstate struct change for consistency
9464170
logic simplification
fd22cd3
Merge branch 'main' into mbrtowc-implementation
sribee8 e9d7cdc
removed unnecessary check
5bcab10
build files fixes
4e31a45
Merge branch 'main' into mbrtowc-implementation
sribee8 db18034
moved wchar to the bottom of entrypoints
c8eae21
removed iscomplete from header
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" | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #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
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,49 @@ | ||
| //===-- Implementation for mbrtowc 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/__support/wchar/mbrtowc.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/error_or.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/wchar/character_converter.h" | ||
| #include "src/__support/wchar/mbstate.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace internal { | ||
|
|
||
| ErrorOr<size_t> mbrtowc(wchar_t *__restrict pwc, const char *__restrict s, | ||
| size_t n, mbstate *__restrict ps) { | ||
| CharacterConverter char_conv(ps); | ||
| if (s == nullptr) | ||
| return 0; | ||
| size_t i = 0; | ||
| // Reading in bytes until we have a complete wc or error | ||
| for (; i < n && !char_conv.isFull(); ++i) { | ||
| int err = char_conv.push(static_cast<char8_t>(s[i])); | ||
| // Encoding error | ||
| if (err == -1) | ||
| return Error(-1); | ||
| } | ||
| auto wc = char_conv.pop_utf32(); | ||
| if (wc.has_value()) { | ||
| *pwc = wc.value(); | ||
| // null terminator -> return 0 | ||
| if (wc.value() == L'\0') | ||
| return 0; | ||
| return i; | ||
| } | ||
| // Incomplete but potentially valid | ||
| return -2; | ||
| } | ||
|
|
||
| } // 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,29 @@ | ||
| //===-- Implementation header for mbrtowc 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_MBRTOWC | ||
| #define LLVM_LIBC_SRC___SUPPORT_WCHAR_MBRTOWC | ||
|
|
||
| #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/wchar/mbstate.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace internal { | ||
|
|
||
| ErrorOr<size_t> mbrtowc(wchar_t *__restrict pwc, const char *__restrict s, | ||
| size_t n, mbstate *__restrict ps); | ||
|
|
||
| } // namespace internal | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC___SUPPORT_WCHAR_MBRTOWC |
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,38 @@ | ||
| //===-- Implementation of mbrtowc -----------------------------------------===// | ||
| // | ||
| // 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/mbrtowc.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/mbrtowc.h" | ||
| #include "src/__support/wchar/mbstate.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(size_t, mbrtowc, | ||
| (wchar_t *__restrict pwc, const char *__restrict s, size_t n, | ||
| mbstate_t *__restrict ps)) { | ||
| static internal::mbstate internal_mbstate; | ||
| auto ret = internal::mbrtowc(pwc, s, n, | ||
| ps == nullptr | ||
| ? &internal_mbstate | ||
| : reinterpret_cast<internal::mbstate *>(ps)); | ||
| if (!ret.has_value()) { | ||
| // Encoding failure | ||
| libc_errno = EILSEQ; | ||
| 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 mbrtowc ---------------------------------===// | ||
| // | ||
| // 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_MBRTOWC_H | ||
| #define LLVM_LIBC_SRC_WCHAR_MBRTOWC_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 mbrtowc(wchar_t *__restrict pwc, const char *__restrict s, size_t n, | ||
| mbstate_t *__restrict ps); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_WCHAR_MBRTOWC_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.