Skip to content

Commit c9d06fc

Browse files
author
Sriya Pratipati
committed
finished mbstowcs implementation
1 parent 5757dce commit c9d06fc

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

libc/src/wchar/mbstowcs.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,20 @@ LLVM_LIBC_FUNCTION(int, mbstowcs,
2323
size_t n)) {
2424
static internal::mbstate internal_mbstate;
2525
internal::StringConverter<char8_t> str_conv(
26-
reinterpret_cast<const char8_t *>(pwcs), &internal_mbstate, n);
26+
reinterpret_cast<const char8_t *>(s), &internal_mbstate, n);
27+
int dst_idx = 0;
28+
ErrorOr<char32_t> converted = str_conv.popUTF32();
29+
while (converted.has_value()) {
30+
if (pwcs != nullptr)
31+
pwcs[dst_idx] = converted.value();
32+
dst_idx++;
33+
converted = str_conv.popUTF32();
34+
}
35+
if (converted.error() == -1) // if we hit conversion limit
36+
return dst_idx;
37+
38+
libc_errno = converted.error();
39+
return -1;
2740
}
2841

2942
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)