| Index: Source/wtf/text/StringImpl.cpp
|
| diff --git a/Source/wtf/text/StringImpl.cpp b/Source/wtf/text/StringImpl.cpp
|
| index 838b6715fcdce7c992b8a90e4fcfaab69c36ac77..8ecf0face368396acbd9e95aff9ebab5bddf7e43 100644
|
| --- a/Source/wtf/text/StringImpl.cpp
|
| +++ b/Source/wtf/text/StringImpl.cpp
|
| @@ -1893,6 +1893,74 @@ PassRefPtr<StringImpl> StringImpl::replace(StringImpl* pattern, StringImpl* repl
|
| return newImpl.release();
|
| }
|
|
|
| +PassRefPtr<StringImpl> StringImpl::replaceUnpairedSurrogates()
|
| +{
|
| + if (is8Bit())
|
| + return this;
|
| +
|
| + const UChar* characters = characters16();
|
| + const unsigned length = m_length;
|
| +
|
| + // Scan to see if there are any unmatched surrogates.
|
| + bool unmatched = false;
|
| + for (unsigned i = 0; i < length; ++i) {
|
| + UChar c = characters[i];
|
| + if (c < 0xD800 || c > 0xDFFF) {
|
| + // Non-surrogate - no-op.
|
| + } else if (0xDC00 <= c && c <= 0xDFFF) {
|
| + // Unmatched trail surrogate.
|
| + unmatched = true;
|
| + break;
|
| + } else if (i == length - 1) {
|
| + // Unmatched lead surrogate at EOF.
|
| + unmatched = true;
|
| + break;
|
| + } else {
|
| + UChar d = characters[i + 1];
|
| + if (0xDC00 <= d && d <= 0xDFFF) {
|
| + // Matching trail surrogate.
|
| + ++i;
|
| + continue;
|
| + }
|
| + // Unmatched lead.
|
| + unmatched = true;
|
| + break;
|
| + }
|
| + }
|
| + if (!unmatched)
|
| + return this;
|
| +
|
| + UChar* data;
|
| + RefPtr<StringImpl> newImpl = createUninitialized(length, data);
|
| + for (unsigned i = 0; i < length; ++i) {
|
| + UChar c = characters[i];
|
| + if (c < 0xD800 || c > 0xDFFF) {
|
| + // Non-surrogate.
|
| + data[i] = c;
|
| + } else if (0xDC00 <= c && c <= 0xDFFF) {
|
| + // Unmatched trail surrogate.
|
| + data[i] = Unicode::replacementCharacter;
|
| + } else if (i == length - 1) {
|
| + // Unmatched lead surrogate at EOF.
|
| + data[i] = Unicode::replacementCharacter;
|
| + } else {
|
| + UChar d = characters[i + 1];
|
| + if (0xDC00 <= d && d <= 0xDFFF) {
|
| + // Matching trail surrogate.
|
| + data[i] = c;
|
| + data[i + 1] = d;
|
| + ++i;
|
| + } else {
|
| + // Unmatched lead.
|
| + data[i] = Unicode::replacementCharacter;
|
| + }
|
| + }
|
| + }
|
| +
|
| + return newImpl.release();
|
| +}
|
| +
|
| +
|
| PassRefPtr<StringImpl> StringImpl::upconvertedString()
|
| {
|
| if (is8Bit())
|
|
|