Rename typefdefs for C++17
These typedefs are no longer necessary:
base::char16 -> char16_t
base::string16 -> std::u16string
base::StringPiece -> std::string_view
base::StringPiece16 -> std::u16string_view
There should be no behavior change since the objects are already the
C++17 types.
Change-Id: I482a9371bc668e72d12811c17b4f1128715ea62a
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/6060
Reviewed-by: Scott Graham <scottmg@chromium.org>
Commit-Queue: Brett Wilson <brettw@chromium.org>
diff --git a/base/json/json_parser.cc b/base/json/json_parser.cc
index 713f692..eac52a6 100644
--- a/base/json/json_parser.cc
+++ b/base/json/json_parser.cc
@@ -5,6 +5,7 @@
#include "base/json/json_parser.h"
#include <cmath>
+#include <string_view>
#include <utility>
#include <vector>
@@ -12,7 +13,6 @@
#include "base/macros.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
-#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversion_utils.h"
@@ -68,7 +68,7 @@
JSONParser::~JSONParser() = default;
-Optional<Value> JSONParser::Parse(StringPiece input) {
+Optional<Value> JSONParser::Parse(std::string_view input) {
input_ = input;
index_ = 0;
line_number_ = 1;
@@ -163,30 +163,30 @@
// JSONParser private //////////////////////////////////////////////////////////
-Optional<StringPiece> JSONParser::PeekChars(int count) {
+Optional<std::string_view> JSONParser::PeekChars(int count) {
if (static_cast<size_t>(index_) + count > input_.length())
return nullopt;
- // Using StringPiece::substr() is significantly slower (according to
+ // Using std::string_view::substr() is significantly slower (according to
// base_perftests) than constructing a substring manually.
- return StringPiece(input_.data() + index_, count);
+ return std::string_view(input_.data() + index_, count);
}
Optional<char> JSONParser::PeekChar() {
- Optional<StringPiece> chars = PeekChars(1);
+ Optional<std::string_view> chars = PeekChars(1);
if (chars)
return (*chars)[0];
return nullopt;
}
-Optional<StringPiece> JSONParser::ConsumeChars(int count) {
- Optional<StringPiece> chars = PeekChars(count);
+Optional<std::string_view> JSONParser::ConsumeChars(int count) {
+ Optional<std::string_view> chars = PeekChars(count);
if (chars)
index_ += count;
return chars;
}
Optional<char> JSONParser::ConsumeChar() {
- Optional<StringPiece> chars = ConsumeChars(1);
+ Optional<std::string_view> chars = ConsumeChars(1);
if (chars)
return (*chars)[0];
return nullopt;
@@ -268,7 +268,7 @@
}
bool JSONParser::EatComment() {
- Optional<StringPiece> comment_start = ConsumeChars(2);
+ Optional<std::string_view> comment_start = ConsumeChars(2);
if (!comment_start)
return false;
@@ -444,7 +444,7 @@
return false;
}
- // StringBuilder will internally build a StringPiece unless a UTF-16
+ // StringBuilder will internally build a std::string_view unless a UTF-16
// conversion occurs, at which point it will perform a copy into a
// std::string.
StringBuilder string(pos());
@@ -475,12 +475,12 @@
} else {
// And if it is an escape sequence, the input string will be adjusted
// (either by combining the two characters of an encoded escape sequence,
- // or with a UTF conversion), so using StringPiece isn't possible -- force
- // a conversion.
+ // or with a UTF conversion), so using std::string_view isn't possible --
+ // force a conversion.
string.Convert();
// Read past the escape '\' and ensure there's a character following.
- Optional<StringPiece> escape_sequence = ConsumeChars(2);
+ Optional<std::string_view> escape_sequence = ConsumeChars(2);
if (!escape_sequence) {
ReportError(JSONReader::JSON_INVALID_ESCAPE, 0);
return false;
@@ -558,7 +558,7 @@
// Entry is at the first X in \uXXXX.
bool JSONParser::DecodeUTF16(uint32_t* out_code_point) {
- Optional<StringPiece> escape_sequence = ConsumeChars(4);
+ Optional<std::string_view> escape_sequence = ConsumeChars(4);
if (!escape_sequence)
return false;
@@ -671,7 +671,7 @@
index_ = exit_index;
- StringPiece num_string(num_start, end_index - start_index);
+ std::string_view num_string(num_start, end_index - start_index);
int num_int;
if (StringToInt(num_string, &num_int))
@@ -717,7 +717,7 @@
}
}
-bool JSONParser::ConsumeIfMatch(StringPiece match) {
+bool JSONParser::ConsumeIfMatch(std::string_view match) {
if (match == PeekChars(match.size())) {
ConsumeChars(match.size());
return true;
diff --git a/base/json/json_parser.h b/base/json/json_parser.h
index 1289560..6bd61a4 100644
--- a/base/json/json_parser.h
+++ b/base/json/json_parser.h
@@ -10,13 +10,13 @@
#include <memory>
#include <string>
+#include <string_view>
#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "base/json/json_reader.h"
#include "base/macros.h"
#include "base/optional.h"
-#include "base/strings/string_piece.h"
namespace base {
@@ -49,7 +49,7 @@
// result as a Value.
// Wrap this in base::FooValue::From() to check the Value is of type Foo and
// convert to a FooValue at the same time.
- Optional<Value> Parse(StringPiece input);
+ Optional<Value> Parse(std::string_view input);
// Returns the error code.
JSONReader::JsonParseError error_code() const;
@@ -83,7 +83,7 @@
};
// A helper class used for parsing strings. One optimization performed is to
- // create base::Value with a StringPiece to avoid unnecessary std::string
+ // create base::Value with a std::string_view to avoid unnecessary std::string
// copies. This is not possible if the input string needs to be decoded from
// UTF-16 to UTF-8, or if an escape sequence causes characters to be skipped.
// This class centralizes that logic.
@@ -104,9 +104,9 @@
// converted, or by appending the UTF8 bytes for the code point.
void Append(uint32_t point);
- // Converts the builder from its default StringPiece to a full std::string,
- // performing a copy. Once a builder is converted, it cannot be made a
- // StringPiece again.
+ // Converts the builder from its default std::string_view to a full
+ // std::string, performing a copy. Once a builder is converted, it cannot be
+ // made a std::string_view again.
void Convert();
// Returns the builder as a string, invalidating all state. This allows
@@ -128,14 +128,14 @@
// Returns the next |count| bytes of the input stream, or nullopt if fewer
// than |count| bytes remain.
- Optional<StringPiece> PeekChars(int count);
+ Optional<std::string_view> PeekChars(int count);
// Calls PeekChars() with a |count| of 1.
Optional<char> PeekChar();
// Returns the next |count| bytes of the input stream, or nullopt if fewer
// than |count| bytes remain, and advances the parser position by |count|.
- Optional<StringPiece> ConsumeChars(int count);
+ Optional<std::string_view> ConsumeChars(int count);
// Calls ConsumeChars() with a |count| of 1.
Optional<char> ConsumeChar();
@@ -198,7 +198,7 @@
// consumed at the current parser position. Returns false if there are fewer
// than |match|-length bytes or if the sequence does not match, and the
// parser state is unchanged.
- bool ConsumeIfMatch(StringPiece match);
+ bool ConsumeIfMatch(std::string_view match);
// Sets the error information to |code| at the current column, based on
// |index_| and |index_last_line_|, with an optional positive/negative
@@ -218,7 +218,7 @@
const int max_depth_;
// The input stream being parsed. Note: Not guaranteed to NUL-terminated.
- StringPiece input_;
+ std::string_view input_;
// The index in the input stream to which the parser is wound.
int index_;
diff --git a/base/json/json_reader.cc b/base/json/json_reader.cc
index 3d5475e..d7eb7d9 100644
--- a/base/json/json_reader.cc
+++ b/base/json/json_reader.cc
@@ -41,7 +41,7 @@
JSONReader::~JSONReader() = default;
// static
-std::unique_ptr<Value> JSONReader::Read(StringPiece json,
+std::unique_ptr<Value> JSONReader::Read(std::string_view json,
int options,
int max_depth) {
internal::JSONParser parser(options, max_depth);
@@ -51,7 +51,7 @@
// static
std::unique_ptr<Value> JSONReader::ReadAndReturnError(
- StringPiece json,
+ std::string_view json,
int options,
int* error_code_out,
std::string* error_msg_out,
@@ -103,7 +103,7 @@
return std::string();
}
-std::unique_ptr<Value> JSONReader::ReadToValue(StringPiece json) {
+std::unique_ptr<Value> JSONReader::ReadToValue(std::string_view json) {
Optional<Value> value = parser_->Parse(json);
return value ? std::make_unique<Value>(std::move(*value)) : nullptr;
}
diff --git a/base/json/json_reader.h b/base/json/json_reader.h
index c4f7c8f..c454981 100644
--- a/base/json/json_reader.h
+++ b/base/json/json_reader.h
@@ -30,8 +30,7 @@
#include <memory>
#include <string>
-
-#include "base/strings/string_piece.h"
+#include <string_view>
namespace base {
@@ -94,7 +93,7 @@
// If |json| is not a properly formed JSON string, returns nullptr.
// Wrap this in base::FooValue::From() to check the Value is of type Foo and
// convert to a FooValue at the same time.
- static std::unique_ptr<Value> Read(StringPiece json,
+ static std::unique_ptr<Value> Read(std::string_view json,
int options = JSON_PARSE_RFC,
int max_depth = kStackMaxDepth);
@@ -103,7 +102,7 @@
// an error code and a formatted error message (including error location if
// appropriate). Otherwise, they will be unmodified.
static std::unique_ptr<Value> ReadAndReturnError(
- StringPiece json,
+ std::string_view json,
int options, // JSONParserOptions
int* error_code_out,
std::string* error_msg_out,
@@ -115,7 +114,7 @@
static std::string ErrorCodeToString(JsonParseError error_code);
// Non-static version of Read() above.
- std::unique_ptr<Value> ReadToValue(StringPiece json);
+ std::unique_ptr<Value> ReadToValue(std::string_view json);
// Returns the error code if the last call to ReadToValue() failed.
// Returns JSON_NO_ERROR otherwise.
diff --git a/base/json/json_value_converter.cc b/base/json/json_value_converter.cc
index 55506e6..ee7b094 100644
--- a/base/json/json_value_converter.cc
+++ b/base/json/json_value_converter.cc
@@ -17,8 +17,8 @@
return value.GetAsString(field);
}
-bool BasicValueConverter<string16>::Convert(const base::Value& value,
- string16* field) const {
+bool BasicValueConverter<std::u16string>::Convert(const base::Value& value,
+ std::u16string* field) const {
return value.GetAsString(field);
}
diff --git a/base/json/json_value_converter.h b/base/json/json_value_converter.h
index d91bead..f3030f2 100644
--- a/base/json/json_value_converter.h
+++ b/base/json/json_value_converter.h
@@ -9,13 +9,12 @@
#include <memory>
#include <string>
+#include <string_view>
#include <vector>
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
-#include "base/strings/string16.h"
-#include "base/strings/string_piece.h"
#include "base/values.h"
// JSONValueConverter converts a JSON value into a C++ struct in a
@@ -70,8 +69,8 @@
//
// Sometimes JSON format uses string representations for other types such
// like enum, timestamp, or URL. You can use RegisterCustomField method
-// and specify a function to convert a StringPiece to your type.
-// bool ConvertFunc(StringPiece s, YourEnum* result) {
+// and specify a function to convert a std::string_view to your type.
+// bool ConvertFunc(std::string_view s, YourEnum* result) {
// // do something and return true if succeed...
// }
// struct Message {
@@ -158,11 +157,12 @@
};
template <>
-class BasicValueConverter<string16> : public ValueConverter<string16> {
+class BasicValueConverter<std::u16string>
+ : public ValueConverter<std::u16string> {
public:
BasicValueConverter() = default;
- bool Convert(const base::Value& value, string16* field) const override;
+ bool Convert(const base::Value& value, std::u16string* field) const override;
private:
DISALLOW_COPY_AND_ASSIGN(BasicValueConverter);
@@ -211,7 +211,7 @@
template <typename FieldType>
class CustomFieldConverter : public ValueConverter<FieldType> {
public:
- typedef bool (*ConvertFunc)(StringPiece value, FieldType* field);
+ typedef bool (*ConvertFunc)(std::string_view value, FieldType* field);
explicit CustomFieldConverter(ConvertFunc convert_func)
: convert_func_(convert_func) {}
@@ -367,10 +367,11 @@
}
void RegisterStringField(const std::string& field_name,
- string16 StructType::*field) {
+ std::u16string StructType::*field) {
fields_.push_back(
- std::make_unique<internal::FieldConverter<StructType, string16>>(
- field_name, field, new internal::BasicValueConverter<string16>));
+ std::make_unique<internal::FieldConverter<StructType, std::u16string>>(
+ field_name, field,
+ new internal::BasicValueConverter<std::u16string>));
}
void RegisterBoolField(const std::string& field_name,
@@ -398,7 +399,7 @@
template <typename FieldType>
void RegisterCustomField(const std::string& field_name,
FieldType StructType::*field,
- bool (*convert_func)(StringPiece, FieldType*)) {
+ bool (*convert_func)(std::string_view, FieldType*)) {
fields_.push_back(
std::make_unique<internal::FieldConverter<StructType, FieldType>>(
field_name, field,
@@ -436,10 +437,12 @@
void RegisterRepeatedString(
const std::string& field_name,
- std::vector<std::unique_ptr<string16>> StructType::*field) {
- fields_.push_back(std::make_unique<internal::FieldConverter<
- StructType, std::vector<std::unique_ptr<string16>>>>(
- field_name, field, new internal::RepeatedValueConverter<string16>));
+ std::vector<std::unique_ptr<std::u16string>> StructType::*field) {
+ fields_.push_back(
+ std::make_unique<internal::FieldConverter<
+ StructType, std::vector<std::unique_ptr<std::u16string>>>>(
+ field_name, field,
+ new internal::RepeatedValueConverter<std::u16string>));
}
void RegisterRepeatedDouble(
diff --git a/base/json/string_escape.cc b/base/json/string_escape.cc
index 471a9d3..ea674f5 100644
--- a/base/json/string_escape.cc
+++ b/base/json/string_escape.cc
@@ -116,38 +116,41 @@
} // namespace
-bool EscapeJSONString(StringPiece str, bool put_in_quotes, std::string* dest) {
- return EscapeJSONStringImpl(str, put_in_quotes, dest);
-}
-
-bool EscapeJSONString(StringPiece16 str,
+bool EscapeJSONString(std::string_view str,
bool put_in_quotes,
std::string* dest) {
return EscapeJSONStringImpl(str, put_in_quotes, dest);
}
-std::string GetQuotedJSONString(StringPiece str) {
+bool EscapeJSONString(std::u16string_view str,
+ bool put_in_quotes,
+ std::string* dest) {
+ return EscapeJSONStringImpl(str, put_in_quotes, dest);
+}
+
+std::string GetQuotedJSONString(std::string_view str) {
std::string dest;
bool ok = EscapeJSONStringImpl(str, true, &dest);
DCHECK(ok);
return dest;
}
-std::string GetQuotedJSONString(StringPiece16 str) {
+std::string GetQuotedJSONString(std::u16string_view str) {
std::string dest;
bool ok = EscapeJSONStringImpl(str, true, &dest);
DCHECK(ok);
return dest;
}
-std::string EscapeBytesAsInvalidJSONString(StringPiece str,
+std::string EscapeBytesAsInvalidJSONString(std::string_view str,
bool put_in_quotes) {
std::string dest;
if (put_in_quotes)
dest.push_back('"');
- for (StringPiece::const_iterator it = str.begin(); it != str.end(); ++it) {
+ for (std::string_view::const_iterator it = str.begin(); it != str.end();
+ ++it) {
unsigned char c = *it;
if (EscapeSpecialCodePoint(c, &dest))
continue;
diff --git a/base/json/string_escape.h b/base/json/string_escape.h
index d318b6a..ada5179 100644
--- a/base/json/string_escape.h
+++ b/base/json/string_escape.h
@@ -8,8 +8,7 @@
#define BASE_JSON_STRING_ESCAPE_H_
#include <string>
-
-#include "base/strings/string_piece.h"
+#include <string_view>
namespace base {
@@ -25,17 +24,21 @@
//
// If |put_in_quotes| is true, then a leading and trailing double-quote mark
// will be appended to |dest| as well.
-bool EscapeJSONString(StringPiece str, bool put_in_quotes, std::string* dest);
+bool EscapeJSONString(std::string_view str,
+ bool put_in_quotes,
+ std::string* dest);
-// Performs a similar function to the UTF-8 StringPiece version above,
+// Performs a similar function to the UTF-8 std::string_view version above,
// converting UTF-16 code units to UTF-8 code units and escaping non-printing
// control characters. On return, |dest| will contain a valid UTF-8 JSON string.
-bool EscapeJSONString(StringPiece16 str, bool put_in_quotes, std::string* dest);
+bool EscapeJSONString(std::u16string_view str,
+ bool put_in_quotes,
+ std::string* dest);
// Helper functions that wrap the above two functions but return the value
// instead of appending. |put_in_quotes| is always true.
-std::string GetQuotedJSONString(StringPiece str);
-std::string GetQuotedJSONString(StringPiece16 str);
+std::string GetQuotedJSONString(std::string_view str);
+std::string GetQuotedJSONString(std::u16string_view str);
// Given an arbitrary byte string |str|, this will escape all non-ASCII bytes
// as \uXXXX escape sequences. This function is *NOT* meant to be used with
@@ -48,7 +51,8 @@
//
// The output of this function takes the *appearance* of JSON but is not in
// fact valid according to RFC 4627.
-std::string EscapeBytesAsInvalidJSONString(StringPiece str, bool put_in_quotes);
+std::string EscapeBytesAsInvalidJSONString(std::string_view str,
+ bool put_in_quotes);
} // namespace base