Skip to content

Commit ac49c6d

Browse files
committed
Avoid duplicate pass in utf8 decode
1 parent a441f63 commit ac49c6d

1 file changed

Lines changed: 13 additions & 29 deletions

File tree

src/cpp/encoding/Encodings.cpp

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -329,34 +329,18 @@ String cpp::encoding::Utf8::decode(const cpp::marshal::View<uint8_t>& buffer)
329329
return String::emptyString;
330330
}
331331

332-
#if defined(HX_SMART_STRINGS)
333-
auto chars = int64_t{ 0 };
334-
auto i = int64_t{ 0 };
335-
bool isAscii = true;
336-
337-
while (i < buffer.length)
338-
{
339-
auto p = codepoint(buffer.slice(i));
340-
341-
if (p > 127)
342-
{
343-
isAscii = false;
344-
}
345-
346-
i += getByteCount(p);
347-
chars += Utf16::getCharCount(p);
348-
}
349-
350-
if (isAscii)
332+
if (isAsciiUtf8Buffer(buffer))
351333
{
352334
return Ascii::decode(buffer);
353335
}
336+
#if defined(HX_SMART_STRINGS)
337+
auto estimated_length = buffer.length;
354338

355-
auto backing = View<char16_t>(::String::allocChar16Ptr(chars), chars);
356-
auto output = backing.reinterpret<uint8_t>();
357-
auto k = int64_t{ 0 };
339+
auto temp = ::String::allocChar16Ptr(estimated_length);
340+
auto output = View<char16_t>(temp, estimated_length).reinterpret<uint8_t>();
341+
int64_t k = 0;
342+
int64_t i = 0;
358343

359-
i = 0;
360344
while (i < buffer.length)
361345
{
362346
auto p = codepoint(buffer.slice(i));
@@ -365,13 +349,13 @@ String cpp::encoding::Utf8::decode(const cpp::marshal::View<uint8_t>& buffer)
365349
k += Utf16::encode(p, output.slice(k));
366350
}
367351

368-
return String(backing.ptr.ptr, chars);
369-
#else
370-
if (isAsciiUtf8Buffer(buffer))
371-
{
372-
return Ascii::decode(buffer);
373-
}
352+
auto chars = static_cast<int>(k / sizeof(char16_t));
353+
auto exact = ::String::allocChar16Ptr(chars);
354+
355+
std::memcpy(exact, temp, k);
374356

357+
return String(exact, chars);
358+
#else
375359
auto backing = View<char>(hx::InternalNew(buffer.length, false), buffer.length);
376360

377361
std::memcpy(backing.ptr.ptr, buffer.ptr.ptr, buffer.length);

0 commit comments

Comments
 (0)