0

Let JsonDoubleQuote accept StringPiece

So that the functions can escape 'const char*' or 'const char16*'
without creating a std::string or string16 object for the parameter.

BUG=none
TEST=StringEscapeTest.JsonDoubleQuoteNarrow

Review URL: https://chromiumcodereview.appspot.com/18648003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221625 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
wangxianzhu@chromium.org
2013-09-06 07:35:27 +00:00
parent 2847450d99
commit 7eac0c3f0a
3 changed files with 15 additions and 11 deletions

@ -78,25 +78,25 @@ void JsonDoubleQuoteT(const STR& str,
} // namespace
void JsonDoubleQuote(const std::string& str,
void JsonDoubleQuote(const StringPiece& str,
bool put_in_quotes,
std::string* dst) {
JsonDoubleQuoteT(str, put_in_quotes, dst);
}
std::string GetDoubleQuotedJson(const std::string& str) {
std::string GetDoubleQuotedJson(const StringPiece& str) {
std::string dst;
JsonDoubleQuote(str, true, &dst);
return dst;
}
void JsonDoubleQuote(const string16& str,
void JsonDoubleQuote(const StringPiece16& str,
bool put_in_quotes,
std::string* dst) {
JsonDoubleQuoteT(str, put_in_quotes, dst);
}
std::string GetDoubleQuotedJson(const string16& str) {
std::string GetDoubleQuotedJson(const StringPiece16& str) {
std::string dst;
JsonDoubleQuote(str, true, &dst);
return dst;

@ -10,7 +10,7 @@
#include <string>
#include "base/base_export.h"
#include "base/strings/string16.h"
#include "base/strings/string_piece.h"
namespace base {
@ -19,19 +19,19 @@ namespace base {
// If |put_in_quotes| is true, the result will be surrounded in double quotes.
// The outputted literal, when interpreted by the browser, should result in a
// javascript string that is identical and the same length as the input |str|.
BASE_EXPORT void JsonDoubleQuote(const std::string& str,
BASE_EXPORT void JsonDoubleQuote(const StringPiece& str,
bool put_in_quotes,
std::string* dst);
// Same as above, but always returns the result double quoted.
BASE_EXPORT std::string GetDoubleQuotedJson(const std::string& str);
BASE_EXPORT std::string GetDoubleQuotedJson(const StringPiece& str);
BASE_EXPORT void JsonDoubleQuote(const string16& str,
BASE_EXPORT void JsonDoubleQuote(const StringPiece16& str,
bool put_in_quotes,
std::string* dst);
// Same as above, but always returns the result double quoted.
BASE_EXPORT std::string GetDoubleQuotedJson(const string16& str);
BASE_EXPORT std::string GetDoubleQuotedJson(const StringPiece16& str);
} // namespace base

@ -25,9 +25,13 @@ const struct json_narrow_test_data {
TEST(StringEscapeTest, JsonDoubleQuoteNarrow) {
for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) {
std::string in = json_narrow_cases[i].to_escape;
const char* in_ptr = json_narrow_cases[i].to_escape;
std::string in_str = in_ptr;
std::string out;
JsonDoubleQuote(in, false, &out);
JsonDoubleQuote(in_ptr, false, &out);
EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out);
out.erase();
JsonDoubleQuote(in_str, false, &out);
EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out);
}