Skip to content

Commit

Permalink
Move EmptyString, kWhitespace and the BOM to base.
Browse files Browse the repository at this point in the history
This moves EmptyString*, kWhitespace*, and the UTF 8 Byte Order Marker to the base:: namespace.

Many of them just got changed to a default-constructed string when a reference was not required.

I qualified some string16s with base:: when I was changing adjacent code. I need to do another pass to finish these up.

BUG=
TBR=sky@chromium.org

Review URL: https://codereview.chromium.org/89243003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238032 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
brettw@chromium.org committed Dec 2, 2013
1 parent 05fd507 commit 8790210
Show file tree
Hide file tree
Showing 50 changed files with 125 additions and 111 deletions.
2 changes: 1 addition & 1 deletion ash/shell/launcher_delegate_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LauncherID LauncherDelegateImpl::GetLauncherIDForAppID(
}

const std::string& LauncherDelegateImpl::GetAppIDForLauncherID(LauncherID id) {
return EmptyString();
return base::EmptyString();
}

void LauncherDelegateImpl::PinAppWithID(const std::string& app_id) {
Expand Down
2 changes: 1 addition & 1 deletion ash/test/test_launcher_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ LauncherID TestLauncherDelegate::GetLauncherIDForAppID(
}

const std::string& TestLauncherDelegate::GetAppIDForLauncherID(LauncherID id) {
return EmptyString();
return base::EmptyString();
}

void TestLauncherDelegate::PinAppWithID(const std::string& app_id) {
Expand Down
17 changes: 8 additions & 9 deletions base/strings/string_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ bool IsWprintfFormatPortable(const wchar_t* format) {
return true;
}

} // namespace base


const std::string& EmptyString() {
return EmptyStrings::GetInstance()->s;
}
Expand All @@ -115,6 +112,8 @@ const string16& EmptyString16() {
return EmptyStrings::GetInstance()->s16;
}

} // namespace base

template<typename STR>
bool ReplaceCharsT(const STR& input,
const typename STR::value_type replace_chars[],
Expand Down Expand Up @@ -241,16 +240,16 @@ void TruncateUTF8ToByteSize(const std::string& input,
output->clear();
}

TrimPositions TrimWhitespace(const string16& input,
TrimPositions TrimWhitespace(const base::string16& input,
TrimPositions positions,
string16* output) {
return TrimStringT(input, kWhitespaceUTF16, positions, output);
base::string16* output) {
return TrimStringT(input, base::kWhitespaceUTF16, positions, output);
}

TrimPositions TrimWhitespaceASCII(const std::string& input,
TrimPositions positions,
std::string* output) {
return TrimStringT(input, kWhitespaceASCII, positions, output);
return TrimStringT(input, base::kWhitespaceASCII, positions, output);
}

// This function is only for backward-compatibility.
Expand Down Expand Up @@ -321,8 +320,8 @@ bool ContainsOnlyWhitespaceASCII(const std::string& str) {
return true;
}

bool ContainsOnlyWhitespace(const string16& str) {
return str.find_first_not_of(kWhitespaceUTF16) == string16::npos;
bool ContainsOnlyWhitespace(const base::string16& str) {
return str.find_first_not_of(base::kWhitespaceUTF16) == string16::npos;
}

template<typename STR>
Expand Down
41 changes: 24 additions & 17 deletions base/strings/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,34 +123,41 @@ template<typename Char> struct CaseInsensitiveCompareASCII {
}
};

} // namespace base

#if defined(OS_WIN)
#include "base/strings/string_util_win.h"
#elif defined(OS_POSIX)
#include "base/strings/string_util_posix.h"
#else
#error Define string operations appropriately for your platform
#endif

// These threadsafe functions return references to globally unique empty
// strings.
//
// DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT CONSTRUCTORS.
// There is only one case where you should use these: functions which need to
// return a string by reference (e.g. as a class member accessor), and don't
// have an empty string to use (e.g. in an error case). These should not be
// used as initializers, function arguments, or return values for functions
// which return by value or outparam.
// It is likely faster to construct a new empty string object (just a few
// instructions to set the length to 0) than to get the empty string singleton
// returned by these functions (which requires threadsafe singleton access).
//
// Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT
// CONSTRUCTORS. There is only one case where you should use these: functions
// which need to return a string by reference (e.g. as a class member
// accessor), and don't have an empty string to use (e.g. in an error case).
// These should not be used as initializers, function arguments, or return
// values for functions which return by value or outparam.
BASE_EXPORT const std::string& EmptyString();
BASE_EXPORT const string16& EmptyString16();

// Contains the set of characters representing whitespace in the corresponding
// encoding. Null-terminated.
BASE_EXPORT extern const wchar_t kWhitespaceWide[];
BASE_EXPORT extern const char16 kWhitespaceUTF16[];
BASE_EXPORT extern const char kWhitespaceASCII[];

// Null-terminated string representing the UTF-8 byte order mark.
BASE_EXPORT extern const char kUtf8ByteOrderMark[];

} // namespace base

