static char* AtomicStringToUTF8String(const AtomicString& utf16) { SkASSERT(sizeof(uint16_t) == sizeof(utf16.characters()[0])); const uint16_t* uni = (uint16_t*)utf16.characters(); size_t bytes = SkUTF16_ToUTF8(uni, utf16.length(), NULL); char* utf8 = (char*)sk_malloc_throw(bytes + 1); (void)SkUTF16_ToUTF8(uni, utf16.length(), utf8); utf8[bytes] = 0; return utf8; }
static void write_string(WTF::Vector<char>& v, const WebCore::String& str) { unsigned strLen = str.length(); // Only do work if the string has data. if (strLen) { // Determine how much to grow the vector. Use the worst case for utf8 to // avoid reading the string twice. Add sizeof(unsigned) to hold the // string length in utf8. unsigned vectorLen = v.size() + sizeof(unsigned); unsigned length = (strLen << 2) + vectorLen; // Grow the vector. This will change the value of v.size() but we // remember the original size above. v.grow(length); // Grab the position to write to. char* data = v.begin() + vectorLen; // Write the actual string int l = SkUTF16_ToUTF8(str.characters(), strLen, data); LOGV("Writing string %d %.*s", l, l, data); // Go back and write the utf8 length. Subtract sizeof(unsigned) from // data to get the position to write the length. memcpy(data - sizeof(unsigned), (char*)&l, sizeof(unsigned)); // Shrink the internal state of the vector so we match what was // actually written. v.shrink(vectorLen + l); } else v.append((char*)&strLen, sizeof(unsigned)); }
SkString* SkObjectParser::TextToString(const void* text, size_t byteLength, SkPaint::TextEncoding encoding) { SkString* decodedText = new SkString(); switch (encoding) { case SkPaint::kUTF8_TextEncoding: { decodedText->append("UTF-8: "); decodedText->append((const char*)text, byteLength); break; } case SkPaint::kUTF16_TextEncoding: { decodedText->append("UTF-16: "); size_t sizeNeeded = SkUTF16_ToUTF8((uint16_t*)text, SkToS32(byteLength / 2), nullptr); SkAutoSTMalloc<0x100, char> utf8(sizeNeeded); SkUTF16_ToUTF8((uint16_t*)text, SkToS32(byteLength / 2), utf8); decodedText->append(utf8, sizeNeeded); break; } case SkPaint::kUTF32_TextEncoding: { decodedText->append("UTF-32: "); const SkUnichar* begin = (const SkUnichar*)text; const SkUnichar* end = (const SkUnichar*)((const char*)text + byteLength); for (const SkUnichar* unichar = begin; unichar < end; ++unichar) { decodedText->appendUnichar(*unichar); } break; } case SkPaint::kGlyphID_TextEncoding: { decodedText->append("GlyphID: "); const uint16_t* begin = (const uint16_t*)text; const uint16_t* end = (const uint16_t*)((const char*)text + byteLength); for (const uint16_t* glyph = begin; glyph < end; ++glyph) { decodedText->append("0x"); decodedText->appendHex(*glyph); decodedText->append(" "); } break; } default: decodedText->append("Unknown text encoding."); break; } return decodedText; }