Chromium Code Reviews| Index: ui/base/text/text_elider.cc |
| diff --git a/ui/base/text/text_elider.cc b/ui/base/text/text_elider.cc |
| index 525cea0df61eb373507b91372f144dbf327cf419..dd97e68f893c88e3c8cd227805c3a59d145f3093 100644 |
| --- a/ui/base/text/text_elider.cc |
| +++ b/ui/base/text/text_elider.cc |
| @@ -35,6 +35,8 @@ namespace ui { |
| const char kEllipsis[] = "\xE2\x80\xA6"; |
| const char16 kForwardSlash = '/'; |
| +const size_t kMaxProfileUsernameLength = 20; |
| + |
| namespace { |
| // Helper class to split + elide text, while respecting UTF16 surrogate pairs. |
| @@ -149,6 +151,52 @@ string16 ElideComponentizedPath(const string16& url_path_prefix, |
| } // namespace |
| +// Elides only the username portion of the email, if possible, so that the |
| +// overall email is at most max_email_length characters (e.g. |
| +// longusername@host.com --> longuser...@host.com for max_email_length == 20). |
| +// If the domain name by itself already has more characters than available, |
| +// we elide the domain name in the middle so that only max_email_length / 2 |
| +// characters remain. We then proceed with the regular eliding described above. |
| +// In any scenario: the string returned will never be longer than |
| +// max_email_length including the inserted ellipses; and the elided strings |
| +// will have at least one character remaining on either side of the '@'(other |
| +// than the ellipsis-es). |
| +// This function assumes max_email_length is >= 5. |
|
Alexei Svitkine (slow)
2012/02/28 15:46:22
It is sufficient to only include the comment in th
gab
2012/02/28 19:20:11
Ok, was mimicking ElideUrl which has the comment i
|
| +string16 ElideEmail(const string16& email, size_t max_email_length) { |
| + DCHECK_GE(max_email_length, size_t(5)); |
| + |
| + if (email.length() <= max_email_length) |
| + return email; |
| + |
| + std::vector<string16> email_split; |
| + base::SplitString(email, '@', &email_split); |
| + DCHECK_EQ(email_split.size(), size_t(2)); |
|
Alexei Svitkine (slow)
2012/02/28 15:46:22
We don't use constructor-style casts in the Chromi
|
| + string16& username = email_split.front(); |
| + string16& domain = email_split.back(); |
|
Alexei Svitkine (slow)
2012/02/28 15:46:22
Non-const references are discouraged - remove the
gab
2012/02/28 19:20:11
Ok, but this will force an implicit copy of the st
|
| + |
| + const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); |
| + const size_t kEllipsisLength = kEllipsisUTF16.length(); |
| + |
| + // Reserve characters for each of: username ellipsis, @ sign, and at least |
| + // one character remaining in the username. |
| + const size_t kAvailableDomainChars = max_email_length - kEllipsisLength - 2; |
|
Alexei Svitkine (slow)
2012/02/28 15:46:22
Rename to |available_domain_chars| since this is a
|
| + if (domain.length() > kAvailableDomainChars) { |
| + StringSlicer slicer(domain, kEllipsisUTF16, true); |
| + const size_t n = max_email_length / 2 - kEllipsisLength; |
|
Alexei Svitkine (slow)
2012/02/28 15:46:22
Nit: Rename |n| to |length|. Same thing in the blo
|
| + domain = slicer.CutString(n, true); |
| + } |
| + |
| + const size_t kFinalDomainLength = domain.length(); |
|
Alexei Svitkine (slow)
2012/02/28 15:46:22
Same comment as for kAvailableDomainChars above. A
gab
2012/02/28 19:20:11
|domain.length()| is shorter, but is a function (h
|
| + if (username.length() + kFinalDomainLength + 1 > max_email_length) { |
| + StringSlicer slicer(username, kEllipsisUTF16, false); |
| + const size_t n = max_email_length - kFinalDomainLength - |
| + kEllipsisLength - 1; |
| + username = slicer.CutString(n, true); |
| + } |
| + |
| + return username + ASCIIToUTF16("@") + domain; |
| +} |
| + |
| // This function takes a GURL object and elides it. It returns a string |
| // which composed of parts from subdomain, domain, path, filename and query. |
| // A "..." is added automatically at the end if the elided string is bigger |