#if defined(OS_WIN)
#include "base/strings/string_util_win.h"
#elif defined(OS_POSIX)
#include "base/strings/string_util_posix.h"
#else
#error Define string operations appropriately for your platform
#endif

// Removes characters in |remove_chars| from anywhere in |input|. Returns true
// if any characters were removed. |remove_chars| must be null-terminated.
// NOTE: Safe to use the same variable for both |input| and |output|.
Expand Down Expand Up @@ -370,7 +377,7 @@ inline Char HexDigitToInt(Char c) {

// Returns true if it's a whitespace character.
inline bool IsWhitespace(wchar_t c) {
return wcschr(kWhitespaceWide, c) != NULL;
return wcschr(base::kWhitespaceWide, c) != NULL;
}

// Return a byte string in human-readable format with a unit suffix. Not
Expand Down
4 changes: 4 additions & 0 deletions base/strings/string_util_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "base/strings/string_util.h"

namespace base {

#define WHITESPACE_UNICODE \
0x0009, /* <control-0009> to <control-000D> */ \
0x000A, \
Expand Down Expand Up @@ -53,3 +55,5 @@ const char kWhitespaceASCII[] = {
};

const char kUtf8ByteOrderMark[] = "\xEF\xBB\xBF";

} // namespace base
8 changes: 4 additions & 4 deletions chrome/browser/autocomplete/autocomplete_input.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ std::string AutocompleteInput::TypeToString(Type type) {

// static
AutocompleteInput::Type AutocompleteInput::Parse(
const string16& text,
const string16& desired_tld,
const base::string16& text,
const base::string16& desired_tld,
url_parse::Parsed* parts,
string16* scheme,
base::string16* scheme,
GURL* canonicalized_url) {
const size_t first_non_white = text.find_first_not_of(kWhitespaceUTF16, 0);
size_t first_non_white = text.find_first_not_of(base::kWhitespaceUTF16, 0);
if (first_non_white == string16::npos)
return INVALID; // All whitespace.

Expand Down
5 changes: 3 additions & 2 deletions chrome/browser/autocomplete/keyword_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ string16 KeywordProvider::SplitKeywordFromInput(
string16* remaining_input) {
// Find end of first token. The AutocompleteController has trimmed leading
// whitespace, so we need not skip over that.
const size_t first_white(input.find_first_of(kWhitespaceUTF16));
const size_t first_white(input.find_first_of(base::kWhitespaceUTF16));
DCHECK_NE(0U, first_white);
if (first_white == string16::npos)
return input; // Only one token provided.

// Set |remaining_input| to everything after the first token.
DCHECK(remaining_input != NULL);
const size_t remaining_start = trim_leading_whitespace ?
input.find_first_not_of(kWhitespaceUTF16, first_white) : first_white + 1;
input.find_first_not_of(base::kWhitespaceUTF16, first_white) :
first_white + 1;

if (remaining_start < input.length())
remaining_input->assign(input.begin() + remaining_start, input.end());
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/background/background_contents_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ const string16& BackgroundContentsService::GetParentApplicationId(
if (contents == it->second.contents)
return it->first;
}
return EmptyString16();
return base::EmptyString16();
}

void BackgroundContentsService::AddWebContents(
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/chromeos/imageburner/burn_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ const std::string& ConfigFile::GetProperty(
if (property != block_it->properties.end()) {
return property->second;
} else {
return EmptyString();
return base::EmptyString();
}
}
}

return EmptyString();
return base::EmptyString();
}

// Check if last block has a hwid associated with it, and erase it if it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class DeviceSettingsTestHelper : public SessionManagerClient {
const std::map<std::string, PolicyState>::const_iterator entry =
device_local_account_policy_.find(id);
return entry == device_local_account_policy_.end() ?
EmptyString() : entry->second.policy_blob_;
base::EmptyString() : entry->second.policy_blob_;
}

void set_device_local_account_policy_blob(const std::string& id,
Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/drive/drive_api_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ std::string TranslateQuery(const std::string& original_query) {
// In order to handle non-ascii white spaces correctly, convert to UTF16.
base::string16 query = UTF8ToUTF16(original_query);
const base::string16 kDelimiter(
kWhitespaceUTF16 + base::string16(1, static_cast<char16>('"')));
base::kWhitespaceUTF16 + base::string16(1, static_cast<char16>('"')));

std::string result;
for (size_t index = query.find_first_not_of(kWhitespaceUTF16);
for (size_t index = query.find_first_not_of(base::kWhitespaceUTF16);
index != base::string16::npos;
index = query.find_first_not_of(kWhitespaceUTF16, index)) {
index = query.find_first_not_of(base::kWhitespaceUTF16, index)) {
bool is_exclusion = (query[index] == '-');
if (is_exclusion)
++index;
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/extensions/user_script_master.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ static bool LoadScriptContent(UserScript::File* script_file,
}

// Remove BOM from the content.
std::string::size_type index = content.find(kUtf8ByteOrderMark);
std::string::size_type index = content.find(base::kUtf8ByteOrderMark);
if (index == 0) {
script_file->set_content(content.substr(strlen(kUtf8ByteOrderMark)));
script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark)));
} else {
script_file->set_content(content);
}
Expand Down
3 changes: 2 additions & 1 deletion chrome/browser/history/url_index_private_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms(
// escaped whitespace. When the user types "colspec=ID%20Mstone Release" we
// get two 'terms': "colspec=id%20mstone" and "release".
history::String16Vector lower_raw_terms;
if (Tokenize(lower_raw_string, kWhitespaceUTF16, &lower_raw_terms) == 0) {
if (Tokenize(lower_raw_string, base::kWhitespaceUTF16,
&lower_raw_terms) == 0) {
// Don't score matches when there are no terms to score against. (It's
// possible that the word break iterater that extracts words to search
// for in the database allows some whitespace "words" whereas Tokenize
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/metrics/metrics_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -860,9 +860,9 @@ void MetricsLog::RecordProfilerData(
void MetricsLog::RecordOmniboxOpenedURL(const OmniboxLog& log) {
DCHECK(!locked());

std::vector<string16> terms;
std::vector<base::string16> terms;
const int num_terms =
static_cast<int>(Tokenize(log.text, kWhitespaceUTF16, &terms));
static_cast<int>(Tokenize(log.text, base::kWhitespaceUTF16, &terms));

OmniboxEventProto* omnibox_event = uma_proto()->add_omnibox_event();
omnibox_event->set_time(MetricsLogBase::GetCurrentTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const std::string& ComponentCloudPolicyStore::GetCachedHash(
DCHECK(CalledOnValidThread());
std::map<PolicyNamespace, std::string>::const_iterator it =
cached_hashes_.find(ns);
return it == cached_hashes_.end() ? EmptyString() : it->second;
return it == cached_hashes_.end() ? base::EmptyString() : it->second;
}

void ComponentCloudPolicyStore::SetCredentials(const std::string& username,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class MockRequestJobBase : public DeviceManagementRequestJob {
return entry->second;
}

return EmptyString();
return base::EmptyString();
}

MockDeviceManagementService* service_;
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/signin/token_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ bool TokenService::HasTokenForService(const char* service) const {

const std::string& TokenService::GetTokenForService(
const char* const service) const {
return EmptyString();
return base::EmptyString();
}

bool TokenService::HasOAuthLoginToken() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ void MultiUserWindowManagerChromeOS::SetWindowOwner(
const std::string& MultiUserWindowManagerChromeOS::GetWindowOwner(
aura::Window* window) {
WindowToEntryMap::iterator it = window_to_entry_.find(window);
return it != window_to_entry_.end() ? it->second->owner() : EmptyString();
return it != window_to_entry_.end() ? it->second->owner()
: base::EmptyString();
}

void MultiUserWindowManagerChromeOS::ShowWindowForUser(
Expand Down Expand Up @@ -264,7 +265,7 @@ const std::string& MultiUserWindowManagerChromeOS::GetUserPresentingWindow(
// If the window is not owned by anyone it is shown on all desktops and we
// return the empty string.
if (it == window_to_entry_.end())
return EmptyString();
return base::EmptyString();
// Otherwise we ask the object for its desktop.
return it->second->show_for_user();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void MultiUserWindowManagerStub::SetWindowOwner(aura::Window* window,

const std::string& MultiUserWindowManagerStub::GetWindowOwner(
aura::Window* window) {
return EmptyString();
return base::EmptyString();
}

void MultiUserWindowManagerStub::ShowWindowForUser(aura::Window* window,
Expand All @@ -36,7 +36,7 @@ bool MultiUserWindowManagerStub::IsWindowOnDesktopOfUser(

const std::string& MultiUserWindowManagerStub::GetUserPresentingWindow(
aura::Window* window) {
return EmptyString();
return base::EmptyString();
}

void MultiUserWindowManagerStub::AddUser(Profile* profile) {
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ NSRange ComponentToNSRange(const url_parse::Component& component) {
// We need to do this first, else |SetSelectedRange()| won't work.
FocusLocation(true);

const string16 current_text(GetText());
const size_t start = current_text.find_first_not_of(kWhitespaceUTF16);
const base::string16 current_text(GetText());
const size_t start = current_text.find_first_not_of(base::kWhitespaceUTF16);
if (start == string16::npos || (current_text[start] != '?')) {
SetUserText(ASCIIToUTF16("?"));
} else {
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,8 @@ void OmniboxViewGtk::SetWindowTextAndCaretPos(const string16& text,
}

void OmniboxViewGtk::SetForcedQuery() {
const string16 current_text(GetText());
const size_t start = current_text.find_first_not_of(kWhitespaceUTF16);
const base::string16 current_text(GetText());
const size_t start = current_text.find_first_not_of(base::kWhitespaceUTF16);
if (start == string16::npos || (current_text[start] != '?')) {
SetUserText(ASCIIToUTF16("?"));
} else {
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/ui/omnibox/location_bar_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace location_bar_util {
string16 CalculateMinString(const string16& description) {
// Chop at the first '.' or whitespace.
const size_t dot_index = description.find('.');
const size_t ws_index = description.find_first_of(kWhitespaceUTF16);
const size_t ws_index = description.find_first_of(base::kWhitespaceUTF16);
size_t chop_index = std::min(dot_index, ws_index);
string16 min_string;
if (chop_index == string16::npos) {
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/views/omnibox/omnibox_view_views.cc
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ void OmniboxViewViews::SetWindowTextAndCaretPos(const string16& text,
}

void OmniboxViewViews::SetForcedQuery() {
const string16 current_text(text());
const size_t start = current_text.find_first_not_of(kWhitespaceUTF16);
const base::string16 current_text(text());
const size_t start = current_text.find_first_not_of(base::kWhitespaceUTF16);
if (start == string16::npos || (current_text[start] != '?'))
OmniboxView::SetUserText(ASCIIToUTF16("?"));
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace errors = manifest_errors;
const std::string& LocaleInfo::GetDefaultLocale(const Extension* extension) {
LocaleInfo* info = static_cast<LocaleInfo*>(
extension->GetManifestData(keys::kDefaultLocale));
return info ? info->default_locale : EmptyString();
return info ? info->default_locale : base::EmptyString();
}

DefaultLocaleHandler::DefaultLocaleHandler() {
Expand Down
2 changes: 1 addition & 1 deletion chrome/common/extensions/api/omnibox/omnibox_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const char kKeyword[] = "keyword";
const std::string& OmniboxInfo::GetKeyword(const Extension* extension) {
OmniboxInfo* info = static_cast<OmniboxInfo*>(
extension->GetManifestData(manifest_keys::kOmnibox));
return info ? info->keyword : EmptyString();
return info ? info->keyword : base::EmptyString();
}

OmniboxHandler::OmniboxHandler() {
Expand Down
Loading

0 comments on commit 8790210

Please sign in to comment.