Skip to content

Commit ecc950f

Browse files
committed
Estimate utf8 length instead of iterating
1 parent c696b23 commit ecc950f

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

src/cpp/encoding/Encodings.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ Array<uint8_t> cpp::encoding::Utf8::encode(const String& string)
249249
}
250250

251251
#if defined(HX_SMART_STRINGS)
252-
Array<uint8_t> out(getByteCount(string), 0);
252+
// estimate the utf8 length with an upper bound
253+
Array<uint8_t> out(string.length * 3, 0);
253254
View<uint8_t> buffer(out->Pointer(), out->length);
254255

255256
auto initialPtr = buffer.ptr.ptr;
@@ -265,6 +266,8 @@ Array<uint8_t> cpp::encoding::Utf8::encode(const String& string)
265266
k += encode(p, buffer.slice(k));
266267
}
267268

269+
out->resize(i);
270+
268271
return out;
269272
#else
270273
return hx::Throw(HX_CSTRING("Unexpected encoding error"));

0 commit comments

Comments
 (0)