From 045b20a36b8a1fe86716a99cb25f03021e583592 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 6 Nov 2019 11:40:36 +0300 Subject: [PATCH] Reserve space for strings. (#3546) --- runtime/src/main/cpp/Console.cpp | 1 + runtime/src/main/cpp/KString.cpp | 2 ++ runtime/src/main/cpp/dtoa/dblparse.cpp | 1 + runtime/src/main/cpp/dtoa/fltparse.cpp | 1 + 4 files changed, 5 insertions(+) diff --git a/runtime/src/main/cpp/Console.cpp b/runtime/src/main/cpp/Console.cpp index 07b4eb4165b..e4b33ca42c8 100644 --- a/runtime/src/main/cpp/Console.cpp +++ b/runtime/src/main/cpp/Console.cpp @@ -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()); diff --git a/runtime/src/main/cpp/KString.cpp b/runtime/src/main/cpp/KString.cpp index 924b9ce3c7e..3bdaa05bdf3 100644 --- a/runtime/src/main/cpp/KString.cpp +++ b/runtime/src/main/cpp/KString.cpp @@ -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(konan::calloc(1, utf8.size() + 1)); ::memcpy(result, utf8.c_str(), utf8.size()); diff --git a/runtime/src/main/cpp/dtoa/dblparse.cpp b/runtime/src/main/cpp/dtoa/dblparse.cpp index 5a836c1b7f3..3a8fe51175c 100644 --- a/runtime/src/main/cpp/dtoa/dblparse.cpp +++ b/runtime/src/main/cpp/dtoa/dblparse.cpp @@ -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()); diff --git a/runtime/src/main/cpp/dtoa/fltparse.cpp b/runtime/src/main/cpp/dtoa/fltparse.cpp index d72f9c07584..e27da3758ce 100644 --- a/runtime/src/main/cpp/dtoa/fltparse.cpp +++ b/runtime/src/main/cpp/dtoa/fltparse.cpp @@ -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());