From 73c5d5c00aa6e7c8a2702492b5074c2211edc94e Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 24 Feb 2017 12:52:12 -0800 Subject: [core] Avoid some string copies in bidi implementation As of C++11, it's safe to preallocate std::[u16]string and write to the buffer via &s[0] (C++17 makes it possible via .data() as well). --- platform/default/bidi.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'platform/default/bidi.cpp') diff --git a/platform/default/bidi.cpp b/platform/default/bidi.cpp index eb4e658733..64baddaa5b 100644 --- a/platform/default/bidi.cpp +++ b/platform/default/bidi.cpp @@ -36,8 +36,9 @@ std::u16string applyArabicShaping(const std::u16string& input) { // Pre-flighting will always set U_BUFFER_OVERFLOW_ERROR errorCode = U_ZERO_ERROR; - auto outputText = std::make_unique(outputLength); - u_shapeArabic(input.c_str(), static_cast(input.size()), outputText.get(), outputLength, + std::u16string outputText(outputLength, 0); + + u_shapeArabic(input.c_str(), static_cast(input.size()), &outputText[0], outputLength, (U_SHAPE_LETTERS_SHAPE & U_SHAPE_LETTERS_MASK) | (U_SHAPE_TEXT_DIRECTION_LOGICAL & U_SHAPE_TEXT_DIRECTION_MASK), &errorCode); @@ -46,7 +47,7 @@ std::u16string applyArabicShaping(const std::u16string& input) { if (U_FAILURE(errorCode)) return input; - return std::u16string(outputText.get(), outputLength); + return outputText; } void BiDi::mergeParagraphLineBreaks(std::set& lineBreakPoints) { @@ -108,12 +109,12 @@ std::u16string BiDi::getLine(std::size_t start, std::size_t end) { // Setting UBIDI_INSERT_LRM_FOR_NUMERIC would require // ubidi_getLength(pBiDi)+2*ubidi_countRuns(pBiDi) const int32_t outputLength = ubidi_getProcessedLength(impl->bidiLine); - auto outputText = std::make_unique(outputLength); + std::u16string outputText(outputLength, 0); // UBIDI_DO_MIRRORING: Apply unicode mirroring of characters like parentheses // UBIDI_REMOVE_BIDI_CONTROLS: Now that all the lines are set, remove control characters so that // they don't show up on screen (some fonts have glyphs representing them) - ubidi_writeReordered(impl->bidiLine, outputText.get(), outputLength, + ubidi_writeReordered(impl->bidiLine, &outputText[0], outputLength, UBIDI_DO_MIRRORING | UBIDI_REMOVE_BIDI_CONTROLS, &errorCode); if (U_FAILURE(errorCode)) { @@ -121,7 +122,7 @@ std::u16string BiDi::getLine(std::size_t start, std::size_t end) { u_errorName(errorCode)); } - return std::u16string(outputText.get(), outputLength); + return outputText; } } // end namespace mbgl -- cgit v1.2.1