| Index: ui/gfx/text_elider.cc
|
| diff --git a/ui/gfx/text_elider.cc b/ui/gfx/text_elider.cc
|
| index 56ed501b834ed8b0d8b329051f4ca08082a557bd..c87b817b45077c4f705540fa43a5cf0bc57c3f9a 100644
|
| --- a/ui/gfx/text_elider.cc
|
| +++ b/ui/gfx/text_elider.cc
|
| @@ -755,7 +755,8 @@ 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, bool only_at_word_breaks) {
|
| if (string.size() <= length)
|
| // String fits, return it.
|
| return string;
|
| @@ -773,47 +774,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 (only_at_word_breaks) {
|
| + // 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;
|
| }
|
|
|
|
|