Skip to content

[libc] Refactored internal wcrtomb #145945

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions libc/src/__support/wchar/wcrtomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,26 @@
namespace LIBC_NAMESPACE_DECL {
namespace internal {

ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc,
mbstate *__restrict ps) {
ErrorOr<wcrtomb_result> wcrtomb(wchar_t wc, mbstate *ps) {
static_assert(sizeof(wchar_t) == 4);

wcrtomb_result out;
CharacterConverter cr(ps);

if (!cr.isValidState())
return Error(EINVAL);

if (s == nullptr)
return Error(EILSEQ);

int status = cr.push(static_cast<char32_t>(wc));
if (status != 0)
return Error(EILSEQ);

size_t count = 0;
while (!cr.isEmpty()) {
auto utf8 = cr.pop_utf8(); // can never fail as long as the push succeeded
LIBC_ASSERT(utf8.has_value());

*s = utf8.value();
s++;
count++;
out.mbs[out.count++] = utf8.value();
}
return count;

return out;
}

} // namespace internal
Expand Down
7 changes: 6 additions & 1 deletion libc/src/__support/wchar/wcrtomb.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
namespace LIBC_NAMESPACE_DECL {
namespace internal {

ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc, mbstate *__restrict ps);
struct wcrtomb_result {
char mbs[4];
size_t count = 0;
};

ErrorOr<wcrtomb_result> wcrtomb(wchar_t wc, mbstate* ps);

} // namespace internal
} // namespace LIBC_NAMESPACE_DECL
Expand Down
8 changes: 4 additions & 4 deletions libc/src/wchar/wcrtomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ LLVM_LIBC_FUNCTION(size_t, wcrtomb,
}

auto result = internal::wcrtomb(
s, wc,
ps == nullptr ? &internal_mbstate
: reinterpret_cast<internal::mbstate *>(ps));
wc, ps == nullptr ? &internal_mbstate
: reinterpret_cast<internal::mbstate *>(ps));

if (!result.has_value()) {
libc_errno = result.error();
return -1;
}

return result.value();
__builtin_memcpy(s, result.value().mbs, result.value().count);
return result.value().count;
}

} // namespace LIBC_NAMESPACE_DECL
5 changes: 3 additions & 2 deletions libc/src/wchar/wctomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ LLVM_LIBC_FUNCTION(int, wctomb, (char *s, wchar_t wc)) {
if (s == nullptr)
return 0;

auto result = internal::wcrtomb(s, wc, &internal_mbstate);
auto result = internal::wcrtomb(wc, &internal_mbstate);

if (!result.has_value()) { // invalid wide character
libc_errno = EILSEQ;
return -1;
}

return static_cast<int>(result.value());
__builtin_memcpy(s, result.value().mbs, result.value().count);
return static_cast<int>(result.value().count);
}

} // namespace LIBC_NAMESPACE_DECL
Loading