Chromium Code Reviews| Index: ui/gfx/text_elider.cc |
| diff --git a/ui/gfx/text_elider.cc b/ui/gfx/text_elider.cc |
| index 56ed501b834ed8b0d8b329051f4ca08082a557bd..4ab6098cc0c5d3bf91e75bc70c6fd2085c48ffdd 100644 |
| --- a/ui/gfx/text_elider.cc |
| +++ b/ui/gfx/text_elider.cc |
| @@ -755,7 +755,10 @@ int ElideRectangleText(const base::string16& input, |
| return rect.Finalize(); |
| } |
| -base::string16 TruncateString(const base::string16& string, size_t length) { |
| +base::string16 TruncateString( |
| + const base::string16& string, size_t length, BreakType break_type) { |
|
msw
2014/07/21 15:26:07
nit: one parameter per line
Marc Treib
2014/07/21 16:43:52
Done.
|
| + DCHECK(break_type == CHARACTER_BREAK || break_type == WORD_BREAK); |
| + |
| if (string.size() <= length) |
| // String fits, return it. |
| return string; |
| @@ -773,47 +776,47 @@ base::string16 TruncateString(const base::string16& string, size_t length) { |
| // Just enough room for the elide string. |
| return kElideString; |
| - // Use a line iterator to find the first boundary. |
| - UErrorCode status = U_ZERO_ERROR; |
| - scoped_ptr<icu::RuleBasedBreakIterator> bi( |
| - static_cast<icu::RuleBasedBreakIterator*>( |
| - icu::RuleBasedBreakIterator::createLineInstance( |
| - icu::Locale::getDefault(), status))); |
| - if (U_FAILURE(status)) |
| - return string.substr(0, max) + kElideString; |
| - bi->setText(string.c_str()); |
| - int32_t index = bi->preceding(static_cast<int32_t>(max)); |
| - if (index == icu::BreakIterator::DONE) { |
| - index = static_cast<int32_t>(max); |
| - } else { |
| - // Found a valid break (may be the beginning of the string). Now use |
| - // a character iterator to find the previous non-whitespace character. |
| - icu::StringCharacterIterator char_iterator(string.c_str()); |
| - if (index == 0) { |
| - // No valid line breaks. Start at the end again. This ensures we break |
| - // on a valid character boundary. |
| + int32_t index = static_cast<int32_t>(max); |
| + if (break_type == WORD_BREAK) { |
| + // Use a line iterator to find the first boundary. |
| + UErrorCode status = U_ZERO_ERROR; |
| + scoped_ptr<icu::BreakIterator> bi( |
| + icu::RuleBasedBreakIterator::createLineInstance( |
| + icu::Locale::getDefault(), status)); |
| + if (U_FAILURE(status)) |
| + return string.substr(0, max) + kElideString; |
| + bi->setText(string.c_str()); |
| + index = bi->preceding(index); |
| + if (index == icu::BreakIterator::DONE || index == 0) { |
| + // We either found no valid line break at all, or one right at the |
| + // beginning of the string. Go back to the end; we'll have to break in the |
| + // middle of a word. |
| index = static_cast<int32_t>(max); |
| } |
| - char_iterator.setIndex(index); |
| - while (char_iterator.hasPrevious()) { |
| - char_iterator.previous(); |
| - if (!(u_isspace(char_iterator.current()) || |
| - u_charType(char_iterator.current()) == U_CONTROL_CHAR || |
| - u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) { |
| - // Not a whitespace character. Advance the iterator so that we |
| - // include the current character in the truncated string. |
| - char_iterator.next(); |
| - break; |
| - } |
| - } |
| - if (char_iterator.hasPrevious()) { |
| - // Found a valid break point. |
| - index = char_iterator.getIndex(); |
| - } else { |
| - // String has leading whitespace, return the elide string. |
| - return kElideString; |
| + } |
| + |
| + // Use a character iterator to find the previous non-whitespace character. |
| + icu::StringCharacterIterator char_iterator(string.c_str()); |
| + char_iterator.setIndex(index); |
| + while (char_iterator.hasPrevious()) { |
| + char_iterator.previous(); |
| + if (!(u_isspace(char_iterator.current()) || |
| + u_charType(char_iterator.current()) == U_CONTROL_CHAR || |
| + u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) { |
| + // Not a whitespace character. Advance the iterator so that we |
| + // include the current character in the truncated string. |
| + char_iterator.next(); |
| + break; |
| } |
| } |
| + if (char_iterator.hasPrevious()) { |
| + // Found a valid break point. |
| + index = char_iterator.getIndex(); |
| + } else { |
| + // String has leading whitespace, return the elide string. |
| + return kElideString; |
| + } |
| + |
| return string.substr(0, index) + kElideString; |
| } |