Index: Source/core/dom/Document.cpp |
diff --git a/Source/core/dom/Document.cpp b/Source/core/dom/Document.cpp |
index 39bdbdc2d5f0a5290b2ddd970a7c00a912fe2d8f..d9a630de3c73ca361085f431aac6b5feb906a181 100644 |
--- a/Source/core/dom/Document.cpp |
+++ b/Source/core/dom/Document.cpp |
@@ -1318,49 +1318,28 @@ PassRefPtrWillBeRawPtr<Range> Document::caretRangeFromPoint(int x, int y) |
template <typename CharacterType> |
static inline String canonicalizedTitle(Document* document, const String& title) |
{ |
- const CharacterType* characters = title.getCharacters<CharacterType>(); |
unsigned length = title.length(); |
- unsigned i; |
- |
- StringBuffer<CharacterType> buffer(length); |
unsigned builderIndex = 0; |
+ const CharacterType* characters = title.getCharacters<CharacterType>(); |
- // Skip leading spaces and leading characters that would convert to spaces |
- for (i = 0; i < length; ++i) { |
- CharacterType c = characters[i]; |
- if (!(c <= 0x20 || c == 0x7F)) |
- break; |
- } |
- |
- if (i == length) |
- return String(); |
+ StringBuffer<CharacterType> buffer(length); |
- // Replace control characters with spaces, and backslashes with currency symbols, and collapse whitespace. |
- bool previousCharWasWS = false; |
- for (; i < length; ++i) { |
- CharacterType c = characters[i]; |
+ // Replace control characters with spaces and collapse whitespace. |
+ bool pendingWhitespace = false; |
+ for (unsigned i = 0; i < length; ++i) { |
+ UChar32 c = characters[i]; |
if (c <= 0x20 || c == 0x7F || (WTF::Unicode::category(c) & (WTF::Unicode::Separator_Line | WTF::Unicode::Separator_Paragraph))) { |
- if (previousCharWasWS) |
- continue; |
- buffer[builderIndex++] = ' '; |
- previousCharWasWS = true; |
+ if (builderIndex != 0) |
+ pendingWhitespace = true; |
} else { |
+ if (pendingWhitespace) { |
+ buffer[builderIndex++] = ' '; |
+ pendingWhitespace = false; |
+ } |
buffer[builderIndex++] = c; |
- previousCharWasWS = false; |
} |
} |
- |
- // Strip trailing spaces |
- while (builderIndex > 0) { |
- --builderIndex; |
- if (buffer[builderIndex] != ' ') |
- break; |
- } |
- |
- if (!builderIndex && buffer[builderIndex] == ' ') |
- return String(); |
- |
- buffer.shrink(builderIndex + 1); |
+ buffer.shrink(builderIndex); |
return String::adopt(buffer); |
} |