Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file implements utility functions for eliding and formatting UI text. | 5 // This file implements utility functions for eliding and formatting UI text. |
| 6 // | 6 // |
| 7 // Note that several of the functions declared in text_elider.h are implemented | 7 // Note that several of the functions declared in text_elider.h are implemented |
| 8 // in this file using helper classes in an unnamed namespace. | 8 // in this file using helper classes in an unnamed namespace. |
| 9 | 9 |
| 10 #include "ui/gfx/text_elider.h" | 10 #include "ui/gfx/text_elider.h" |
| 11 | 11 |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/i18n/break_iterator.h" | 16 #include "base/i18n/break_iterator.h" |
| 17 #include "base/i18n/char_iterator.h" | 17 #include "base/i18n/char_iterator.h" |
| 18 #include "base/i18n/rtl.h" | 18 #include "base/i18n/rtl.h" |
| 19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/numerics/safe_conversions.h" | |
| 20 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
| 21 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| 22 #include "base/strings/sys_string_conversions.h" | 23 #include "base/strings/sys_string_conversions.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 24 #include "third_party/icu/source/common/unicode/rbbi.h" | 25 #include "third_party/icu/source/common/unicode/rbbi.h" |
| 25 #include "third_party/icu/source/common/unicode/uloc.h" | 26 #include "third_party/icu/source/common/unicode/uloc.h" |
| 26 #include "ui/gfx/font_list.h" | 27 #include "ui/gfx/font_list.h" |
| 27 #include "ui/gfx/geometry/rect_conversions.h" | 28 #include "ui/gfx/geometry/rect_conversions.h" |
| 28 #include "ui/gfx/render_text.h" | 29 #include "ui/gfx/render_text.h" |
| 29 #include "ui/gfx/text_utils.h" | 30 #include "ui/gfx/text_utils.h" |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 | 140 |
| 140 size_t StringSlicer::FindValidBoundaryBefore(size_t index) const { | 141 size_t StringSlicer::FindValidBoundaryBefore(size_t index) const { |
| 141 DCHECK_LE(index, text_.length()); | 142 DCHECK_LE(index, text_.length()); |
| 142 if (index != text_.length()) | 143 if (index != text_.length()) |
| 143 U16_SET_CP_START(text_.data(), 0, index); | 144 U16_SET_CP_START(text_.data(), 0, index); |
| 144 return index; | 145 return index; |
| 145 } | 146 } |
| 146 | 147 |
| 147 size_t StringSlicer::FindValidBoundaryAfter(size_t index) const { | 148 size_t StringSlicer::FindValidBoundaryAfter(size_t index) const { |
| 148 DCHECK_LE(index, text_.length()); | 149 DCHECK_LE(index, text_.length()); |
| 149 if (index != text_.length()) | 150 if (index == text_.length()) |
| 150 U16_SET_CP_LIMIT(text_.data(), 0, index, text_.length()); | 151 return index; |
| 151 return index; | 152 |
| 153 int32_t text_index = base::checked_cast<int32_t>(index); | |
| 154 int32_t text_length = base::checked_cast<int32_t>(text_.length()); | |
| 155 U16_SET_CP_LIMIT(text_.data(), 0, text_index, text_length); | |
|
msw
2014/10/29 20:46:43
Didn't Peter suggest using c_str() instead of data
Ben Chan
2014/10/29 21:08:30
I believe .c_str() isn't necessary if the length i
msw
2014/10/29 21:15:46
If c_str() offers additional safety without seriou
Ben Chan
2014/10/29 21:23:36
The only thing I can think of may be related to pe
| |
| 156 return static_cast<size_t>(text_index); | |
|
msw
2014/10/29 20:46:44
Shouldn't this check that the 32-bit index won't o
Ben Chan
2014/10/29 21:08:30
do we support a platform with 16-bit size_t? if so
msw
2014/10/29 21:15:46
Heh, perhaps not, I guess this ought to be fine ot
| |
| 152 } | 157 } |
| 153 | 158 |
| 154 base::string16 ElideFilename(const base::FilePath& filename, | 159 base::string16 ElideFilename(const base::FilePath& filename, |
| 155 const FontList& font_list, | 160 const FontList& font_list, |
| 156 float available_pixel_width) { | 161 float available_pixel_width) { |
| 157 #if defined(OS_WIN) | 162 #if defined(OS_WIN) |
| 158 base::string16 filename_utf16 = filename.value(); | 163 base::string16 filename_utf16 = filename.value(); |
| 159 base::string16 extension = filename.Extension(); | 164 base::string16 extension = filename.Extension(); |
| 160 base::string16 rootname = filename.BaseName().RemoveExtension().value(); | 165 base::string16 rootname = filename.BaseName().RemoveExtension().value(); |
| 161 #elif defined(OS_POSIX) | 166 #elif defined(OS_POSIX) |
| (...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 817 index = char_iterator.getIndex(); | 822 index = char_iterator.getIndex(); |
| 818 } else { | 823 } else { |
| 819 // String has leading whitespace, return the elide string. | 824 // String has leading whitespace, return the elide string. |
| 820 return kElideString; | 825 return kElideString; |
| 821 } | 826 } |
| 822 | 827 |
| 823 return string.substr(0, index) + kElideString; | 828 return string.substr(0, index) + kElideString; |
| 824 } | 829 } |
| 825 | 830 |
| 826 } // namespace gfx | 831 } // namespace gfx |
| OLD | NEW |