| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/l10n/l10n_util.h" | 5 #include "ui/base/l10n/l10n_util.h" |
| 6 | 6 |
| 7 #if defined(TOOLKIT_USES_GTK) | 7 #if defined(TOOLKIT_USES_GTK) |
| 8 #include <glib/gutils.h> | 8 #include <glib/gutils.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| (...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 } | 734 } |
| 735 | 735 |
| 736 string16 GetStringFUTF16Int(int message_id, int a) { | 736 string16 GetStringFUTF16Int(int message_id, int a) { |
| 737 return GetStringFUTF16(message_id, UTF8ToUTF16(base::IntToString(a))); | 737 return GetStringFUTF16(message_id, UTF8ToUTF16(base::IntToString(a))); |
| 738 } | 738 } |
| 739 | 739 |
| 740 string16 GetStringFUTF16Int(int message_id, int64 a) { | 740 string16 GetStringFUTF16Int(int message_id, int64 a) { |
| 741 return GetStringFUTF16(message_id, UTF8ToUTF16(base::Int64ToString(a))); | 741 return GetStringFUTF16(message_id, UTF8ToUTF16(base::Int64ToString(a))); |
| 742 } | 742 } |
| 743 | 743 |
| 744 string16 TruncateString(const string16& string, size_t length) { | |
| 745 if (string.size() <= length) | |
| 746 // String fits, return it. | |
| 747 return string; | |
| 748 | |
| 749 if (length == 0) { | |
| 750 // No room for the elide string, return an empty string. | |
| 751 return string16(); | |
| 752 } | |
| 753 size_t max = length - 1; | |
| 754 | |
| 755 // Added to the end of strings that are too big. | |
| 756 static const char16 kElideString[] = { 0x2026, 0 }; | |
| 757 | |
| 758 if (max == 0) { | |
| 759 // Just enough room for the elide string. | |
| 760 return kElideString; | |
| 761 } | |
| 762 | |
| 763 // Use a line iterator to find the first boundary. | |
| 764 UErrorCode status = U_ZERO_ERROR; | |
| 765 scoped_ptr<icu::RuleBasedBreakIterator> bi( | |
| 766 static_cast<icu::RuleBasedBreakIterator*>( | |
| 767 icu::RuleBasedBreakIterator::createLineInstance( | |
| 768 icu::Locale::getDefault(), status))); | |
| 769 if (U_FAILURE(status)) | |
| 770 return string.substr(0, max) + kElideString; | |
| 771 bi->setText(string.c_str()); | |
| 772 int32_t index = bi->preceding(static_cast<int32_t>(max)); | |
| 773 if (index == icu::BreakIterator::DONE) { | |
| 774 index = static_cast<int32_t>(max); | |
| 775 } else { | |
| 776 // Found a valid break (may be the beginning of the string). Now use | |
| 777 // a character iterator to find the previous non-whitespace character. | |
| 778 icu::StringCharacterIterator char_iterator(string.c_str()); | |
| 779 if (index == 0) { | |
| 780 // No valid line breaks. Start at the end again. This ensures we break | |
| 781 // on a valid character boundary. | |
| 782 index = static_cast<int32_t>(max); | |
| 783 } | |
| 784 char_iterator.setIndex(index); | |
| 785 while (char_iterator.hasPrevious()) { | |
| 786 char_iterator.previous(); | |
| 787 if (!(u_isspace(char_iterator.current()) || | |
| 788 u_charType(char_iterator.current()) == U_CONTROL_CHAR || | |
| 789 u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) { | |
| 790 // Not a whitespace character. Advance the iterator so that we | |
| 791 // include the current character in the truncated string. | |
| 792 char_iterator.next(); | |
| 793 break; | |
| 794 } | |
| 795 } | |
| 796 if (char_iterator.hasPrevious()) { | |
| 797 // Found a valid break point. | |
| 798 index = char_iterator.getIndex(); | |
| 799 } else { | |
| 800 // String has leading whitespace, return the elide string. | |
| 801 return kElideString; | |
| 802 } | |
| 803 } | |
| 804 return string.substr(0, index) + kElideString; | |
| 805 } | |
| 806 | |
| 807 // Compares the character data stored in two different string16 strings by | 744 // Compares the character data stored in two different string16 strings by |
| 808 // specified Collator instance. | 745 // specified Collator instance. |
| 809 UCollationResult CompareString16WithCollator(const icu::Collator* collator, | 746 UCollationResult CompareString16WithCollator(const icu::Collator* collator, |
| 810 const string16& lhs, | 747 const string16& lhs, |
| 811 const string16& rhs) { | 748 const string16& rhs) { |
| 812 DCHECK(collator); | 749 DCHECK(collator); |
| 813 UErrorCode error = U_ZERO_ERROR; | 750 UErrorCode error = U_ZERO_ERROR; |
| 814 UCollationResult result = collator->compare( | 751 UCollationResult result = collator->compare( |
| 815 static_cast<const UChar*>(lhs.c_str()), static_cast<int>(lhs.length()), | 752 static_cast<const UChar*>(lhs.c_str()), static_cast<int>(lhs.length()), |
| 816 static_cast<const UChar*>(rhs.c_str()), static_cast<int>(rhs.length()), | 753 static_cast<const UChar*>(rhs.c_str()), static_cast<int>(rhs.length()), |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 874 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) { | 811 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) { |
| 875 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale)) | 812 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale)) |
| 876 // TODO(jungshik) : Put them at the of the list with language codes | 813 // TODO(jungshik) : Put them at the of the list with language codes |
| 877 // enclosed by brackets instead of skipping. | 814 // enclosed by brackets instead of skipping. |
| 878 continue; | 815 continue; |
| 879 locale_codes->push_back(kAcceptLanguageList[i]); | 816 locale_codes->push_back(kAcceptLanguageList[i]); |
| 880 } | 817 } |
| 881 } | 818 } |
| 882 | 819 |
| 883 } // namespace l10n_util | 820 } // namespace l10n_util |
| OLD | NEW |