Compare commits

..

No commits in common. "e3ef29e80dd3d2d50e805895347d32e3f2d3926a" and "da805eec835bed19d21eb8f3bf3b5533eb7640a4" have entirely different histories.

4 changed files with 16 additions and 27 deletions

View File

@ -110,9 +110,6 @@ extern "C"
/* Convert a multibyte character string to a wide character string. */ /* Convert a multibyte character string to a wide character string. */
size_t mbstowcs(wchar_t* buf, const char* src, size_t max); size_t mbstowcs(wchar_t* buf, const char* src, size_t max);
/* Convert a wide character string to a multibyte character string. */
size_t wcstombs(char* buf, const wchar_t* src, size_t max);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -104,26 +104,22 @@ extern "C"
__builtin_unreachable(); __builtin_unreachable();
} }
// FIXME: This is walking a UTF-8 string twice. Once to decode, and another to count code points.
size_t mbstowcs(wchar_t* buf, const char* src, size_t max) size_t mbstowcs(wchar_t* buf, const char* src, size_t max)
{ {
if (max == 0) return 0; if (max == 0) return 0;
Utf8StringDecoder decoder(src); Utf8StringDecoder decoder(src);
if (!buf) { return decoder.code_points().value_or((size_t)-1); } auto rc = decoder.decode(buf, max);
return decoder.decode(buf, max).value_or((size_t)-1); if (rc.has_error()) return (size_t)-1;
}
size_t wcstombs(char* buf, const wchar_t* src, size_t max) size_t code_points = decoder.code_points().value_or(0);
{
if (max == 0) return 0;
Utf8StringEncoder encoder(src); if (code_points >= max) return max - 1;
if (!buf) { return encoder.byte_length().value_or((size_t)-1); } return code_points;
return encoder.encode(buf, max).value_or((size_t)-1);
} }
void* malloc(size_t size) void* malloc(size_t size)

View File

@ -16,9 +16,9 @@ class Utf8StringDecoder
Result<usize> code_points() const; Result<usize> code_points() const;
// The caller must ensure that 'buf' is at least code_points() + a NULL wide. // The caller must ensure that 'buf' is at least code_points() + a NULL wide.
Result<usize> decode(wchar_t* buf) const; Result<void> decode(wchar_t* buf) const;
Result<usize> decode(wchar_t* buf, usize max) const; Result<void> decode(wchar_t* buf, usize max) const;
private: private:
const char* m_str; const char* m_str;
@ -38,9 +38,9 @@ class Utf8StringEncoder
Result<usize> byte_length() const; Result<usize> byte_length() const;
// The caller must ensure that 'buf' is at least byte_length() + a NULL wide. // The caller must ensure that 'buf' is at least byte_length() + a NULL wide.
Result<usize> encode(char* buf) const; Result<void> encode(char* buf) const;
Result<usize> encode(char* buf, usize max) const; Result<void> encode(char* buf, usize max) const;
private: private:
const wchar_t* m_str; const wchar_t* m_str;

View File

@ -2,8 +2,6 @@
#include <luna/CString.h> #include <luna/CString.h>
#include <luna/Utf8.h> #include <luna/Utf8.h>
// FIXME: Not enough space for a sequence is not an error. (mbstowcs(3) and wcstombs(3), case 2 when buf is not NULL)
static_assert(WCHAR_MAX > 0x10ffff); static_assert(WCHAR_MAX > 0x10ffff);
static Result<usize> utf8_char_length(char c) static Result<usize> utf8_char_length(char c)
@ -148,10 +146,9 @@ Result<usize> Utf8StringDecoder::code_points() const
return len; return len;
} }
Result<usize> Utf8StringDecoder::decode(wchar_t* buf, usize max) const Result<void> Utf8StringDecoder::decode(wchar_t* buf, usize max) const
{ {
const char* it = m_str; const char* it = m_str;
wchar_t* const buf_start = buf;
while ((usize)(it - m_str) < m_byte_length && max--) while ((usize)(it - m_str) < m_byte_length && max--)
{ {
@ -163,10 +160,10 @@ Result<usize> Utf8StringDecoder::decode(wchar_t* buf, usize max) const
*buf = 0; *buf = 0;
return (usize)(buf - buf_start); return {};
} }
Result<usize> Utf8StringDecoder::decode(wchar_t* buf) const Result<void> Utf8StringDecoder::decode(wchar_t* buf) const
{ {
return decode(buf, (usize)-1); return decode(buf, (usize)-1);
} }
@ -189,10 +186,9 @@ Result<usize> Utf8StringEncoder::byte_length() const
return len; return len;
} }
Result<usize> Utf8StringEncoder::encode(char* buf, usize max) const Result<void> Utf8StringEncoder::encode(char* buf, usize max) const
{ {
const wchar_t* it = m_str; const wchar_t* it = m_str;
char* const buf_start = buf;
while (*it && max > 1) while (*it && max > 1)
{ {
@ -205,10 +201,10 @@ Result<usize> Utf8StringEncoder::encode(char* buf, usize max) const
*buf = 0; *buf = 0;
return (usize)(buf - buf_start); return {};
} }
Result<usize> Utf8StringEncoder::encode(char* buf) const Result<void> Utf8StringEncoder::encode(char* buf) const
{ {
return encode(buf, (usize)-1); return encode(buf, (usize)-1);
} }