diff options
author | mgiuca <mgiuca@chromium.org> | 2015-02-04 23:31:18 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-02-05 07:32:17 +0000 |
commit | 77752c3e55c59747a62a362799ac6de5ff56fd45 (patch) | |
tree | 3335837d27339353c3dbef87320aadd90c0df664 /url | |
parent | bf7077883810ce30907c2d8e68bd50c1a5853af1 (diff) | |
download | chromium_src-77752c3e55c59747a62a362799ac6de5ff56fd45.zip chromium_src-77752c3e55c59747a62a362799ac6de5ff56fd45.tar.gz chromium_src-77752c3e55c59747a62a362799ac6de5ff56fd45.tar.bz2 |
GURL::Replacements methods accept a StringPiece instead of std::string&.
Previously, it was unsafe to pass a char* because it would be implicitly
converted to a string with a very short lifetime. Now, you can safely
pass a char* as long as the memory pointed to by the char* outlives the
Replacements (which is always true for string literals).
All existing usage of Replacements should continue to work as usual.
Updated every call to Replacements Set*Str methods that that could be
simplified by the new StringPiece API (for example, passing string
literals directly, instead of copying them into a string object).
Internally, renamed StdStringReplacements to StringPieceReplacements.
BUG=454274
Review URL: https://codereview.chromium.org/889463003
Cr-Commit-Position: refs/heads/master@{#314771}
Diffstat (limited to 'url')
-rw-r--r-- | url/gurl.h | 4 | ||||
-rw-r--r-- | url/url_canon_stdstring.h | 25 |
2 files changed, 15 insertions, 14 deletions
@@ -18,8 +18,8 @@ class URL_EXPORT GURL { public: - typedef url::StdStringReplacements<std::string> Replacements; - typedef url::StdStringReplacements<base::string16> ReplacementsW; + typedef url::StringPieceReplacements<std::string> Replacements; + typedef url::StringPieceReplacements<base::string16> ReplacementsW; // Creates an empty, invalid URL. GURL(); diff --git a/url/url_canon_stdstring.h b/url/url_canon_stdstring.h index c3d8ba1..662cac7 100644 --- a/url/url_canon_stdstring.h +++ b/url/url_canon_stdstring.h @@ -12,6 +12,7 @@ #include <string> #include "base/compiler_specific.h" +#include "base/strings/string_piece.h" #include "url/url_canon.h" #include "url/url_export.h" @@ -48,35 +49,35 @@ class URL_EXPORT StdStringCanonOutput : public CanonOutput { }; // An extension of the Replacements class that allows the setters to use -// standard strings. +// StringPieces (implicitly allowing strings or char*s). // -// The strings passed as arguments are not copied and must remain valid until -// this class goes out of scope. +// The contents of the StringPieces are not copied and must remain valid until +// the StringPieceReplacements object goes out of scope. template<typename STR> -class StdStringReplacements : public Replacements<typename STR::value_type> { +class StringPieceReplacements : public Replacements<typename STR::value_type> { public: - void SetSchemeStr(const STR& s) { + void SetSchemeStr(const base::BasicStringPiece<STR>& s) { this->SetScheme(s.data(), Component(0, static_cast<int>(s.length()))); } - void SetUsernameStr(const STR& s) { + void SetUsernameStr(const base::BasicStringPiece<STR>& s) { this->SetUsername(s.data(), Component(0, static_cast<int>(s.length()))); } - void SetPasswordStr(const STR& s) { + void SetPasswordStr(const base::BasicStringPiece<STR>& s) { this->SetPassword(s.data(), Component(0, static_cast<int>(s.length()))); } - void SetHostStr(const STR& s) { + void SetHostStr(const base::BasicStringPiece<STR>& s) { this->SetHost(s.data(), Component(0, static_cast<int>(s.length()))); } - void SetPortStr(const STR& s) { + void SetPortStr(const base::BasicStringPiece<STR>& s) { this->SetPort(s.data(), Component(0, static_cast<int>(s.length()))); } - void SetPathStr(const STR& s) { + void SetPathStr(const base::BasicStringPiece<STR>& s) { this->SetPath(s.data(), Component(0, static_cast<int>(s.length()))); } - void SetQueryStr(const STR& s) { + void SetQueryStr(const base::BasicStringPiece<STR>& s) { this->SetQuery(s.data(), Component(0, static_cast<int>(s.length()))); } - void SetRefStr(const STR& s) { + void SetRefStr(const base::BasicStringPiece<STR>& s) { this->SetRef(s.data(), Component(0, static_cast<int>(s.length()))); } }; |