diff options
author | Hal Canary <halcanary@google.com> | 2018-03-01 12:32:18 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-03-01 19:03:19 +0000 |
commit | 2d0e1248d6eef36ecc510785e9bf8a28530c56fe (patch) | |
tree | ae146e3478a79c02508a466a6a77e8d03440d943 /src/core/SkString.cpp | |
parent | 44b61204d9f5681b9474db017577d56f42a32d66 (diff) |
SkString: fix ::setUTF16
Change-Id: I7ab4af9ae55a43cc05f81b0c236f29a64beac982
Reviewed-on: https://skia-review.googlesource.com/111223
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
Diffstat (limited to 'src/core/SkString.cpp')
-rw-r--r-- | src/core/SkString.cpp | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/src/core/SkString.cpp b/src/core/SkString.cpp index 1242b3333d..d388f18ec7 100644 --- a/src/core/SkString.cpp +++ b/src/core/SkString.cpp @@ -378,30 +378,34 @@ void SkString::setUTF16(const uint16_t src[]) { this->setUTF16(src, count); } -void SkString::setUTF16(const uint16_t src[], size_t count) { - count = trim_size_t_to_u32(count); - - if (0 == count) { - this->reset(); - } else if (count <= fRec->fLength) { - // should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1)) - if (count < fRec->fLength) { - this->resize(count); - } - char* p = this->writable_str(); - for (size_t i = 0; i < count; i++) { - p[i] = SkToU8(src[i]); +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; } - p[count] = 0; - } else { - SkString tmp(count); // puts a null terminator at the end of the string - char* p = tmp.writable_str(); - - for (size_t i = 0; i < count; i++) { - p[i] = SkToU8(src[i]); + ret = SkString(n); + char* out = ret.writable_str(); + for (const uint16_t* ptr = src; ptr < end;) { + out += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&ptr), out); } - this->swap(tmp); + 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[]) { |