Reserve space for strings. (#3546)
This commit is contained in:
@@ -33,6 +33,7 @@ void Kotlin_io_Console_print(KString message) {
|
||||
// TODO: system stdout must be aware about UTF-8.
|
||||
const KChar* utf16 = CharArrayAddressOfElementAt(message, 0);
|
||||
KStdString utf8;
|
||||
utf8.reserve(message->count_);
|
||||
// Replace incorrect sequences with a default codepoint (see utf8::with_replacement::default_replacement)
|
||||
utf8::with_replacement::utf16to8(utf16, utf16 + message->count_, back_inserter(utf8));
|
||||
konan::consoleWriteUtf8(utf8.c_str(), utf8.size());
|
||||
|
||||
@@ -53,6 +53,7 @@ OBJ_GETTER(unsafeUtf16ToUtf8Impl, KString thiz, KInt start, KInt size) {
|
||||
RuntimeAssert(thiz->type_info() == theStringTypeInfo, "Must use String");
|
||||
const KChar* utf16 = CharArrayAddressOfElementAt(thiz, start);
|
||||
KStdString utf8;
|
||||
utf8.reserve(size);
|
||||
conversion(utf16, utf16 + size, back_inserter(utf8));
|
||||
ArrayHeader* result = AllocArrayInstance(theByteArrayTypeInfo, utf8.size(), OBJ_RESULT)->array();
|
||||
::memcpy(ByteArrayAddressOfElementAt(result, 0), utf8.c_str(), utf8.size());
|
||||
@@ -714,6 +715,7 @@ char* CreateCStringFromString(KConstRef kref) {
|
||||
KString kstring = kref->array();
|
||||
const KChar* utf16 = CharArrayAddressOfElementAt(kstring, 0);
|
||||
KStdString utf8;
|
||||
utf8.reserve(kstring->count_);
|
||||
utf8::unchecked::utf16to8(utf16, utf16 + kstring->count_, back_inserter(utf8));
|
||||
char* result = reinterpret_cast<char*>(konan::calloc(1, utf8.size() + 1));
|
||||
::memcpy(result, utf8.c_str(), utf8.size());
|
||||
|
||||
@@ -642,6 +642,7 @@ KDouble Kotlin_native_FloatingPointParser_parseDoubleImpl (KString s, KInt e)
|
||||
{
|
||||
const KChar* utf16 = CharArrayAddressOfElementAt(s, 0);
|
||||
KStdString utf8;
|
||||
utf8.reserve(s->count_);
|
||||
TRY_CATCH(utf8::utf16to8(utf16, utf16 + s->count_, back_inserter(utf8)),
|
||||
utf8::unchecked::utf16to8(utf16, utf16 + s->count_, back_inserter(utf8)),
|
||||
/* Illegal UTF-16 string. */ ThrowNumberFormatException());
|
||||
|
||||
@@ -542,6 +542,7 @@ Kotlin_native_FloatingPointParser_parseFloatImpl(KString s, KInt e)
|
||||
{
|
||||
const KChar* utf16 = CharArrayAddressOfElementAt(s, 0);
|
||||
KStdString utf8;
|
||||
utf8.reserve(s->count_);
|
||||
TRY_CATCH(utf8::utf16to8(utf16, utf16 + s->count_, back_inserter(utf8)),
|
||||
utf8::unchecked::utf16to8(utf16, utf16 + s->count_, back_inserter(utf8)),
|
||||
/* Illegal UTF-16 string. */ ThrowNumberFormatException());
|
||||
|
||||
Reference in New Issue
Block a user