void WordSuggest::specialPipeNotification(NotificationType notification, Layout* pLayout) { // Pipe notifications to notifier template including own data switch (notification) { case NotificationType::WORD_SUGGEST_CHOSEN: { // Notify listener method with UTF-16 string notifyListener(&WordSuggestListener::chosen, pLayout, getId(), mLastChosenSuggestion); // Convert suggestion value to UTF-8 string std::string lastChosenSuggestionValue8; convertUTF16ToUTF8(mLastChosenSuggestion, lastChosenSuggestionValue8); // Notify listener method with UTF-8 string notifyListener(&WordSuggestListener::chosen, pLayout, getId(), lastChosenSuggestionValue8); break; } default: throwWarning( OperationNotifier::Operation::BUG, "Word suggest got notification which is not thought for it."); break; } }
CString UString::UTF8String(bool strict) const { // Allocate a buffer big enough to hold all the characters. const int length = size(); Vector<char, 1024> buffer(length * 3); // Convert to runs of 8-bit characters. char* p = buffer.data(); const UChar* d = reinterpret_cast<const UChar*>(&data()[0]); ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), strict); if (result != conversionOK) return CString(); return CString(buffer.data(), p - buffer.data()); }
PassRefPtr<SharedBuffer> utf8Buffer(const String& string) { // Allocate a buffer big enough to hold all the characters. const int length = string.length(); Vector<char> buffer(length * 3); // Convert to runs of 8-bit characters. char* p = buffer.data(); const UChar* d = string.characters(); ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), true); if (result != conversionOK) return 0; buffer.shrink(p - buffer.data()); return SharedBuffer::adoptVector(buffer); }
std::string String16::utf8() const { unsigned length = this->length(); if (!length) return std::string(""); // Allocate a buffer big enough to hold all the characters // (an individual UTF-16 UChar can only expand to 3 UTF-8 bytes). // Optimization ideas, if we find this function is hot: // * We could speculatively create a CStringBuffer to contain 'length' // characters, and resize if necessary (i.e. if the buffer contains // non-ascii characters). (Alternatively, scan the buffer first for // ascii characters, so we know this will be sufficient). // * We could allocate a CStringBuffer with an appropriate size to // have a good chance of being able to write the string into the // buffer without reallocing (say, 1.5 x length). if (length > std::numeric_limits<unsigned>::max() / 3) return std::string(); std::vector<char> bufferVector(length * 3); char* buffer = bufferVector.data(); const UChar* characters = m_impl.data(); ConversionResult result = convertUTF16ToUTF8(&characters, characters + length, &buffer, buffer + bufferVector.size(), false); DCHECK(result != targetExhausted); // (length * 3) should be sufficient for any conversion // Only produced from strict conversion. DCHECK(result != sourceIllegal); // Check for an unconverted high surrogate. if (result == sourceExhausted) { // This should be one unpaired high surrogate. Treat it the same // was as an unpaired high surrogate would have been handled in // the middle of a string with non-strict conversion - which is // to say, simply encode it to UTF-8. DCHECK((characters + 1) == (m_impl.data() + length)); DCHECK((*characters >= 0xD800) && (*characters <= 0xDBFF)); // There should be room left, since one UChar hasn't been // converted. DCHECK((buffer + 3) <= (buffer + bufferVector.size())); putUTF8Triple(buffer, *characters); } return std::string(bufferVector.data(), buffer - bufferVector.data()); }
CString String::utf8(UTF8ConversionMode mode) const { unsigned length = this->length(); if (!length) return CString("", 0); // Allocate a buffer big enough to hold all the characters // (an individual UTF-16 UChar can only expand to 3 UTF-8 bytes). // Optimization ideas, if we find this function is hot: // * We could speculatively create a CStringBuffer to contain 'length' // characters, and resize if necessary (i.e. if the buffer contains // non-ascii characters). (Alternatively, scan the buffer first for // ascii characters, so we know this will be sufficient). // * We could allocate a CStringBuffer with an appropriate size to // have a good chance of being able to write the string into the // buffer without reallocing (say, 1.5 x length). if (length > std::numeric_limits<unsigned>::max() / 3) return CString(); Vector<char, 1024> bufferVector(length * 3); char* buffer = bufferVector.data(); if (is8Bit()) { const LChar* characters = this->characters8(); ConversionResult result = convertLatin1ToUTF8(&characters, characters + length, &buffer, buffer + bufferVector.size()); ASSERT_UNUSED(result, result != targetExhausted); // (length * 3) should be sufficient for any conversion } else { const UChar* characters = this->characters16(); if (mode == StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD) { const UChar* charactersEnd = characters + length; char* bufferEnd = buffer + bufferVector.size(); while (characters < charactersEnd) { // Use strict conversion to detect unpaired surrogates. ConversionResult result = convertUTF16ToUTF8(&characters, charactersEnd, &buffer, bufferEnd, true); ASSERT(result != targetExhausted); // Conversion fails when there is an unpaired surrogate. Put // replacement character (U+FFFD) instead of the unpaired // surrogate. if (result != conversionOK) { ASSERT((0xD800 <= *characters && *characters <= 0xDFFF)); // There should be room left, since one UChar hasn't been // converted. ASSERT((buffer + 3) <= bufferEnd); putUTF8Triple(buffer, replacementCharacter); ++characters; } } } else { bool strict = mode == StrictUTF8Conversion; ConversionResult result = convertUTF16ToUTF8(&characters, characters + length, &buffer, buffer + bufferVector.size(), strict); ASSERT(result != targetExhausted); // (length * 3) should be sufficient for any conversion // Only produced from strict conversion. if (result == sourceIllegal) { ASSERT(strict); return CString(); } // Check for an unconverted high surrogate. if (result == sourceExhausted) { if (strict) return CString(); // This should be one unpaired high surrogate. Treat it the same // was as an unpaired high surrogate would have been handled in // the middle of a string with non-strict conversion - which is // to say, simply encode it to UTF-8. ASSERT((characters + 1) == (this->characters16() + length)); ASSERT((*characters >= 0xD800) && (*characters <= 0xDBFF)); // There should be room left, since one UChar hasn't been // converted. ASSERT((buffer + 3) <= (buffer + bufferVector.size())); putUTF8Triple(buffer, *characters); } } } return CString(bufferVector.data(), buffer - bufferVector.data()); }
inline std::string MakeAnything(ThingToMake<std::string>, JNIEnv& env, const String& string) { return convertUTF16ToUTF8(Make<std::u16string>(env, string)); }