summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjschuh@google.com <jschuh@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-17 17:24:59 +0000
committerjschuh@google.com <jschuh@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-17 17:24:59 +0000
commit173293170c18302eae5d2cf77d995daad74d4db1 (patch)
tree07b1516c8dc0e42859ae538e4a217d68f6bea958
parent1f2d03b835c575406136a347cbbee6074b284793 (diff)
downloadchromium_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
-rw-r--r--app/text_elider.cc37
-rw-r--r--app/text_elider.h13
-rw-r--r--app/text_elider_unittest.cc27
-rw-r--r--base/string_util.cc37
-rw-r--r--base/string_util.h8
-rw-r--r--base/string_util_unittest.cc27
-rw-r--r--chrome/browser/chromeos/external_protocol_dialog.cc3
-rw-r--r--chrome/browser/gtk/external_protocol_dialog_gtk.cc5
-rw-r--r--chrome/browser/tab_contents/navigation_entry.cc5
-rw-r--r--chrome/browser/ui/app_modal_dialogs/js_modal_dialog.cc7
-rw-r--r--chrome/browser/ui/cocoa/download/download_item_controller.mm7
-rw-r--r--chrome/browser/ui/cocoa/external_protocol_dialog.mm5
-rw-r--r--chrome/browser/ui/views/download_item_view.cc6
-rw-r--r--chrome/browser/ui/views/external_protocol_dialog.cc7
14 files changed, 105 insertions, 89 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);
+ }
+}
diff --git a/base/string_util.cc b/base/string_util.cc
index fc1372b..ce12705 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -1094,40 +1094,3 @@ size_t base::strlcpy(char* dst, const char* src, size_t dst_size) {
size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) {
return lcpyT<wchar_t>(dst, src, dst_size);
}
-
-bool ElideString(const std::wstring& input, int max_len, std::wstring* output) {
- DCHECK(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;
-}
diff --git a/base/string_util.h b/base/string_util.h
index 498ccc5..f65652c 100644
--- a/base/string_util.h
+++ b/base/string_util.h
@@ -523,14 +523,6 @@ string16 ReplaceStringPlaceholders(const string16& format_string,
const string16& a,
size_t* offset);
-// 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.
-bool ElideString(const std::wstring& input, int max_len, std::wstring* output);
-
// Returns true if the string passed in matches the pattern. The pattern
// string can contain wildcards like * and ?
// The backslash character (\) is an escape character for * and ?
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc
index b7639bb..cd45642 100644
--- a/base/string_util_unittest.cc
+++ b/base/string_util_unittest.cc
@@ -1056,33 +1056,6 @@ TEST(StringUtilTest, WprintfFormatPortabilityTest) {
}
}
-TEST(StringUtilTest, 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,
- ElideString(cases[i].input, cases[i].max_len, &output));
- EXPECT_TRUE(output == cases[i].output);
- }
-}
-
TEST(StringUtilTest, RemoveChars) {
const char* kRemoveChars = "-/+*";
std::string input = "A-+bc/d!*";
diff --git a/chrome/browser/chromeos/external_protocol_dialog.cc b/chrome/browser/chromeos/external_protocol_dialog.cc
index 38d1cdf..928508f 100644
--- a/chrome/browser/chromeos/external_protocol_dialog.cc
+++ b/chrome/browser/chromeos/external_protocol_dialog.cc
@@ -6,6 +6,7 @@
#include "app/l10n_util.h"
#include "app/message_box_flags.h"
+#include "app/text_elider.h"
#include "base/metrics/histogram.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
@@ -86,7 +87,7 @@ ExternalProtocolDialog::ExternalProtocolDialog(TabContents* tab_contents,
scheme_(url.scheme()) {
const int kMaxUrlWithoutSchemeSize = 256;
std::wstring elided_url_without_scheme;
- ElideString(ASCIIToWide(url.possibly_invalid_spec()),
+ gfx::ElideString(ASCIIToWide(url.possibly_invalid_spec()),
kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
std::wstring message_text = l10n_util::GetStringF(
diff --git a/chrome/browser/gtk/external_protocol_dialog_gtk.cc b/chrome/browser/gtk/external_protocol_dialog_gtk.cc
index 290d859..34c2f78 100644
--- a/chrome/browser/gtk/external_protocol_dialog_gtk.cc
+++ b/chrome/browser/gtk/external_protocol_dialog_gtk.cc
@@ -9,6 +9,7 @@
#include <string>
#include "app/l10n_util.h"
+#include "app/text_elider.h"
#include "base/metrics/histogram.h"
#include "base/message_loop.h"
#include "base/string_util.h"
@@ -62,9 +63,9 @@ ExternalProtocolDialogGtk::ExternalProtocolDialogGtk(const GURL& url)
const int kMaxCommandSize = 256;
std::wstring elided_url_without_scheme;
std::wstring elided_command;
- ElideString(ASCIIToWide(url.possibly_invalid_spec()),
+ gfx::ElideString(ASCIIToWide(url.possibly_invalid_spec()),
kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
- ElideString(ASCIIToWide(std::string("xdg-open ") + url.spec()),
+ gfx::ElideString(ASCIIToWide(std::string("xdg-open ") + url.spec()),
kMaxCommandSize, &elided_command);
std::string message_text = l10n_util::GetStringFUTF8(
diff --git a/chrome/browser/tab_contents/navigation_entry.cc b/chrome/browser/tab_contents/navigation_entry.cc
index e09a99c..a14fc08 100644
--- a/chrome/browser/tab_contents/navigation_entry.cc
+++ b/chrome/browser/tab_contents/navigation_entry.cc
@@ -5,6 +5,7 @@
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "app/resource_bundle.h"
+#include "app/text_elider.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/prefs/pref_service.h"
@@ -102,7 +103,9 @@ const string16& NavigationEntry::GetTitleForDisplay(
} else if (!url_.is_empty()) {
title = net::FormatUrl(url_, languages);
}
- ElideString(UTF16ToWideHack(title), chrome::kMaxTitleChars, &elided_title);
+ gfx::ElideString(UTF16ToWideHack(title),
+ chrome::kMaxTitleChars,
+ &elided_title);
cached_display_title_ = WideToUTF16Hack(elided_title);
return cached_display_title_;
}
diff --git a/chrome/browser/ui/app_modal_dialogs/js_modal_dialog.cc b/chrome/browser/ui/app_modal_dialogs/js_modal_dialog.cc
index 9e1d74b..8fe0499 100644
--- a/chrome/browser/ui/app_modal_dialogs/js_modal_dialog.cc
+++ b/chrome/browser/ui/app_modal_dialogs/js_modal_dialog.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h"
+#include "app/text_elider.h"
#include "base/string_util.h"
#include "chrome/browser/browser_shutdown.h"
#include "chrome/browser/extensions/extension_host.h"
@@ -39,9 +40,9 @@ JavaScriptAppModalDialog::JavaScriptAppModalDialog(
reply_msg_(reply_msg) {
// We trim the various parts of the message dialog because otherwise we can
// overflow the message dialog (and crash/hang the GTK+ version).
- ElideString(message_text, kMessageTextMaxSize, &message_text_);
- ElideString(default_prompt_text, kDefaultPromptTextSize,
- &default_prompt_text_);
+ gfx::ElideString(message_text, kMessageTextMaxSize, &message_text_);
+ gfx::ElideString(default_prompt_text, kDefaultPromptTextSize,
+ &default_prompt_text_);
DCHECK((tab_contents_ != NULL) != (extension_host_ != NULL));
InitNotifications();
diff --git a/chrome/browser/ui/cocoa/download/download_item_controller.mm b/chrome/browser/ui/cocoa/download/download_item_controller.mm
index 39ff0de..8d10b2a 100644
--- a/chrome/browser/ui/cocoa/download/download_item_controller.mm
+++ b/chrome/browser/ui/cocoa/download/download_item_controller.mm
@@ -191,14 +191,15 @@ class DownloadShelfContextMenuMac : public DownloadShelfContextMenu {
// Elide giant extensions.
if (extension.length() > kFileNameMaxLength / 2) {
std::wstring wide_extension;
- ElideString(UTF8ToWide(extension), kFileNameMaxLength / 2,
- &wide_extension);
+ gfx::ElideString(UTF8ToWide(extension), kFileNameMaxLength / 2,
+ &wide_extension);
extension = WideToUTF8(wide_extension);
}
// Rebuild the filename.extension.
std::wstring rootname = UTF8ToWide(filename.RemoveExtension().value());
- ElideString(rootname, kFileNameMaxLength - extension.length(), &rootname);
+ gfx::ElideString(rootname, kFileNameMaxLength - extension.length(),
+ &rootname);
std::string new_filename = WideToUTF8(rootname);
if (extension.length())
new_filename += std::string(".") + extension;
diff --git a/chrome/browser/ui/cocoa/external_protocol_dialog.mm b/chrome/browser/ui/cocoa/external_protocol_dialog.mm
index 8dafab9..e72394b 100644
--- a/chrome/browser/ui/cocoa/external_protocol_dialog.mm
+++ b/chrome/browser/ui/cocoa/external_protocol_dialog.mm
@@ -5,6 +5,7 @@
#import "chrome/browser/ui/cocoa/external_protocol_dialog.h"
#include "app/l10n_util_mac.h"
+#include "app/text_elider.h"
#include "base/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/string_util.h"
@@ -61,8 +62,8 @@ void ExternalProtocolHandler::RunExternalProtocolDialog(
const int kMaxUrlWithoutSchemeSize = 256;
std::wstring elided_url_without_scheme;
- ElideString(ASCIIToWide(url_.possibly_invalid_spec()),
- kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
+ gfx::ElideString(ASCIIToWide(url_.possibly_invalid_spec()),
+ kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
NSString* urlString = l10n_util::GetNSStringFWithFixup(
IDS_EXTERNAL_PROTOCOL_INFORMATION,
diff --git a/chrome/browser/ui/views/download_item_view.cc b/chrome/browser/ui/views/download_item_view.cc
index 1c1e101..f6d022d 100644
--- a/chrome/browser/ui/views/download_item_view.cc
+++ b/chrome/browser/ui/views/download_item_view.cc
@@ -278,14 +278,16 @@ DownloadItemView::DownloadItemView(DownloadItem* download,
// Elide giant extensions (this shouldn't currently be hit, but might
// in future, should we ever notice unsafe giant extensions).
if (extension.length() > kFileNameMaxLength / 2)
- ElideString(extension, kFileNameMaxLength / 2, &extension);
+ gfx::ElideString(extension, kFileNameMaxLength / 2, &extension);
// The dangerous download label text is different for an extension file.
if (download->is_extension_install()) {
dangerous_download_label_ = new views::Label(
l10n_util::GetString(IDS_PROMPT_DANGEROUS_DOWNLOAD_EXTENSION));
} else {
- ElideString(rootname, kFileNameMaxLength - extension.length(), &rootname);
+ gfx::ElideString(rootname,
+ kFileNameMaxLength - extension.length(),
+ &rootname);
std::wstring filename = rootname + L"." + extension;
filename = UTF16ToWide(base::i18n::GetDisplayStringInLTRDirectionality(
WideToUTF16(filename)));
diff --git a/chrome/browser/ui/views/external_protocol_dialog.cc b/chrome/browser/ui/views/external_protocol_dialog.cc
index 9d5c9b8..6f402c3 100644
--- a/chrome/browser/ui/views/external_protocol_dialog.cc
+++ b/chrome/browser/ui/views/external_protocol_dialog.cc
@@ -6,6 +6,7 @@
#include "app/l10n_util.h"
#include "app/message_box_flags.h"
+#include "app/text_elider.h"
#include "base/metrics/histogram.h"
#include "base/string_util.h"
#include "base/thread.h"
@@ -122,9 +123,9 @@ ExternalProtocolDialog::ExternalProtocolDialog(TabContents* tab_contents,
const int kMaxCommandSize = 256;
std::wstring elided_url_without_scheme;
std::wstring elided_command;
- ElideString(ASCIIToWide(url.possibly_invalid_spec()),
- kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
- ElideString(command, kMaxCommandSize, &elided_command);
+ gfx::ElideString(ASCIIToWide(url.possibly_invalid_spec()),
+ kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
+ gfx::ElideString(command, kMaxCommandSize, &elided_command);
std::wstring message_text = l10n_util::GetStringF(
IDS_EXTERNAL_PROTOCOL_INFORMATION,