diff options
-rw-r--r-- | include/core/SkString.h | 2 | ||||
-rw-r--r-- | src/core/SkString.cpp | 39 | ||||
-rw-r--r-- | src/core/SkStringUtils.cpp | 27 | ||||
-rw-r--r-- | src/core/SkStringUtils.h | 2 | ||||
-rw-r--r-- | src/ports/SkOSFile_win.cpp | 6 | ||||
-rw-r--r-- | src/sfnt/SkOTTable_name.cpp | 2 | ||||
-rw-r--r-- | src/utils/SkLuaCanvas.cpp | 10 | ||||
-rw-r--r-- | tests/StringTest.cpp | 18 |
8 files changed, 47 insertions, 59 deletions
diff --git a/include/core/SkString.h b/include/core/SkString.h index 3d2373758d..c2fd9c0e36 100644 --- a/include/core/SkString.h +++ b/include/core/SkString.h @@ -186,8 +186,6 @@ public: void set(const SkString& src) { *this = src; } void set(const char text[]); void set(const char text[], size_t len); - void setUTF16(const uint16_t[]); - void setUTF16(const uint16_t[], size_t len); void insert(size_t offset, const SkString& src) { this->insert(offset, src.c_str(), src.size()); } void insert(size_t offset, const char text[]); diff --git a/src/core/SkString.cpp b/src/core/SkString.cpp index d388f18ec7..554cde8f9f 100644 --- a/src/core/SkString.cpp +++ b/src/core/SkString.cpp @@ -369,45 +369,6 @@ void SkString::set(const char text[], size_t len) { } } -void SkString::setUTF16(const uint16_t src[]) { - int count = 0; - - while (src[count]) { - count += 1; - } - this->setUTF16(src, count); -} - -static SkString utf8_from_utf16(const uint16_t src[], size_t count) { - SkString ret; - if (count > 0) { - SkASSERT(src); - size_t n = 0; - const uint16_t* end = src + count; - for (const uint16_t* ptr = src; ptr < end;) { - const uint16_t* last = ptr; - SkUnichar u = SkUTF16_NextUnichar(&ptr); - size_t s = SkUTF8_FromUnichar(u); - if (n > SK_MaxU32 - s) { - end = last; // truncate input string - break; - } - n += s; - } - ret = SkString(n); - char* out = ret.writable_str(); - for (const uint16_t* ptr = src; ptr < end;) { - out += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&ptr), out); - } - SkASSERT(out == ret.writable_str() + n); - } - return ret; -} - -void SkString::setUTF16(const uint16_t src[], size_t count) { - *this = utf8_from_utf16(src, count); -} - void SkString::insert(size_t offset, const char text[]) { this->insert(offset, text, text ? strlen(text) : 0); } diff --git a/src/core/SkStringUtils.cpp b/src/core/SkStringUtils.cpp index 6f5699c805..9745b2325b 100644 --- a/src/core/SkStringUtils.cpp +++ b/src/core/SkStringUtils.cpp @@ -7,6 +7,7 @@ #include "SkString.h" #include "SkStringUtils.h" +#include "SkUtils.h" void SkAddFlagToString(SkString* string, bool flag, const char* flagStr, bool* needSeparator) { if (flag) { @@ -61,3 +62,29 @@ SkString SkTabString(const SkString& string, int tabCnt) { } return result; } + +SkString SkStringFromUTF16(const uint16_t* src, size_t count) { + SkString ret; + if (count > 0) { + SkASSERT(src); + size_t n = 0; + const uint16_t* end = src + count; + for (const uint16_t* ptr = src; ptr < end;) { + const uint16_t* last = ptr; + SkUnichar u = SkUTF16_NextUnichar(&ptr); + size_t s = SkUTF8_FromUnichar(u); + if (n > SK_MaxU32 - s) { + end = last; // truncate input string + break; + } + n += s; + } + ret = SkString(n); + char* out = ret.writable_str(); + for (const uint16_t* ptr = src; ptr < end;) { + out += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&ptr), out); + } + SkASSERT(out == ret.writable_str() + n); + } + return ret; +} diff --git a/src/core/SkStringUtils.h b/src/core/SkStringUtils.h index fecf833d73..f2a1443ff4 100644 --- a/src/core/SkStringUtils.h +++ b/src/core/SkStringUtils.h @@ -40,4 +40,6 @@ static inline void SkAppendScalarHex(SkString* str, SkScalar value) { /** Indents every non-empty line of the string by tabCnt tabs */ SkString SkTabString(const SkString& string, int tabCnt); +SkString SkStringFromUTF16(const uint16_t* src, size_t count); + #endif diff --git a/src/ports/SkOSFile_win.cpp b/src/ports/SkOSFile_win.cpp index 05afcc6350..5488712d39 100644 --- a/src/ports/SkOSFile_win.cpp +++ b/src/ports/SkOSFile_win.cpp @@ -11,6 +11,7 @@ #include "SkLeanWindows.h" #include "SkMalloc.h" #include "SkOSFile.h" +#include "SkStringUtils.h" #include "SkTFitsIn.h" #include <io.h> @@ -250,7 +251,10 @@ static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPt } // if we get here, we've found a file/dir if (name) { - name->setUTF16((uint16_t*)dataPtr->cFileName); + const uint16_t* utf16name = (const uint16_t*)dataPtr->cFileName; + const uint16_t* ptr = utf16name; + while (*ptr != 0) { ++ptr; } + *name = SkStringFromUTF16(utf16name, ptr - utf16name); } return true; } diff --git a/src/sfnt/SkOTTable_name.cpp b/src/sfnt/SkOTTable_name.cpp index 9670b6f91e..20d97b6cd5 100644 --- a/src/sfnt/SkOTTable_name.cpp +++ b/src/sfnt/SkOTTable_name.cpp @@ -8,7 +8,7 @@ #include "SkOTTable_name.h" #include "SkEndian.h" -#include "SkString.h" +#include "SkStringUtils.h" #include "SkTSearch.h" #include "SkTemplates.h" #include "SkUtils.h" diff --git a/src/utils/SkLuaCanvas.cpp b/src/utils/SkLuaCanvas.cpp index 9d2e8c9041..53e462af0d 100644 --- a/src/utils/SkLuaCanvas.cpp +++ b/src/utils/SkLuaCanvas.cpp @@ -6,7 +6,9 @@ */ #include "SkLuaCanvas.h" + #include "SkLua.h" +#include "SkStringUtils.h" extern "C" { #include "lua.h" @@ -51,11 +53,9 @@ void AutoCallLua::pushEncodedText(SkPaint::TextEncoding enc, const void* text, case SkPaint::kUTF8_TextEncoding: this->pushString((const char*)text, length, "text"); break; - case SkPaint::kUTF16_TextEncoding: { - SkString str; - str.setUTF16((const uint16_t*)text, length); - this->pushString(str, "text"); - } break; + case SkPaint::kUTF16_TextEncoding: + this->pushString(SkStringFromUTF16((const uint16_t*)text, length), "text"); + break; case SkPaint::kGlyphID_TextEncoding: this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1), "glyphs"); diff --git a/tests/StringTest.cpp b/tests/StringTest.cpp index 5ff24125b0..7c0c8663f9 100644 --- a/tests/StringTest.cpp +++ b/tests/StringTest.cpp @@ -5,12 +5,14 @@ * found in the LICENSE file. */ +#include "Test.h" + #include <stdarg.h> #include <stdio.h> -#include "SkString.h" -#include "Test.h" #include <thread> +#include "SkString.h" +#include "SkStringUtils.h" DEF_TEST(String, reporter) { SkString a; @@ -304,28 +306,22 @@ DEF_TEST(String_huge, r) { } } -static SkString utf16_to_utf8(const uint16_t* utf16, size_t len) { - SkString s; - s.setUTF16(utf16, len); - return s; -} - DEF_TEST(String_fromUTF16, r) { // test data produced with `iconv`. const uint16_t test1[] = { 0xD835, 0xDCD0, 0xD835, 0xDCD1, 0xD835, 0xDCD2, 0xD835, 0xDCD3, 0xD835, 0xDCD4, 0x0020, 0xD835, 0xDCD5, 0xD835, 0xDCD6, 0xD835, 0xDCD7, 0xD835, 0xDCD8, 0xD835, 0xDCD9 }; - REPORTER_ASSERT(r, utf16_to_utf8(test1, SK_ARRAY_COUNT(test1)).equals("πππππ πππππ")); + REPORTER_ASSERT(r, SkStringFromUTF16(test1, SK_ARRAY_COUNT(test1)).equals("πππππ πππππ")); const uint16_t test2[] = { 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0020, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, }; - REPORTER_ASSERT(r, utf16_to_utf8(test2, SK_ARRAY_COUNT(test2)).equals("ABCDE FGHIJ")); + REPORTER_ASSERT(r, SkStringFromUTF16(test2, SK_ARRAY_COUNT(test2)).equals("ABCDE FGHIJ")); const uint16_t test3[] = { 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x0020, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, }; - REPORTER_ASSERT(r, utf16_to_utf8(test3, SK_ARRAY_COUNT(test3)).equals("αβγδΡ ΞΆΞ·ΞΈΞΉΞΊ")); + REPORTER_ASSERT(r, SkStringFromUTF16(test3, SK_ARRAY_COUNT(test3)).equals("αβγδΡ ΞΆΞ·ΞΈΞΉΞΊ")); } |