diff options
author | jschuh@google.com <jschuh@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-17 17:24:59 +0000 |
---|---|---|
committer | jschuh@google.com <jschuh@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-17 17:24:59 +0000 |
commit | 173293170c18302eae5d2cf77d995daad74d4db1 (patch) | |
tree | 07b1516c8dc0e42859ae538e4a217d68f6bea958 /app | |
parent | 1f2d03b835c575406136a347cbbee6074b284793 (diff) | |
download | chromium_src-173293170c18302eae5d2cf77d995daad74d4db1.zip chromium_src-173293170c18302eae5d2cf77d995daad74d4db1.tar.gz chromium_src-173293170c18302eae5d2cf77d995daad74d4db1.tar.bz2 |
Move ElideString() from base/string_util.cc to app/text_elider.cc to
reduce size of widely-included base libraries.
Committing for tsepez.
BUG=49747
TEST=TextEliderTest.*
Review URL: http://codereview.chromium.org/6017001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69555 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r-- | app/text_elider.cc | 37 | ||||
-rw-r--r-- | app/text_elider.h | 13 | ||||
-rw-r--r-- | app/text_elider_unittest.cc | 27 |
3 files changed, 77 insertions, 0 deletions
diff --git a/app/text_elider.cc b/app/text_elider.cc index 79a865e..bfaec34 100644 --- a/app/text_elider.cc +++ b/app/text_elider.cc @@ -460,4 +460,41 @@ string16 SortedDisplayURL::AfterHost() const { return display_url_.substr(slash_index + sort_host_.length()); } +bool ElideString(const std::wstring& input, int max_len, std::wstring* output) { + DCHECK_GE(max_len, 0); + if (static_cast<int>(input.length()) <= max_len) { + output->assign(input); + return false; + } + + switch (max_len) { + case 0: + output->clear(); + break; + case 1: + output->assign(input.substr(0, 1)); + break; + case 2: + output->assign(input.substr(0, 2)); + break; + case 3: + output->assign(input.substr(0, 1) + L"." + + input.substr(input.length() - 1)); + break; + case 4: + output->assign(input.substr(0, 1) + L".." + + input.substr(input.length() - 1)); + break; + default: { + int rstr_len = (max_len - 3) / 2; + int lstr_len = rstr_len + ((max_len - 3) % 2); + output->assign(input.substr(0, lstr_len) + L"..." + + input.substr(input.length() - rstr_len)); + break; + } + } + + return true; +} + } // namespace gfx diff --git a/app/text_elider.h b/app/text_elider.h index 36d12ae..c0d6c33 100644 --- a/app/text_elider.h +++ b/app/text_elider.h @@ -88,6 +88,19 @@ class SortedDisplayURL { string16 display_url_; }; +// Function to elide strings when the font information is unknown. As +// opposed to the above functions, the ElideString() function operates +// in terms of character units, not pixels. +// If the size of |input| is more than |max_len|, this function returns +// true and |input| is shortened into |output| by removing chars in the +// middle (they are replaced with up to 3 dots, as size permits). +// Ex: ElideString(L"Hello", 10, &str) puts Hello in str and returns false. +// ElideString(L"Hello my name is Tom", 10, &str) puts "Hell...Tom" in str +// and returns true. +// TODO(tsepez): Doesn't handle UTF-16 surrogate pairs properly. +// TODO(tsepez): Doesn't handle bidi properly +bool ElideString(const std::wstring& input, int max_len, std::wstring* output); + } // namespace gfx. #endif // APP_TEXT_ELIDER_H_ diff --git a/app/text_elider_unittest.cc b/app/text_elider_unittest.cc index 8bb224c..e5a29d9 100644 --- a/app/text_elider_unittest.cc +++ b/app/text_elider_unittest.cc @@ -295,3 +295,30 @@ TEST(TextEliderTest, SortedDisplayURLCompare) { EXPECT_EQ(-tests[i].compare_result, url2.Compare(url1, collator.get())); } } + +TEST(TextEliderTest, ElideString) { + struct TestData { + const wchar_t* input; + int max_len; + bool result; + const wchar_t* output; + } cases[] = { + { L"Hello", 0, true, L"" }, + { L"", 0, false, L"" }, + { L"Hello, my name is Tom", 1, true, L"H" }, + { L"Hello, my name is Tom", 2, true, L"He" }, + { L"Hello, my name is Tom", 3, true, L"H.m" }, + { L"Hello, my name is Tom", 4, true, L"H..m" }, + { L"Hello, my name is Tom", 5, true, L"H...m" }, + { L"Hello, my name is Tom", 6, true, L"He...m" }, + { L"Hello, my name is Tom", 7, true, L"He...om" }, + { L"Hello, my name is Tom", 10, true, L"Hell...Tom" }, + { L"Hello, my name is Tom", 100, false, L"Hello, my name is Tom" } + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + std::wstring output; + EXPECT_EQ(cases[i].result, + gfx::ElideString(cases[i].input, cases[i].max_len, &output)); + EXPECT_TRUE(output == cases[i].output); + } +} |