Skip to content

Commit 8637db4

Browse files
committed
Refactored internal wcrtomb
1 parent 5c310d1 commit 8637db4

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

libc/src/__support/wchar/wcrtomb.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,26 @@
2121
namespace LIBC_NAMESPACE_DECL {
2222
namespace internal {
2323

24-
ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc,
25-
mbstate *__restrict ps) {
24+
ErrorOr<wcrtomb_result> wcrtomb(wchar_t wc, mbstate *ps) {
2625
static_assert(sizeof(wchar_t) == 4);
2726

27+
wcrtomb_result out;
2828
CharacterConverter cr(ps);
2929

3030
if (!cr.isValidState())
3131
return Error(EINVAL);
3232

33-
if (s == nullptr)
34-
return Error(EILSEQ);
35-
3633
int status = cr.push(static_cast<char32_t>(wc));
3734
if (status != 0)
3835
return Error(EILSEQ);
3936

40-
size_t count = 0;
4137
while (!cr.isEmpty()) {
4238
auto utf8 = cr.pop_utf8(); // can never fail as long as the push succeeded
4339
LIBC_ASSERT(utf8.has_value());
44-
45-
*s = utf8.value();
46-
s++;
47-
count++;
40+
out.mbs[out.count++] = utf8.value();
4841
}
49-
return count;
42+
43+
return out;
5044
}
5145

5246
} // namespace internal

libc/src/__support/wchar/wcrtomb.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
namespace LIBC_NAMESPACE_DECL {
1919
namespace internal {
2020

21-
ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc, mbstate *__restrict ps);
21+
struct wcrtomb_result {
22+
char mbs[4];
23+
size_t count = 0;
24+
};
25+
26+
ErrorOr<wcrtomb_result> wcrtomb(wchar_t wc, mbstate* ps);
2227

2328
} // namespace internal
2429
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcrtomb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ LLVM_LIBC_FUNCTION(size_t, wcrtomb,
3030
}
3131

3232
auto result = internal::wcrtomb(
33-
s, wc,
34-
ps == nullptr ? &internal_mbstate
35-
: reinterpret_cast<internal::mbstate *>(ps));
33+
wc, ps == nullptr ? &internal_mbstate
34+
: reinterpret_cast<internal::mbstate *>(ps));
3635

3736
if (!result.has_value()) {
3837
libc_errno = result.error();
3938
return -1;
4039
}
4140

42-
return result.value();
41+
__builtin_memcpy(s, result.value().mbs, result.value().count);
42+
return result.value().count;
4343
}
4444

4545
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wctomb.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ LLVM_LIBC_FUNCTION(int, wctomb, (char *s, wchar_t wc)) {
2222
if (s == nullptr)
2323
return 0;
2424

25-
auto result = internal::wcrtomb(s, wc, &internal_mbstate);
25+
auto result = internal::wcrtomb(wc, &internal_mbstate);
2626

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

32-
return static_cast<int>(result.value());
32+
__builtin_memcpy(s, result.value().mbs, result.value().count);
33+
return static_cast<int>(result.value().count);
3334
}
3435

3536
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)