Skip to content
Merged
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
1 change: 1 addition & 0 deletions libc/src/__support/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_object_library(
SRCS
character_converter.cpp
DEPENDS
libc.hdr.errno_macros
libc.hdr.types.char8_t
libc.hdr.types.char32_t
libc.src.__support.error_or
Expand Down
7 changes: 4 additions & 3 deletions libc/src/__support/wchar/character_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "hdr/errno_macros.h"
#include "hdr/types/char32_t.h"
#include "hdr/types/char8_t.h"
#include "src/__support/CPP/bit.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ int CharacterConverter::push(char8_t utf8_byte) {
else {
// bytes_stored and total_bytes will always be 0 here
state->partial = static_cast<char32_t>(0);
return -1;
return EILSEQ;
}
state->partial = static_cast<char32_t>(utf8_byte);
state->bytes_stored++;
Expand All @@ -93,7 +94,7 @@ int CharacterConverter::push(char8_t utf8_byte) {
}
// Invalid byte -> reset the state
clear();
return -1;
return EILSEQ;
}

int CharacterConverter::push(char32_t utf32) {
Expand All @@ -115,7 +116,7 @@ int CharacterConverter::push(char32_t utf32) {
// `utf32` contains a value that is too large to actually represent a valid
// unicode character
clear();
return -1;
return EILSEQ;
}

ErrorOr<char32_t> CharacterConverter::pop_utf32() {
Expand Down
4 changes: 2 additions & 2 deletions libc/src/__support/wchar/mbrtowc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ ErrorOr<size_t> mbrtowc(wchar_t *__restrict pwc, const char *__restrict s,
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(EILSEQ);
if (err == EILSEQ)
return Error(err);
}
auto wc = char_conv.pop_utf32();
if (wc.has_value()) {
Expand Down
5 changes: 1 addition & 4 deletions libc/src/__support/wchar/wcrtomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc,
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);
return Error(status);

size_t count = 0;
while (!cr.isEmpty()) {
Expand Down
11 changes: 6 additions & 5 deletions libc/test/src/__support/wchar/utf8_to_32_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "hdr/errno_macros.h"
#include "src/__support/error_or.h"
#include "src/__support/wchar/character_converter.h"
#include "src/__support/wchar/mbstate.h"
Expand Down Expand Up @@ -87,7 +88,7 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidByte) {
LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state);
int err = char_conv.push(static_cast<char8_t>(ch));

ASSERT_EQ(err, -1);
ASSERT_EQ(err, EILSEQ);
}

TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidMultiByte) {
Expand All @@ -100,12 +101,12 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidMultiByte) {

LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state);
int err = char_conv.push(static_cast<char8_t>(ch[0]));
ASSERT_EQ(err, -1);
ASSERT_EQ(err, EILSEQ);
err = char_conv.push(static_cast<char8_t>(ch[1]));
ASSERT_EQ(err, 0);
// Prev byte was single byte so trying to push another should error.
err = char_conv.push(static_cast<char8_t>(ch[2]));
ASSERT_EQ(err, -1);
ASSERT_EQ(err, EILSEQ);
err = char_conv.push(static_cast<char8_t>(ch[3]));
ASSERT_EQ(err, 0);
}
Expand All @@ -127,7 +128,7 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidLastByte) {
err = char_conv.push(static_cast<char8_t>(ch[2]));
ASSERT_EQ(err, 0);
err = char_conv.push(static_cast<char8_t>(ch[3]));
ASSERT_EQ(err, -1);
ASSERT_EQ(err, EILSEQ);
}

TEST(LlvmLibcCharacterConverterUTF8To32Test, ValidTwoByteWithExtraRead) {
Expand All @@ -144,7 +145,7 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, ValidTwoByteWithExtraRead) {
ASSERT_EQ(err, 0);
// Should produce an error on 3rd byte
err = char_conv.push(static_cast<char8_t>(ch[2]));
ASSERT_EQ(err, -1);
ASSERT_EQ(err, EILSEQ);

// Should produce an error since mbstate was reset
auto wch = char_conv.pop_utf32();
Expand Down
Loading