📄 stringimpl.cpp
字号:
}static bool equal(const UChar* a, const char* b, int length){ ASSERT(length >= 0); while (length--) { unsigned char bc = *b++; if (*a++ != bc) return false; } return true;}static bool equalIgnoringCase(const UChar* a, const char* b, int length){ ASSERT(length >= 0); while (length--) { unsigned char bc = *b++; if (foldCase(*a++) != foldCase(bc)) return false; } return true;}static inline bool equalIgnoringCase(const UChar* a, const UChar* b, int length){ ASSERT(length >= 0); return umemcasecmp(a, b, length) == 0;}int StringImpl::find(const char* chs, int index, bool caseSensitive){ if (!chs || index < 0) return -1; int chsLength = strlen(chs); int n = m_length - index; if (n < 0) return -1; n -= chsLength - 1; if (n <= 0) return -1; const char* chsPlusOne = chs + 1; int chsLengthMinusOne = chsLength - 1; const UChar* ptr = m_data + index - 1; if (caseSensitive) { UChar c = *chs; do { if (*++ptr == c && equal(ptr + 1, chsPlusOne, chsLengthMinusOne)) return m_length - chsLength - n + 1; } while (--n); } else { UChar lc = Unicode::foldCase(*chs); do { if (Unicode::foldCase(*++ptr) == lc && equalIgnoringCase(ptr + 1, chsPlusOne, chsLengthMinusOne)) return m_length - chsLength - n + 1; } while (--n); } return -1;}int StringImpl::find(UChar c, int start){ return WebCore::find(m_data, m_length, c, start);}int StringImpl::find(CharacterMatchFunctionPtr matchFunction, int start){ return WebCore::find(m_data, m_length, matchFunction, start);}int StringImpl::find(StringImpl* str, int index, bool caseSensitive){ /* We use a simple trick for efficiency's sake. Instead of comparing strings, we compare the sum of str with that of a part of this string. Only if that matches, we call memcmp or ucstrnicmp. */ ASSERT(str); if (index < 0) index += m_length; int lstr = str->m_length; int lthis = m_length - index; if ((unsigned)lthis > m_length) return -1; int delta = lthis - lstr; if (delta < 0) return -1; const UChar* uthis = m_data + index; const UChar* ustr = str->m_data; unsigned hthis = 0; unsigned hstr = 0; if (caseSensitive) { for (int i = 0; i < lstr; i++) { hthis += uthis[i]; hstr += ustr[i]; } int i = 0; while (1) { if (hthis == hstr && memcmp(uthis + i, ustr, lstr * sizeof(UChar)) == 0) return index + i; if (i == delta) return -1; hthis += uthis[i + lstr]; hthis -= uthis[i]; i++; } } else { for (int i = 0; i < lstr; i++ ) { hthis += toASCIILower(uthis[i]); hstr += toASCIILower(ustr[i]); } int i = 0; while (1) { if (hthis == hstr && equalIgnoringCase(uthis + i, ustr, lstr)) return index + i; if (i == delta) return -1; hthis += toASCIILower(uthis[i + lstr]); hthis -= toASCIILower(uthis[i]); i++; } }}int StringImpl::reverseFind(UChar c, int index){ return WebCore::reverseFind(m_data, m_length, c, index);}int StringImpl::reverseFind(StringImpl* str, int index, bool caseSensitive){ /* See StringImpl::find() for explanations. */ ASSERT(str); int lthis = m_length; if (index < 0) index += lthis; int lstr = str->m_length; int delta = lthis - lstr; if ( index < 0 || index > lthis || delta < 0 ) return -1; if ( index > delta ) index = delta; const UChar *uthis = m_data; const UChar *ustr = str->m_data; unsigned hthis = 0; unsigned hstr = 0; int i; if (caseSensitive) { for ( i = 0; i < lstr; i++ ) { hthis += uthis[index + i]; hstr += ustr[i]; } i = index; while (1) { if (hthis == hstr && memcmp(uthis + i, ustr, lstr * sizeof(UChar)) == 0) return i; if (i == 0) return -1; i--; hthis -= uthis[i + lstr]; hthis += uthis[i]; } } else { for (i = 0; i < lstr; i++) { hthis += toASCIILower(uthis[index + i]); hstr += toASCIILower(ustr[i]); } i = index; while (1) { if (hthis == hstr && equalIgnoringCase(uthis + i, ustr, lstr) ) return i; if (i == 0) return -1; i--; hthis -= toASCIILower(uthis[i + lstr]); hthis += toASCIILower(uthis[i]); } } // Should never get here. return -1;}bool StringImpl::endsWith(StringImpl* m_data, bool caseSensitive){ ASSERT(m_data); int start = m_length - m_data->m_length; if (start >= 0) return (find(m_data, start, caseSensitive) == start); return false;}PassRefPtr<StringImpl> StringImpl::replace(UChar oldC, UChar newC){ if (oldC == newC) return this; unsigned i; for (i = 0; i != m_length; ++i) if (m_data[i] == oldC) break; if (i == m_length) return this; StringBuffer data(m_length); for (i = 0; i != m_length; ++i) { UChar ch = m_data[i]; if (ch == oldC) ch = newC; data[i] = ch; } return adopt(data);}PassRefPtr<StringImpl> StringImpl::replace(unsigned position, unsigned lengthToReplace, StringImpl* str){ position = min(position, length()); lengthToReplace = min(lengthToReplace, length() - position); unsigned lengthToInsert = str ? str->length() : 0; if (!lengthToReplace && !lengthToInsert) return this; StringBuffer buffer(length() - lengthToReplace + lengthToInsert); memcpy(buffer.characters(), characters(), position * sizeof(UChar)); if (str) memcpy(buffer.characters() + position, str->characters(), lengthToInsert * sizeof(UChar)); memcpy(buffer.characters() + position + lengthToInsert, characters() + position + lengthToReplace, (length() - position - lengthToReplace) * sizeof(UChar)); return adopt(buffer);}PassRefPtr<StringImpl> StringImpl::replace(UChar pattern, StringImpl* replacement){ if (!replacement) return this; int repStrLength = replacement->length(); int srcSegmentStart = 0; int matchCount = 0; // Count the matches while ((srcSegmentStart = find(pattern, srcSegmentStart)) >= 0) { ++matchCount; ++srcSegmentStart; } // If we have 0 matches, we don't have to do any more work if (!matchCount) return this; StringBuffer data(m_length - matchCount + (matchCount * repStrLength)); // Construct the new data int srcSegmentEnd; int srcSegmentLength; srcSegmentStart = 0; int dstOffset = 0; while ((srcSegmentEnd = find(pattern, srcSegmentStart)) >= 0) { srcSegmentLength = srcSegmentEnd - srcSegmentStart; memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar)); dstOffset += srcSegmentLength; memcpy(data.characters() + dstOffset, replacement->m_data, repStrLength * sizeof(UChar)); dstOffset += repStrLength; srcSegmentStart = srcSegmentEnd + 1; } srcSegmentLength = m_length - srcSegmentStart; memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar)); ASSERT(dstOffset + srcSegmentLength == static_cast<int>(data.length())); return adopt(data);}PassRefPtr<StringImpl> StringImpl::replace(StringImpl* pattern, StringImpl* replacement){ if (!pattern || !replacement) return this; int patternLength = pattern->length(); if (!patternLength) return this; int repStrLength = replacement->length(); int srcSegmentStart = 0; int matchCount = 0; // Count the matches while ((srcSegmentStart = find(pattern, srcSegmentStart)) >= 0) { ++matchCount; srcSegmentStart += patternLength; } // If we have 0 matches, we don't have to do any more work if (!matchCount) return this; StringBuffer data(m_length + matchCount * (repStrLength - patternLength)); // Construct the new data int srcSegmentEnd; int srcSegmentLength; srcSegmentStart = 0; int dstOffset = 0; while ((srcSegmentEnd = find(pattern, srcSegmentStart)) >= 0) { srcSegmentLength = srcSegmentEnd - srcSegmentStart; memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar)); dstOffset += srcSegmentLength; memcpy(data.characters() + dstOffset, replacement->m_data, repStrLength * sizeof(UChar)); dstOffset += repStrLength; srcSegmentStart = srcSegmentEnd + patternLength; } srcSegmentLength = m_length - srcSegmentStart; memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar)); ASSERT(dstOffset + srcSegmentLength == static_cast<int>(data.length())); return adopt(data);}bool equal(StringImpl* a, StringImpl* b){ return StringHash::equal(a, b);}bool equal(StringImpl* a, const char* b){ if (!a) return !b; if (!b) return !a; unsigned length = a->length(); const UChar* as = a->characters(); for (unsigned i = 0; i != length; ++i) { unsigned char bc = b[i]; if (!bc) return false; if (as[i] != bc) return false; } return !b[length];}bool equalIgnoringCase(StringImpl* a, StringImpl* b){ return CaseFoldingHash::equal(a, b);}bool equalIgnoringCase(StringImpl* a, const char* b){ if (!a) return !b; if (!b) return !a; unsigned length = a->length(); const UChar* as = a->characters(); // Do a faster loop for the case where all the characters are ASCII. UChar ored = 0; bool equal = true; for (unsigned i = 0; i != length; ++i) { char bc = b[i]; if (!bc) return false; UChar ac = as[i]; ored |= ac; equal = equal && (toASCIILower(ac) == toASCIILower(bc)); } // Do a slower implementation for cases that include non-ASCII characters. if (ored & ~0x7F) { equal = true; for (unsigned i = 0; i != length; ++i) { unsigned char bc = b[i]; equal = equal && (foldCase(as[i]) == foldCase(bc)); } } return equal && !b[length];}Vector<char> StringImpl::ascii(){ Vector<char> buffer(m_length + 1); for (unsigned i = 0; i != m_length; ++i) { UChar c = m_data[i]; if ((c >= 0x20 && c < 0x7F) || c == 0x00) buffer[i] = c; else buffer[i] = '?'; } buffer[m_length] = '\0'; return buffer;}WTF::Unicode::Direction StringImpl::defaultWritingDirection(){ for (unsigned i = 0; i < m_length; ++i) { WTF::Unicode::Direction charDirection = WTF::Unicode::direction(m_data[i]); if (charDirection == WTF::Unicode::LeftToRight) return WTF::Unicode::LeftToRight; if (charDirection == WTF::Unicode::RightToLeft || charDirection == WTF::Unicode::RightToLeftArabic) return WTF::Unicode::RightToLeft; } return WTF::Unicode::LeftToRight;}// This is a hot function because it's used when parsing HTML.PassRefPtr<StringImpl> StringImpl::createStrippingNullCharactersSlowCase(const UChar* characters, unsigned length){ StringBuffer strippedCopy(length); unsigned strippedLength = 0; for (unsigned i = 0; i < length; i++) { if (int c = characters[i]) strippedCopy[strippedLength++] = c; } ASSERT(strippedLength < length); // Only take the slow case when stripping. strippedCopy.shrink(strippedLength); return adopt(strippedCopy);}PassRefPtr<StringImpl> StringImpl::adopt(StringBuffer& buffer){ unsigned length = buffer.length(); if (length == 0) return empty(); return adoptRef(new StringImpl(buffer.release(), length, AdoptBuffer()));}PassRefPtr<StringImpl> StringImpl::adopt(Vector<UChar>& vector){ size_t size = vector.size(); if (size == 0) return empty(); return adoptRef(new StringImpl(vector.releaseBuffer(), size, AdoptBuffer()));}PassRefPtr<StringImpl> StringImpl::create(const UChar* characters, unsigned length){ if (!characters || !length) return empty(); return adoptRef(new StringImpl(characters, length));}PassRefPtr<StringImpl> StringImpl::create(const char* characters, unsigned length){ if (!characters || !length) return empty(); return adoptRef(new StringImpl(characters, length));}PassRefPtr<StringImpl> StringImpl::create(const char* string){ if (!string) return empty(); unsigned length = strlen(string); if (!length) return empty(); return adoptRef(new StringImpl(string, length));}PassRefPtr<StringImpl> StringImpl::createWithTerminatingNullCharacter(const StringImpl& string){ return adoptRef(new StringImpl(string, WithTerminatingNullCharacter()));}PassRefPtr<StringImpl> StringImpl::copy(){ return adoptRef(new StringImpl(m_data, m_length));}} // namespace WebCore
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -