Commit 0a9188d9 authored by Daniel Cheng's avatar Daniel Cheng Committed by Commit Bot

Reuse base::EmptyString in GURL implementation.

Also update base::EmptyString to use base::NoDestructor.

Change-Id: I5004a7c4881af0dd7e499ccae59df4cab0369e87
Reviewed-on: https://chromium-review.googlesource.com/1162989Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580744}
parent 47c9d947
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/singleton.h" #include "base/no_destructor.h"
#include "base/strings/utf_string_conversion_utils.h" #include "base/strings/utf_string_conversion_utils.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/third_party/icu/icu_utf.h" #include "base/third_party/icu/icu_utf.h"
...@@ -32,19 +32,6 @@ namespace base { ...@@ -32,19 +32,6 @@ namespace base {
namespace { namespace {
// Force the singleton used by EmptyString[16] to be a unique type. This
// prevents other code that might accidentally use Singleton<string> from
// getting our internal one.
struct EmptyStrings {
EmptyStrings() = default;
const std::string s;
const string16 s16;
static EmptyStrings* GetInstance() {
return Singleton<EmptyStrings>::get();
}
};
// Used by ReplaceStringPlaceholders to track the position in the string of // Used by ReplaceStringPlaceholders to track the position in the string of
// replaced parameters. // replaced parameters.
struct ReplacementOffset { struct ReplacementOffset {
...@@ -238,11 +225,13 @@ bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b) { ...@@ -238,11 +225,13 @@ bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b) {
} }
const std::string& EmptyString() { const std::string& EmptyString() {
return EmptyStrings::GetInstance()->s; static const base::NoDestructor<std::string> s;
return *s;
} }
const string16& EmptyString16() { const string16& EmptyString16() {
return EmptyStrings::GetInstance()->s16; static const base::NoDestructor<string16> s16;
return *s16;
} }
template <class StringType> template <class StringType>
......
...@@ -147,8 +147,8 @@ BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b); ...@@ -147,8 +147,8 @@ BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b);
// strings. // strings.
// //
// It is likely faster to construct a new empty string object (just a few // 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 // instructions to set the length to 0) than to get the empty string instance
// returned by these functions (which requires threadsafe singleton access). // returned by these functions (which requires threadsafe static access).
// //
// Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT
// CONSTRUCTORS. There is only one case where you should use these: functions // CONSTRUCTORS. There is only one case where you should use these: functions
......
...@@ -19,8 +19,6 @@ ...@@ -19,8 +19,6 @@
namespace { namespace {
static base::LazyInstance<std::string>::Leaky empty_string =
LAZY_INSTANCE_INITIALIZER;
static base::LazyInstance<GURL>::Leaky empty_gurl = LAZY_INSTANCE_INITIALIZER; static base::LazyInstance<GURL>::Leaky empty_gurl = LAZY_INSTANCE_INITIALIZER;
} // namespace } // namespace
...@@ -166,7 +164,7 @@ const std::string& GURL::spec() const { ...@@ -166,7 +164,7 @@ const std::string& GURL::spec() const {
return spec_; return spec_;
DCHECK(false) << "Trying to get the spec of an invalid URL!"; DCHECK(false) << "Trying to get the spec of an invalid URL!";
return empty_string.Get(); return base::EmptyString();
} }
bool GURL::operator<(const GURL& other) const { bool GURL::operator<(const GURL& other) const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment