diff --git a/.bazelrc b/.bazelrc index f93facf00a8c..732859d082ba 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1 +1 @@ -build --cxxopt=-std=c++14 +build --cxxopt=-std=c++14 \ No newline at end of file diff --git a/cmake/abseil-cpp.cmake b/cmake/abseil-cpp.cmake index 94c25de863bd..b8dd22d8eec0 100644 --- a/cmake/abseil-cpp.cmake +++ b/cmake/abseil-cpp.cmake @@ -34,7 +34,10 @@ set(protobuf_ABSL_USED_TARGETS absl::base absl::bind_front absl::cleanup + absl::cord + absl::core_headers absl::debugging + absl::dynamic_annotations absl::flags absl::flat_hash_map absl::flat_hash_set diff --git a/conformance/BUILD.bazel b/conformance/BUILD.bazel index d29860188c9f..0c37e159ed64 100644 --- a/conformance/BUILD.bazel +++ b/conformance/BUILD.bazel @@ -155,6 +155,7 @@ cc_library( ":jsoncpp", ":test_messages_proto2_proto_cc", ":test_messages_proto3_proto_cc", + "@com_google_absl//absl/status", ], ) @@ -190,6 +191,8 @@ cc_binary( "//:protobuf", "//:test_messages_proto2_cc_proto", "//:test_messages_proto3_cc_proto", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", ], ) diff --git a/conformance/binary_json_conformance_suite.cc b/conformance/binary_json_conformance_suite.cc index 3719a527aab7..52443ab0fc5f 100644 --- a/conformance/binary_json_conformance_suite.cc +++ b/conformance/binary_json_conformance_suite.cc @@ -34,11 +34,11 @@ #include #include #include +#include "absl/status/status.h" #include "third_party/jsoncpp/json.h" #include "conformance_test.h" #include #include -#include namespace proto2_messages = protobuf_test_messages::proto2; @@ -354,7 +354,7 @@ bool BinaryAndJsonConformanceSuite::ParseJsonResponse( const ConformanceResponse& response, Message* test_message) { string binary_protobuf; - util::Status status = + absl::Status status = JsonToBinaryString(type_resolver_.get(), type_url_, response.json_payload(), &binary_protobuf); @@ -384,7 +384,7 @@ bool BinaryAndJsonConformanceSuite::ParseResponse( case ConformanceResponse::kProtobufPayload: { if (requested_output != conformance::PROTOBUF) { ReportFailure(test_name, level, request, response, - StrCat("Test was asked for ", + absl::StrCat("Test was asked for ", WireFormatToString(requested_output), " output but provided PROTOBUF instead.") .c_str()); @@ -403,7 +403,7 @@ bool BinaryAndJsonConformanceSuite::ParseResponse( case ConformanceResponse::kJsonPayload: { if (requested_output != conformance::JSON) { ReportFailure(test_name, level, request, response, - StrCat("Test was asked for ", + absl::StrCat("Test was asked for ", WireFormatToString(requested_output), " output but provided JSON instead.") .c_str()); @@ -440,7 +440,7 @@ void BinaryAndJsonConformanceSuite::ExpectParseFailureForProtoWithProtoVersion ( const ConformanceRequest& request = setting.GetRequest(); ConformanceResponse response; - string effective_test_name = StrCat( + string effective_test_name = absl::StrCat( setting.ConformanceLevelToString(level), (is_proto3 ? ".Proto3" : ".Proto2"), ".ProtobufInput.", test_name); @@ -571,7 +571,7 @@ void BinaryAndJsonConformanceSuite::RunValidJsonTestWithValidator( const ConformanceRequest& request = setting.GetRequest(); ConformanceResponse response; string effective_test_name = - StrCat(setting.ConformanceLevelToString(level), + absl::StrCat(setting.ConformanceLevelToString(level), is_proto3 ? ".Proto3.JsonInput." : ".Proto2.JsonInput.", test_name, ".Validator"); @@ -615,7 +615,7 @@ void BinaryAndJsonConformanceSuite::ExpectParseFailureForJson( prototype, test_name, input_json); const ConformanceRequest& request = setting.GetRequest(); ConformanceResponse response; - string effective_test_name = StrCat( + string effective_test_name = absl::StrCat( setting.ConformanceLevelToString(level), ".Proto3.JsonInput.", test_name); RunTest(effective_test_name, request, &response); @@ -642,7 +642,7 @@ void BinaryAndJsonConformanceSuite::ExpectSerializeFailureForJson( prototype, test_name, payload_message.SerializeAsString()); const ConformanceRequest& request = setting.GetRequest(); ConformanceResponse response; - string effective_test_name = StrCat( + string effective_test_name = absl::StrCat( setting.ConformanceLevelToString(level), ".", test_name, ".JsonOutput"); RunTest(effective_test_name, request, &response); @@ -768,10 +768,10 @@ void BinaryAndJsonConformanceSuite::TestValidDataForType( string text = test_message->DebugString(); RunValidProtobufTest( - StrCat("ValidDataScalar", type_name, "[", i, "]"), REQUIRED, + absl::StrCat("ValidDataScalar", type_name, "[", i, "]"), REQUIRED, proto, text, is_proto3); RunValidBinaryProtobufTest( - StrCat("ValidDataScalarBinary", type_name, "[", i, "]"), + absl::StrCat("ValidDataScalarBinary", type_name, "[", i, "]"), RECOMMENDED, proto, expected_proto, is_proto3); } @@ -852,10 +852,10 @@ void BinaryAndJsonConformanceSuite::TestValidDataForType( // Ensures both packed and unpacked data can be parsed. RunValidProtobufTest( - StrCat("ValidDataRepeated", type_name, ".UnpackedInput"), + absl::StrCat("ValidDataRepeated", type_name, ".UnpackedInput"), REQUIRED, default_proto_unpacked, text, is_proto3); RunValidProtobufTest( - StrCat("ValidDataRepeated", type_name, ".PackedInput"), + absl::StrCat("ValidDataRepeated", type_name, ".PackedInput"), REQUIRED, default_proto_packed, text, is_proto3); // proto2 should encode as unpacked by default and proto3 should encode as @@ -863,27 +863,27 @@ void BinaryAndJsonConformanceSuite::TestValidDataForType( string expected_proto = rep_field->is_packed() ? default_proto_packed_expected : default_proto_unpacked_expected; - RunValidBinaryProtobufTest(StrCat("ValidDataRepeated", type_name, + RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name, ".UnpackedInput.DefaultOutput"), RECOMMENDED, default_proto_unpacked, expected_proto, is_proto3); - RunValidBinaryProtobufTest(StrCat("ValidDataRepeated", type_name, + RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name, ".PackedInput.DefaultOutput"), RECOMMENDED, default_proto_packed, expected_proto, is_proto3); - RunValidBinaryProtobufTest(StrCat("ValidDataRepeated", type_name, + RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name, ".UnpackedInput.PackedOutput"), RECOMMENDED, packed_proto_unpacked, packed_proto_expected, is_proto3); - RunValidBinaryProtobufTest(StrCat("ValidDataRepeated", type_name, + RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name, ".PackedInput.PackedOutput"), RECOMMENDED, packed_proto_packed, packed_proto_expected, is_proto3); - RunValidBinaryProtobufTest(StrCat("ValidDataRepeated", type_name, + RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name, ".UnpackedInput.UnpackedOutput"), RECOMMENDED, unpacked_proto_unpacked, unpacked_proto_expected, is_proto3); - RunValidBinaryProtobufTest(StrCat("ValidDataRepeated", type_name, + RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name, ".PackedInput.UnpackedOutput"), RECOMMENDED, unpacked_proto_packed, unpacked_proto_expected, is_proto3); @@ -899,7 +899,7 @@ void BinaryAndJsonConformanceSuite::TestValidDataForType( test_message->MergeFromString(expected_proto); string text = test_message->DebugString(); - RunValidProtobufTest(StrCat("ValidDataRepeated", type_name), + RunValidProtobufTest(absl::StrCat("ValidDataRepeated", type_name), REQUIRED, proto, text, is_proto3); } } @@ -975,7 +975,7 @@ void BinaryAndJsonConformanceSuite::TestValidDataForMapType( std::unique_ptr test_message = NewTestMessage(is_proto3); test_message->MergeFromString(proto); string text = test_message->DebugString(); - RunValidProtobufTest(StrCat("ValidDataMap", key_type_name, + RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name, value_type_name, ".Default"), REQUIRED, proto, text, is_proto3); } @@ -988,7 +988,7 @@ void BinaryAndJsonConformanceSuite::TestValidDataForMapType( std::unique_ptr test_message = NewTestMessage(is_proto3); test_message->MergeFromString(proto); string text = test_message->DebugString(); - RunValidProtobufTest(StrCat("ValidDataMap", key_type_name, + RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name, value_type_name, ".MissingDefault"), REQUIRED, proto, text, is_proto3); } @@ -1001,7 +1001,7 @@ void BinaryAndJsonConformanceSuite::TestValidDataForMapType( std::unique_ptr test_message = NewTestMessage(is_proto3); test_message->MergeFromString(proto); string text = test_message->DebugString(); - RunValidProtobufTest(StrCat("ValidDataMap", key_type_name, + RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name, value_type_name, ".NonDefault"), REQUIRED, proto, text, is_proto3); } @@ -1014,7 +1014,7 @@ void BinaryAndJsonConformanceSuite::TestValidDataForMapType( std::unique_ptr test_message = NewTestMessage(is_proto3); test_message->MergeFromString(proto); string text = test_message->DebugString(); - RunValidProtobufTest(StrCat("ValidDataMap", key_type_name, + RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name, value_type_name, ".Unordered"), REQUIRED, proto, text, is_proto3); } @@ -1031,7 +1031,7 @@ void BinaryAndJsonConformanceSuite::TestValidDataForMapType( std::unique_ptr test_message = NewTestMessage(is_proto3); test_message->MergeFromString(proto2); string text = test_message->DebugString(); - RunValidProtobufTest(StrCat("ValidDataMap", key_type_name, + RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name, value_type_name, ".DuplicateKey"), REQUIRED, proto, text, is_proto3); } @@ -1045,7 +1045,7 @@ void BinaryAndJsonConformanceSuite::TestValidDataForMapType( test_message->MergeFromString(proto); string text = test_message->DebugString(); RunValidProtobufTest( - StrCat("ValidDataMap", key_type_name, value_type_name, + absl::StrCat("ValidDataMap", key_type_name, value_type_name, ".DuplicateKeyInMapEntry"), REQUIRED, proto, text, is_proto3); } @@ -1059,7 +1059,7 @@ void BinaryAndJsonConformanceSuite::TestValidDataForMapType( test_message->MergeFromString(proto); string text = test_message->DebugString(); RunValidProtobufTest( - StrCat("ValidDataMap", key_type_name, value_type_name, + absl::StrCat("ValidDataMap", key_type_name, value_type_name, ".DuplicateValueInMapEntry"), REQUIRED, proto, text, is_proto3); } @@ -1125,10 +1125,10 @@ void BinaryAndJsonConformanceSuite::TestValidDataForOneofType( string text = test_message->DebugString(); RunValidProtobufTest( - StrCat("ValidDataOneof", type_name, ".DefaultValue"), REQUIRED, + absl::StrCat("ValidDataOneof", type_name, ".DefaultValue"), REQUIRED, proto, text, is_proto3); RunValidBinaryProtobufTest( - StrCat("ValidDataOneofBinary", type_name, ".DefaultValue"), + absl::StrCat("ValidDataOneofBinary", type_name, ".DefaultValue"), RECOMMENDED, proto, proto, is_proto3); } @@ -1140,25 +1140,25 @@ void BinaryAndJsonConformanceSuite::TestValidDataForOneofType( string text = test_message->DebugString(); RunValidProtobufTest( - StrCat("ValidDataOneof", type_name, ".NonDefaultValue"), + absl::StrCat("ValidDataOneof", type_name, ".NonDefaultValue"), REQUIRED, proto, text, is_proto3); RunValidBinaryProtobufTest( - StrCat("ValidDataOneofBinary", type_name, ".NonDefaultValue"), + absl::StrCat("ValidDataOneofBinary", type_name, ".NonDefaultValue"), RECOMMENDED, proto, proto, is_proto3); } { // Tests oneof with multiple values of the same field. - const string proto = StrCat(default_value, non_default_value); + const string proto = absl::StrCat(default_value, non_default_value); const string expected_proto = non_default_value; std::unique_ptr test_message = NewTestMessage(is_proto3); test_message->MergeFromString(expected_proto); string text = test_message->DebugString(); - RunValidProtobufTest(StrCat("ValidDataOneof", type_name, + RunValidProtobufTest(absl::StrCat("ValidDataOneof", type_name, ".MultipleValuesForSameField"), REQUIRED, proto, text, is_proto3); - RunValidBinaryProtobufTest(StrCat("ValidDataOneofBinary", type_name, + RunValidBinaryProtobufTest(absl::StrCat("ValidDataOneofBinary", type_name, ".MultipleValuesForSameField"), RECOMMENDED, proto, expected_proto, is_proto3); } @@ -1175,17 +1175,17 @@ void BinaryAndJsonConformanceSuite::TestValidDataForOneofType( cat(tag(other_field->number(), other_wire_type), GetDefaultValue(other_type)); - const string proto = StrCat(other_value, non_default_value); + const string proto = absl::StrCat(other_value, non_default_value); const string expected_proto = non_default_value; std::unique_ptr test_message = NewTestMessage(is_proto3); test_message->MergeFromString(expected_proto); string text = test_message->DebugString(); - RunValidProtobufTest(StrCat("ValidDataOneof", type_name, + RunValidProtobufTest(absl::StrCat("ValidDataOneof", type_name, ".MultipleValuesForDifferentField"), REQUIRED, proto, text, is_proto3); RunValidBinaryProtobufTest( - StrCat("ValidDataOneofBinary", type_name, + absl::StrCat("ValidDataOneofBinary", type_name, ".MultipleValuesForDifferentField"), RECOMMENDED, proto, expected_proto, is_proto3); } diff --git a/conformance/conformance.proto b/conformance/conformance.proto index 7248e98961f3..a9edbab823e7 100644 --- a/conformance/conformance.proto +++ b/conformance/conformance.proto @@ -92,10 +92,6 @@ message ConformanceRequest { // The payload (whether protobuf of JSON) is always for a // protobuf_test_messages.proto3.TestAllTypes proto (as defined in // src/google/protobuf/proto3_test_messages.proto). - // - // TODO(haberman): if/when we expand the conformance tests to support proto2, - // we will want to include a field that lets the payload/response be a - // protobuf_test_messages.google.protobuf.TestAllTypes message instead. oneof payload { bytes protobuf_payload = 1; string json_payload = 2; diff --git a/conformance/conformance_cpp.cc b/conformance/conformance_cpp.cc index 85fa9d0b5cad..95a0a0096139 100644 --- a/conformance/conformance_cpp.cc +++ b/conformance/conformance_cpp.cc @@ -42,15 +42,14 @@ #include #include #include -#include -#include +#include "absl/status/status.h" +#include "absl/status/statusor.h" #include "conformance/conformance.pb.h" #include "conformance/conformance.pb.h" #include #include #include #include -#include #include // Must be included last. @@ -69,29 +68,29 @@ using ::google::protobuf::util::TypeResolver; using ::protobuf_test_messages::proto2::TestAllTypesProto2; using ::protobuf_test_messages::proto3::TestAllTypesProto3; -util::Status ReadFd(int fd, char* buf, size_t len) { +absl::Status ReadFd(int fd, char* buf, size_t len) { while (len > 0) { ssize_t bytes_read = read(fd, buf, len); if (bytes_read == 0) { - return util::DataLossError("unexpected EOF"); + return absl::DataLossError("unexpected EOF"); } if (bytes_read < 0) { - return util::ErrnoToStatus(errno, "error reading from test runner"); + return absl::ErrnoToStatus(errno, "error reading from test runner"); } len -= bytes_read; buf += bytes_read; } - return util::OkStatus(); + return absl::OkStatus(); } -util::Status WriteFd(int fd, const void* buf, size_t len) { +absl::Status WriteFd(int fd, const void* buf, size_t len) { if (write(fd, buf, len) != len) { - return util::ErrnoToStatus(errno, "error reading to test runner"); + return absl::ErrnoToStatus(errno, "error reading to test runner"); } - return util::OkStatus(); + return absl::OkStatus(); } class Harness { @@ -102,15 +101,15 @@ class Harness { resolver_.reset(NewTypeResolverForDescriptorPool( "type.googleapis.com", DescriptorPool::generated_pool())); - type_url_ = StrCat("type.googleapis.com/", + type_url_ = absl::StrCat("type.googleapis.com/", TestAllTypesProto3::GetDescriptor()->full_name()); } - util::StatusOr RunTest( + absl::StatusOr RunTest( const ConformanceRequest& request); // Returns Ok(true) if we're done processing requests. - util::StatusOr ServeConformanceRequest(); + absl::StatusOr ServeConformanceRequest(); private: bool verbose_ = false; @@ -118,14 +117,14 @@ class Harness { std::string type_url_; }; -util::StatusOr Harness::RunTest( +absl::StatusOr Harness::RunTest( const ConformanceRequest& request) { const Descriptor* descriptor = DescriptorPool::generated_pool()->FindMessageTypeByName( request.message_type()); if (descriptor == nullptr) { - return util::NotFoundError( - StrCat("No such message type: ", request.message_type())); + return absl::NotFoundError( + absl::StrCat("No such message type: ", request.message_type())); } std::unique_ptr test_message( @@ -148,12 +147,12 @@ util::StatusOr Harness::RunTest( conformance::JSON_IGNORE_UNKNOWN_PARSING_TEST); std::string proto_binary; - util::Status status = + absl::Status status = JsonToBinaryString(resolver_.get(), type_url_, request.json_payload(), &proto_binary, options); if (!status.ok()) { response.set_parse_error( - StrCat("parse error: ", status.message())); + absl::StrCat("parse error: ", status.message())); return response; } @@ -176,16 +175,16 @@ util::StatusOr Harness::RunTest( } case ConformanceRequest::PAYLOAD_NOT_SET: - return util::InvalidArgumentError("request didn't have payload"); + return absl::InvalidArgumentError("request didn't have payload"); default: - return util::InvalidArgumentError( - StrCat("unknown payload type", request.payload_case())); + return absl::InvalidArgumentError( + absl::StrCat("unknown payload type", request.payload_case())); } switch (request.requested_output_format()) { case conformance::UNSPECIFIED: - return util::InvalidArgumentError("unspecified output format"); + return absl::InvalidArgumentError("unspecified output format"); case conformance::PROTOBUF: { GOOGLE_CHECK( @@ -196,11 +195,11 @@ util::StatusOr Harness::RunTest( case conformance::JSON: { std::string proto_binary; GOOGLE_CHECK(test_message->SerializeToString(&proto_binary)); - util::Status status = + absl::Status status = BinaryToJsonString(resolver_.get(), type_url_, proto_binary, response.mutable_json_payload()); if (!status.ok()) { - response.set_serialize_error(StrCat( + response.set_serialize_error(absl::StrCat( "failed to serialize JSON output: ", status.message())); } break; @@ -215,14 +214,14 @@ util::StatusOr Harness::RunTest( } default: - return util::InvalidArgumentError(StrCat( + return absl::InvalidArgumentError(absl::StrCat( "unknown output format", request.requested_output_format())); } return response; } -util::StatusOr Harness::ServeConformanceRequest() { +absl::StatusOr Harness::ServeConformanceRequest() { uint32_t in_len; if (!ReadFd(STDIN_FILENO, reinterpret_cast(&in_len), sizeof(in_len)) .ok()) { @@ -237,7 +236,7 @@ util::StatusOr Harness::ServeConformanceRequest() { ConformanceRequest request; GOOGLE_CHECK(request.ParseFromString(serialized_input)); - util::StatusOr response = RunTest(request); + absl::StatusOr response = RunTest(request); RETURN_IF_ERROR(response.status()); std::string serialized_output; diff --git a/conformance/conformance_test.cc b/conformance/conformance_test.cc index 9b8b71f984a1..3848ade94a71 100644 --- a/conformance/conformance_test.cc +++ b/conformance/conformance_test.cc @@ -129,7 +129,7 @@ string ConformanceTestSuite::ConformanceRequestSetting:: prototype_message_.GetDescriptor()->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 ? "Proto3" : "Proto2"; - return StrCat(ConformanceLevelToString(level_), ".", rname, ".", + return absl::StrCat(ConformanceLevelToString(level_), ".", rname, ".", InputFormatString(input_format_), ".", test_name_, ".", OutputFormatString(output_format_)); } @@ -290,7 +290,7 @@ void ConformanceTestSuite::VerifyResponse( GOOGLE_DCHECK_EQ(response.result_case(), ConformanceResponse::kProtobufPayload); const string& protobuf_payload = response.protobuf_payload(); check = equivalent_wire_format == protobuf_payload; - differences = StrCat("Expect: ", ToOctString(equivalent_wire_format), + differences = absl::StrCat("Expect: ", ToOctString(equivalent_wire_format), ", but got: ", ToOctString(protobuf_payload)); } else { check = differencer.Compare(*reference_message, *test_message); diff --git a/conformance/text_format_conformance_suite.cc b/conformance/text_format_conformance_suite.cc index 460bc24900e5..15a0f6d034c6 100644 --- a/conformance/text_format_conformance_suite.cc +++ b/conformance/text_format_conformance_suite.cc @@ -85,7 +85,7 @@ bool TextFormatConformanceTestSuite::ParseResponse( case ConformanceResponse::kProtobufPayload: { if (requested_output != conformance::PROTOBUF) { ReportFailure(test_name, level, request, response, - StrCat("Test was asked for ", + absl::StrCat("Test was asked for ", WireFormatToString(requested_output), " output but provided PROTOBUF instead.") .c_str()); @@ -104,7 +104,7 @@ bool TextFormatConformanceTestSuite::ParseResponse( case ConformanceResponse::kTextPayload: { if (requested_output != conformance::TEXT_FORMAT) { ReportFailure(test_name, level, request, response, - StrCat("Test was asked for ", + absl::StrCat("Test was asked for ", WireFormatToString(requested_output), " output but provided TEXT_FORMAT instead.") .c_str()); @@ -141,7 +141,7 @@ void TextFormatConformanceTestSuite::ExpectParseFailure(const string& test_name, const ConformanceRequest& request = setting.GetRequest(); ConformanceResponse response; string effective_test_name = - StrCat(setting.ConformanceLevelToString(level), + absl::StrCat(setting.ConformanceLevelToString(level), ".Proto3.TextFormatInput.", test_name); RunTest(effective_test_name, request, &response); @@ -263,64 +263,64 @@ void TextFormatConformanceTestSuite::RunSuiteImpl() { const std::string field_name = field_type == "String" ? "optional_string" : "optional_bytes"; RunValidTextFormatTest( - StrCat("StringLiteralConcat", field_type), REQUIRED, - StrCat(field_name, ": 'first' \"second\"\n'third'")); + absl::StrCat("StringLiteralConcat", field_type), REQUIRED, + absl::StrCat(field_name, ": 'first' \"second\"\n'third'")); RunValidTextFormatTest( - StrCat("StringLiteralBasicEscapes", field_type), REQUIRED, - StrCat(field_name, ": '\\a\\b\\f\\n\\r\\t\\v\\?\\\\\\'\\\"'")); + absl::StrCat("StringLiteralBasicEscapes", field_type), REQUIRED, + absl::StrCat(field_name, ": '\\a\\b\\f\\n\\r\\t\\v\\?\\\\\\'\\\"'")); RunValidTextFormatTest( - StrCat("StringLiteralOctalEscapes", field_type), REQUIRED, - StrCat(field_name, ": '\\341\\210\\264'")); - RunValidTextFormatTest(StrCat("StringLiteralHexEscapes", field_type), + absl::StrCat("StringLiteralOctalEscapes", field_type), REQUIRED, + absl::StrCat(field_name, ": '\\341\\210\\264'")); + RunValidTextFormatTest(absl::StrCat("StringLiteralHexEscapes", field_type), REQUIRED, - StrCat(field_name, ": '\\xe1\\x88\\xb4'")); + absl::StrCat(field_name, ": '\\xe1\\x88\\xb4'")); RunValidTextFormatTest( - StrCat("StringLiteralShortUnicodeEscape", field_type), - RECOMMENDED, StrCat(field_name, ": '\\u1234'")); + absl::StrCat("StringLiteralShortUnicodeEscape", field_type), + RECOMMENDED, absl::StrCat(field_name, ": '\\u1234'")); RunValidTextFormatTest( - StrCat("StringLiteralLongUnicodeEscapes", field_type), - RECOMMENDED, StrCat(field_name, ": '\\U00001234\\U00010437'")); + absl::StrCat("StringLiteralLongUnicodeEscapes", field_type), + RECOMMENDED, absl::StrCat(field_name, ": '\\U00001234\\U00010437'")); // String literals don't include line feeds. - ExpectParseFailure(StrCat("StringLiteralIncludesLF", field_type), + ExpectParseFailure(absl::StrCat("StringLiteralIncludesLF", field_type), REQUIRED, - StrCat(field_name, ": 'first line\nsecond line'")); + absl::StrCat(field_name, ": 'first line\nsecond line'")); // Unicode escapes don't include code points that lie beyond the planes // (> 0x10ffff). ExpectParseFailure( - StrCat("StringLiteralLongUnicodeEscapeTooLarge", field_type), - REQUIRED, StrCat(field_name, ": '\\U00110000'")); + absl::StrCat("StringLiteralLongUnicodeEscapeTooLarge", field_type), + REQUIRED, absl::StrCat(field_name, ": '\\U00110000'")); // Unicode escapes don't include surrogates. ExpectParseFailure( - StrCat("StringLiteralShortUnicodeEscapeSurrogatePair", + absl::StrCat("StringLiteralShortUnicodeEscapeSurrogatePair", field_type), - RECOMMENDED, StrCat(field_name, ": '\\ud801\\udc37'")); + RECOMMENDED, absl::StrCat(field_name, ": '\\ud801\\udc37'")); ExpectParseFailure( - StrCat("StringLiteralShortUnicodeEscapeSurrogateFirstOnly", + absl::StrCat("StringLiteralShortUnicodeEscapeSurrogateFirstOnly", field_type), - RECOMMENDED, StrCat(field_name, ": '\\ud800'")); + RECOMMENDED, absl::StrCat(field_name, ": '\\ud800'")); ExpectParseFailure( - StrCat("StringLiteralShortUnicodeEscapeSurrogateSecondOnly", + absl::StrCat("StringLiteralShortUnicodeEscapeSurrogateSecondOnly", field_type), - RECOMMENDED, StrCat(field_name, ": '\\udc00'")); + RECOMMENDED, absl::StrCat(field_name, ": '\\udc00'")); ExpectParseFailure( - StrCat("StringLiteralLongUnicodeEscapeSurrogateFirstOnly", + absl::StrCat("StringLiteralLongUnicodeEscapeSurrogateFirstOnly", field_type), - RECOMMENDED, StrCat(field_name, ": '\\U0000d800'")); + RECOMMENDED, absl::StrCat(field_name, ": '\\U0000d800'")); ExpectParseFailure( - StrCat("StringLiteralLongUnicodeEscapeSurrogateSecondOnly", + absl::StrCat("StringLiteralLongUnicodeEscapeSurrogateSecondOnly", field_type), - RECOMMENDED, StrCat(field_name, ": '\\U0000dc00'")); + RECOMMENDED, absl::StrCat(field_name, ": '\\U0000dc00'")); ExpectParseFailure( - StrCat("StringLiteralLongUnicodeEscapeSurrogatePair", field_type), - RECOMMENDED, StrCat(field_name, ": '\\U0000d801\\U00000dc37'")); + absl::StrCat("StringLiteralLongUnicodeEscapeSurrogatePair", field_type), + RECOMMENDED, absl::StrCat(field_name, ": '\\U0000d801\\U00000dc37'")); ExpectParseFailure( - StrCat("StringLiteralUnicodeEscapeSurrogatePairLongShort", + absl::StrCat("StringLiteralUnicodeEscapeSurrogatePairLongShort", field_type), - RECOMMENDED, StrCat(field_name, ": '\\U0000d801\\udc37'")); + RECOMMENDED, absl::StrCat(field_name, ": '\\U0000d801\\udc37'")); ExpectParseFailure( - StrCat("StringLiteralUnicodeEscapeSurrogatePairShortLong", + absl::StrCat("StringLiteralUnicodeEscapeSurrogatePairShortLong", field_type), - RECOMMENDED, StrCat(field_name, ": '\\ud801\\U0000dc37'")); + RECOMMENDED, absl::StrCat(field_name, ": '\\ud801\\U0000dc37'")); // The following method depend on the type of field, as strings have extra // validation. @@ -330,10 +330,10 @@ void TextFormatConformanceTestSuite::RunSuiteImpl() { : &TextFormatConformanceTestSuite::RunValidTextFormatTest; // String fields reject invalid UTF-8 byte sequences; bytes fields don't. - (this->*test_method)(StrCat(field_type, "FieldBadUTF8Octal"), - REQUIRED, StrCat(field_name, ": '\\300'")); - (this->*test_method)(StrCat(field_type, "FieldBadUTF8Hex"), REQUIRED, - StrCat(field_name, ": '\\xc0'")); + (this->*test_method)(absl::StrCat(field_type, "FieldBadUTF8Octal"), + REQUIRED, absl::StrCat(field_name, ": '\\300'")); + (this->*test_method)(absl::StrCat(field_type, "FieldBadUTF8Hex"), REQUIRED, + absl::StrCat(field_name, ": '\\xc0'")); } // Group fields diff --git a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java index ccb86b5ecd0d..1a76848d68d4 100644 --- a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java @@ -1773,6 +1773,7 @@ public void testParseInfoTreeBuilding() throws Exception { } } + @SuppressWarnings("LenientFormatStringValidation") private void assertLocation( TextFormatParseInfoTree tree, final Descriptor descriptor, @@ -1786,6 +1787,7 @@ private void assertLocation( TextFormatParseLocation expected = TextFormatParseLocation.create(line, column); assertThat(location).isEqualTo(expected); } else if (line != -1 && column != -1) { + // Expected 0 args, but got 3. assertWithMessage( "Tree/descriptor/fieldname did not contain index %d, line %d column %d expected", index, line, column) diff --git a/python/google/protobuf/pyext/descriptor.cc b/python/google/protobuf/pyext/descriptor.cc index ed094af92442..9689adf36f20 100644 --- a/python/google/protobuf/pyext/descriptor.cc +++ b/python/google/protobuf/pyext/descriptor.cc @@ -47,8 +47,8 @@ #include #include #include +#include "absl/strings/string_view.h" #include -#include #define PyString_AsStringAndSize(ob, charpp, sizep) \ (PyUnicode_Check(ob) \ @@ -1747,7 +1747,8 @@ static PyObject* FindMethodByName(PyBaseDescriptor *self, PyObject* arg) { } const MethodDescriptor* method_descriptor = - _GetDescriptor(self)->FindMethodByName(StringParam(name, name_size)); + _GetDescriptor(self)->FindMethodByName( + absl::string_view(name, name_size)); if (method_descriptor == nullptr) { PyErr_Format(PyExc_KeyError, "Couldn't find method %.200s", name); return nullptr; diff --git a/python/google/protobuf/pyext/descriptor.h b/python/google/protobuf/pyext/descriptor.h index d97e2f8103ad..a115ef4ed4e8 100644 --- a/python/google/protobuf/pyext/descriptor.h +++ b/python/google/protobuf/pyext/descriptor.h @@ -42,9 +42,6 @@ namespace google { namespace protobuf { namespace python { -// Should match the type of ConstStringParam. -using StringParam = std::string; - extern PyTypeObject PyMessageDescriptor_Type; extern PyTypeObject PyFieldDescriptor_Type; extern PyTypeObject PyEnumDescriptor_Type; diff --git a/python/google/protobuf/pyext/descriptor_containers.cc b/python/google/protobuf/pyext/descriptor_containers.cc index a87155bc4e4b..e6d675c74ae4 100644 --- a/python/google/protobuf/pyext/descriptor_containers.cc +++ b/python/google/protobuf/pyext/descriptor_containers.cc @@ -49,14 +49,19 @@ // because the Python API is based on C, and does not play well with C++ // inheritance. +// clang-format off #define PY_SSIZE_T_CLEAN +// This inclusion must appear before all the others. #include -#include #include -#include +// clang-format on + +#include #include +#include #include +#include "absl/strings/string_view.h" #define PyString_AsStringAndSize(ob, charpp, sizep) \ (PyUnicode_Check(ob) \ @@ -75,9 +80,9 @@ struct PyContainer; typedef int (*CountMethod)(PyContainer* self); typedef const void* (*GetByIndexMethod)(PyContainer* self, int index); typedef const void* (*GetByNameMethod)(PyContainer* self, - ConstStringParam name); + absl::string_view name); typedef const void* (*GetByCamelcaseNameMethod)(PyContainer* self, - ConstStringParam name); + absl::string_view name); typedef const void* (*GetByNumberMethod)(PyContainer* self, int index); typedef PyObject* (*NewObjectFromItemMethod)(const void* descriptor); typedef const std::string& (*GetItemNameMethod)(const void* descriptor); @@ -175,8 +180,8 @@ static bool _GetItemByKey(PyContainer* self, PyObject* key, const void** item) { } return false; } - *item = self->container_def->get_by_name_fn(self, - StringParam(name, name_size)); + *item = self->container_def->get_by_name_fn( + self, absl::string_view(name, name_size)); return true; } case PyContainer::KIND_BYCAMELCASENAME: { @@ -192,7 +197,7 @@ static bool _GetItemByKey(PyContainer* self, PyObject* key, const void** item) { return false; } *item = self->container_def->get_by_camelcase_name_fn( - self, StringParam(camelcase_name, name_size)); + self, absl::string_view(camelcase_name, name_size)); return true; } case PyContainer::KIND_BYNUMBER: { @@ -958,12 +963,12 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->field_count(); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindFieldByName(name); } static const void* GetByCamelcaseName(PyContainer* self, - ConstStringParam name) { + absl::string_view name) { return GetDescriptor(self)->FindFieldByCamelcaseName(name); } @@ -1028,7 +1033,7 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->nested_type_count(); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindNestedTypeByName(name); } @@ -1080,7 +1085,7 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->enum_type_count(); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindEnumTypeByName(name); } @@ -1143,7 +1148,7 @@ static int Count(PyContainer* self) { return count; } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindEnumValueByName(name); } @@ -1194,7 +1199,7 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->extension_count(); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindExtensionByName(name); } @@ -1246,7 +1251,7 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->oneof_decl_count(); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindOneofByName(name); } @@ -1304,7 +1309,7 @@ static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->value(index); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindValueByName(name); } @@ -1408,7 +1413,7 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->method_count(); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindMethodByName(name); } @@ -1462,7 +1467,7 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->message_type_count(); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindMessageTypeByName(name); } @@ -1502,7 +1507,7 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->enum_type_count(); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindEnumTypeByName(name); } @@ -1542,7 +1547,7 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->extension_count(); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindExtensionByName(name); } @@ -1582,7 +1587,7 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->service_count(); } -static const void* GetByName(PyContainer* self, ConstStringParam name) { +static const void* GetByName(PyContainer* self, absl::string_view name) { return GetDescriptor(self)->FindServiceByName(name); } diff --git a/python/google/protobuf/pyext/descriptor_pool.cc b/python/google/protobuf/pyext/descriptor_pool.cc index 2f392818303e..c69a113320d4 100644 --- a/python/google/protobuf/pyext/descriptor_pool.cc +++ b/python/google/protobuf/pyext/descriptor_pool.cc @@ -42,7 +42,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" #define PyString_AsStringAndSize(ob, charpp, sizep) \ (PyUnicode_Check(ob) \ @@ -246,7 +246,7 @@ static PyObject* FindMessageByName(PyObject* self, PyObject* arg) { const Descriptor* message_descriptor = reinterpret_cast(self)->pool->FindMessageTypeByName( - StringParam(name, name_size)); + absl::string_view(name, name_size)); if (message_descriptor == nullptr) { return SetErrorFromCollector( @@ -270,7 +270,7 @@ static PyObject* FindFileByName(PyObject* self, PyObject* arg) { PyDescriptorPool* py_pool = reinterpret_cast(self); const FileDescriptor* file_descriptor = - py_pool->pool->FindFileByName(StringParam(name, name_size)); + py_pool->pool->FindFileByName(absl::string_view(name, name_size)); if (file_descriptor == nullptr) { return SetErrorFromCollector(py_pool->error_collector, name, "file"); @@ -286,7 +286,7 @@ PyObject* FindFieldByName(PyDescriptorPool* self, PyObject* arg) { } const FieldDescriptor* field_descriptor = - self->pool->FindFieldByName(StringParam(name, name_size)); + self->pool->FindFieldByName(absl::string_view(name, name_size)); if (field_descriptor == nullptr) { return SetErrorFromCollector(self->error_collector, name, "field"); } @@ -307,7 +307,7 @@ PyObject* FindExtensionByName(PyDescriptorPool* self, PyObject* arg) { } const FieldDescriptor* field_descriptor = - self->pool->FindExtensionByName(StringParam(name, name_size)); + self->pool->FindExtensionByName(absl::string_view(name, name_size)); if (field_descriptor == nullptr) { return SetErrorFromCollector(self->error_collector, name, "extension field"); @@ -329,7 +329,7 @@ PyObject* FindEnumTypeByName(PyDescriptorPool* self, PyObject* arg) { } const EnumDescriptor* enum_descriptor = - self->pool->FindEnumTypeByName(StringParam(name, name_size)); + self->pool->FindEnumTypeByName(absl::string_view(name, name_size)); if (enum_descriptor == nullptr) { return SetErrorFromCollector(self->error_collector, name, "enum"); } @@ -350,7 +350,7 @@ PyObject* FindOneofByName(PyDescriptorPool* self, PyObject* arg) { } const OneofDescriptor* oneof_descriptor = - self->pool->FindOneofByName(StringParam(name, name_size)); + self->pool->FindOneofByName(absl::string_view(name, name_size)); if (oneof_descriptor == nullptr) { return SetErrorFromCollector(self->error_collector, name, "oneof"); } @@ -372,7 +372,7 @@ static PyObject* FindServiceByName(PyObject* self, PyObject* arg) { const ServiceDescriptor* service_descriptor = reinterpret_cast(self)->pool->FindServiceByName( - StringParam(name, name_size)); + absl::string_view(name, name_size)); if (service_descriptor == nullptr) { return SetErrorFromCollector( reinterpret_cast(self)->error_collector, name, @@ -392,7 +392,7 @@ static PyObject* FindMethodByName(PyObject* self, PyObject* arg) { const MethodDescriptor* method_descriptor = reinterpret_cast(self)->pool->FindMethodByName( - StringParam(name, name_size)); + absl::string_view(name, name_size)); if (method_descriptor == nullptr) { return SetErrorFromCollector( reinterpret_cast(self)->error_collector, name, @@ -412,7 +412,7 @@ static PyObject* FindFileContainingSymbol(PyObject* self, PyObject* arg) { const FileDescriptor* file_descriptor = reinterpret_cast(self)->pool->FindFileContainingSymbol( - StringParam(name, name_size)); + absl::string_view(name, name_size)); if (file_descriptor == nullptr) { return SetErrorFromCollector( reinterpret_cast(self)->error_collector, name, diff --git a/python/google/protobuf/pyext/extension_dict.cc b/python/google/protobuf/pyext/extension_dict.cc index 5e61dbf6b337..6eed0e83fddd 100644 --- a/python/google/protobuf/pyext/extension_dict.cc +++ b/python/google/protobuf/pyext/extension_dict.cc @@ -48,6 +48,7 @@ #include #include #include +#include "absl/strings/string_view.h" #define PyString_AsStringAndSize(ob, charpp, sizep) \ (PyUnicode_Check(ob) \ @@ -239,11 +240,11 @@ PyObject* _FindExtensionByName(ExtensionDict* self, PyObject* arg) { PyDescriptorPool* pool = cmessage::GetFactoryForMessage(self->parent)->pool; const FieldDescriptor* message_extension = - pool->pool->FindExtensionByName(StringParam(name, name_size)); + pool->pool->FindExtensionByName(absl::string_view(name, name_size)); if (message_extension == nullptr) { // Is is the name of a message set extension? const Descriptor* message_descriptor = - pool->pool->FindMessageTypeByName(StringParam(name, name_size)); + pool->pool->FindMessageTypeByName(absl::string_view(name, name_size)); if (message_descriptor && message_descriptor->extension_count() > 0) { const FieldDescriptor* extension = message_descriptor->extension(0); if (extension->is_extension() && diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index 792eb09cf33c..650f4913b395 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -69,6 +69,7 @@ #include #include #include +#include "absl/strings/string_view.h" #include #include #include @@ -408,7 +409,7 @@ static PyObject* GetClassAttribute(CMessageClass *self, PyObject* name) { Py_ssize_t attr_size; static const char kSuffix[] = "_FIELD_NUMBER"; if (PyString_AsStringAndSize(name, &attr, &attr_size) >= 0 && - HasSuffixString(StringPiece(attr, attr_size), kSuffix)) { + HasSuffixString(absl::string_view(attr, attr_size), kSuffix)) { std::string field_name(attr, attr_size - sizeof(kSuffix) + 1); LowerString(&field_name); @@ -913,7 +914,7 @@ static PyObject* GetIntegerEnumValue(const FieldDescriptor& descriptor, return nullptr; } const EnumValueDescriptor* enum_value_descriptor = - enum_descriptor->FindValueByName(StringParam(enum_label, size)); + enum_descriptor->FindValueByName(absl::string_view(enum_label, size)); if (enum_value_descriptor == nullptr) { PyErr_Format(PyExc_ValueError, "unknown enum label \"%s\"", enum_label); return nullptr; @@ -1340,7 +1341,7 @@ int HasFieldByDescriptor(CMessage* self, } const FieldDescriptor* FindFieldWithOneofs(const Message* message, - ConstStringParam field_name, + absl::string_view field_name, bool* in_oneof) { *in_oneof = false; const Descriptor* descriptor = message->GetDescriptor(); @@ -1389,8 +1390,8 @@ PyObject* HasField(CMessage* self, PyObject* arg) { Message* message = self->message; bool is_in_oneof; - const FieldDescriptor* field_descriptor = - FindFieldWithOneofs(message, StringParam(field_name, size), &is_in_oneof); + const FieldDescriptor* field_descriptor = FindFieldWithOneofs( + message, absl::string_view(field_name, size), &is_in_oneof); if (field_descriptor == nullptr) { if (!is_in_oneof) { PyErr_Format(PyExc_ValueError, "Protocol message %s has no field %s.", @@ -1574,7 +1575,7 @@ PyObject* ClearField(CMessage* self, PyObject* arg) { AssureWritable(self); bool is_in_oneof; const FieldDescriptor* field_descriptor = FindFieldWithOneofs( - self->message, StringParam(field_name, field_size), &is_in_oneof); + self->message, absl::string_view(field_name, field_size), &is_in_oneof); if (field_descriptor == nullptr) { if (is_in_oneof) { // We gave the name of a oneof, and none of its fields are set. @@ -1884,7 +1885,7 @@ static PyObject* MergeFromString(CMessage* self, PyObject* arg) { const char* ptr; internal::ParseContext ctx( depth, false, &ptr, - StringPiece(static_cast(data.buf), data.len)); + absl::string_view(static_cast(data.buf), data.len)); PyBuffer_Release(&data); ctx.data().pool = factory->pool->pool; ctx.data().factory = factory->message_factory; @@ -1976,7 +1977,7 @@ static PyObject* WhichOneof(CMessage* self, PyObject* arg) { if (PyString_AsStringAndSize(arg, &name_data, &name_size) < 0) return nullptr; const OneofDescriptor* oneof_desc = self->message->GetDescriptor()->FindOneofByName( - StringParam(name_data, name_size)); + absl::string_view(name_data, name_size)); if (oneof_desc == nullptr) { PyErr_Format(PyExc_ValueError, "Protocol message has no oneof \"%s\" field.", name_data); diff --git a/python/setup.py b/python/setup.py index eee6c82ef465..3efc7728a7b8 100755 --- a/python/setup.py +++ b/python/setup.py @@ -297,8 +297,8 @@ def HasLibraryDirsOpt(): extra_objects = ['../bazel-bin/src/google/protobuf/libprotobuf.a'] else: extra_objects = ['../libprotobuf.a'] - extra_objects += ['../third_party/abseil-cpp/absl/synchronization/libabsl_synchronization.a'] - # Include all of these twice to eliminate order-dependence. + # Repeat all of these enough times to eliminate order-dependence. + extra_objects += list(glob.iglob('../third_party/abseil-cpp/absl/**/*.a')) extra_objects += list(glob.iglob('../third_party/abseil-cpp/absl/**/*.a')) extra_objects += list(glob.iglob('../third_party/abseil-cpp/absl/**/*.a')) else: diff --git a/src/file_lists.cmake b/src/file_lists.cmake index f083729839ad..f95f5279ad4a 100644 --- a/src/file_lists.cmake +++ b/src/file_lists.cmake @@ -62,14 +62,10 @@ set(libprotobuf_srcs ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/bytestream.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/common.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/int128.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/status.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/statusor.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringpiece.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringprintf.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/structurally_valid.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/strutil.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/substitute.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/time.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/text_format.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/timestamp.pb.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/type.pb.cc @@ -166,27 +162,19 @@ set(libprotobuf_hdrs ${protobuf_SOURCE_DIR}/src/google/protobuf/struct.pb.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/bytestream.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/callback.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/casts.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/common.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/hash.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/int128.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/logging.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/macros.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/map_util.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/mathutil.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/once.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/platform_macros.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/port.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/status.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/status_macros.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/statusor.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stl_util.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringpiece.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringprintf.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/strutil.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/substitute.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/template_util.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/time.h ${protobuf_SOURCE_DIR}/src/google/protobuf/text_format.h ${protobuf_SOURCE_DIR}/src/google/protobuf/timestamp.pb.h ${protobuf_SOURCE_DIR}/src/google/protobuf/type.pb.h @@ -246,13 +234,9 @@ set(libprotobuf_lite_srcs ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/bytestream.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/common.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/int128.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/status.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/statusor.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringpiece.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringprintf.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/structurally_valid.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/strutil.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/time.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/wire_format_lite.cc ) @@ -291,26 +275,18 @@ set(libprotobuf_lite_hdrs ${protobuf_SOURCE_DIR}/src/google/protobuf/repeated_ptr_field.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/bytestream.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/callback.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/casts.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/common.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/hash.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/int128.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/logging.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/macros.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/map_util.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/mathutil.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/once.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/platform_macros.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/port.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/status.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/status_macros.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/statusor.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stl_util.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringpiece.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringprintf.h ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/strutil.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/template_util.h - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/time.h ${protobuf_SOURCE_DIR}/src/google/protobuf/wire_format_lite.h ) @@ -872,12 +848,7 @@ set(stubs_test_files ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/bytestream_unittest.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/common_unittest.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/int128_unittest.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/status_test.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/statusor_test.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringpiece_unittest.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringprintf_unittest.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/structurally_valid_unittest.cc ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/strutil_unittest.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/template_util_unittest.cc - ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/time_test.cc ) diff --git a/src/google/protobuf/BUILD.bazel b/src/google/protobuf/BUILD.bazel index 7570a4528fa2..114b67cb89c8 100644 --- a/src/google/protobuf/BUILD.bazel +++ b/src/google/protobuf/BUILD.bazel @@ -198,7 +198,9 @@ cc_library( ":arena", "//src/google/protobuf/io", "//src/google/protobuf/stubs:lite", + "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/synchronization", + "@com_google_absl//absl/time", ], ) @@ -281,7 +283,13 @@ cc_library( "//src/google/protobuf/io:printer", "//src/google/protobuf/io:tokenizer", "//src/google/protobuf/stubs", + "@com_google_absl//absl/base", + "@com_google_absl//absl/base:dynamic_annotations", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/hash", "@com_google_absl//absl/synchronization", + "@com_google_absl//absl/time", ], ) diff --git a/src/google/protobuf/any.cc b/src/google/protobuf/any.cc index 346fa19f17ac..a3efd762e83c 100644 --- a/src/google/protobuf/any.cc +++ b/src/google/protobuf/any.cc @@ -47,7 +47,7 @@ bool AnyMetadata::PackFrom(Arena* arena, const Message& message) { } bool AnyMetadata::PackFrom(Arena* arena, const Message& message, - StringPiece type_url_prefix) { + absl::string_view type_url_prefix) { type_url_->Set( GetTypeUrl(message.GetDescriptor()->full_name(), type_url_prefix), arena); return message.SerializeToString(value_->Mutable(arena)); diff --git a/src/google/protobuf/any.h b/src/google/protobuf/any.h index 92ea2bb2c767..95a68246add7 100644 --- a/src/google/protobuf/any.h +++ b/src/google/protobuf/any.h @@ -52,8 +52,8 @@ extern const char kAnyFullTypeName[]; // "google.protobuf.Any". extern const char kTypeGoogleApisComPrefix[]; // "type.googleapis.com/". extern const char kTypeGoogleProdComPrefix[]; // "type.googleprod.com/". -std::string GetTypeUrl(StringPiece message_name, - StringPiece type_url_prefix); +std::string GetTypeUrl(absl::string_view message_name, + absl::string_view type_url_prefix); // Helper class used to implement google::protobuf::Any. class PROTOBUF_EXPORT AnyMetadata { @@ -84,13 +84,13 @@ class PROTOBUF_EXPORT AnyMetadata { // Returns false if serializing the message failed. template bool PackFrom(Arena* arena, const T& message, - StringPiece type_url_prefix) { + absl::string_view type_url_prefix) { return InternalPackFrom(arena, message, type_url_prefix, T::FullMessageName()); } bool PackFrom(Arena* arena, const Message& message, - StringPiece type_url_prefix); + absl::string_view type_url_prefix); // Unpacks the payload into the given message. Returns false if the message's // type doesn't match the type specified in the type URL (i.e., the full @@ -113,11 +113,11 @@ class PROTOBUF_EXPORT AnyMetadata { private: bool InternalPackFrom(Arena* arena, const MessageLite& message, - StringPiece type_url_prefix, - StringPiece type_name); - bool InternalUnpackTo(StringPiece type_name, + absl::string_view type_url_prefix, + absl::string_view type_name); + bool InternalUnpackTo(absl::string_view type_name, MessageLite* message) const; - bool InternalIs(StringPiece type_name) const; + bool InternalIs(absl::string_view type_name) const; UrlType* type_url_; ValueType* value_; @@ -132,14 +132,14 @@ class PROTOBUF_EXPORT AnyMetadata { // // NOTE: this function is available publicly as a static method on the // generated message type: google::protobuf::Any::ParseAnyTypeUrl() -bool ParseAnyTypeUrl(StringPiece type_url, std::string* full_type_name); +bool ParseAnyTypeUrl(absl::string_view type_url, std::string* full_type_name); // Get the proto type name and prefix from Any::type_url value. For example, // passing "type.googleapis.com/rpc.QueryOrigin" will return // "type.googleapis.com/" in *url_prefix and "rpc.QueryOrigin" in // *full_type_name. Returns false if the type_url does not have a "/" in the // type url separating the full type name. -bool ParseAnyTypeUrl(StringPiece type_url, std::string* url_prefix, +bool ParseAnyTypeUrl(absl::string_view type_url, std::string* url_prefix, std::string* full_type_name); // See if message is of type google.protobuf.Any, if so, return the descriptors diff --git a/src/google/protobuf/any.pb.cc b/src/google/protobuf/any.pb.cc index 379991e24a08..b7ffd2d71446 100644 --- a/src/google/protobuf/any.pb.cc +++ b/src/google/protobuf/any.pb.cc @@ -73,7 +73,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fany_2eproto[] PROTOBUF_ "anypb\242\002\003GPB\252\002\036Google.Protobuf.WellKnownT" "ypesb\006proto3" ; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2fany_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2fany_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2fany_2eproto = { false, false, 212, descriptor_table_protodef_google_2fprotobuf_2fany_2eproto, "google/protobuf/any.proto", @@ -100,7 +100,7 @@ bool Any::GetAnyFieldDescriptors( message, type_url_field, value_field); } bool Any::ParseAnyTypeUrl( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url, + ::absl::string_view type_url, std::string* full_type_name) { return ::_pbi::ParseAnyTypeUrl(type_url, full_type_name); } diff --git a/src/google/protobuf/any.pb.h b/src/google/protobuf/any.pb.h index 41ee5c2a9032..31e1663db850 100644 --- a/src/google/protobuf/any.pb.h +++ b/src/google/protobuf/any.pb.h @@ -113,7 +113,7 @@ class PROTOBUF_EXPORT Any final : return _impl_._any_metadata_.PackFrom(GetArena(), message); } bool PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message& message, - ::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url_prefix) { + ::absl::string_view type_url_prefix) { GOOGLE_DCHECK_NE(&message, this); return _impl_._any_metadata_.PackFrom(GetArena(), message, type_url_prefix); } @@ -130,7 +130,7 @@ class PROTOBUF_EXPORT Any final : } template ::value>::type> bool PackFrom(const T& message, - ::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url_prefix) { + ::absl::string_view type_url_prefix) { return _impl_._any_metadata_.PackFrom(GetArena(), message, type_url_prefix);} template ::value>::type> bool UnpackTo(T* message) const { @@ -139,7 +139,7 @@ class PROTOBUF_EXPORT Any final : template bool Is() const { return _impl_._any_metadata_.Is(); } - static bool ParseAnyTypeUrl(::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url, + static bool ParseAnyTypeUrl(::absl::string_view type_url, std::string* full_type_name); friend void swap(Any& a, Any& b) { a.Swap(&b); @@ -194,7 +194,7 @@ class PROTOBUF_EXPORT Any final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Any"; } protected: diff --git a/src/google/protobuf/any_lite.cc b/src/google/protobuf/any_lite.cc index f283a31b0cc3..767ed7e1982f 100644 --- a/src/google/protobuf/any_lite.cc +++ b/src/google/protobuf/any_lite.cc @@ -30,6 +30,7 @@ #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -38,13 +39,13 @@ namespace google { namespace protobuf { namespace internal { -std::string GetTypeUrl(StringPiece message_name, - StringPiece type_url_prefix) { +std::string GetTypeUrl(absl::string_view message_name, + absl::string_view type_url_prefix) { if (!type_url_prefix.empty() && type_url_prefix[type_url_prefix.size() - 1] == '/') { - return StrCat(type_url_prefix, message_name); + return absl::StrCat(type_url_prefix, message_name); } else { - return StrCat(type_url_prefix, "/", message_name); + return absl::StrCat(type_url_prefix, "/", message_name); } } @@ -53,13 +54,13 @@ const char kTypeGoogleApisComPrefix[] = "type.googleapis.com/"; const char kTypeGoogleProdComPrefix[] = "type.googleprod.com/"; bool AnyMetadata::InternalPackFrom(Arena* arena, const MessageLite& message, - StringPiece type_url_prefix, - StringPiece type_name) { + absl::string_view type_url_prefix, + absl::string_view type_name) { type_url_->Set(GetTypeUrl(type_name, type_url_prefix), arena); return message.SerializeToString(value_->Mutable(arena)); } -bool AnyMetadata::InternalUnpackTo(StringPiece type_name, +bool AnyMetadata::InternalUnpackTo(absl::string_view type_name, MessageLite* message) const { if (!InternalIs(type_name)) { return false; @@ -67,14 +68,14 @@ bool AnyMetadata::InternalUnpackTo(StringPiece type_name, return message->ParseFromString(value_->Get()); } -bool AnyMetadata::InternalIs(StringPiece type_name) const { - StringPiece type_url = type_url_->Get(); +bool AnyMetadata::InternalIs(absl::string_view type_name) const { + absl::string_view type_url = type_url_->Get(); return type_url.size() >= type_name.size() + 1 && type_url[type_url.size() - type_name.size() - 1] == '/' && HasSuffixString(type_url, type_name); } -bool ParseAnyTypeUrl(StringPiece type_url, std::string* url_prefix, +bool ParseAnyTypeUrl(absl::string_view type_url, std::string* url_prefix, std::string* full_type_name) { size_t pos = type_url.find_last_of('/'); if (pos == std::string::npos || pos + 1 == type_url.size()) { @@ -87,7 +88,7 @@ bool ParseAnyTypeUrl(StringPiece type_url, std::string* url_prefix, return true; } -bool ParseAnyTypeUrl(StringPiece type_url, std::string* full_type_name) { +bool ParseAnyTypeUrl(absl::string_view type_url, std::string* full_type_name) { return ParseAnyTypeUrl(type_url, nullptr, full_type_name); } diff --git a/src/google/protobuf/any_test.cc b/src/google/protobuf/any_test.cc index a82afb21cb38..49685dbb9701 100644 --- a/src/google/protobuf/any_test.cc +++ b/src/google/protobuf/any_test.cc @@ -31,6 +31,7 @@ #include #include #include +#include "absl/strings/str_cat.h" // Must be included last. diff --git a/src/google/protobuf/api.pb.cc b/src/google/protobuf/api.pb.cc index bf30abba2cad..f0828f7a171c 100644 --- a/src/google/protobuf/api.pb.cc +++ b/src/google/protobuf/api.pb.cc @@ -157,7 +157,7 @@ static const ::_pbi::DescriptorTable* const descriptor_table_google_2fprotobuf_2 &::descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto, &::descriptor_table_google_2fprotobuf_2ftype_2eproto, }; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2fapi_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2fapi_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2fapi_2eproto = { false, false, 751, descriptor_table_protodef_google_2fprotobuf_2fapi_2eproto, "google/protobuf/api.proto", diff --git a/src/google/protobuf/api.pb.h b/src/google/protobuf/api.pb.h index 31c09b5c5fbc..94f588af8e89 100644 --- a/src/google/protobuf/api.pb.h +++ b/src/google/protobuf/api.pb.h @@ -169,7 +169,7 @@ class PROTOBUF_EXPORT Api final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Api"; } protected: @@ -429,7 +429,7 @@ class PROTOBUF_EXPORT Method final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Method"; } protected: @@ -667,7 +667,7 @@ class PROTOBUF_EXPORT Mixin final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Mixin"; } protected: diff --git a/src/google/protobuf/arena.cc b/src/google/protobuf/arena.cc index 8045c4b76eea..610d9f7a1778 100644 --- a/src/google/protobuf/arena.cc +++ b/src/google/protobuf/arena.cc @@ -105,13 +105,11 @@ class GetDeallocator { size_t* space_allocated_; }; -SerialArena::SerialArena(Block* b, void* owner, ThreadSafeArenaStats* stats) - : space_allocated_(b->size()) { +SerialArena::SerialArena(Block* b, void* owner) : space_allocated_(b->size()) { owner_ = owner; set_head(b); set_ptr(b->Pointer(kBlockHeaderSize + ThreadSafeArena::kSerialArenaSize)); limit_ = b->Pointer(b->size() & static_cast(-8)); - arena_stats_ = stats; } SerialArena* SerialArena::New(Memory mem, void* owner, @@ -120,7 +118,7 @@ SerialArena* SerialArena::New(Memory mem, void* owner, ThreadSafeArenaStats::RecordAllocateStats( stats, /*used=*/0, /*allocated=*/mem.size, /*wasted=*/0); auto b = new (mem.ptr) Block{nullptr, mem.size}; - return new (b->Pointer(kBlockHeaderSize)) SerialArena(b, owner, stats); + return new (b->Pointer(kBlockHeaderSize)) SerialArena(b, owner); } template @@ -137,29 +135,32 @@ SerialArena::Memory SerialArena::Free(Deallocator deallocator) { PROTOBUF_NOINLINE void* SerialArena::AllocateAlignedFallback(size_t n, - const AllocationPolicy* policy) { - AllocateNewBlock(n, policy); + const AllocationPolicy* policy, + ThreadSafeArenaStats* stats) { + AllocateNewBlock(n, policy, stats); return AllocateFromExisting(n); } PROTOBUF_NOINLINE void* SerialArena::AllocateAlignedWithCleanupFallback( size_t n, size_t align, void (*destructor)(void*), - const AllocationPolicy* policy) { + const AllocationPolicy* policy, ThreadSafeArenaStats* stats) { size_t required = AlignUpTo(n, align) + cleanup::Size(destructor); - AllocateNewBlock(required, policy); + AllocateNewBlock(required, policy, stats); return AllocateFromExistingWithCleanupFallback(n, align, destructor); } PROTOBUF_NOINLINE void SerialArena::AddCleanupFallback(void* elem, void (*destructor)(void*), - const AllocationPolicy* policy) { + const AllocationPolicy* policy, + ThreadSafeArenaStats* stats) { size_t required = cleanup::Size(destructor); - AllocateNewBlock(required, policy); + AllocateNewBlock(required, policy, stats); AddCleanupFromExisting(elem, destructor); } -void SerialArena::AllocateNewBlock(size_t n, const AllocationPolicy* policy) { +void SerialArena::AllocateNewBlock(size_t n, const AllocationPolicy* policy, + ThreadSafeArenaStats* stats) { // Sync limit to block head()->cleanup_nodes = limit_; @@ -181,7 +182,7 @@ void SerialArena::AllocateNewBlock(size_t n, const AllocationPolicy* policy) { space_allocated_.store( space_allocated_.load(std::memory_order_relaxed) + mem.size, std::memory_order_relaxed); - ThreadSafeArenaStats::RecordAllocateStats(arena_stats_, /*used=*/used, + ThreadSafeArenaStats::RecordAllocateStats(stats, /*used=*/used, /*allocated=*/mem.size, wasted); set_head(new (mem.ptr) Block{head(), mem.size}); set_ptr(head()->Pointer(kBlockHeaderSize)); @@ -317,12 +318,6 @@ void ThreadSafeArena::InitializeWithPolicy(void* mem, size_t size, // first block. constexpr size_t kAPSize = internal::AlignUpTo8(sizeof(AllocationPolicy)); constexpr size_t kMinimumSize = kBlockHeaderSize + kSerialArenaSize + kAPSize; - - // The value for alloc_policy_ stores whether or not allocations should be - // recorded. - alloc_policy_.set_should_record_allocs( - policy.metrics_collector != nullptr && - policy.metrics_collector->RecordAllocs()); // Make sure we have an initial block to store the AllocationPolicy. if (mem != nullptr && size >= kMinimumSize) { alloc_policy_.set_is_user_owned_initial_block(true); @@ -395,11 +390,6 @@ ThreadSafeArena::~ThreadSafeArena() { size_t space_allocated = 0; auto mem = Free(&space_allocated); - - // Policy is about to get deleted. - auto* p = alloc_policy_.get(); - ArenaMetricsCollector* collector = p ? p->metrics_collector : nullptr; - if (alloc_policy_.is_user_owned_initial_block()) { #ifdef ADDRESS_SANITIZER // Unpoison the initial block, now that it's going back to the user. @@ -409,8 +399,6 @@ ThreadSafeArena::~ThreadSafeArena() { } else { GetDeallocator(alloc_policy_.get(), &space_allocated)(mem); } - - if (collector) collector->OnDestroy(space_allocated); } SerialArena::Memory ThreadSafeArena::Free(size_t* space_allocated) { @@ -442,11 +430,8 @@ uint64_t ThreadSafeArena::Reset() { mem.ptr = nullptr; mem.size = 0; } - ArenaMetricsCollector* collector = saved_policy.metrics_collector; - if (collector) collector->OnReset(space_allocated); InitializeWithPolicy(mem.ptr, mem.size, saved_policy); } else { - GOOGLE_DCHECK(!alloc_policy_.should_record_allocs()); // Nullptr policy if (alloc_policy_.is_user_owned_initial_block()) { space_allocated += mem.size; @@ -461,15 +446,13 @@ uint64_t ThreadSafeArena::Reset() { } void* ThreadSafeArena::AllocateAlignedWithCleanup(size_t n, size_t align, - void (*destructor)(void*), - const std::type_info* type) { + void (*destructor)(void*)) { SerialArena* arena; - if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && - GetSerialArenaFast(&arena))) { - return arena->AllocateAlignedWithCleanup(n, align, destructor, - alloc_policy_.get()); + if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { + return arena->AllocateAlignedWithCleanup( + n, align, destructor, alloc_policy_.get(), arena_stats_.MutableStats()); } else { - return AllocateAlignedWithCleanupFallback(n, align, destructor, type); + return AllocateAlignedWithCleanupFallback(n, align, destructor); } } @@ -478,37 +461,15 @@ void ThreadSafeArena::AddCleanup(void* elem, void (*cleanup)(void*)) { if (PROTOBUF_PREDICT_FALSE(!GetSerialArenaFast(&arena))) { arena = GetSerialArenaFallback(&thread_cache()); } - arena->AddCleanup(elem, cleanup, AllocPolicy()); -} - -PROTOBUF_NOINLINE -void* ThreadSafeArena::AllocateAlignedFallback(size_t n, - const std::type_info* type) { - if (alloc_policy_.should_record_allocs()) { - alloc_policy_.RecordAlloc(type, n); - SerialArena* arena; - if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { - return arena->AllocateAligned(n, alloc_policy_.get()); - } - } - return GetSerialArenaFallback(&thread_cache()) - ->AllocateAligned(n, alloc_policy_.get()); + arena->AddCleanup(elem, cleanup, AllocPolicy(), arena_stats_.MutableStats()); } PROTOBUF_NOINLINE void* ThreadSafeArena::AllocateAlignedWithCleanupFallback( - size_t n, size_t align, void (*destructor)(void*), - const std::type_info* type) { - if (alloc_policy_.should_record_allocs()) { - alloc_policy_.RecordAlloc(type, internal::AlignUpTo(n, align)); - SerialArena* arena; - if (GetSerialArenaFast(&arena)) { - return arena->AllocateAlignedWithCleanup(n, align, destructor, - alloc_policy_.get()); - } - } + size_t n, size_t align, void (*destructor)(void*)) { return GetSerialArenaFallback(&thread_cache()) - ->AllocateAlignedWithCleanup(n, align, destructor, alloc_policy_.get()); + ->AllocateAlignedWithCleanup(n, align, destructor, alloc_policy_.get(), + arena_stats_.MutableStats()); } uint64_t ThreadSafeArena::SpaceAllocated() const { @@ -563,27 +524,15 @@ SerialArena* ThreadSafeArena::GetSerialArenaFallback(void* me) { } // namespace internal -PROTOBUF_FUNC_ALIGN(32) -void* Arena::AllocateAlignedNoHook(size_t n) { - return impl_.AllocateAligned(n, nullptr); -} - -PROTOBUF_FUNC_ALIGN(32) -void* Arena::AllocateAlignedWithHook(size_t n, const std::type_info* type) { - return impl_.AllocateAligned(n, type); -} +void* Arena::Allocate(size_t n) { return impl_.AllocateAligned(n); } -PROTOBUF_FUNC_ALIGN(32) -void* Arena::AllocateAlignedWithHookForArray(size_t n, - const std::type_info* type) { - return impl_.AllocateAligned(n, type); +void* Arena::AllocateForArray(size_t n) { + return impl_.AllocateAligned(n); } -PROTOBUF_FUNC_ALIGN(32) void* Arena::AllocateAlignedWithCleanup(size_t n, size_t align, - void (*destructor)(void*), - const std::type_info* type) { - return impl_.AllocateAlignedWithCleanup(n, align, destructor, type); + void (*destructor)(void*)) { + return impl_.AllocateAlignedWithCleanup(n, align, destructor); } } // namespace protobuf diff --git a/src/google/protobuf/arena.h b/src/google/protobuf/arena.h index 06e196fbb721..286f22186ead 100644 --- a/src/google/protobuf/arena.h +++ b/src/google/protobuf/arena.h @@ -165,7 +165,6 @@ struct ArenaOptions { res.max_block_size = max_block_size; res.block_alloc = block_alloc; res.block_dealloc = block_dealloc; - res.metrics_collector = MetricsCollector(); return res; } @@ -175,14 +174,6 @@ struct ArenaOptions { friend class ArenaOptionsTestFriend; }; -// Support for non-RTTI environments. (The metrics hooks API uses type -// information.) -#if PROTOBUF_RTTI -#define RTTI_TYPE_ID(type) (&typeid(type)) -#else -#define RTTI_TYPE_ID(type) (nullptr) -#endif - // Arena allocator. Arena allocation replaces ordinary (heap-based) allocation // with new/delete, and improves performance by aggregating allocations into // larger blocks and freeing allocations all at once. Protocol messages are @@ -316,14 +307,14 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { // Allocates memory with the specific size and alignment. void* AllocateAligned(size_t size, size_t align = 8) { if (align <= 8) { - return AllocateAlignedNoHook(internal::AlignUpTo8(size)); + return Allocate(internal::AlignUpTo8(size)); } else { // We are wasting space by over allocating align - 8 bytes. Compared // to a dedicated function that takes current alignment in consideration. // Such a scheme would only waste (align - 8)/2 bytes on average, but // requires a dedicated function in the outline arena allocation // functions. Possibly re-evaluate tradeoffs later. - return internal::AlignTo(AllocateAlignedNoHook(size + align - 8), align); + return internal::AlignTo(Allocate(size + align - 8), align); } } @@ -438,7 +429,6 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { template static Arena* GetOwningArena(Rank1, const U* p) { - (void) p; return nullptr; } @@ -469,7 +459,6 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { template static Arena* GetArenaForAllocation(Rank2, const U* p) { - (void) p; return nullptr; } @@ -594,16 +583,13 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { } } - // Allocate and also optionally call collector with the allocated type info - // when allocation recording is enabled. PROTOBUF_NDEBUG_INLINE void* AllocateInternal(size_t size, size_t align, - void (*destructor)(void*), - const std::type_info* type) { + void (*destructor)(void*)) { // Monitor allocation if needed. if (destructor == nullptr) { - return AllocateAlignedWithHook(size, align, type); + return AllocateAligned(size, align); } else { - return AllocateAlignedWithCleanup(size, align, destructor, type); + return AllocateAlignedWithCleanup(size, align, destructor); } } @@ -642,8 +628,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { // We count on compiler to realize that if sizeof(T) is a multiple of // 8 AlignUpTo can be elided. const size_t n = sizeof(T) * num_elements; - return static_cast( - AllocateAlignedWithHookForArray(n, alignof(T), RTTI_TYPE_ID(T))); + return static_cast(AllocateAlignedForArray(n, alignof(T))); } template @@ -652,8 +637,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { AllocateInternal(sizeof(T), alignof(T), internal::ObjectDestructor< InternalHelper::is_destructor_skippable::value, - T>::destructor, - RTTI_TYPE_ID(T)), + T>::destructor), this, std::forward(args)...); } @@ -704,9 +688,8 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { internal::ObjectDestructor::value, T>::destructor; T* result = - new (arena->AllocateInternal(sizeof(T), alignof(T), destructor, - RTTI_TYPE_ID(T))) - T(std::forward(args)...); + new (arena->AllocateInternal(sizeof(T), alignof(T), destructor)) + T(std::forward(args)...); return result; } } @@ -719,8 +702,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { auto destructor = internal::ObjectDestructor::value, T>::destructor; - return new (arena->AllocateInternal(sizeof(T), alignof(T), destructor, - RTTI_TYPE_ID(T))) + return new (arena->AllocateInternal(sizeof(T), alignof(T), destructor)) T(std::forward(args)...); } } @@ -751,42 +733,23 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { return InternalHelper::GetArena(value); } - void* AllocateAlignedWithHookForArray(size_t n, size_t align, - const std::type_info* type) { + void* AllocateAlignedForArray(size_t n, size_t align) { if (align <= 8) { - return AllocateAlignedWithHookForArray(internal::AlignUpTo8(n), type); + return AllocateForArray(internal::AlignUpTo8(n)); } else { // We are wasting space by over allocating align - 8 bytes. Compared // to a dedicated function that takes current alignment in consideration. // Such a scheme would only waste (align - 8)/2 bytes on average, but // requires a dedicated function in the outline arena allocation // functions. Possibly re-evaluate tradeoffs later. - return internal::AlignTo( - AllocateAlignedWithHookForArray(n + align - 8, type), align); + return internal::AlignTo(AllocateForArray(n + align - 8), align); } } - void* AllocateAlignedWithHook(size_t n, size_t align, - const std::type_info* type) { - if (align <= 8) { - return AllocateAlignedWithHook(internal::AlignUpTo8(n), type); - } else { - // We are wasting space by over allocating align - 8 bytes. Compared - // to a dedicated function that takes current alignment in consideration. - // Such a scheme would only waste (align - 8)/2 bytes on average, but - // requires a dedicated function in the outline arena allocation - // functions. Possibly re-evaluate tradeoffs later. - return internal::AlignTo(AllocateAlignedWithHook(n + align - 8, type), - align); - } - } - - void* AllocateAlignedNoHook(size_t n); - void* AllocateAlignedWithHook(size_t n, const std::type_info* type); - void* AllocateAlignedWithHookForArray(size_t n, const std::type_info* type); + void* Allocate(size_t n); + void* AllocateForArray(size_t n); void* AllocateAlignedWithCleanup(size_t n, size_t align, - void (*destructor)(void*), - const std::type_info* type); + void (*destructor)(void*)); template friend class internal::GenericTypeHandler; @@ -802,9 +765,6 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { friend struct internal::ArenaTestPeer; }; -// Defined above for supporting environments without RTTI. -#undef RTTI_TYPE_ID - } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/arena_impl.h b/src/google/protobuf/arena_impl.h index 7a4dfebba4c9..ad556613c1ba 100644 --- a/src/google/protobuf/arena_impl.h +++ b/src/google/protobuf/arena_impl.h @@ -249,12 +249,11 @@ struct AllocationPolicy { size_t max_block_size = kDefaultMaxBlockSize; void* (*block_alloc)(size_t) = nullptr; void (*block_dealloc)(void*, size_t) = nullptr; - ArenaMetricsCollector* metrics_collector = nullptr; bool IsDefault() const { return start_block_size == kDefaultStartBlockSize && max_block_size == kDefaultMaxBlockSize && block_alloc == nullptr && - block_dealloc == nullptr && metrics_collector == nullptr; + block_dealloc == nullptr; } }; @@ -291,22 +290,11 @@ class TaggedAllocationPolicyPtr { set_mask(v); } - bool should_record_allocs() const { - return static_cast(get_mask()); - } - void set_should_record_allocs(bool v) { set_mask(v); } - uintptr_t get_raw() const { return policy_; } - inline void RecordAlloc(const std::type_info* allocated_type, - size_t n) const { - get()->metrics_collector->OnAlloc(allocated_type, n); - } - private: enum : uintptr_t { kUserOwnedInitialBlock = 1, - kRecordAllocs = 2, }; static constexpr uintptr_t kTagsMask = 7; @@ -384,7 +372,8 @@ class PROTOBUF_EXPORT SerialArena { // the right size. We can statically know if the allocation size can benefit // from it. template - void* AllocateAligned(size_t n, const AllocationPolicy* policy) { + void* AllocateAligned(size_t n, const AllocationPolicy* policy, + ThreadSafeArenaStats* stats) { GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned. GOOGLE_DCHECK_GE(limit_, ptr()); @@ -395,7 +384,7 @@ class PROTOBUF_EXPORT SerialArena { } if (PROTOBUF_PREDICT_FALSE(!HasSpace(n))) { - return AllocateAlignedFallback(n, policy); + return AllocateAlignedFallback(n, policy, stats); } return AllocateFromExisting(n); } @@ -476,20 +465,22 @@ class PROTOBUF_EXPORT SerialArena { PROTOBUF_ALWAYS_INLINE void* AllocateAlignedWithCleanup(size_t n, size_t align, void (*destructor)(void*), - const AllocationPolicy* policy) { + const AllocationPolicy* policy, + ThreadSafeArenaStats* stats) { size_t required = AlignUpTo(n, align) + cleanup::Size(destructor); if (PROTOBUF_PREDICT_FALSE(!HasSpace(required))) { - return AllocateAlignedWithCleanupFallback(n, align, destructor, policy); + return AllocateAlignedWithCleanupFallback(n, align, destructor, policy, + stats); } return AllocateFromExistingWithCleanupFallback(n, align, destructor); } PROTOBUF_ALWAYS_INLINE void AddCleanup(void* elem, void (*destructor)(void*), - const AllocationPolicy* policy) { + const AllocationPolicy* policy, ThreadSafeArenaStats* stats) { size_t required = cleanup::Size(destructor); if (PROTOBUF_PREDICT_FALSE(!HasSpace(required))) { - return AddCleanupFallback(elem, destructor, policy); + return AddCleanupFallback(elem, destructor, policy, stats); } AddCleanupFromExisting(elem, destructor); } @@ -582,9 +573,6 @@ class PROTOBUF_EXPORT SerialArena { // Limiting address up to which memory can be allocated from the head block. char* limit_; - // For holding sampling information. The pointer is owned by the - // ThreadSafeArena that holds this serial arena. - ThreadSafeArenaStats* arena_stats_; // Repeated*Field and Arena play together to reduce memory consumption by // reusing blocks. Currently, natural growth of the repeated field types makes @@ -601,14 +589,18 @@ class PROTOBUF_EXPORT SerialArena { CachedBlock** cached_blocks_ = nullptr; // Constructor is private as only New() should be used. - inline SerialArena(Block* b, void* owner, ThreadSafeArenaStats* stats); - void* AllocateAlignedFallback(size_t n, const AllocationPolicy* policy); + inline SerialArena(Block* b, void* owner); + void* AllocateAlignedFallback(size_t n, const AllocationPolicy* policy, + ThreadSafeArenaStats* stats); void* AllocateAlignedWithCleanupFallback(size_t n, size_t align, void (*destructor)(void*), - const AllocationPolicy* policy); + const AllocationPolicy* policy, + ThreadSafeArenaStats* stats); void AddCleanupFallback(void* elem, void (*destructor)(void*), - const AllocationPolicy* policy); - void AllocateNewBlock(size_t n, const AllocationPolicy* policy); + const AllocationPolicy* policy, + ThreadSafeArenaStats* stats); + void AllocateNewBlock(size_t n, const AllocationPolicy* policy, + ThreadSafeArenaStats* stats); public: static constexpr size_t kBlockHeaderSize = AlignUpTo8(sizeof(Block)); @@ -662,13 +654,14 @@ class PROTOBUF_EXPORT ThreadSafeArena { uint64_t SpaceUsed() const; template - void* AllocateAligned(size_t n, const std::type_info* type) { + void* AllocateAligned(size_t n) { SerialArena* arena; - if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && - GetSerialArenaFast(&arena))) { - return arena->AllocateAligned(n, AllocPolicy()); + if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { + return arena->AllocateAligned(n, AllocPolicy(), + arena_stats_.MutableStats()); } else { - return AllocateAlignedFallback(n, type); + return GetSerialArenaFallback(&thread_cache()) + ->AllocateAligned(n, AllocPolicy(), arena_stats_.MutableStats()); } } @@ -686,16 +679,14 @@ class PROTOBUF_EXPORT ThreadSafeArena { // code for the happy path. PROTOBUF_NDEBUG_INLINE bool MaybeAllocateAligned(size_t n, void** out) { SerialArena* arena; - if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && - GetSerialArenaFast(&arena))) { + if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { return arena->MaybeAllocateAligned(n, out); } return false; } void* AllocateAlignedWithCleanup(size_t n, size_t align, - void (*destructor)(void*), - const std::type_info* type); + void (*destructor)(void*)); // Add object pointer and cleanup function pointer to the list. void AddCleanup(void* elem, void (*cleanup)(void*)); @@ -723,10 +714,8 @@ class PROTOBUF_EXPORT ThreadSafeArena { const AllocationPolicy* AllocPolicy() const { return alloc_policy_.get(); } void InitializeFrom(void* mem, size_t size); void InitializeWithPolicy(void* mem, size_t size, AllocationPolicy policy); - void* AllocateAlignedFallback(size_t n, const std::type_info* type); void* AllocateAlignedWithCleanupFallback(size_t n, size_t align, - void (*destructor)(void*), - const std::type_info* type); + void (*destructor)(void*)); void Init(); void SetInitialBlock(void* mem, size_t size); diff --git a/src/google/protobuf/arena_unittest.cc b/src/google/protobuf/arena_unittest.cc index 1cbfeaad5234..3f10a66e375f 100644 --- a/src/google/protobuf/arena_unittest.cc +++ b/src/google/protobuf/arena_unittest.cc @@ -45,7 +45,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" #include #include #include diff --git a/src/google/protobuf/arenastring.cc b/src/google/protobuf/arenastring.cc index 2f454c226d91..b393d42475fd 100644 --- a/src/google/protobuf/arenastring.cc +++ b/src/google/protobuf/arenastring.cc @@ -35,7 +35,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include #include @@ -98,7 +98,7 @@ class ScopedCheckPtrInvariants { #endif // NDEBUG || !GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL // Creates a heap allocated std::string value. -inline TaggedStringPtr CreateString(ConstStringParam value) { +inline TaggedStringPtr CreateString(absl::string_view value) { TaggedStringPtr res; res.SetAllocated(new std::string(value.data(), value.length())); return res; @@ -107,7 +107,7 @@ inline TaggedStringPtr CreateString(ConstStringParam value) { #ifndef GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL // Creates an arena allocated std::string value. -TaggedStringPtr CreateArenaString(Arena& arena, ConstStringParam s) { +TaggedStringPtr CreateArenaString(Arena& arena, absl::string_view s) { TaggedStringPtr res; res.SetMutableArena(Arena::Create(&arena, s.data(), s.length())); return res; @@ -117,7 +117,7 @@ TaggedStringPtr CreateArenaString(Arena& arena, ConstStringParam s) { } // namespace -void ArenaStringPtr::Set(ConstStringParam value, Arena* arena) { +void ArenaStringPtr::Set(absl::string_view value, Arena* arena) { ScopedCheckPtrInvariants check(&tagged_ptr_); if (IsDefault()) { // If we're not on an arena, skip straight to a true string to avoid diff --git a/src/google/protobuf/arenastring.h b/src/google/protobuf/arenastring.h index 6bc8395f233f..547d432f13d0 100644 --- a/src/google/protobuf/arenastring.h +++ b/src/google/protobuf/arenastring.h @@ -40,6 +40,7 @@ #include #include #include +#include "absl/strings/string_view.h" #include // must be last: @@ -259,12 +260,12 @@ struct PROTOBUF_EXPORT ArenaStringPtr { // instance known to not carry any heap allocated value. inline void InitAllocated(std::string* str, Arena* arena); - void Set(ConstStringParam value, Arena* arena); + void Set(absl::string_view value, Arena* arena); void Set(std::string&& value, Arena* arena); void Set(const char* s, Arena* arena); void Set(const char* s, size_t n, Arena* arena); - void SetBytes(ConstStringParam value, Arena* arena); + void SetBytes(absl::string_view value, Arena* arena); void SetBytes(std::string&& value, Arena* arena); void SetBytes(const char* s, Arena* arena); void SetBytes(const void* p, size_t n, Arena* arena); @@ -406,14 +407,14 @@ inline void ArenaStringPtr::InitAllocated(std::string* str, Arena* arena) { } inline void ArenaStringPtr::Set(const char* s, Arena* arena) { - Set(ConstStringParam{s}, arena); + Set(absl::string_view{s}, arena); } inline void ArenaStringPtr::Set(const char* s, size_t n, Arena* arena) { - Set(ConstStringParam{s, n}, arena); + Set(absl::string_view{s, n}, arena); } -inline void ArenaStringPtr::SetBytes(ConstStringParam value, Arena* arena) { +inline void ArenaStringPtr::SetBytes(absl::string_view value, Arena* arena) { Set(value, arena); } @@ -426,7 +427,7 @@ inline void ArenaStringPtr::SetBytes(const char* s, Arena* arena) { } inline void ArenaStringPtr::SetBytes(const void* p, size_t n, Arena* arena) { - Set(ConstStringParam{static_cast(p), n}, arena); + Set(absl::string_view{static_cast(p), n}, arena); } // Make sure rhs_arena allocated rhs, and lhs_arena allocated lhs. diff --git a/src/google/protobuf/arenastring_unittest.cc b/src/google/protobuf/arenastring_unittest.cc index 0e5ea9b39593..7f7aa71c97dd 100644 --- a/src/google/protobuf/arenastring_unittest.cc +++ b/src/google/protobuf/arenastring_unittest.cc @@ -44,7 +44,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" // Must be included last. diff --git a/src/google/protobuf/arenaz_sampler_test.cc b/src/google/protobuf/arenaz_sampler_test.cc index 31ef0cd673ea..ec9429f1373e 100644 --- a/src/google/protobuf/arenaz_sampler_test.cc +++ b/src/google/protobuf/arenaz_sampler_test.cc @@ -455,7 +455,7 @@ class ThreadSafeArenazSamplerTestThread : public Thread { public: ThreadSafeArenazSamplerTestThread(const thread::Options& options, - StringPiece name, + absl::string_view name, google::protobuf::Arena* arena, absl::Barrier* barrier) : Thread(options, name), arena_(arena), barrier_(barrier) {} @@ -483,7 +483,7 @@ TEST(ThreadSafeArenazSamplerTest, MultiThread) { std::vector> threads; for (int i = 0; i < kNumThreads; i++) { auto t = std::make_unique( - options, StrCat("thread", i), &arena, barrier); + options, absl::StrCat("thread", i), &arena, barrier); t->Start(); threads.push_back(std::move(t)); } diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index d8ffc57a3e8b..517362424b09 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -82,7 +82,6 @@ #include #include #include -#include #include @@ -1946,12 +1945,11 @@ CommandLineInterface::InterpretArgument(const std::string& name, print_mode_ = PRINT_FREE_FIELDS; } else { // Some other flag. Look it up in the generators list. - const GeneratorInfo* generator_info = - FindOrNull(generators_by_flag_name_, name); + const GeneratorInfo* generator_info = FindGeneratorByFlag(name); if (generator_info == nullptr && (plugin_prefix_.empty() || !HasSuffixString(name, "_out"))) { // Check if it's a generator option flag. - generator_info = FindOrNull(generators_by_option_name_, name); + generator_info = FindGeneratorByOption(name); if (generator_info != nullptr) { std::string* parameters = &generator_parameters_[generator_info->flag_name]; @@ -2083,16 +2081,15 @@ Parse PROTO_FILES and generate output based on the options given: the executable's own name differs.)"; } - for (GeneratorMap::iterator iter = generators_by_flag_name_.begin(); - iter != generators_by_flag_name_.end(); ++iter) { + for (const auto& kv : generators_by_flag_name_) { // FIXME(kenton): If the text is long enough it will wrap, which is ugly, // but fixing this nicely (e.g. splitting on spaces) is probably more // trouble than it's worth. std::cout << std::endl - << " " << iter->first << "=OUT_DIR " - << std::string(19 - iter->first.size(), + << " " << kv.first << "=OUT_DIR " + << std::string(19 - kv.first.size(), ' ') // Spaces for alignment. - << iter->second.help_text; + << kv.second.help_text; } std::cout << R"( @ Read options and filenames from file. If a @@ -2215,12 +2212,6 @@ bool CommandLineInterface::GenerateDependencyManifestFile( output_filenames.push_back(descriptor_set_out_name_); } - // Some consumers consider first output as the "primary output" (ninja) - // If this doesn't match their primary output it ignores the manifest. - // The unordered map for output_directories means we can't guarantee the order of files. - // Sorting here makes the "primary output" can be whichever is first alphabetically - std::sort(output_filenames.begin(), output_filenames.end()); - int fd; do { fd = open(dependency_out_name_.c_str(), @@ -2514,6 +2505,20 @@ void CommandLineInterface::GetTransitiveDependencies( } } +const CommandLineInterface::GeneratorInfo* +CommandLineInterface::FindGeneratorByFlag(const std::string& name) const { + auto it = generators_by_flag_name_.find(name); + if (it == generators_by_flag_name_.end()) return nullptr; + return &it->second; +} + +const CommandLineInterface::GeneratorInfo* +CommandLineInterface::FindGeneratorByOption(const std::string& option) const { + auto it = generators_by_option_name_.find(option); + if (it == generators_by_option_name_.end()) return nullptr; + return &it->second; +} + namespace { // Utility function for PrintFreeFieldNumbers. diff --git a/src/google/protobuf/compiler/command_line_interface.h b/src/google/protobuf/compiler/command_line_interface.h index 8064b397f8e1..21715cc75cea 100644 --- a/src/google/protobuf/compiler/command_line_interface.h +++ b/src/google/protobuf/compiler/command_line_interface.h @@ -39,6 +39,7 @@ #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ #include +#include #include #include #include @@ -49,6 +50,7 @@ #include #include +#include "absl/strings/string_view.h" // Must be included last. #include @@ -352,7 +354,11 @@ class PROTOC_EXPORT CommandLineInterface { CodeGenerator* generator; std::string help_text; }; - typedef std::map GeneratorMap; + + const GeneratorInfo* FindGeneratorByFlag(const std::string& name) const; + const GeneratorInfo* FindGeneratorByOption(const std::string& option) const; + + using GeneratorMap = std::map; GeneratorMap generators_by_flag_name_; GeneratorMap generators_by_option_name_; // A map from generator names to the parameters specified using the option diff --git a/src/google/protobuf/compiler/command_line_interface_unittest.cc b/src/google/protobuf/compiler/command_line_interface_unittest.cc index f82710fe9db1..c135c7b3b09c 100644 --- a/src/google/protobuf/compiler/command_line_interface_unittest.cc +++ b/src/google/protobuf/compiler/command_line_interface_unittest.cc @@ -56,7 +56,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" #include #include #include @@ -67,6 +67,7 @@ #include #include +#include // Must be included last. #include @@ -1779,41 +1780,6 @@ TEST_F(CommandLineInterfaceTest, "$tmpdir/bar.pb: " "$tmpdir/foo.proto\\\n $tmpdir/bar.proto"); } - -TEST_F(CommandLineInterfaceTest, - DependencyManifestFileIsOrderedByFilePath) { - CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); - CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); - - Run("protocol_compiler --dependency_out=$tmpdir/manifest_a " - "--test_out=$tmpdir " - "--descriptor_set_out=$tmpdir/a.pb --proto_path=$tmpdir bar.proto"); - - ExpectNoErrors(); - - ExpectFileContent("manifest_a", - "$tmpdir/a.pb \\\n" - "$tmpdir/bar.proto.MockCodeGenerator.test_generator: " - "$tmpdir/foo.proto\\\n $tmpdir/bar.proto"); - - Run("protocol_compiler --dependency_out=$tmpdir/manifest_z " - "--test_out=$tmpdir " - "--descriptor_set_out=$tmpdir/z.pb --proto_path=$tmpdir bar.proto"); - - ExpectNoErrors(); - - ExpectFileContent("manifest_z", - "$tmpdir/bar.proto.MockCodeGenerator.test_generator \\\n" - "$tmpdir/z.pb: " - "$tmpdir/foo.proto\\\n $tmpdir/bar.proto"); -} #endif // !_WIN32 TEST_F(CommandLineInterfaceTest, TestArgumentFile) { @@ -2622,7 +2588,7 @@ class EncodeDecodeTest : public testing::TestWithParam { bool Run(const std::string& command, bool specify_proto_files = true) { std::vector args; args.push_back("protoc"); - for (StringPiece split_piece : + for (absl::string_view split_piece : Split(command, " ", true)) { args.push_back(std::string(split_piece)); } @@ -2632,7 +2598,7 @@ class EncodeDecodeTest : public testing::TestWithParam { args.push_back("--proto_path=" + TestUtil::TestSourceDir()); break; case DESCRIPTOR_SET_IN: - args.push_back(StrCat("--descriptor_set_in=", + args.push_back(absl::StrCat("--descriptor_set_in=", unittest_proto_descriptor_set_filename_)); break; default: diff --git a/src/google/protobuf/compiler/cpp/bootstrap_unittest.cc b/src/google/protobuf/compiler/cpp/bootstrap_unittest.cc index 2619e604f5c4..93e469b7b1d6 100644 --- a/src/google/protobuf/compiler/cpp/bootstrap_unittest.cc +++ b/src/google/protobuf/compiler/cpp/bootstrap_unittest.cc @@ -42,7 +42,7 @@ // "generate_descriptor_proto.sh" and add // descriptor.pb.{h,cc} to your changelist. -#include +#include #include #include @@ -51,20 +51,25 @@ #include #include #include -#include #include #include +#include "absl/container/flat_hash_map.h" #include #include -#include #include namespace google { namespace protobuf { namespace compiler { namespace cpp { - namespace { +std::string FindWithDefault( + const absl::flat_hash_map& m, + const std::string& k, const std::string& v) { + auto it = m.find(k); + if (it == m.end()) return v; + return it->second; +} class MockErrorCollector : public MultiFileErrorCollector { public: @@ -95,7 +100,6 @@ class MockGeneratorContext : public GeneratorContext { File::GetContents(TestUtil::TestSourceDir() + "/" + physical_filename, &actual_contents, true)) << physical_filename; - CleanStringLineEndings(&actual_contents, false); #ifdef WRITE_FILES // Define to debug mismatched files. GOOGLE_CHECK_OK(File::SetContents("/tmp/expected.cc", expected_contents, @@ -135,8 +139,8 @@ const char* test_protos[][2] = { TEST(BootstrapTest, GeneratedFilesMatch) { // We need a mapping from the actual file to virtual and actual path // of the data to compare to. - std::map vpath_map; - std::map rpath_map; + absl::flat_hash_map vpath_map; + absl::flat_hash_map rpath_map; rpath_map["third_party/protobuf/test_messages_proto2"] = "net/proto2/z_generated_example/test_messages_proto2"; rpath_map["third_party/protobuf/test_messages_proto3"] = diff --git a/src/google/protobuf/compiler/cpp/enum.cc b/src/google/protobuf/compiler/cpp/enum.cc index edc10d70e404..a6539c34b07a 100644 --- a/src/google/protobuf/compiler/cpp/enum.cc +++ b/src/google/protobuf/compiler/cpp/enum.cc @@ -228,7 +228,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { } format( "inline bool $classname$_Parse(\n" - " ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, $classname$* " + " ::absl::string_view name, $classname$* " "value) " "{\n" " return ::$proto_ns$::internal::ParseNamedEnum<$classname$>(\n" @@ -237,7 +237,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { } else { format( "bool $classname$_Parse(\n" - " ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, $classname$* " + " ::absl::string_view name, $classname$* " "value);\n"); } } @@ -305,7 +305,7 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* printer) const { "}\n"); format( "static inline bool " - "$nested_name$_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name,\n" + "$nested_name$_Parse(::absl::string_view name,\n" " $resolved_name$* value) {\n" " return $classname$_Parse(name, value);\n" "}\n"); @@ -435,7 +435,7 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* printer) { CountUniqueValues(descriptor_)); format( "bool $classname$_Parse(\n" - " ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, $classname$* " + " ::absl::string_view name, $classname$* " "value) " "{\n" " int int_value;\n" diff --git a/src/google/protobuf/compiler/cpp/enum.h b/src/google/protobuf/compiler/cpp/enum.h index 610d4fc70684..88cbd6a74d46 100644 --- a/src/google/protobuf/compiler/cpp/enum.h +++ b/src/google/protobuf/compiler/cpp/enum.h @@ -61,6 +61,8 @@ class EnumGenerator { EnumGenerator(const EnumDescriptor* descriptor, const std::map& vars, const Options& options); + EnumGenerator(const EnumGenerator&) = delete; + EnumGenerator& operator=(const EnumGenerator&) = delete; ~EnumGenerator(); // Generate header code defining the enum. This code should be placed @@ -95,7 +97,6 @@ class EnumGenerator { std::map variables_; friend class FileGenerator; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/enum_field.h b/src/google/protobuf/compiler/cpp/enum_field.h index 61bae855cd64..56e046802d2a 100644 --- a/src/google/protobuf/compiler/cpp/enum_field.h +++ b/src/google/protobuf/compiler/cpp/enum_field.h @@ -48,6 +48,8 @@ namespace cpp { class EnumFieldGenerator : public FieldGenerator { public: EnumFieldGenerator(const FieldDescriptor* descriptor, const Options& options); + EnumFieldGenerator(const EnumFieldGenerator&) = delete; + EnumFieldGenerator& operator=(const EnumFieldGenerator&) = delete; ~EnumFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -66,15 +68,14 @@ class EnumFieldGenerator : public FieldGenerator { io::Printer* printer) const override; void GenerateAggregateInitializer(io::Printer* printer) const override; void GenerateCopyAggregateInitializer(io::Printer* printer) const override; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumFieldGenerator); }; class EnumOneofFieldGenerator : public EnumFieldGenerator { public: EnumOneofFieldGenerator(const FieldDescriptor* descriptor, const Options& options); + EnumOneofFieldGenerator(const EnumOneofFieldGenerator&) = delete; + EnumOneofFieldGenerator& operator=(const EnumOneofFieldGenerator&) = delete; ~EnumOneofFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -82,9 +83,6 @@ class EnumOneofFieldGenerator : public EnumFieldGenerator { void GenerateClearingCode(io::Printer* printer) const override; void GenerateSwappingCode(io::Printer* printer) const override; void GenerateConstructorCode(io::Printer* printer) const override; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumOneofFieldGenerator); }; class RepeatedEnumFieldGenerator : public FieldGenerator { @@ -92,6 +90,9 @@ class RepeatedEnumFieldGenerator : public FieldGenerator { RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor, const Options& options); ~RepeatedEnumFieldGenerator() override; + RepeatedEnumFieldGenerator(const RepeatedEnumFieldGenerator&) = delete; + RepeatedEnumFieldGenerator& operator=(const RepeatedEnumFieldGenerator&) = + delete; // implements FieldGenerator --------------------------------------- void GeneratePrivateMembers(io::Printer* printer) const override; @@ -112,9 +113,6 @@ class RepeatedEnumFieldGenerator : public FieldGenerator { io::Printer* printer) const override; void GenerateAggregateInitializer(io::Printer* printer) const override; void GenerateCopyAggregateInitializer(io::Printer* printer) const override; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedEnumFieldGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/extension.cc b/src/google/protobuf/compiler/cpp/extension.cc index 950ed9e7041b..0e1b67fa2959 100644 --- a/src/google/protobuf/compiler/cpp/extension.cc +++ b/src/google/protobuf/compiler/cpp/extension.cc @@ -38,6 +38,7 @@ #include #include +#include "absl/strings/str_cat.h" #include #include @@ -86,14 +87,14 @@ ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor, variables_["name"] = ResolveKeyword(name); variables_["constant_name"] = FieldConstantName(descriptor_); variables_["field_type"] = - StrCat(static_cast(descriptor_->type())); + absl::StrCat(static_cast(descriptor_->type())); variables_["packed"] = descriptor_->is_packed() ? "true" : "false"; std::string scope = IsScoped() ? ClassName(descriptor_->extension_scope(), false) + "::" : ""; variables_["scope"] = scope; variables_["scoped_name"] = ExtensionName(descriptor_); - variables_["number"] = StrCat(descriptor_->number()); + variables_["number"] = absl::StrCat(descriptor_->number()); bool add_verify_fn = // Only verify msgs. @@ -104,7 +105,7 @@ ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor, variables_["verify_fn"] = add_verify_fn - ? StrCat("&", FieldMessageTypeName(descriptor_, options_), + ? absl::StrCat("&", FieldMessageTypeName(descriptor_, options_), "::InternalVerify") : "nullptr"; } diff --git a/src/google/protobuf/compiler/cpp/extension.h b/src/google/protobuf/compiler/cpp/extension.h index 282931fa4375..d6d73152f02e 100644 --- a/src/google/protobuf/compiler/cpp/extension.h +++ b/src/google/protobuf/compiler/cpp/extension.h @@ -66,6 +66,8 @@ class ExtensionGenerator { explicit ExtensionGenerator(const FieldDescriptor* descriptor, const Options& options, MessageSCCAnalyzer* scc_analyzer); + ExtensionGenerator(const ExtensionGenerator&) = delete; + ExtensionGenerator& operator=(const ExtensionGenerator&) = delete; ~ExtensionGenerator(); // Header stuff. @@ -83,8 +85,6 @@ class ExtensionGenerator { MessageSCCAnalyzer* scc_analyzer_; std::map variables_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/field.cc b/src/google/protobuf/compiler/cpp/field.cc index fdd31abff111..218e23afec33 100644 --- a/src/google/protobuf/compiler/cpp/field.cc +++ b/src/google/protobuf/compiler/cpp/field.cc @@ -38,7 +38,8 @@ #include #include -#include +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" #include #include #include @@ -47,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -62,22 +64,22 @@ using internal::WireFormat; namespace { void MaySetAnnotationVariable(const Options& options, - StringPiece annotation_name, - StringPiece substitute_template_prefix, - StringPiece prepared_template, - int field_index, StringPiece access_type, + absl::string_view annotation_name, + absl::string_view substitute_template_prefix, + absl::string_view prepared_template, + int field_index, absl::string_view access_type, std::map* variables) { if (options.field_listener_options.forbidden_field_listener_events.count( std::string(annotation_name))) return; - (*variables)[StrCat("annotate_", annotation_name)] = strings::Substitute( - StrCat(substitute_template_prefix, prepared_template, ");\n"), + (*variables)[absl::StrCat("annotate_", annotation_name)] = strings::Substitute( + absl::StrCat(substitute_template_prefix, prepared_template, ");\n"), field_index, access_type); } std::string GenerateTemplateForOneofString(const FieldDescriptor* descriptor, - StringPiece proto_ns, - StringPiece field_member) { + absl::string_view proto_ns, + absl::string_view field_member) { std::string field_name = google::protobuf::compiler::cpp::FieldName(descriptor); std::string field_pointer = descriptor->options().ctype() == google::protobuf::FieldOptions::STRING @@ -85,13 +87,13 @@ std::string GenerateTemplateForOneofString(const FieldDescriptor* descriptor, : "$0"; if (descriptor->default_value_string().empty()) { - return strings::Substitute(StrCat("_internal_has_", field_name, "() ? ", + return strings::Substitute(absl::StrCat("_internal_has_", field_name, "() ? ", field_pointer, ": nullptr"), field_member); } if (descriptor->options().ctype() == google::protobuf::FieldOptions::STRING_PIECE) { - return strings::Substitute(StrCat("_internal_has_", field_name, "() ? ", + return strings::Substitute(absl::StrCat("_internal_has_", field_name, "() ? ", field_pointer, ": nullptr"), field_member); } @@ -101,15 +103,15 @@ std::string GenerateTemplateForOneofString(const FieldDescriptor* descriptor, ? "&$1.get()" : "&$1"; return strings::Substitute( - StrCat("_internal_has_", field_name, "() ? ", field_pointer, " : ", + absl::StrCat("_internal_has_", field_name, "() ? ", field_pointer, " : ", default_value_pointer), field_member, MakeDefaultFieldName(descriptor)); } std::string GenerateTemplateForSingleString(const FieldDescriptor* descriptor, - StringPiece field_member) { + absl::string_view field_member) { if (descriptor->default_value_string().empty()) { - return StrCat("&", field_member); + return absl::StrCat("&", field_member); } if (descriptor->options().ctype() == google::protobuf::FieldOptions::STRING) { @@ -118,7 +120,7 @@ std::string GenerateTemplateForSingleString(const FieldDescriptor* descriptor, MakeDefaultFieldName(descriptor)); } - return StrCat("&", field_member); + return absl::StrCat("&", field_member); } } // namespace @@ -134,12 +136,12 @@ void AddAccessorAnnotations(const FieldDescriptor* descriptor, "annotate_release", "annotate_set", "annotate_size", "annotate_clear", "annotate_add_mutable", }; - for (size_t i = 0; i < GOOGLE_ARRAYSIZE(kAccessorsAnnotations); ++i) { + for (size_t i = 0; i < ABSL_ARRAYSIZE(kAccessorsAnnotations); ++i) { (*variables)[kAccessorsAnnotations[i]] = ""; } if (options.annotate_accessor) { - for (size_t i = 0; i < GOOGLE_ARRAYSIZE(kAccessorsAnnotations); ++i) { - (*variables)[kAccessorsAnnotations[i]] = StrCat( + for (size_t i = 0; i < ABSL_ARRAYSIZE(kAccessorsAnnotations); ++i) { + (*variables)[kAccessorsAnnotations[i]] = absl::StrCat( " ", FieldName(descriptor), "_AccessedNoStrip = true;\n"); } } @@ -155,7 +157,7 @@ void AddAccessorAnnotations(const FieldDescriptor* descriptor, descriptor->real_containing_oneof(); const std::string proto_ns = (*variables)["proto_ns"]; const std::string substitute_template_prefix = - StrCat(" ", (*variables)["tracker"], ".$1<$0>(this, "); + absl::StrCat(" ", (*variables)["tracker"], ".$1<$0>(this, "); std::string prepared_template; // Flat template is needed if the prepared one is introspecting the values @@ -187,12 +189,12 @@ void AddAccessorAnnotations(const FieldDescriptor* descriptor, GenerateTemplateForSingleString(descriptor, field_member); } } else { - prepared_template = StrCat("&", field_member); + prepared_template = absl::StrCat("&", field_member); } if (descriptor->is_repeated() && !descriptor->is_map() && descriptor->type() != FieldDescriptor::TYPE_MESSAGE && descriptor->type() != FieldDescriptor::TYPE_GROUP) { - prepared_flat_template = StrCat("&", field_member); + prepared_flat_template = absl::StrCat("&", field_member); } else { prepared_flat_template = prepared_template; } @@ -240,14 +242,14 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor, (*variables)["ns"] = Namespace(descriptor, options); (*variables)["name"] = FieldName(descriptor); - (*variables)["index"] = StrCat(descriptor->index()); - (*variables)["number"] = StrCat(descriptor->number()); + (*variables)["index"] = absl::StrCat(descriptor->index()); + (*variables)["number"] = absl::StrCat(descriptor->number()); (*variables)["classname"] = ClassName(FieldScope(descriptor), false); (*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type()); bool split = ShouldSplit(descriptor, options); (*variables)["field"] = FieldMemberName(descriptor, split); - (*variables)["tag_size"] = StrCat( + (*variables)["tag_size"] = absl::StrCat( WireFormat::TagSize(descriptor->number(), descriptor->type())); (*variables)["deprecated_attr"] = DeprecatedAttribute(options, descriptor); @@ -271,12 +273,12 @@ void FieldGenerator::SetHasBitIndex(int32_t has_bit_index) { GOOGLE_CHECK_EQ(has_bit_index, -1); return; } - variables_["set_hasbit"] = StrCat( + variables_["set_hasbit"] = absl::StrCat( variables_["has_bits"], "[", has_bit_index / 32, "] |= 0x", - strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;"); - variables_["clear_hasbit"] = StrCat( + absl::Hex(1u << (has_bit_index % 32), absl::kZeroPad8), "u;"); + variables_["clear_hasbit"] = absl::StrCat( variables_["has_bits"], "[", has_bit_index / 32, "] &= ~0x", - strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;"); + absl::Hex(1u << (has_bit_index % 32), absl::kZeroPad8), "u;"); } void FieldGenerator::SetInlinedStringIndex(int32_t inlined_string_index) { @@ -287,16 +289,16 @@ void FieldGenerator::SetInlinedStringIndex(int32_t inlined_string_index) { // The first bit is the tracking bit for on demand registering ArenaDtor. GOOGLE_CHECK_GT(inlined_string_index, 0) << "_inlined_string_donated_'s bit 0 is reserved for arena dtor tracking"; - variables_["inlined_string_donated"] = StrCat( + variables_["inlined_string_donated"] = absl::StrCat( "(", variables_["inlined_string_donated_array"], "[", inlined_string_index / 32, "] & 0x", - strings::Hex(1u << (inlined_string_index % 32), strings::ZERO_PAD_8), + absl::Hex(1u << (inlined_string_index % 32), absl::kZeroPad8), "u) != 0;"); variables_["donating_states_word"] = - StrCat(variables_["inlined_string_donated_array"], "[", + absl::StrCat(variables_["inlined_string_donated_array"], "[", inlined_string_index / 32, "]"); - variables_["mask_for_undonate"] = StrCat( - "~0x", strings::Hex(1u << (inlined_string_index % 32), strings::ZERO_PAD_8), + variables_["mask_for_undonate"] = absl::StrCat( + "~0x", absl::Hex(1u << (inlined_string_index % 32), absl::kZeroPad8), "u"); } diff --git a/src/google/protobuf/compiler/cpp/field.h b/src/google/protobuf/compiler/cpp/field.h index 3fcbda371c59..dbd6db09d000 100644 --- a/src/google/protobuf/compiler/cpp/field.h +++ b/src/google/protobuf/compiler/cpp/field.h @@ -74,6 +74,8 @@ class FieldGenerator { explicit FieldGenerator(const FieldDescriptor* descriptor, const Options& options) : descriptor_(descriptor), options_(options) {} + FieldGenerator(const FieldGenerator&) = delete; + FieldGenerator& operator=(const FieldGenerator&) = delete; virtual ~FieldGenerator(); virtual void GenerateSerializeWithCachedSizes( io::Printer* printer) const final{}; @@ -219,9 +221,6 @@ class FieldGenerator { const FieldDescriptor* descriptor_; const Options& options_; std::map variables_; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator); }; // Convenience class which constructs FieldGenerators for a Descriptor. @@ -229,6 +228,8 @@ class FieldGeneratorMap { public: FieldGeneratorMap(const Descriptor* descriptor, const Options& options, MessageSCCAnalyzer* scc_analyzer); + FieldGeneratorMap(const FieldGeneratorMap&) = delete; + FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete; ~FieldGeneratorMap(); const FieldGenerator& get(const FieldDescriptor* field) const; @@ -255,8 +256,6 @@ class FieldGeneratorMap { static FieldGenerator* MakeGenerator(const FieldDescriptor* field, const Options& options, MessageSCCAnalyzer* scc_analyzer); - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/file.cc b/src/google/protobuf/compiler/cpp/file.cc index 0a73e3e4185b..3809633b9ed9 100644 --- a/src/google/protobuf/compiler/cpp/file.cc +++ b/src/google/protobuf/compiler/cpp/file.cc @@ -45,6 +45,8 @@ #include #include #include +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include #include #include @@ -172,7 +174,7 @@ void FileGenerator::GenerateMacroUndefs(io::Printer* printer) { for (int i = 0; i < fields.size(); i++) { const std::string& name = fields[i]->name(); static const char* kMacroNames[] = {"major", "minor"}; - for (int j = 0; j < GOOGLE_ARRAYSIZE(kMacroNames); ++j) { + for (int j = 0; j < ABSL_ARRAYSIZE(kMacroNames); ++j) { if (name == kMacroNames[j]) { names_to_undef.push_back(name); break; @@ -500,7 +502,7 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, " $1$ _instance;\n" " };\n" "};\n", - StrCat(generator->classname_, "::Impl_::Split")); + absl::StrCat(generator->classname_, "::Impl_::Split")); // NO_DESTROY is not necessary for correctness. The empty destructor is // enough. However, the empty destructor fails to be elided in some // configurations (like non-opt or with certain sanitizers). NO_DESTROY is @@ -879,7 +881,7 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { format("{ "); for (int i = 0; i < file_data.size();) { for (int j = 0; j < kBytesPerLine && i < file_data.size(); ++i, ++j) { - format("'$1$', ", CEscape(file_data.substr(i, 1))); + format("'$1$', ", absl::CEscape(file_data.substr(i, 1))); } format("\n"); } @@ -890,7 +892,7 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { for (int i = 0; i < file_data.size(); i += kBytesPerLine) { format( "\"$1$\"\n", - EscapeTrigraphs(CEscape(file_data.substr(i, kBytesPerLine)))); + EscapeTrigraphs(absl::CEscape(file_data.substr(i, kBytesPerLine)))); } } format(";\n"); @@ -925,7 +927,7 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { // so disable for now. bool eager = false; format( - "static ::_pbi::once_flag $desc_table$_once;\n" + "static ::absl::once_flag $desc_table$_once;\n" "const ::_pbi::DescriptorTable $desc_table$ = {\n" " false, $1$, $2$, $3$,\n" " \"$filename$\",\n" diff --git a/src/google/protobuf/compiler/cpp/file.h b/src/google/protobuf/compiler/cpp/file.h index ca05361b4244..063610f2f660 100644 --- a/src/google/protobuf/compiler/cpp/file.h +++ b/src/google/protobuf/compiler/cpp/file.h @@ -70,6 +70,8 @@ class FileGenerator { public: // See generator.cc for the meaning of dllexport_decl. FileGenerator(const FileDescriptor* file, const Options& options); + FileGenerator(const FileGenerator&) = delete; + FileGenerator& operator=(const FileGenerator&) = delete; ~FileGenerator(); // Shared code between the two header generators below. @@ -197,8 +199,6 @@ class FileGenerator { std::vector> enum_generators_; std::vector> service_generators_; std::vector> extension_generators_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/generator.cc b/src/google/protobuf/compiler/cpp/generator.cc index f26bb6b97e69..f7ce405cf1de 100644 --- a/src/google/protobuf/compiler/cpp/generator.cc +++ b/src/google/protobuf/compiler/cpp/generator.cc @@ -42,6 +42,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -56,7 +57,7 @@ CppGenerator::~CppGenerator() {} namespace { std::string NumberedCcFileName(const std::string& basename, int number) { - return StrCat(basename, ".out/", number, ".cc"); + return absl::StrCat(basename, ".out/", number, ".cc"); } } // namespace diff --git a/src/google/protobuf/compiler/cpp/generator.h b/src/google/protobuf/compiler/cpp/generator.h index 1a374b9f1685..4f3da26c4c06 100644 --- a/src/google/protobuf/compiler/cpp/generator.h +++ b/src/google/protobuf/compiler/cpp/generator.h @@ -55,6 +55,8 @@ namespace cpp { class PROTOC_EXPORT CppGenerator : public CodeGenerator { public: CppGenerator(); + CppGenerator(const CppGenerator&) = delete; + CppGenerator& operator=(const CppGenerator&) = delete; ~CppGenerator() override; enum class Runtime { @@ -94,7 +96,6 @@ class PROTOC_EXPORT CppGenerator : public CodeGenerator { private: bool opensource_runtime_ = true; std::string runtime_include_base_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CppGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/helpers.cc b/src/google/protobuf/compiler/cpp/helpers.cc index 26d313a900c8..5e128afe9cb5 100644 --- a/src/google/protobuf/compiler/cpp/helpers.cc +++ b/src/google/protobuf/compiler/cpp/helpers.cc @@ -45,22 +45,26 @@ #include #include -#include -#include "absl/container/flat_hash_map.h" -#include "absl/container/flat_hash_set.h" -#include -#include "absl/synchronization/mutex.h" -#include -#include -#include #include #include #include +#include #include #include #include +#include +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" #include -#include +#include "absl/synchronization/mutex.h" +#include +#include +#include + // Must be last. #include @@ -418,7 +422,7 @@ std::string QualifiedClassName(const EnumDescriptor* d) { std::string ExtensionName(const FieldDescriptor* d) { if (const Descriptor* scope = d->extension_scope()) - return StrCat(ClassName(scope), "::", ResolveKeyword(d->name())); + return absl::StrCat(ClassName(scope), "::", ResolveKeyword(d->name())); return ResolveKeyword(d->name()); } @@ -529,15 +533,15 @@ std::string FieldName(const FieldDescriptor* field) { } std::string FieldMemberName(const FieldDescriptor* field, bool split) { - StringPiece prefix = + absl::string_view prefix = IsMapEntryMessage(field->containing_type()) ? "" : "_impl_."; - StringPiece split_prefix = split ? "_split_->" : ""; + absl::string_view split_prefix = split ? "_split_->" : ""; if (field->real_containing_oneof() == nullptr) { - return StrCat(prefix, split_prefix, FieldName(field), "_"); + return absl::StrCat(prefix, split_prefix, FieldName(field), "_"); } // Oneof fields are never split. GOOGLE_CHECK(!split); - return StrCat(prefix, field->containing_oneof()->name(), "_.", + return absl::StrCat(prefix, field->containing_oneof()->name(), "_.", FieldName(field), "_"); } @@ -551,7 +555,7 @@ std::string QualifiedOneofCaseConstantName(const FieldDescriptor* field) { GOOGLE_DCHECK(field->containing_oneof()); const std::string qualification = QualifiedClassName(field->containing_type()); - return StrCat(qualification, "::", OneofCaseConstantName(field)); + return absl::StrCat(qualification, "::", OneofCaseConstantName(field)); } std::string EnumValueName(const EnumValueDescriptor* enum_value) { @@ -596,7 +600,7 @@ std::string FieldConstantName(const FieldDescriptor* field) { // This field's camelcase name is not unique. As a hack, add the field // number to the constant name. This makes the constant rather useless, // but what can we do? - result += "_" + StrCat(field->number()); + result += "_" + absl::StrCat(field->number()); } return result; @@ -732,9 +736,9 @@ std::string Int32ToString(int number) { if (number == std::numeric_limits::min()) { // This needs to be special-cased, see explanation here: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661 - return StrCat(number + 1, " - 1"); + return absl::StrCat(number + 1, " - 1"); } else { - return StrCat(number); + return absl::StrCat(number); } } @@ -742,13 +746,13 @@ static std::string Int64ToString(int64_t number) { if (number == std::numeric_limits::min()) { // This needs to be special-cased, see explanation here: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661 - return StrCat("int64_t{", number + 1, "} - 1"); + return absl::StrCat("int64_t{", number + 1, "} - 1"); } - return StrCat("int64_t{", number, "}"); + return absl::StrCat("int64_t{", number, "}"); } static std::string UInt64ToString(uint64_t number) { - return StrCat("uint64_t{", number, "u}"); + return absl::StrCat("uint64_t{", number, "u}"); } std::string DefaultValue(const FieldDescriptor* field) { @@ -760,7 +764,7 @@ std::string DefaultValue(const Options& options, const FieldDescriptor* field) { case FieldDescriptor::CPPTYPE_INT32: return Int32ToString(field->default_value_int32()); case FieldDescriptor::CPPTYPE_UINT32: - return StrCat(field->default_value_uint32()) + "u"; + return absl::StrCat(field->default_value_uint32()) + "u"; case FieldDescriptor::CPPTYPE_INT64: return Int64ToString(field->default_value_int64()); case FieldDescriptor::CPPTYPE_UINT64: @@ -806,7 +810,7 @@ std::string DefaultValue(const Options& options, const FieldDescriptor* field) { Int32ToString(field->default_value_enum()->number())); case FieldDescriptor::CPPTYPE_STRING: return "\"" + - EscapeTrigraphs(CEscape(field->default_value_string())) + + EscapeTrigraphs(absl::CEscape(field->default_value_string())) + "\""; case FieldDescriptor::CPPTYPE_MESSAGE: return "*" + FieldMessageTypeName(field, options) + @@ -823,13 +827,13 @@ std::string DefaultValue(const Options& options, const FieldDescriptor* field) { std::string FilenameIdentifier(const std::string& filename) { std::string result; for (int i = 0; i < filename.size(); i++) { - if (ascii_isalnum(filename[i])) { + if (absl::ascii_isalnum(filename[i])) { result.push_back(filename[i]); } else { // Not alphanumeric. To avoid any possibility of name conflicts we // use the hex code for the character. - StrAppend(&result, "_", - strings::Hex(static_cast(filename[i]))); + absl::StrAppend(&result, "_", + absl::Hex(static_cast(filename[i]))); } } return result; @@ -845,9 +849,9 @@ std::string QualifiedFileLevelSymbol(const FileDescriptor* file, const std::string& name, const Options& options) { if (file->package().empty()) { - return StrCat("::", name); + return absl::StrCat("::", name); } - return StrCat(Namespace(file, options), "::", name); + return absl::StrCat(Namespace(file, options), "::", name); } // Escape C++ trigraphs by escaping question marks to \? diff --git a/src/google/protobuf/compiler/cpp/helpers.h b/src/google/protobuf/compiler/cpp/helpers.h index 12bc9bbd1c78..4f15bada386f 100644 --- a/src/google/protobuf/compiler/cpp/helpers.h +++ b/src/google/protobuf/compiler/cpp/helpers.h @@ -50,6 +50,7 @@ #include #include #include +#include "absl/strings/str_cat.h" // Must be included last. #include @@ -468,7 +469,7 @@ inline bool IsCrossFileMessage(const FieldDescriptor* field) { } inline std::string MakeDefaultName(const FieldDescriptor* field) { - return StrCat("_i_give_permission_to_break_this_code_default_", + return absl::StrCat("_i_give_permission_to_break_this_code_default_", FieldName(field), "_"); } @@ -483,11 +484,11 @@ inline std::string MakeDefaultName(const FieldDescriptor* field) { // exists at some nested level like: // internal_container_._i_give_permission_to_break_this_code_default_field_; inline std::string MakeDefaultFieldName(const FieldDescriptor* field) { - return StrCat("Impl_::", MakeDefaultName(field)); + return absl::StrCat("Impl_::", MakeDefaultName(field)); } inline std::string MakeVarintCachedSizeName(const FieldDescriptor* field) { - return StrCat("_", FieldName(field), "_cached_byte_size_"); + return absl::StrCat("_", FieldName(field), "_cached_byte_size_"); } // Semantically distinct from MakeVarintCachedSizeName in that it gives the C++ @@ -503,7 +504,7 @@ inline std::string MakeVarintCachedSizeName(const FieldDescriptor* field) { // internal_container_._field_cached_byte_size_; inline std::string MakeVarintCachedSizeFieldName(const FieldDescriptor* field, bool split) { - return StrCat("_impl_.", split ? "_split_->" : "", "_", + return absl::StrCat("_impl_.", split ? "_split_->" : "", "_", FieldName(field), "_cached_byte_size_"); } @@ -835,9 +836,9 @@ class PROTOC_EXPORT Formatter { template ::value>::type> static std::string ToString(I x) { - return StrCat(x); + return absl::StrCat(x); } - static std::string ToString(strings::Hex x) { return StrCat(x); } + static std::string ToString(absl::Hex x) { return absl::StrCat(x); } static std::string ToString(const FieldDescriptor* d) { return Payload(d); } static std::string ToString(const Descriptor* d) { return Payload(d); } static std::string ToString(const EnumDescriptor* d) { return Payload(d); } diff --git a/src/google/protobuf/compiler/cpp/map_field.cc b/src/google/protobuf/compiler/cpp/map_field.cc index 1810c6496b72..242de769f089 100644 --- a/src/google/protobuf/compiler/cpp/map_field.cc +++ b/src/google/protobuf/compiler/cpp/map_field.cc @@ -33,6 +33,7 @@ #include #include #include +#include "absl/strings/ascii.h" #include @@ -65,8 +66,8 @@ void SetMessageVariables(const FieldDescriptor* descriptor, (*variables)["val_wire_type"] = "TYPE_" + ToUpper(DeclaredTypeMethodName(val->type())); (*variables)["map_classname"] = ClassName(descriptor->message_type(), false); - (*variables)["number"] = StrCat(descriptor->number()); - (*variables)["tag"] = StrCat(internal::WireFormat::MakeTag(descriptor)); + (*variables)["number"] = absl::StrCat(descriptor->number()); + (*variables)["tag"] = absl::StrCat(internal::WireFormat::MakeTag(descriptor)); if (HasDescriptorMethods(descriptor->file(), options)) { (*variables)["lite"] = ""; diff --git a/src/google/protobuf/compiler/cpp/map_field.h b/src/google/protobuf/compiler/cpp/map_field.h index 678a128bd185..9bb63d74baf5 100644 --- a/src/google/protobuf/compiler/cpp/map_field.h +++ b/src/google/protobuf/compiler/cpp/map_field.h @@ -46,6 +46,8 @@ class MapFieldGenerator : public FieldGenerator { public: MapFieldGenerator(const FieldDescriptor* descriptor, const Options& options, MessageSCCAnalyzer* scc_analyzer); + MapFieldGenerator(const MapFieldGenerator&) = delete; + MapFieldGenerator& operator=(const MapFieldGenerator&) = delete; ~MapFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -71,8 +73,6 @@ class MapFieldGenerator : public FieldGenerator { private: const bool has_required_fields_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapFieldGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index d37e70c3299a..5d06fcbe450d 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -53,7 +53,11 @@ #include #include #include +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" #include +#include "absl/strings/str_join.h" +#include "absl/strings/string_view.h" #include #include #include @@ -62,7 +66,6 @@ #include #include #include -#include // Must be included last. @@ -88,21 +91,21 @@ static constexpr int kNoHasbit = -1; // masks must be non-zero. std::string ConditionalToCheckBitmasks( const std::vector& masks, bool return_success = true, - StringPiece has_bits_var = "_impl_._has_bits_") { + absl::string_view has_bits_var = "_impl_._has_bits_") { std::vector parts; for (int i = 0; i < masks.size(); i++) { if (masks[i] == 0) continue; - std::string m = StrCat("0x", strings::Hex(masks[i], strings::ZERO_PAD_8)); + std::string m = absl::StrCat("0x", absl::Hex(masks[i], absl::kZeroPad8)); // Each xor evaluates to 0 if the expected bits are present. parts.push_back( - StrCat("((", has_bits_var, "[", i, "] & ", m, ") ^ ", m, ")")); + absl::StrCat("((", has_bits_var, "[", i, "] & ", m, ") ^ ", m, ")")); } GOOGLE_CHECK(!parts.empty()); // If we have multiple parts, each expected to be 0, then bitwise-or them. std::string result = parts.size() == 1 ? parts[0] - : StrCat("(", Join(parts, "\n | "), ")"); + : absl::StrCat("(", absl::StrJoin(parts, "\n | "), ")"); return result + (return_success ? " == 0" : " != 0"); } @@ -116,7 +119,7 @@ void PrintPresenceCheck(const Formatter& format, const FieldDescriptor* field, format("cached_has_bits = $has_bits$[$1$];\n", *cached_has_word_index); } const std::string mask = - StrCat(strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); + absl::StrCat(absl::Hex(1u << (has_bit_index % 32), absl::kZeroPad8)); format("if (cached_has_bits & 0x$1$u) {\n", mask); } else { format("if (has_$1$()) {\n", FieldName(field)); @@ -496,7 +499,7 @@ void ColdChunkSkipper::OnStartChunk(int chunk, int cached_has_word_index, if (this_word != first_word) { format(" ||\n "); } - format.Set("mask", strings::Hex(mask, strings::ZERO_PAD_8)); + format.Set("mask", absl::Hex(mask, absl::kZeroPad8)); if (this_word == cached_has_word_index) { format("(cached_has_bits & 0x$mask$u) != 0"); } else { @@ -518,15 +521,15 @@ bool ColdChunkSkipper::OnEndChunk(int chunk, io::Printer* printer) { } void MaySetAnnotationVariable(const Options& options, - StringPiece annotation_name, - StringPiece injector_template_prefix, - StringPiece injector_template_suffix, + absl::string_view annotation_name, + absl::string_view injector_template_prefix, + absl::string_view injector_template_suffix, std::map* variables) { if (options.field_listener_options.forbidden_field_listener_events.count( std::string(annotation_name))) return; - (*variables)[StrCat("annotate_", annotation_name)] = strings::Substitute( - StrCat(injector_template_prefix, injector_template_suffix), + (*variables)[absl::StrCat("annotate_", annotation_name)] = strings::Substitute( + absl::StrCat(injector_template_prefix, injector_template_suffix), (*variables)["classtype"]); } @@ -555,8 +558,8 @@ void GenerateExtensionAnnotations( if (!HasTracker(descriptor, options)) { return; } - StringPiece tracker = (*variables)["tracker"]; - StringPiece extensions = (*variables)["extensions"]; + absl::string_view tracker = (*variables)["tracker"]; + absl::string_view extensions = (*variables)["extensions"]; for (const auto& annotation : accessor_annotations_to_hooks) { const std::string& annotation_name = annotation.first; const std::string& listener_call = annotation.second; @@ -565,7 +568,7 @@ void GenerateExtensionAnnotations( !StrContains(annotation_name, "clear")) { // Primitive fields accessors. // "Has" is here as users calling "has" on a repeated field is a mistake. - (*variables)[annotation_name] = StrCat( + (*variables)[annotation_name] = absl::StrCat( " ", tracker, ".", listener_call, "(this, id.number(), _proto_TypeTraits::GetPtr(id.number(), ", extensions, ", id.default_value_ref()));"); @@ -575,17 +578,17 @@ void GenerateExtensionAnnotations( // Repeated index accessors. std::string str_index = "index"; if (StrContains(annotation_name, "add")) { - str_index = StrCat(extensions, ".ExtensionSize(id.number()) - 1"); + str_index = absl::StrCat(extensions, ".ExtensionSize(id.number()) - 1"); } (*variables)[annotation_name] = - StrCat(" ", tracker, ".", listener_call, + absl::StrCat(" ", tracker, ".", listener_call, "(this, id.number(), " "_proto_TypeTraits::GetPtr(id.number(), ", extensions, ", ", str_index, "));"); } else if (StrContains(annotation_name, "list") || StrContains(annotation_name, "size")) { // Repeated full accessors. - (*variables)[annotation_name] = StrCat( + (*variables)[annotation_name] = absl::StrCat( " ", tracker, ".", listener_call, "(this, id.number(), _proto_TypeTraits::GetRepeatedPtr(id.number(), ", extensions, "));"); @@ -635,7 +638,7 @@ MessageGenerator::MessageGenerator( if (HasTracker(descriptor_, options_)) { const std::string injector_template = - StrCat(" ", variables_["tracker"], "."); + absl::StrCat(" ", variables_["tracker"], "."); MaySetAnnotationVariable(options, "serialize", injector_template, "OnSerialize(this);\n", &variables_); @@ -1079,7 +1082,7 @@ void MessageGenerator::GenerateSingularFieldHasBits( format.Set("has_array_index", has_bit_index / 32); format.Set("has_mask", - strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); + absl::Hex(1u << (has_bit_index % 32), absl::kZeroPad8)); format( "inline bool $classname$::_internal_has_$name$() const {\n" " bool value = " @@ -1216,7 +1219,7 @@ void MessageGenerator::GenerateFieldClear(const FieldDescriptor* field, int has_bit_index = HasBitIndex(field); format.Set("has_array_index", has_bit_index / 32); format.Set("has_mask", - strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); + absl::Hex(1u << (has_bit_index % 32), absl::kZeroPad8)); format("$has_bits$[$has_array_index$] &= ~0x$has_mask$u;\n"); } } @@ -1266,7 +1269,7 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* printer) { format.Set("field_name", UnderscoresToCamelCase(field->name(), true)); format.Set("oneof_name", field->containing_oneof()->name()); format.Set("oneof_index", - StrCat(field->containing_oneof()->index())); + absl::StrCat(field->containing_oneof()->index())); GenerateOneofMemberHasBits(field, format); } else { // Singular field. @@ -1525,8 +1528,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { " return $any_metadata$.PackFrom(GetArena(), message);\n" "}\n" "bool PackFrom(const ::$proto_ns$::Message& message,\n" - " ::PROTOBUF_NAMESPACE_ID::ConstStringParam " - "type_url_prefix) {\n" + " ::absl::string_view type_url_prefix) {\n" " $DCHK$_NE(&message, this);\n" " return $any_metadata$.PackFrom(GetArena(), message, " "type_url_prefix);\n" @@ -1548,8 +1550,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { "!std::is_convertible" "::value>::type>\n" "bool PackFrom(const T& message,\n" - " ::PROTOBUF_NAMESPACE_ID::ConstStringParam " - "type_url_prefix) {\n" + " ::absl::string_view type_url_prefix) {\n" " return $any_metadata$.PackFrom(GetArena(), message, " "type_url_prefix);" "}\n" @@ -1567,8 +1568,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { "}\n" "template \n" "bool PackFrom(const T& message,\n" - " ::PROTOBUF_NAMESPACE_ID::ConstStringParam " - "type_url_prefix) {\n" + " ::absl::string_view type_url_prefix) {\n" " return $any_metadata$.PackFrom(GetArena(), message, " "type_url_prefix);\n" "}\n" @@ -1581,8 +1581,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { "template bool Is() const {\n" " return $any_metadata$.Is();\n" "}\n" - "static bool ParseAnyTypeUrl(::PROTOBUF_NAMESPACE_ID::ConstStringParam " - "type_url,\n" + "static bool ParseAnyTypeUrl(::absl::string_view type_url,\n" " std::string* full_type_name);\n"); } @@ -1700,11 +1699,9 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { // Friend AnyMetadata so that it can call this FullMessageName() method. "\nprivate:\n" "friend class ::$proto_ns$::internal::AnyMetadata;\n" - "static $1$ FullMessageName() {\n" + "static ::absl::string_view FullMessageName() {\n" " return \"$full_name$\";\n" - "}\n", - options_.opensource_runtime ? "::PROTOBUF_NAMESPACE_ID::StringPiece" - : "::StringPiece"); + "}\n"); format( // TODO(gerbens) Make this private! Currently people are deriving from @@ -1859,7 +1856,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { const size_t sizeof_has_bits = HasBitsSize(); const std::string has_bits_decl = sizeof_has_bits == 0 ? "" - : StrCat("::$proto_ns$::internal::HasBits<", + : absl::StrCat("::$proto_ns$::internal::HasBits<", sizeof_has_bits, "> _has_bits_;\n"); format( @@ -2092,7 +2089,7 @@ void MessageGenerator::GenerateClassMethods(io::Printer* printer) { } format( "bool $classname$::ParseAnyTypeUrl(\n" - " ::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url,\n" + " ::absl::string_view type_url,\n" " std::string* full_type_name) {\n" " return ::_pbi::ParseAnyTypeUrl(type_url, full_type_name);\n" "}\n" @@ -2359,7 +2356,7 @@ std::pair MessageGenerator::GenerateOffsets( entries += has_bit_indices_.size(); for (int i = 0; i < has_bit_indices_.size(); i++) { const std::string index = - has_bit_indices_[i] >= 0 ? StrCat(has_bit_indices_[i]) : "~0u"; + has_bit_indices_[i] >= 0 ? absl::StrCat(has_bit_indices_[i]) : "~0u"; format("$1$,\n", index); } } @@ -2368,7 +2365,7 @@ std::pair MessageGenerator::GenerateOffsets( for (int inlined_string_index : inlined_string_indices_) { const std::string index = inlined_string_index >= 0 - ? StrCat(inlined_string_index, ", // inlined_string_index") + ? absl::StrCat(inlined_string_index, ", // inlined_string_index") : "~0u,"; format("$1$\n", index); } @@ -3112,7 +3109,7 @@ void MessageGenerator::GenerateClear(io::Printer* printer) { // Emit an if() that will let us skip the whole chunk if none are set. uint32_t chunk_mask = GenChunkMask(chunk, has_bit_indices_); std::string chunk_mask_str = - StrCat(strings::Hex(chunk_mask, strings::ZERO_PAD_8)); + absl::StrCat(absl::Hex(chunk_mask, absl::kZeroPad8)); // Check (up to) 8 has_bits at a time if we have more than one field in // this chunk. Due to field layout ordering, we may check @@ -3472,7 +3469,7 @@ void MessageGenerator::GenerateClassSpecificMergeImpl(io::Printer* printer) { // Emit an if() that will let us skip the whole chunk if none are set. uint32_t chunk_mask = GenChunkMask(chunk, has_bit_indices_); std::string chunk_mask_str = - StrCat(strings::Hex(chunk_mask, strings::ZERO_PAD_8)); + absl::StrCat(absl::Hex(chunk_mask, absl::kZeroPad8)); // Check (up to) 8 has_bits at a time if we have more than one field in // this chunk. Due to field layout ordering, we may check @@ -3520,8 +3517,8 @@ void MessageGenerator::GenerateClassSpecificMergeImpl(io::Printer* printer) { // Check hasbit, using cached bits. GOOGLE_CHECK(HasHasbit(field)); int has_bit_index = has_bit_indices_[field->index()]; - const std::string mask = StrCat( - strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); + const std::string mask = absl::StrCat( + absl::Hex(1u << (has_bit_index % 32), absl::kZeroPad8)); format("if (cached_has_bits & 0x$1$u) {\n", mask); format.Indent(); @@ -3710,7 +3707,7 @@ void MessageGenerator::GenerateSerializeOneField(io::Printer* printer, int has_bit_index = HasBitIndex(field); if (cached_has_bits_index == has_bit_index / 32) { const std::string mask = - StrCat(strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); + absl::StrCat(absl::Hex(1u << (has_bit_index % 32), absl::kZeroPad8)); format("if (cached_has_bits & 0x$1$u) {\n", mask); } else { @@ -3735,8 +3732,8 @@ void MessageGenerator::GenerateSerializeOneField(io::Printer* printer, void MessageGenerator::GenerateSerializeOneExtensionRange( io::Printer* printer, const Descriptor::ExtensionRange* range) { std::map vars = variables_; - vars["start"] = StrCat(range->start); - vars["end"] = StrCat(range->end); + vars["start"] = absl::StrCat(range->start); + vars["end"] = absl::StrCat(range->end); Formatter format(printer, vars); format("// Extension range [$start$, $end$)\n"); format( @@ -4265,7 +4262,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* printer) { // Emit an if() that will let us skip the whole chunk if none are set. uint32_t chunk_mask = GenChunkMask(chunk, has_bit_indices_); std::string chunk_mask_str = - StrCat(strings::Hex(chunk_mask, strings::ZERO_PAD_8)); + absl::StrCat(absl::Hex(chunk_mask, absl::kZeroPad8)); // Check (up to) 8 has_bits at a time if we have more than one field in // this chunk. Due to field layout ordering, we may check diff --git a/src/google/protobuf/compiler/cpp/message.h b/src/google/protobuf/compiler/cpp/message.h index b25291930216..c5a2051bd547 100644 --- a/src/google/protobuf/compiler/cpp/message.h +++ b/src/google/protobuf/compiler/cpp/message.h @@ -70,6 +70,8 @@ class MessageGenerator { const std::map& vars, int index_in_file_messages, const Options& options, MessageSCCAnalyzer* scc_analyzer); + MessageGenerator(const MessageGenerator&) = delete; + MessageGenerator& operator=(const MessageGenerator&) = delete; ~MessageGenerator(); // Append the two types of nested generators to the corresponding vector. @@ -221,7 +223,6 @@ class MessageGenerator { std::map variables_; friend class FileGenerator; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/message_field.h b/src/google/protobuf/compiler/cpp/message_field.h index 70c42c0eaccf..4f6d9101aabc 100644 --- a/src/google/protobuf/compiler/cpp/message_field.h +++ b/src/google/protobuf/compiler/cpp/message_field.h @@ -51,6 +51,8 @@ class MessageFieldGenerator : public FieldGenerator { MessageFieldGenerator(const FieldDescriptor* descriptor, const Options& options, MessageSCCAnalyzer* scc_analyzer); + MessageFieldGenerator(const MessageFieldGenerator&) = delete; + MessageFieldGenerator& operator=(const MessageFieldGenerator&) = delete; ~MessageFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -81,9 +83,6 @@ class MessageFieldGenerator : public FieldGenerator { protected: const bool implicit_weak_field_; const bool has_required_fields_; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFieldGenerator); }; class MessageOneofFieldGenerator : public MessageFieldGenerator { @@ -91,6 +90,9 @@ class MessageOneofFieldGenerator : public MessageFieldGenerator { MessageOneofFieldGenerator(const FieldDescriptor* descriptor, const Options& options, MessageSCCAnalyzer* scc_analyzer); + MessageOneofFieldGenerator(const MessageOneofFieldGenerator&) = delete; + MessageOneofFieldGenerator& operator=(const MessageOneofFieldGenerator&) = + delete; ~MessageOneofFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -106,9 +108,6 @@ class MessageOneofFieldGenerator : public MessageFieldGenerator { void GenerateDestructorCode(io::Printer* printer) const override; void GenerateConstructorCode(io::Printer* printer) const override; void GenerateIsInitialized(io::Printer* printer) const override; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageOneofFieldGenerator); }; class RepeatedMessageFieldGenerator : public FieldGenerator { @@ -116,6 +115,9 @@ class RepeatedMessageFieldGenerator : public FieldGenerator { RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, const Options& options, MessageSCCAnalyzer* scc_analyzer); + RepeatedMessageFieldGenerator(const RepeatedMessageFieldGenerator&) = delete; + RepeatedMessageFieldGenerator& operator=( + const RepeatedMessageFieldGenerator&) = delete; ~RepeatedMessageFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -136,8 +138,6 @@ class RepeatedMessageFieldGenerator : public FieldGenerator { private: const bool implicit_weak_field_; const bool has_required_fields_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedMessageFieldGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/parse_function_generator.cc b/src/google/protobuf/compiler/cpp/parse_function_generator.cc index fb238f3d742d..cb813a7e2146 100644 --- a/src/google/protobuf/compiler/cpp/parse_function_generator.cc +++ b/src/google/protobuf/compiler/cpp/parse_function_generator.cc @@ -36,6 +36,7 @@ #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -634,7 +635,7 @@ void ParseFunctionGenerator::GenerateFastFieldEntries(Formatter& format) { ? "uint32_t" : "uint64_t"; func_name = - StrCat("::_pbi::TcParser::SingularVarintNoZag1<", field_type, + absl::StrCat("::_pbi::TcParser::SingularVarintNoZag1<", field_type, ", offsetof(", // ClassName(info.field->containing_type()), // ", ", // @@ -929,7 +930,7 @@ void ParseFunctionGenerator::GenerateStrings(Formatter& format, std::string field_name; field_name = "nullptr"; if (HasDescriptorMethods(field->file(), options_)) { - field_name = StrCat("\"", field->full_name(), "\""); + field_name = absl::StrCat("\"", field->full_name(), "\""); } format("::_pbi::VerifyUTF8(str, $1$)", field_name); switch (level) { @@ -998,7 +999,7 @@ void ParseFunctionGenerator::GenerateLengthDelim(Formatter& format, format( "ctx->set_lazy_eager_verify_func($1$);\n", eager_verify - ? StrCat("&", ClassName(field->message_type(), true), + ? absl::StrCat("&", ClassName(field->message_type(), true), "::InternalVerify") : "nullptr"); } @@ -1087,12 +1088,12 @@ void ParseFunctionGenerator::GenerateFieldBody( {{"name", FieldName(field)}, {"primitive_type", PrimitiveTypeName(options_, field->cpp_type())}}); if (field->is_repeated()) { - format.AddMap({{"put_field", StrCat("add_", FieldName(field))}, - {"mutable_field", StrCat("add_", FieldName(field))}}); + format.AddMap({{"put_field", absl::StrCat("add_", FieldName(field))}, + {"mutable_field", absl::StrCat("add_", FieldName(field))}}); } else { format.AddMap( - {{"put_field", StrCat("set_", FieldName(field))}, - {"mutable_field", StrCat("mutable_", FieldName(field))}}); + {{"put_field", absl::StrCat("set_", FieldName(field))}, + {"mutable_field", absl::StrCat("mutable_", FieldName(field))}}); } uint32_t tag = WireFormatLite::MakeTag(field->number(), wiretype); switch (wiretype) { @@ -1353,7 +1354,7 @@ void PopulateFastFieldEntry(const Descriptor* descriptor, TailCallTableInfo::FastFieldInfo& info) { ..... if (name == "V8S1") { - info.func_name = StrCat( + info.func_name = absl::StrCat( "::_pbi::TcParser::SingularVarintNoZag1()"); } else if (name == "V32S1") { - info.func_name = StrCat( + info.func_name = absl::StrCat( "::_pbi::TcParser::SingularVarintNoZag1()"); } else if (name == "V64S1") { - info.func_name = StrCat( + info.func_name = absl::StrCat( "::_pbi::TcParser::SingularVarintNoZag1()"); } else { - info.func_name = StrCat("::_pbi::TcParser::Fast", name); + info.func_name = absl::StrCat("::_pbi::TcParser::Fast", name); } info.aux_idx = aux_idx; } diff --git a/src/google/protobuf/compiler/cpp/primitive_field.cc b/src/google/protobuf/compiler/cpp/primitive_field.cc index 342a3b07317b..8bde8549bc32 100644 --- a/src/google/protobuf/compiler/cpp/primitive_field.cc +++ b/src/google/protobuf/compiler/cpp/primitive_field.cc @@ -37,6 +37,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include namespace google { @@ -108,10 +109,10 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, bool cold = ShouldSplit(descriptor, options); (*variables)["cached_byte_size_field"] = MakeVarintCachedSizeFieldName(descriptor, cold); - (*variables)["tag"] = StrCat(internal::WireFormat::MakeTag(descriptor)); + (*variables)["tag"] = absl::StrCat(internal::WireFormat::MakeTag(descriptor)); int fixed_size = FixedSize(descriptor->type()); if (fixed_size != -1) { - (*variables)["fixed_size"] = StrCat(fixed_size); + (*variables)["fixed_size"] = absl::StrCat(fixed_size); } (*variables)["wire_format_field_type"] = FieldDescriptorProto_Type_Name( static_cast(descriptor->type())); diff --git a/src/google/protobuf/compiler/cpp/primitive_field.h b/src/google/protobuf/compiler/cpp/primitive_field.h index bb8a08aa1415..a307b1840fc7 100644 --- a/src/google/protobuf/compiler/cpp/primitive_field.h +++ b/src/google/protobuf/compiler/cpp/primitive_field.h @@ -49,6 +49,8 @@ class PrimitiveFieldGenerator : public FieldGenerator { public: PrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Options& options); + PrimitiveFieldGenerator(const PrimitiveFieldGenerator&) = delete; + PrimitiveFieldGenerator& operator=(const PrimitiveFieldGenerator&) = delete; ~PrimitiveFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -67,15 +69,15 @@ class PrimitiveFieldGenerator : public FieldGenerator { io::Printer* printer) const override; void GenerateAggregateInitializer(io::Printer* printer) const override; void GenerateCopyAggregateInitializer(io::Printer* printer) const override; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveFieldGenerator); }; class PrimitiveOneofFieldGenerator : public PrimitiveFieldGenerator { public: PrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, const Options& options); + PrimitiveOneofFieldGenerator(const PrimitiveOneofFieldGenerator&) = delete; + PrimitiveOneofFieldGenerator& operator=(const PrimitiveOneofFieldGenerator&) = + delete; ~PrimitiveOneofFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -83,15 +85,16 @@ class PrimitiveOneofFieldGenerator : public PrimitiveFieldGenerator { void GenerateClearingCode(io::Printer* printer) const override; void GenerateSwappingCode(io::Printer* printer) const override; void GenerateConstructorCode(io::Printer* printer) const override; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveOneofFieldGenerator); }; class RepeatedPrimitiveFieldGenerator : public FieldGenerator { public: RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Options& options); + RepeatedPrimitiveFieldGenerator(const RepeatedPrimitiveFieldGenerator&) = + delete; + RepeatedPrimitiveFieldGenerator& operator=( + const RepeatedPrimitiveFieldGenerator&) = delete; ~RepeatedPrimitiveFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -113,9 +116,6 @@ class RepeatedPrimitiveFieldGenerator : public FieldGenerator { io::Printer* printer) const override; void GenerateAggregateInitializer(io::Printer* printer) const override; void GenerateCopyAggregateInitializer(io::Printer* printer) const override; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPrimitiveFieldGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/service.cc b/src/google/protobuf/compiler/cpp/service.cc index 7a0d48054f21..6f9df3bebcdb 100644 --- a/src/google/protobuf/compiler/cpp/service.cc +++ b/src/google/protobuf/compiler/cpp/service.cc @@ -84,6 +84,8 @@ void ServiceGenerator::GenerateInterface(io::Printer* printer) { " // This class should be treated as an abstract interface.\n" " inline $classname$() {};\n" " public:\n" + " $classname$(const $classname$&) = delete;\n" + " $classname$& operator=(const $classname$&) = delete;\n" " virtual ~$classname$();\n"); printer->Indent(); @@ -114,8 +116,6 @@ void ServiceGenerator::GenerateInterface(io::Printer* printer) { printer->Outdent(); format( "\n" - " private:\n" - " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$);\n" "};\n" "\n"); } @@ -132,6 +132,8 @@ void ServiceGenerator::GenerateStubDefinition(io::Printer* printer) { "$classname$_Stub(::$proto_ns$::RpcChannel* channel);\n" "$classname$_Stub(::$proto_ns$::RpcChannel* channel,\n" " ::$proto_ns$::Service::ChannelOwnership ownership);\n" + "$classname$_Stub(const $classname$_Stub&) = delete;\n" + "$classname$_Stub& operator=(const $classname$_Stub&) = delete;\n" "~$classname$_Stub();\n" "\n" "inline ::$proto_ns$::RpcChannel* channel() { return channel_; }\n" @@ -146,7 +148,6 @@ void ServiceGenerator::GenerateStubDefinition(io::Printer* printer) { " private:\n" " ::$proto_ns$::RpcChannel* channel_;\n" " bool owns_channel_;\n" - " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$_Stub);\n" "};\n" "\n"); } @@ -244,9 +245,6 @@ void ServiceGenerator::GenerateCallMethod(io::Printer* printer) { const MethodDescriptor* method = descriptor_->method(i); Formatter format_method(printer, vars_); InitMethodVariables(method, options_, &format_method); - - // Note: down_cast does not work here because it only works on pointers, - // not references. format_method( " case $1$:\n" " $name$(controller,\n" diff --git a/src/google/protobuf/compiler/cpp/service.h b/src/google/protobuf/compiler/cpp/service.h index b3bd2d7d7376..00057638c8e5 100644 --- a/src/google/protobuf/compiler/cpp/service.h +++ b/src/google/protobuf/compiler/cpp/service.h @@ -60,6 +60,8 @@ class ServiceGenerator { explicit ServiceGenerator(const ServiceDescriptor* descriptor, const std::map& vars, const Options& options); + ServiceGenerator(const ServiceGenerator&) = delete; + ServiceGenerator& operator=(const ServiceGenerator&) = delete; ~ServiceGenerator(); // Header stuff. @@ -112,7 +114,6 @@ class ServiceGenerator { int index_in_metadata_; friend class FileGenerator; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/string_field.cc b/src/google/protobuf/compiler/cpp/string_field.cc index 9629e73df20f..090224df8d3c 100644 --- a/src/google/protobuf/compiler/cpp/string_field.cc +++ b/src/google/protobuf/compiler/cpp/string_field.cc @@ -36,6 +36,7 @@ #include #include +#include "absl/strings/str_cat.h" #include #include @@ -57,7 +58,7 @@ void SetStringVariables(const FieldDescriptor* descriptor, (*variables)["default"] = DefaultValue(options, descriptor); (*variables)["default_length"] = - StrCat(descriptor->default_value_string().length()); + absl::StrCat(descriptor->default_value_string().length()); (*variables)["default_variable_name"] = MakeDefaultName(descriptor); (*variables)["default_variable_field"] = MakeDefaultFieldName(descriptor); @@ -67,7 +68,7 @@ void SetStringVariables(const FieldDescriptor* descriptor, (*variables)["lazy_variable_args"] = ""; } else { (*variables)["lazy_variable"] = - StrCat(QualifiedClassName(descriptor->containing_type(), options), + absl::StrCat(QualifiedClassName(descriptor->containing_type(), options), "::", MakeDefaultFieldName(descriptor)); (*variables)["default_string"] = (*variables)["lazy_variable"] + ".get()"; @@ -89,7 +90,7 @@ void SetStringVariables(const FieldDescriptor* descriptor, if (options.opensource_runtime) { (*variables)["string_piece"] = "::std::string"; } else { - (*variables)["string_piece"] = "::StringPiece"; + (*variables)["string_piece"] = "::absl::string_view"; } } @@ -571,7 +572,7 @@ StringOneofFieldGenerator::StringOneofFieldGenerator( SetCommonOneofFieldVariables(descriptor, &variables_); variables_["field_name"] = UnderscoresToCamelCase(descriptor->name(), true); variables_["oneof_index"] = - StrCat(descriptor->containing_oneof()->index()); + absl::StrCat(descriptor->containing_oneof()->index()); } StringOneofFieldGenerator::~StringOneofFieldGenerator() {} @@ -716,7 +717,7 @@ void RepeatedStringFieldGenerator::GenerateAccessorDeclarations( if (!options_.opensource_runtime) { format( "$deprecated_attr$void ${1$set_$name$$}$(int index, " - "StringPiece value);\n", + "absl::string_view value);\n", descriptor_); } format( @@ -729,7 +730,7 @@ void RepeatedStringFieldGenerator::GenerateAccessorDeclarations( descriptor_); if (!options_.opensource_runtime) { format( - "$deprecated_attr$void ${1$add_$name$$}$(StringPiece value);\n", + "$deprecated_attr$void ${1$add_$name$$}$(absl::string_view value);\n", descriptor_); } format( @@ -811,7 +812,7 @@ void RepeatedStringFieldGenerator::GenerateInlineAccessorDefinitions( if (!options_.opensource_runtime) { format( "inline void " - "$classname$::set_$name$(int index, StringPiece value) {\n" + "$classname$::set_$name$(int index, absl::string_view value) {\n" " $field$.Mutable(index)->assign(value.data(), value.size());\n" "$annotate_set$" " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" @@ -847,7 +848,7 @@ void RepeatedStringFieldGenerator::GenerateInlineAccessorDefinitions( "}\n"); if (!options_.opensource_runtime) { format( - "inline void $classname$::add_$name$(StringPiece value) {\n" + "inline void $classname$::add_$name$(absl::string_view value) {\n" " $field$.Add()->assign(value.data(), value.size());\n" "$annotate_add$" " // @@protoc_insertion_point(field_add_string_piece:$full_name$)\n" diff --git a/src/google/protobuf/compiler/cpp/string_field.h b/src/google/protobuf/compiler/cpp/string_field.h index b1865ea67d95..b89c9a6c4cd9 100644 --- a/src/google/protobuf/compiler/cpp/string_field.h +++ b/src/google/protobuf/compiler/cpp/string_field.h @@ -49,6 +49,8 @@ class StringFieldGenerator : public FieldGenerator { public: StringFieldGenerator(const FieldDescriptor* descriptor, const Options& options); + StringFieldGenerator(const StringFieldGenerator&) = delete; + StringFieldGenerator& operator=(const StringFieldGenerator&) = delete; ~StringFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -78,13 +80,15 @@ class StringFieldGenerator : public FieldGenerator { private: bool inlined_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringFieldGenerator); }; class StringOneofFieldGenerator : public StringFieldGenerator { public: StringOneofFieldGenerator(const FieldDescriptor* descriptor, const Options& options); + StringOneofFieldGenerator(const StringOneofFieldGenerator&) = delete; + StringOneofFieldGenerator& operator=(const StringOneofFieldGenerator&) = + delete; ~StringOneofFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -96,15 +100,15 @@ class StringOneofFieldGenerator : public StringFieldGenerator { void GenerateMessageClearingCode(io::Printer* printer) const override; void GenerateSwappingCode(io::Printer* printer) const override; void GenerateConstructorCode(io::Printer* printer) const override; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringOneofFieldGenerator); }; class RepeatedStringFieldGenerator : public FieldGenerator { public: RepeatedStringFieldGenerator(const FieldDescriptor* descriptor, const Options& options); + RepeatedStringFieldGenerator(const RepeatedStringFieldGenerator&) = delete; + RepeatedStringFieldGenerator& operator=(const RepeatedStringFieldGenerator&) = + delete; ~RepeatedStringFieldGenerator() override; // implements FieldGenerator --------------------------------------- @@ -122,9 +126,6 @@ class RepeatedStringFieldGenerator : public FieldGenerator { void GenerateSerializeWithCachedSizesToArray( io::Printer* printer) const override; void GenerateByteSize(io::Printer* printer) const override; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedStringFieldGenerator); }; } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/unittest.inc b/src/google/protobuf/compiler/cpp/unittest.inc index 0f841a458b7a..33971bad2449 100644 --- a/src/google/protobuf/compiler/cpp/unittest.inc +++ b/src/google/protobuf/compiler/cpp/unittest.inc @@ -50,7 +50,7 @@ #include #include -#include +#include "absl/strings/string_view.h" #ifndef _MSC_VER // We exclude this large proto because it's too large for // visual studio to compile (report internal errors). @@ -64,7 +64,7 @@ #include #include #include -#include +#include "absl/base/casts.h" #include #include #include @@ -601,7 +601,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, UpcastCopyFrom) { TestUtil::SetAllFields(&message1); - const Message* source = implicit_cast(&message1); + const Message* source = absl::implicit_cast(&message1); message2.CopyFrom(*source); TestUtil::ExpectAllFieldsSet(message2); @@ -1821,7 +1821,7 @@ TEST_F(OneofTest, UpcastCopyFrom) { message1.mutable_foogroup()->set_a(123); EXPECT_TRUE(message1.has_foogroup()); - const Message* source = implicit_cast(&message1); + const Message* source = absl::implicit_cast(&message1); message2.CopyFrom(*source); EXPECT_TRUE(message2.has_foogroup()); diff --git a/src/google/protobuf/compiler/csharp/BUILD.bazel b/src/google/protobuf/compiler/csharp/BUILD.bazel index ca9eb350a03d..ce29ea6b5d98 100644 --- a/src/google/protobuf/compiler/csharp/BUILD.bazel +++ b/src/google/protobuf/compiler/csharp/BUILD.bazel @@ -58,6 +58,8 @@ cc_library( deps = [ "//:protobuf", "//src/google/protobuf/compiler:code_generator", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/strings", ], ) diff --git a/src/google/protobuf/compiler/csharp/csharp_enum.cc b/src/google/protobuf/compiler/csharp/csharp_enum.cc index 73679caf3173..6e59259b7e0c 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum.cc +++ b/src/google/protobuf/compiler/csharp/csharp_enum.cc @@ -80,12 +80,12 @@ void EnumGenerator::Generate(io::Printer* printer) { printer->Print("[pbr::OriginalName(\"$original_name$\", PreferredAlias = false)] $name$ = $number$,\n", "original_name", original_name, "name", name, - "number", StrCat(number)); + "number", absl::StrCat(number)); } else { printer->Print("[pbr::OriginalName(\"$original_name$\")] $name$ = $number$,\n", "original_name", original_name, "name", name, - "number", StrCat(number)); + "number", absl::StrCat(number)); } } printer->Outdent(); diff --git a/src/google/protobuf/compiler/csharp/csharp_field_base.cc b/src/google/protobuf/compiler/csharp/csharp_field_base.cc index 17847e36c99c..0e75af06ddd8 100644 --- a/src/google/protobuf/compiler/csharp/csharp_field_base.cc +++ b/src/google/protobuf/compiler/csharp/csharp_field_base.cc @@ -63,13 +63,13 @@ void FieldGeneratorBase::SetCommonFieldVariables( uint tag = internal::WireFormat::MakeTag(descriptor_); uint8_t tag_array[5]; io::CodedOutputStream::WriteTagToArray(tag, tag_array); - std::string tag_bytes = StrCat(tag_array[0]); + std::string tag_bytes = absl::StrCat(tag_array[0]); for (int i = 1; i < part_tag_size; i++) { - tag_bytes += ", " + StrCat(tag_array[i]); + tag_bytes += ", " + absl::StrCat(tag_array[i]); } - (*variables)["tag"] = StrCat(tag); - (*variables)["tag_size"] = StrCat(tag_size); + (*variables)["tag"] = absl::StrCat(tag); + (*variables)["tag_size"] = absl::StrCat(tag_size); (*variables)["tag_bytes"] = tag_bytes; if (descriptor_->type() == FieldDescriptor::Type::TYPE_GROUP) { @@ -77,12 +77,12 @@ void FieldGeneratorBase::SetCommonFieldVariables( descriptor_->number(), internal::WireFormatLite::WIRETYPE_END_GROUP); io::CodedOutputStream::WriteTagToArray(tag, tag_array); - tag_bytes = StrCat(tag_array[0]); + tag_bytes = absl::StrCat(tag_array[0]); for (int i = 1; i < part_tag_size; i++) { - tag_bytes += ", " + StrCat(tag_array[i]); + tag_bytes += ", " + absl::StrCat(tag_array[i]); } - variables_["end_tag"] = StrCat(tag); + variables_["end_tag"] = absl::StrCat(tag); variables_["end_tag_bytes"] = tag_bytes; } @@ -108,8 +108,8 @@ void FieldGeneratorBase::SetCommonFieldVariables( (*variables)["has_not_property_check"] = "!" + (*variables)["has_property_check"]; (*variables)["other_has_not_property_check"] = "!" + (*variables)["other_has_property_check"]; if (presenceIndex_ != -1) { - std::string hasBitsNumber = StrCat(presenceIndex_ / 32); - std::string hasBitsMask = StrCat(1 << (presenceIndex_ % 32)); + std::string hasBitsNumber = absl::StrCat(presenceIndex_ / 32); + std::string hasBitsMask = absl::StrCat(1 << (presenceIndex_ % 32)); (*variables)["has_field_check"] = "(_hasBits" + hasBitsNumber + " & " + hasBitsMask + ") != 0"; (*variables)["set_has_field"] = "_hasBits" + hasBitsNumber + " |= " + hasBitsMask; (*variables)["clear_has_field"] = "_hasBits" + hasBitsNumber + " &= ~" + hasBitsMask; @@ -325,7 +325,7 @@ std::string FieldGeneratorBase::GetStringDefaultValueInternal(const FieldDescrip else return "global::System.Text.Encoding.UTF8.GetString(global::System." "Convert.FromBase64String(\"" + - StringToBase64(descriptor->default_value_string()) + "\"), 0, " + StrCat(descriptor->default_value_string().length()) + ")"; + StringToBase64(descriptor->default_value_string()) + "\"), 0, " + absl::StrCat(descriptor->default_value_string().length()) + ")"; } std::string FieldGeneratorBase::GetBytesDefaultValueInternal(const FieldDescriptor* descriptor) { @@ -361,7 +361,7 @@ std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) } else if (std::isnan(value)) { return "double.NaN"; } - return StrCat(value) + "D"; + return absl::StrCat(value) + "D"; } case FieldDescriptor::TYPE_FLOAT: { float value = descriptor->default_value_float(); @@ -372,18 +372,18 @@ std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) } else if (std::isnan(value)) { return "float.NaN"; } - return StrCat(value) + "F"; + return absl::StrCat(value) + "F"; } case FieldDescriptor::TYPE_INT64: - return StrCat(descriptor->default_value_int64()) + "L"; + return absl::StrCat(descriptor->default_value_int64()) + "L"; case FieldDescriptor::TYPE_UINT64: - return StrCat(descriptor->default_value_uint64()) + "UL"; + return absl::StrCat(descriptor->default_value_uint64()) + "UL"; case FieldDescriptor::TYPE_INT32: - return StrCat(descriptor->default_value_int32()); + return absl::StrCat(descriptor->default_value_int32()); case FieldDescriptor::TYPE_FIXED64: - return StrCat(descriptor->default_value_uint64()) + "UL"; + return absl::StrCat(descriptor->default_value_uint64()) + "UL"; case FieldDescriptor::TYPE_FIXED32: - return StrCat(descriptor->default_value_uint32()); + return absl::StrCat(descriptor->default_value_uint32()); case FieldDescriptor::TYPE_BOOL: if (descriptor->default_value_bool()) { return "true"; @@ -395,15 +395,15 @@ std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) case FieldDescriptor::TYPE_BYTES: return GetBytesDefaultValueInternal(descriptor); case FieldDescriptor::TYPE_UINT32: - return StrCat(descriptor->default_value_uint32()); + return absl::StrCat(descriptor->default_value_uint32()); case FieldDescriptor::TYPE_SFIXED32: - return StrCat(descriptor->default_value_int32()); + return absl::StrCat(descriptor->default_value_int32()); case FieldDescriptor::TYPE_SFIXED64: - return StrCat(descriptor->default_value_int64()) + "L"; + return absl::StrCat(descriptor->default_value_int64()) + "L"; case FieldDescriptor::TYPE_SINT32: - return StrCat(descriptor->default_value_int32()); + return absl::StrCat(descriptor->default_value_int32()); case FieldDescriptor::TYPE_SINT64: - return StrCat(descriptor->default_value_int64()) + "L"; + return absl::StrCat(descriptor->default_value_int64()) + "L"; default: GOOGLE_LOG(FATAL)<< "Unknown field type."; return ""; @@ -411,7 +411,7 @@ std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) } std::string FieldGeneratorBase::number() { - return StrCat(descriptor_->number()); + return absl::StrCat(descriptor_->number()); } std::string FieldGeneratorBase::capitalized_type_name() { diff --git a/src/google/protobuf/compiler/csharp/csharp_helpers.cc b/src/google/protobuf/compiler/csharp/csharp_helpers.cc index 1b58b956691b..cca81024a7e3 100644 --- a/src/google/protobuf/compiler/csharp/csharp_helpers.cc +++ b/src/google/protobuf/compiler/csharp/csharp_helpers.cc @@ -55,6 +55,10 @@ #include #include +#include "absl/container/flat_hash_set.h" +#include "absl/strings/string_view.h" +#include "absl/strings/ascii.h" + namespace google { namespace protobuf { namespace compiler { @@ -218,18 +222,18 @@ std::string ShoutyToPascalCase(const std::string& input) { char previous = '_'; for (int i = 0; i < input.size(); i++) { char current = input[i]; - if (!ascii_isalnum(current)) { + if (!absl::ascii_isalnum(current)) { previous = current; continue; } - if (!ascii_isalnum(previous)) { - result += ascii_toupper(current); - } else if (ascii_isdigit(previous)) { - result += ascii_toupper(current); - } else if (ascii_islower(previous)) { + if (!absl::ascii_isalnum(previous)) { + result += absl::ascii_toupper(current); + } else if (absl::ascii_isdigit(previous)) { + result += absl::ascii_toupper(current); + } else if (absl::ascii_islower(previous)) { result += current; } else { - result += ascii_tolower(current); + result += absl::ascii_tolower(current); } previous = current; } @@ -247,7 +251,7 @@ std::string TryRemovePrefix(const std::string& prefix, const std::string& value) std::string prefix_to_match = ""; for (size_t i = 0; i < prefix.size(); i++) { if (prefix[i] != '_') { - prefix_to_match += ascii_tolower(prefix[i]); + prefix_to_match += absl::ascii_tolower(prefix[i]); } } @@ -260,7 +264,7 @@ std::string TryRemovePrefix(const std::string& prefix, const std::string& value) if (value[value_index] == '_') { continue; } - if (ascii_tolower(value[value_index]) != prefix_to_match[prefix_index++]) { + if (absl::ascii_tolower(value[value_index]) != prefix_to_match[prefix_index++]) { // Failed to match the prefix - bail out early. return value; } @@ -294,7 +298,7 @@ std::string GetEnumValueName(const std::string& enum_name, const std::string& en std::string result = ShoutyToPascalCase(stripped); // Just in case we have an enum name of FOO and a value of FOO_2... make sure the returned // string is a valid identifier. - if (ascii_isdigit(result[0])) { + if (absl::ascii_isdigit(result[0])) { result = "_" + result; } return result; @@ -398,7 +402,7 @@ std::string GetFieldConstantName(const FieldDescriptor* field) { std::string GetPropertyName(const FieldDescriptor* descriptor) { // Names of members declared or overridden in the message. - static const auto& reserved_member_names = *new std::unordered_set({ + static const auto& reserved_member_names = *new absl::flat_hash_set({ "Types", "Descriptor", "Equals", diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index a119bddeeef1..d887ef54c169 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc @@ -49,6 +49,9 @@ #include #include +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" + namespace google { namespace protobuf { namespace compiler { @@ -161,7 +164,7 @@ void MessageGenerator::Generate(io::Printer* printer) { for (int i = 0; i < has_bit_field_count_; i++) { // don't use arrays since all arrays are heap allocated, saving allocations // use ints instead of bytes since bytes lack bitwise operators, saving casts - printer->Print("private int _hasBits$i$;\n", "i", StrCat(i)); + printer->Print("private int _hasBits$i$;\n", "i", absl::StrCat(i)); } WriteGeneratedCodeAttributes(printer); @@ -173,10 +176,10 @@ void MessageGenerator::Generate(io::Printer* printer) { // Access the message descriptor via the relevant file descriptor or containing message descriptor. if (!descriptor_->containing_type()) { vars["descriptor_accessor"] = GetReflectionClassName(descriptor_->file()) - + ".Descriptor.MessageTypes[" + StrCat(descriptor_->index()) + "]"; + + ".Descriptor.MessageTypes[" + absl::StrCat(descriptor_->index()) + "]"; } else { vars["descriptor_accessor"] = GetClassName(descriptor_->containing_type()) - + ".Descriptor.NestedTypes[" + StrCat(descriptor_->index()) + "]"; + + ".Descriptor.NestedTypes[" + absl::StrCat(descriptor_->index()) + "]"; } WriteGeneratedCodeAttributes(printer); @@ -216,7 +219,7 @@ void MessageGenerator::Generate(io::Printer* printer) { "public const int $field_constant_name$ = $index$;\n", "field_name", fieldDescriptor->name(), "field_constant_name", GetFieldConstantName(fieldDescriptor), - "index", StrCat(fieldDescriptor->number())); + "index", absl::StrCat(fieldDescriptor->number())); std::unique_ptr generator( CreateFieldGeneratorInternal(fieldDescriptor)); generator->GenerateMembers(printer); @@ -240,7 +243,7 @@ void MessageGenerator::Generate(io::Printer* printer) { const FieldDescriptor* field = oneof->field(j); printer->Print("$oneof_case_name$ = $index$,\n", "oneof_case_name", GetOneofCaseName(field), - "index", StrCat(field->number())); + "index", absl::StrCat(field->number())); } printer->Outdent(); printer->Print("}\n"); @@ -382,7 +385,7 @@ void MessageGenerator::GenerateCloningCode(io::Printer* printer) { "public $class_name$($class_name$ other) : this() {\n"); printer->Indent(); for (int i = 0; i < has_bit_field_count_; i++) { - printer->Print("_hasBits$i$ = other._hasBits$i$;\n", "i", StrCat(i)); + printer->Print("_hasBits$i$ = other._hasBits$i$;\n", "i", absl::StrCat(i)); } // Clone non-oneof fields first (treating optional proto3 fields as non-oneof) for (int i = 0; i < descriptor_->field_count(); i++) { @@ -698,7 +701,7 @@ void MessageGenerator::GenerateMainParseLoop(io::Printer* printer, bool use_pars printer->Print( "case $end_tag$:\n" " return;\n", - "end_tag", StrCat(end_tag_)); + "end_tag", absl::StrCat(end_tag_)); } if (has_extension_ranges_) { printer->Print(vars, @@ -727,13 +730,13 @@ void MessageGenerator::GenerateMainParseLoop(io::Printer* printer, bool use_pars printer->Print( "case $packed_tag$:\n", "packed_tag", - StrCat( + absl::StrCat( internal::WireFormatLite::MakeTag( field->number(), internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED))); } - printer->Print("case $tag$: {\n", "tag", StrCat(tag)); + printer->Print("case $tag$: {\n", "tag", absl::StrCat(tag)); printer->Indent(); std::unique_ptr generator( CreateFieldGeneratorInternal(field)); diff --git a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc index e7d51168d173..3625a4355582 100644 --- a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc @@ -215,7 +215,7 @@ void PrimitiveFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { } else { printer->Print( "size += $tag_size$ + $fixed_size$;\n", - "fixed_size", StrCat(fixedSize), + "fixed_size", absl::StrCat(fixedSize), "tag_size", variables_["tag_size"]); } printer->Outdent(); diff --git a/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc b/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc index d9ab4b49b0bd..e8bc7f1e5dfb 100644 --- a/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc +++ b/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc @@ -213,7 +213,7 @@ void ReflectionClassGenerator::WriteDescriptor(io::Printer* printer) { for (int i = 0; i < file_->extension_count(); i++) { extensions.push_back(GetFullExtensionName(file_->extension(i))); } - printer->Print("new pb::Extension[] { $extensions$ }, ", "extensions", Join(extensions, ", ")); + printer->Print("new pb::Extension[] { $extensions$ }, ", "extensions", absl::StrJoin(extensions, ", ")); } else { printer->Print("null, "); @@ -265,7 +265,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript for (int i = 0; i < descriptor->field_count(); i++) { fields.push_back(GetPropertyName(descriptor->field(i))); } - printer->Print("new[]{ \"$fields$\" }, ", "fields", Join(fields, "\", \"")); + printer->Print("new[]{ \"$fields$\" }, ", "fields", absl::StrJoin(fields, "\", \"")); } else { printer->Print("null, "); @@ -278,7 +278,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript for (int i = 0; i < descriptor->oneof_decl_count(); i++) { oneofs.push_back(UnderscoresToCamelCase(descriptor->oneof_decl(i)->name(), true)); } - printer->Print("new[]{ \"$oneofs$\" }, ", "oneofs", Join(oneofs, "\", \"")); + printer->Print("new[]{ \"$oneofs$\" }, ", "oneofs", absl::StrJoin(oneofs, "\", \"")); } else { printer->Print("null, "); @@ -291,7 +291,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript for (int i = 0; i < descriptor->enum_type_count(); i++) { enums.push_back(GetClassName(descriptor->enum_type(i))); } - printer->Print("new[]{ typeof($enums$) }, ", "enums", Join(enums, "), typeof(")); + printer->Print("new[]{ typeof($enums$) }, ", "enums", absl::StrJoin(enums, "), typeof(")); } else { printer->Print("null, "); @@ -303,7 +303,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript for (int i = 0; i < descriptor->extension_count(); i++) { extensions.push_back(GetFullExtensionName(descriptor->extension(i))); } - printer->Print("new pb::Extension[] { $extensions$ }, ", "extensions", Join(extensions, ", ")); + printer->Print("new pb::Extension[] { $extensions$ }, ", "extensions", absl::StrJoin(extensions, ", ")); } else { printer->Print("null, "); diff --git a/src/google/protobuf/compiler/importer.cc b/src/google/protobuf/compiler/importer.cc index f1e26f8bdd1d..6e9b4d35d75b 100644 --- a/src/google/protobuf/compiler/importer.cc +++ b/src/google/protobuf/compiler/importer.cc @@ -50,6 +50,7 @@ #include #include #include +#include "absl/strings/str_join.h" #include #ifdef _WIN32 @@ -297,7 +298,7 @@ static std::string CanonicalizePath(std::string path) { canonical_parts.push_back(part); } } - std::string result = Join(canonical_parts, "/"); + std::string result = absl::StrJoin(canonical_parts, "/"); if (!path.empty() && path[0] == '/') { // Restore leading slash. result = '/' + result; diff --git a/src/google/protobuf/compiler/importer_unittest.cc b/src/google/protobuf/compiler/importer_unittest.cc index d2810ade1285..0c4e1f0e96f5 100644 --- a/src/google/protobuf/compiler/importer_unittest.cc +++ b/src/google/protobuf/compiler/importer_unittest.cc @@ -35,7 +35,6 @@ #include #include -#include #include #include @@ -46,8 +45,9 @@ #include #include #include +#include "absl/container/flat_hash_map.h" #include -#include + #include namespace google { @@ -93,24 +93,22 @@ class MockSourceTree : public SourceTree { MockSourceTree() {} ~MockSourceTree() override {} - void AddFile(const std::string& name, const char* contents) { + void AddFile(absl::string_view name, const char* contents) { files_[name] = contents; } // implements SourceTree ------------------------------------------- io::ZeroCopyInputStream* Open(const std::string& filename) override { - const char* contents = FindPtrOrNull(files_, filename); - if (contents == nullptr) { - return nullptr; - } else { - return new io::ArrayInputStream(contents, strlen(contents)); - } + auto it = files_.find(filename); + if (it == files_.end()) return nullptr; + return new io::ArrayInputStream(it->second, + static_cast(strlen(it->second))); } std::string GetLastErrorMessage() override { return "File not found."; } private: - std::unordered_map files_; + absl::flat_hash_map files_; }; // =================================================================== diff --git a/src/google/protobuf/compiler/java/context.cc b/src/google/protobuf/compiler/java/context.cc index 3272efbcb811..53b5d1e02472 100644 --- a/src/google/protobuf/compiler/java/context.cc +++ b/src/google/protobuf/compiler/java/context.cc @@ -32,10 +32,10 @@ #include #include +#include "absl/strings/str_cat.h" #include #include #include -#include namespace google { namespace protobuf { @@ -159,8 +159,8 @@ void Context::InitializeFieldGeneratorInfoForFields( // For fields conflicting with some other fields, we append the field // number to their field names in generated code to avoid conflicts. if (is_conflict[i]) { - info.name += StrCat(field->number()); - info.capitalized_name += StrCat(field->number()); + info.name += absl::StrCat(field->number()); + info.capitalized_name += absl::StrCat(field->number()); info.disambiguated_reason = conflict_reason[i]; } field_generator_info_map_[field] = info; @@ -169,24 +169,22 @@ void Context::InitializeFieldGeneratorInfoForFields( const FieldGeneratorInfo* Context::GetFieldGeneratorInfo( const FieldDescriptor* field) const { - const FieldGeneratorInfo* result = - FindOrNull(field_generator_info_map_, field); - if (result == NULL) { + auto it = field_generator_info_map_.find(field); + if (it == field_generator_info_map_.end()) { GOOGLE_LOG(FATAL) << "Can not find FieldGeneratorInfo for field: " << field->full_name(); } - return result; + return &it->second; } const OneofGeneratorInfo* Context::GetOneofGeneratorInfo( const OneofDescriptor* oneof) const { - const OneofGeneratorInfo* result = - FindOrNull(oneof_generator_info_map_, oneof); - if (result == NULL) { + auto it = oneof_generator_info_map_.find(oneof); + if (it == oneof_generator_info_map_.end()) { GOOGLE_LOG(FATAL) << "Can not find OneofGeneratorInfo for oneof: " << oneof->name(); } - return result; + return &it->second; } // Does this message class have generated parsing, serialization, and other diff --git a/src/google/protobuf/compiler/java/context.h b/src/google/protobuf/compiler/java/context.h index c224ab73f5e5..dc8fef155e52 100644 --- a/src/google/protobuf/compiler/java/context.h +++ b/src/google/protobuf/compiler/java/context.h @@ -31,11 +31,11 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_CONTEXT_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_CONTEXT_H__ -#include #include #include #include +#include "absl/container/flat_hash_map.h" #include namespace google { @@ -97,9 +97,9 @@ class Context { const std::vector& fields); std::unique_ptr name_resolver_; - std::map + absl::flat_hash_map field_generator_info_map_; - std::map + absl::flat_hash_map oneof_generator_info_map_; Options options_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Context); diff --git a/src/google/protobuf/compiler/java/enum.cc b/src/google/protobuf/compiler/java/enum.cc index ae3cb117a943..2a4b3259201f 100644 --- a/src/google/protobuf/compiler/java/enum.cc +++ b/src/google/protobuf/compiler/java/enum.cc @@ -39,6 +39,7 @@ #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -105,8 +106,8 @@ void EnumGenerator::Generate(io::Printer* printer) { for (int i = 0; i < canonical_values_.size(); i++) { std::map vars; vars["name"] = canonical_values_[i]->name(); - vars["index"] = StrCat(canonical_values_[i]->index()); - vars["number"] = StrCat(canonical_values_[i]->number()); + vars["index"] = absl::StrCat(canonical_values_[i]->index()); + vars["number"] = absl::StrCat(canonical_values_[i]->number()); WriteEnumValueDocComment(printer, canonical_values_[i]); if (canonical_values_[i]->options().deprecated()) { printer->Print("@java.lang.Deprecated\n"); @@ -148,7 +149,7 @@ void EnumGenerator::Generate(io::Printer* printer) { for (int i = 0; i < descriptor_->value_count(); i++) { std::map vars; vars["name"] = descriptor_->value(i)->name(); - vars["number"] = StrCat(descriptor_->value(i)->number()); + vars["number"] = absl::StrCat(descriptor_->value(i)->number()); vars["{"] = ""; vars["}"] = ""; vars["deprecation"] = descriptor_->value(i)->options().deprecated() @@ -220,7 +221,7 @@ void EnumGenerator::Generate(io::Printer* printer) { for (int i = 0; i < canonical_values_.size(); i++) { printer->Print("case $number$: return $name$;\n", "name", canonical_values_[i]->name(), "number", - StrCat(canonical_values_[i]->number())); + absl::StrCat(canonical_values_[i]->number())); } printer->Outdent(); @@ -290,7 +291,7 @@ void EnumGenerator::Generate(io::Printer* printer) { " return $file$.getDescriptor().getEnumTypes().get($index$);\n", "file", name_resolver_->GetClassName(descriptor_->file(), immutable_api_), - "index", StrCat(descriptor_->index())); + "index", absl::StrCat(descriptor_->index())); } else { printer->Print( " return $parent$.$descriptor$.getEnumTypes().get($index$);\n", @@ -303,7 +304,7 @@ void EnumGenerator::Generate(io::Printer* printer) { .no_standard_descriptor_accessor() ? "getDefaultInstance().getDescriptorForType()" : "getDescriptor()", - "index", StrCat(descriptor_->index())); + "index", absl::StrCat(descriptor_->index())); } printer->Print( diff --git a/src/google/protobuf/compiler/java/enum_field.cc b/src/google/protobuf/compiler/java/enum_field.cc index 6518d003ee6e..0ef9ef5393c4 100644 --- a/src/google/protobuf/compiler/java/enum_field.cc +++ b/src/google/protobuf/compiler/java/enum_field.cc @@ -42,6 +42,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -72,10 +73,10 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver, context->options()); (*variables)["default_number"] = - StrCat(descriptor->default_value_enum()->number()); - (*variables)["tag"] = StrCat( + absl::StrCat(descriptor->default_value_enum()->number()); + (*variables)["tag"] = absl::StrCat( static_cast(internal::WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = StrCat( + (*variables)["tag_size"] = absl::StrCat( internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor))); // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler diff --git a/src/google/protobuf/compiler/java/enum_field_lite.cc b/src/google/protobuf/compiler/java/enum_field_lite.cc index 2f94bedc5348..3b0f866a45d9 100644 --- a/src/google/protobuf/compiler/java/enum_field_lite.cc +++ b/src/google/protobuf/compiler/java/enum_field_lite.cc @@ -43,6 +43,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -80,10 +81,10 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver, context->options()); (*variables)["default_number"] = - StrCat(descriptor->default_value_enum()->number()); - (*variables)["tag"] = StrCat( + absl::StrCat(descriptor->default_value_enum()->number()); + (*variables)["tag"] = absl::StrCat( static_cast(internal::WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = StrCat( + (*variables)["tag_size"] = absl::StrCat( internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor))); // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler @@ -98,10 +99,10 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, if (HasHasbit(descriptor)) { if (!context->options().opensource_runtime) { - (*variables)["bit_field_id"] = StrCat(messageBitIndex / 32); + (*variables)["bit_field_id"] = absl::StrCat(messageBitIndex / 32); (*variables)["bit_field_name"] = GetBitFieldNameForBit(messageBitIndex); (*variables)["bit_field_mask"] = - StrCat(1 << (messageBitIndex % 32)); + absl::StrCat(1 << (messageBitIndex % 32)); } // For singular messages and builders, one bit is used for the hasField bit. (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex); diff --git a/src/google/protobuf/compiler/java/enum_lite.cc b/src/google/protobuf/compiler/java/enum_lite.cc index 692f38c5f407..5357d1391ef6 100644 --- a/src/google/protobuf/compiler/java/enum_lite.cc +++ b/src/google/protobuf/compiler/java/enum_lite.cc @@ -39,12 +39,12 @@ #include #include +#include "absl/strings/str_cat.h" #include #include #include #include #include -#include namespace google { namespace protobuf { @@ -89,7 +89,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { for (int i = 0; i < canonical_values_.size(); i++) { std::map vars; vars["name"] = canonical_values_[i]->name(); - vars["number"] = StrCat(canonical_values_[i]->number()); + vars["number"] = absl::StrCat(canonical_values_[i]->number()); WriteEnumValueDocComment(printer, canonical_values_[i]); if (canonical_values_[i]->options().deprecated()) { printer->Print("@java.lang.Deprecated\n"); @@ -123,7 +123,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { for (int i = 0; i < descriptor_->value_count(); i++) { std::map vars; vars["name"] = descriptor_->value(i)->name(); - vars["number"] = StrCat(descriptor_->value(i)->number()); + vars["number"] = absl::StrCat(descriptor_->value(i)->number()); vars["{"] = ""; vars["}"] = ""; vars["deprecation"] = descriptor_->value(i)->options().deprecated() @@ -182,7 +182,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { for (int i = 0; i < canonical_values_.size(); i++) { printer->Print("case $number$: return $name$;\n", "name", canonical_values_[i]->name(), "number", - StrCat(canonical_values_[i]->number())); + absl::StrCat(canonical_values_[i]->number())); } printer->Outdent(); diff --git a/src/google/protobuf/compiler/java/extension.cc b/src/google/protobuf/compiler/java/extension.cc index 41aca0b43b94..58a5eb809b8a 100644 --- a/src/google/protobuf/compiler/java/extension.cc +++ b/src/google/protobuf/compiler/java/extension.cc @@ -36,6 +36,7 @@ #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -74,9 +75,9 @@ void ExtensionGenerator::InitTemplateVars( vars["name"] = UnderscoresToCamelCaseCheckReserved(descriptor); vars["containing_type"] = name_resolver->GetClassName(descriptor->containing_type(), immutable); - vars["number"] = StrCat(descriptor->number()); + vars["number"] = absl::StrCat(descriptor->number()); vars["constant_name"] = FieldConstantName(descriptor); - vars["index"] = StrCat(descriptor->index()); + vars["index"] = absl::StrCat(descriptor->index()); vars["default"] = descriptor->is_repeated() ? "" : DefaultValue(descriptor, immutable, name_resolver, @@ -159,7 +160,7 @@ int ImmutableExtensionGenerator::GenerateNonNestedInitializationCode( printer->Print( "$name$.internalInit(descriptor.getExtensions().get($index$));\n", "name", UnderscoresToCamelCaseCheckReserved(descriptor_), "index", - StrCat(descriptor_->index())); + absl::StrCat(descriptor_->index())); bytecode_estimate += 21; } return bytecode_estimate; diff --git a/src/google/protobuf/compiler/java/field.cc b/src/google/protobuf/compiler/java/field.cc index 6313a2af49a5..b68d27c379f5 100644 --- a/src/google/protobuf/compiler/java/field.cc +++ b/src/google/protobuf/compiler/java/field.cc @@ -40,6 +40,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -250,7 +251,7 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor, (*variables)["capitalized_name"] = info->capitalized_name; (*variables)["disambiguated_reason"] = info->disambiguated_reason; (*variables)["constant_name"] = FieldConstantName(descriptor); - (*variables)["number"] = StrCat(descriptor->number()); + (*variables)["number"] = absl::StrCat(descriptor->number()); (*variables)["kt_dsl_builder"] = "_builder"; // These variables are placeholders to pick out the beginning and ends of // identifiers for annotations (when doing so with existing variables would @@ -285,13 +286,13 @@ void SetCommonOneofVariables(const FieldDescriptor* descriptor, (*variables)["oneof_name"] = info->name; (*variables)["oneof_capitalized_name"] = info->capitalized_name; (*variables)["oneof_index"] = - StrCat(descriptor->containing_oneof()->index()); + absl::StrCat(descriptor->containing_oneof()->index()); (*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor); (*variables)["set_oneof_case_message"] = - info->name + "Case_ = " + StrCat(descriptor->number()); + info->name + "Case_ = " + absl::StrCat(descriptor->number()); (*variables)["clear_oneof_case_message"] = info->name + "Case_ = 0"; (*variables)["has_oneof_case_message"] = - info->name + "Case_ == " + StrCat(descriptor->number()); + info->name + "Case_ == " + absl::StrCat(descriptor->number()); } void PrintExtraFieldInfo(const std::map& variables, diff --git a/src/google/protobuf/compiler/java/file.cc b/src/google/protobuf/compiler/java/file.cc index 8ca5e377456f..09d8e58d651e 100644 --- a/src/google/protobuf/compiler/java/file.cc +++ b/src/google/protobuf/compiler/java/file.cc @@ -42,6 +42,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -175,10 +176,10 @@ void MaybeRestartJavaMethod(io::Printer* printer, int* bytecode_estimate, if ((*bytecode_estimate) > bytesPerMethod) { ++(*method_num); - printer->Print(chain_statement, "method_num", StrCat(*method_num)); + printer->Print(chain_statement, "method_num", absl::StrCat(*method_num)); printer->Outdent(); printer->Print("}\n"); - printer->Print(method_decl, "method_num", StrCat(*method_num)); + printer->Print(method_decl, "method_num", absl::StrCat(*method_num)); printer->Indent(); *bytecode_estimate = 0; } @@ -567,11 +568,11 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable( " $scope$.getExtensions().get($index$),\n" " (com.google.protobuf.Message) defaultExtensionInstance);\n" "}\n", - "scope", scope, "index", StrCat(field->index()), "class", + "scope", scope, "index", absl::StrCat(field->index()), "class", name_resolver_->GetImmutableClassName(field->message_type())); } else { printer->Print("registry.add($scope$.getExtensions().get($index$));\n", - "scope", scope, "index", StrCat(field->index())); + "scope", scope, "index", absl::StrCat(field->index())); } } printer->Print( diff --git a/src/google/protobuf/compiler/java/helpers.cc b/src/google/protobuf/compiler/java/helpers.cc index 849f922c48cb..379607006a87 100644 --- a/src/google/protobuf/compiler/java/helpers.cc +++ b/src/google/protobuf/compiler/java/helpers.cc @@ -42,12 +42,14 @@ #include #include +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include #include #include #include #include -#include // for hash // Must be last. #include @@ -157,8 +159,8 @@ void PrintEnumVerifierLogic(io::Printer* printer, const char* var_name, const char* terminating_string, bool enforce_lite) { std::string enum_verifier_string = - enforce_lite ? StrCat(var_name, ".internalGetVerifier()") - : StrCat( + enforce_lite ? absl::StrCat(var_name, ".internalGetVerifier()") + : absl::StrCat( "new com.google.protobuf.Internal.EnumVerifier() {\n" " @java.lang.Override\n" " public boolean isInRange(int number) {\n" @@ -169,7 +171,7 @@ void PrintEnumVerifierLogic(io::Printer* printer, " }"); printer->Print( variables, - StrCat(enum_verifier_string, terminating_string).c_str()); + absl::StrCat(enum_verifier_string, terminating_string).c_str()); } std::string UnderscoresToCamelCase(const std::string& input, @@ -291,7 +293,7 @@ std::string EscapeKotlinKeywords(std::string name) { escaped_packages.push_back(package); } } - return Join(escaped_packages, "."); + return absl::StrJoin(escaped_packages, "."); } std::string UniqueFileScopeIdentifier(const Descriptor* descriptor) { @@ -602,14 +604,14 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable, // of FieldDescriptor to call. switch (field->cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: - return StrCat(field->default_value_int32()); + return absl::StrCat(field->default_value_int32()); case FieldDescriptor::CPPTYPE_UINT32: // Need to print as a signed int since Java has no unsigned. - return StrCat(static_cast(field->default_value_uint32())); + return absl::StrCat(static_cast(field->default_value_uint32())); case FieldDescriptor::CPPTYPE_INT64: - return StrCat(field->default_value_int64()) + "L"; + return absl::StrCat(field->default_value_int64()) + "L"; case FieldDescriptor::CPPTYPE_UINT64: - return StrCat(static_cast(field->default_value_uint64())) + + return absl::StrCat(static_cast(field->default_value_uint64())) + "L"; case FieldDescriptor::CPPTYPE_DOUBLE: { double value = field->default_value_double(); @@ -643,19 +645,19 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable, // See comments in Internal.java for gory details. return strings::Substitute( "com.google.protobuf.Internal.bytesDefaultValue(\"$0\")", - CEscape(field->default_value_string())); + absl::CEscape(field->default_value_string())); } else { return "com.google.protobuf.ByteString.EMPTY"; } } else { if (AllAscii(field->default_value_string())) { // All chars are ASCII. In this case CEscape() works fine. - return "\"" + CEscape(field->default_value_string()) + "\""; + return "\"" + absl::CEscape(field->default_value_string()) + "\""; } else { // See comments in Internal.java for gory details. return strings::Substitute( "com.google.protobuf.Internal.stringDefaultValue(\"$0\")", - CEscape(field->default_value_string())); + absl::CEscape(field->default_value_string())); } } @@ -728,7 +730,7 @@ const char* bit_masks[] = { std::string GetBitFieldName(int index) { std::string varName = "bitField"; - varName += StrCat(index); + varName += absl::StrCat(index); varName += "_"; return varName; } diff --git a/src/google/protobuf/compiler/java/message.cc b/src/google/protobuf/compiler/java/message.cc index 855090ef32e2..5db824f082da 100644 --- a/src/google/protobuf/compiler/java/message.cc +++ b/src/google/protobuf/compiler/java/message.cc @@ -44,6 +44,8 @@ #include #include #include +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" #include #include #include @@ -115,7 +117,7 @@ void ImmutableMessageGenerator::GenerateStaticVariables( std::map vars; vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); - vars["index"] = StrCat(descriptor_->index()); + vars["index"] = absl::StrCat(descriptor_->index()); vars["classname"] = name_resolver_->GetImmutableClassName(descriptor_); if (descriptor_->containing_type() != NULL) { vars["parent"] = UniqueFileScopeIdentifier(descriptor_->containing_type()); @@ -159,7 +161,7 @@ int ImmutableMessageGenerator::GenerateStaticVariableInitializers( int bytecode_estimate = 0; std::map vars; vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); - vars["index"] = StrCat(descriptor_->index()); + vars["index"] = absl::StrCat(descriptor_->index()); vars["classname"] = name_resolver_->GetImmutableClassName(descriptor_); if (descriptor_->containing_type() != NULL) { vars["parent"] = UniqueFileScopeIdentifier(descriptor_->containing_type()); @@ -432,7 +434,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { vars["oneof_name"] = context_->GetOneofGeneratorInfo(oneof)->name; vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo(oneof)->capitalized_name; - vars["oneof_index"] = StrCat((oneof)->index()); + vars["oneof_index"] = absl::StrCat((oneof)->index()); vars["{"] = ""; vars["}"] = ""; // oneofCase_ and oneof_ @@ -455,7 +457,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { "$deprecation$$field_name$($field_number$),\n", "deprecation", field->options().deprecated() ? "@java.lang.Deprecated " : "", "field_name", ToUpper(field->name()), "field_number", - StrCat(field->number())); + absl::StrCat(field->number())); printer->Annotate("field_name", field); } printer->Print("$cap_oneof_name$_NOT_SET(0);\n", "cap_oneof_name", @@ -486,7 +488,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { for (int j = 0; j < (oneof)->field_count(); j++) { const FieldDescriptor* field = (oneof)->field(j); printer->Print(" case $field_number$: return $field_name$;\n", - "field_number", StrCat(field->number()), + "field_number", absl::StrCat(field->number()), "field_name", ToUpper(field->name())); } printer->Print( @@ -519,7 +521,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { for (int i = 0; i < descriptor_->field_count(); i++) { printer->Print("public static final int $constant_name$ = $number$;\n", "constant_name", FieldConstantName(descriptor_->field(i)), - "number", StrCat(descriptor_->field(i)->number())); + "number", absl::StrCat(descriptor_->field(i)->number())); printer->Annotate("constant_name", descriptor_->field(i)); field_generators_.get(descriptor_->field(i)).GenerateMembers(printer); printer->Print("\n"); @@ -835,7 +837,7 @@ void ImmutableMessageGenerator::GenerateDescriptorMethods( printer->Print( "case $number$:\n" " return internalGet$capitalized_name$();\n", - "number", StrCat(field->number()), "capitalized_name", + "number", absl::StrCat(field->number()), "capitalized_name", info->capitalized_name); } printer->Print( @@ -1045,7 +1047,7 @@ void ImmutableMessageGenerator::GenerateEqualsAndHashCode( for (int j = 0; j < (oneof)->field_count(); j++) { const FieldDescriptor* field = (oneof)->field(j); printer->Print("case $field_number$:\n", "field_number", - StrCat(field->number())); + absl::StrCat(field->number())); printer->Indent(); field_generators_.get(field).GenerateEqualsCode(printer); printer->Print("break;\n"); @@ -1119,7 +1121,7 @@ void ImmutableMessageGenerator::GenerateEqualsAndHashCode( for (int j = 0; j < (oneof)->field_count(); j++) { const FieldDescriptor* field = (oneof)->field(j); printer->Print("case $field_number$:\n", "field_number", - StrCat(field->number())); + absl::StrCat(field->number())); printer->Indent(); field_generators_.get(field).GenerateHashCode(printer); printer->Print("break;\n"); @@ -1223,7 +1225,7 @@ void ImmutableMessageGenerator::GenerateParsingConstructor( field->number(), WireFormat::WireTypeForFieldType(field->type())); printer->Print("case $tag$: {\n", "tag", - StrCat(static_cast(tag))); + absl::StrCat(static_cast(tag))); printer->Indent(); field_generators_.get(field).GenerateParsingCode(printer); @@ -1239,7 +1241,7 @@ void ImmutableMessageGenerator::GenerateParsingConstructor( uint32_t packed_tag = WireFormatLite::MakeTag( field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED); printer->Print("case $tag$: {\n", "tag", - StrCat(static_cast(packed_tag))); + absl::StrCat(static_cast(packed_tag))); printer->Indent(); field_generators_.get(field).GenerateParsingCodeFromPacked(printer); diff --git a/src/google/protobuf/compiler/java/message_builder.cc b/src/google/protobuf/compiler/java/message_builder.cc index b126daa36c86..558f48869b2e 100644 --- a/src/google/protobuf/compiler/java/message_builder.cc +++ b/src/google/protobuf/compiler/java/message_builder.cc @@ -43,6 +43,8 @@ #include #include #include +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" #include #include #include @@ -127,7 +129,7 @@ void MessageBuilderGenerator::Generate(io::Printer* printer) { vars["oneof_name"] = context_->GetOneofGeneratorInfo(oneof)->name; vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo(oneof)->capitalized_name; - vars["oneof_index"] = StrCat(oneof->index()); + vars["oneof_index"] = absl::StrCat(oneof->index()); // oneofCase_ and oneof_ printer->Print(vars, "private int $oneof_name$Case_ = 0;\n" @@ -233,7 +235,7 @@ void MessageBuilderGenerator::GenerateDescriptorMethods(io::Printer* printer) { printer->Print( "case $number$:\n" " return internalGet$capitalized_name$();\n", - "number", StrCat(field->number()), "capitalized_name", + "number", absl::StrCat(field->number()), "capitalized_name", info->capitalized_name); } printer->Print( @@ -258,7 +260,7 @@ void MessageBuilderGenerator::GenerateDescriptorMethods(io::Printer* printer) { printer->Print( "case $number$:\n" " return internalGetMutable$capitalized_name$();\n", - "number", StrCat(field->number()), "capitalized_name", + "number", absl::StrCat(field->number()), "capitalized_name", info->capitalized_name); } printer->Print( diff --git a/src/google/protobuf/compiler/java/message_builder_lite.cc b/src/google/protobuf/compiler/java/message_builder_lite.cc index 526f949b6089..1be22bd5fadb 100644 --- a/src/google/protobuf/compiler/java/message_builder_lite.cc +++ b/src/google/protobuf/compiler/java/message_builder_lite.cc @@ -43,6 +43,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -100,7 +101,7 @@ void MessageBuilderLiteGenerator::Generate(io::Printer* printer) { vars["oneof_name"] = context_->GetOneofGeneratorInfo(oneof)->name; vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo(oneof)->capitalized_name; - vars["oneof_index"] = StrCat(oneof->index()); + vars["oneof_index"] = absl::StrCat(oneof->index()); // oneofCase() and clearOneof() printer->Print(vars, diff --git a/src/google/protobuf/compiler/java/message_field_lite.cc b/src/google/protobuf/compiler/java/message_field_lite.cc index 03e658fb3f65..dc1cd3188743 100644 --- a/src/google/protobuf/compiler/java/message_field_lite.cc +++ b/src/google/protobuf/compiler/java/message_field_lite.cc @@ -41,6 +41,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include diff --git a/src/google/protobuf/compiler/java/message_lite.cc b/src/google/protobuf/compiler/java/message_lite.cc index cee165a6bf49..f8195e5168bf 100644 --- a/src/google/protobuf/compiler/java/message_lite.cc +++ b/src/google/protobuf/compiler/java/message_lite.cc @@ -44,6 +44,8 @@ #include #include #include +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" #include #include #include @@ -239,7 +241,7 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { vars["oneof_name"] = context_->GetOneofGeneratorInfo(oneof)->name; vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo(oneof)->capitalized_name; - vars["oneof_index"] = StrCat((oneof)->index()); + vars["oneof_index"] = absl::StrCat((oneof)->index()); if (context_->options().opensource_runtime) { // oneofCase_ and oneof_ printer->Print(vars, @@ -253,7 +255,7 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { const FieldDescriptor* field = (oneof)->field(j); printer->Print("$field_name$($field_number$),\n", "field_name", ToUpper(field->name()), "field_number", - StrCat(field->number())); + absl::StrCat(field->number())); } printer->Print("$cap_oneof_name$_NOT_SET(0);\n", "cap_oneof_name", ToUpper(vars["oneof_name"])); @@ -281,7 +283,7 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { for (int j = 0; j < (oneof)->field_count(); j++) { const FieldDescriptor* field = (oneof)->field(j); printer->Print(" case $field_number$: return $field_name$;\n", - "field_number", StrCat(field->number()), + "field_number", absl::StrCat(field->number()), "field_name", ToUpper(field->name())); } printer->Print( @@ -317,7 +319,7 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { for (int i = 0; i < descriptor_->field_count(); i++) { printer->Print("public static final int $constant_name$ = $number$;\n", "constant_name", FieldConstantName(descriptor_->field(i)), - "number", StrCat(descriptor_->field(i)->number())); + "number", absl::StrCat(descriptor_->field(i)->number())); field_generators_.get(descriptor_->field(i)).GenerateMembers(printer); printer->Print("\n"); } diff --git a/src/google/protobuf/compiler/java/message_serialization.cc b/src/google/protobuf/compiler/java/message_serialization.cc index 5db627d32208..776ade79c5d5 100644 --- a/src/google/protobuf/compiler/java/message_serialization.cc +++ b/src/google/protobuf/compiler/java/message_serialization.cc @@ -32,6 +32,7 @@ #include #include +#include "absl/strings/str_cat.h" namespace google { namespace protobuf { @@ -41,7 +42,7 @@ namespace java { void GenerateSerializeExtensionRange(io::Printer* printer, const Descriptor::ExtensionRange* range) { printer->Print("extensionWriter.writeUntil($end$, output);\n", "end", - StrCat(range->end)); + absl::StrCat(range->end)); } } // namespace java diff --git a/src/google/protobuf/compiler/java/message_serialization_unittest.cc b/src/google/protobuf/compiler/java/message_serialization_unittest.cc index b2e1fcaed8bf..117550895b52 100644 --- a/src/google/protobuf/compiler/java/message_serialization_unittest.cc +++ b/src/google/protobuf/compiler/java/message_serialization_unittest.cc @@ -40,6 +40,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -60,10 +61,10 @@ int CompileJavaProto(std::string proto_file_name) { CommandLineInterface cli; cli.RegisterGenerator("--java_out", &java_generator, /*help_text=*/""); - std::string proto_path = StrCat( + std::string proto_path = absl::StrCat( "--proto_path=", TestUtil::GetTestDataPath("third_party/protobuf/compiler/java")); - std::string java_out = StrCat("--java_out=", TestTempDir()); + std::string java_out = absl::StrCat("--java_out=", TestTempDir()); const char* argv[] = { "protoc", @@ -83,11 +84,11 @@ TEST(MessageSerializationTest, CollapseAdjacentExtensionRanges) { GOOGLE_CHECK_OK(File::GetContents( // Open-source codebase does not support file::JoinPath, so we manually // concatenate instead. - StrCat(TestTempDir(), + absl::StrCat(TestTempDir(), "/TestMessageWithManyExtensionRanges.java"), &java_source, true)); - // Open-source codebase does not support constexpr StringPiece. + // Open-source codebase does not support constexpr absl::string_view. static constexpr const char kWriteUntilCall[] = "extensionWriter.writeUntil("; std::vector range_ends; diff --git a/src/google/protobuf/compiler/java/primitive_field.cc b/src/google/protobuf/compiler/java/primitive_field.cc index a60705ce2fd7..83a20b9650f6 100644 --- a/src/google/protobuf/compiler/java/primitive_field.cc +++ b/src/google/protobuf/compiler/java/primitive_field.cc @@ -43,6 +43,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -117,8 +118,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, (*variables)["capitalized_type"] = GetCapitalizedType( descriptor, /* immutable = */ true, context->options()); (*variables)["tag"] = - StrCat(static_cast(WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = StrCat( + absl::StrCat(static_cast(WireFormat::MakeTag(descriptor))); + (*variables)["tag_size"] = absl::StrCat( WireFormat::TagSize(descriptor->number(), GetType(descriptor))); if (IsReferenceType(GetJavaType(descriptor))) { (*variables)["null_check"] = @@ -139,7 +140,7 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, : ""; int fixed_size = FixedSize(GetType(descriptor)); if (fixed_size != -1) { - (*variables)["fixed_size"] = StrCat(fixed_size); + (*variables)["fixed_size"] = absl::StrCat(fixed_size); } (*variables)["on_changed"] = "onChanged();"; diff --git a/src/google/protobuf/compiler/java/primitive_field_lite.cc b/src/google/protobuf/compiler/java/primitive_field_lite.cc index 869b304c8501..00fa86e09310 100644 --- a/src/google/protobuf/compiler/java/primitive_field_lite.cc +++ b/src/google/protobuf/compiler/java/primitive_field_lite.cc @@ -43,6 +43,8 @@ #include #include #include +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" #include #include #include @@ -82,8 +84,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, (*variables)["capitalized_type"] = GetCapitalizedType( descriptor, /* immutable = */ true, context->options()); (*variables)["tag"] = - StrCat(static_cast(WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = StrCat( + absl::StrCat(static_cast(WireFormat::MakeTag(descriptor))); + (*variables)["tag_size"] = absl::StrCat( WireFormat::TagSize(descriptor->number(), GetType(descriptor))); (*variables)["required"] = descriptor->is_required() ? "true" : "false"; @@ -147,7 +149,7 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, : ""; int fixed_size = FixedSize(GetType(descriptor)); if (fixed_size != -1) { - (*variables)["fixed_size"] = StrCat(fixed_size); + (*variables)["fixed_size"] = absl::StrCat(fixed_size); } if (HasHasbit(descriptor)) { diff --git a/src/google/protobuf/compiler/java/service.cc b/src/google/protobuf/compiler/java/service.cc index bf98277aeced..3478a59aa733 100644 --- a/src/google/protobuf/compiler/java/service.cc +++ b/src/google/protobuf/compiler/java/service.cc @@ -36,6 +36,7 @@ #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -96,7 +97,7 @@ void ImmutableServiceGenerator::Generate(io::Printer* printer) { " return $file$.getDescriptor().getServices().get($index$);\n" "}\n", "file", name_resolver_->GetImmutableClassName(descriptor_->file()), - "index", StrCat(descriptor_->index())); + "index", absl::StrCat(descriptor_->index())); GenerateGetDescriptorForType(printer); // Generate more stuff. @@ -216,7 +217,7 @@ void ImmutableServiceGenerator::GenerateCallMethod(io::Printer* printer) { for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); std::map vars; - vars["index"] = StrCat(i); + vars["index"] = absl::StrCat(i); vars["method"] = UnderscoresToCamelCase(method); vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); vars["output"] = GetOutput(method); @@ -263,7 +264,7 @@ void ImmutableServiceGenerator::GenerateCallBlockingMethod( for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); std::map vars; - vars["index"] = StrCat(i); + vars["index"] = absl::StrCat(i); vars["method"] = UnderscoresToCamelCase(method); vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); vars["output"] = GetOutput(method); @@ -308,7 +309,7 @@ void ImmutableServiceGenerator::GenerateGetPrototype(RequestOrResponse which, for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); std::map vars; - vars["index"] = StrCat(i); + vars["index"] = absl::StrCat(i); vars["type"] = (which == REQUEST) ? name_resolver_->GetImmutableClassName(method->input_type()) @@ -363,7 +364,7 @@ void ImmutableServiceGenerator::GenerateStub(io::Printer* printer) { printer->Indent(); std::map vars; - vars["index"] = StrCat(i); + vars["index"] = absl::StrCat(i); vars["output"] = GetOutput(method); printer->Print(vars, "channel.callMethod(\n" @@ -427,7 +428,7 @@ void ImmutableServiceGenerator::GenerateBlockingStub(io::Printer* printer) { printer->Indent(); std::map vars; - vars["index"] = StrCat(i); + vars["index"] = absl::StrCat(i); vars["output"] = GetOutput(method); printer->Print(vars, "return ($output$) channel.callBlockingMethod(\n" diff --git a/src/google/protobuf/compiler/java/shared_code_generator.cc b/src/google/protobuf/compiler/java/shared_code_generator.cc index 477b35ee62f8..cfb40867fadb 100644 --- a/src/google/protobuf/compiler/java/shared_code_generator.cc +++ b/src/google/protobuf/compiler/java/shared_code_generator.cc @@ -38,7 +38,7 @@ #include #include #include -#include +#include "absl/strings/escaping.h" #include #include #include @@ -159,7 +159,7 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { } } printer->Print("\"$data$\"", "data", - CEscape(file_data.substr(i, kBytesPerLine))); + absl::CEscape(file_data.substr(i, kBytesPerLine))); } printer->Outdent(); diff --git a/src/google/protobuf/compiler/java/string_field.cc b/src/google/protobuf/compiler/java/string_field.cc index 90593306fbf3..db2803d285f0 100644 --- a/src/google/protobuf/compiler/java/string_field.cc +++ b/src/google/protobuf/compiler/java/string_field.cc @@ -44,6 +44,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -76,8 +77,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, ImmutableDefaultValue(descriptor, name_resolver, context->options()); (*variables)["capitalized_type"] = "String"; (*variables)["tag"] = - StrCat(static_cast(WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = StrCat( + absl::StrCat(static_cast(WireFormat::MakeTag(descriptor))); + (*variables)["tag_size"] = absl::StrCat( WireFormat::TagSize(descriptor->number(), GetType(descriptor))); (*variables)["null_check"] = " if (value == null) {\n" diff --git a/src/google/protobuf/compiler/java/string_field_lite.cc b/src/google/protobuf/compiler/java/string_field_lite.cc index a7ba88f7f6a5..61c7e1284476 100644 --- a/src/google/protobuf/compiler/java/string_field_lite.cc +++ b/src/google/protobuf/compiler/java/string_field_lite.cc @@ -44,6 +44,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -77,8 +78,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, ImmutableDefaultValue(descriptor, name_resolver, context->options()); (*variables)["capitalized_type"] = "java.lang.String"; (*variables)["tag"] = - StrCat(static_cast(WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = StrCat( + absl::StrCat(static_cast(WireFormat::MakeTag(descriptor))); + (*variables)["tag_size"] = absl::StrCat( WireFormat::TagSize(descriptor->number(), GetType(descriptor))); // We use `x.getClass()` as a null check because it generates less bytecode // than an `if (x == null) { throw ... }` statement. @@ -101,10 +102,10 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, if (HasHasbit(descriptor)) { if (!context->options().opensource_runtime) { - (*variables)["bit_field_id"] = StrCat(messageBitIndex / 32); + (*variables)["bit_field_id"] = absl::StrCat(messageBitIndex / 32); (*variables)["bit_field_name"] = GetBitFieldNameForBit(messageBitIndex); (*variables)["bit_field_mask"] = - StrCat(1 << (messageBitIndex % 32)); + absl::StrCat(1 << (messageBitIndex % 32)); } // For singular messages and builders, one bit is used for the hasField bit. (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex); diff --git a/src/google/protobuf/compiler/mock_code_generator.cc b/src/google/protobuf/compiler/mock_code_generator.cc index 4d045114235b..52002a9067a7 100644 --- a/src/google/protobuf/compiler/mock_code_generator.cc +++ b/src/google/protobuf/compiler/mock_code_generator.cc @@ -74,7 +74,7 @@ std::string CommaSeparatedList( for (size_t i = 0; i < all_files.size(); i++) { names.push_back(all_files[i]->name()); } - return Join(names, ","); + return absl::StrJoin(names, ","); } static const char* kFirstInsertionPointName = "first_mock_insertion_point"; diff --git a/src/google/protobuf/compiler/objectivec/BUILD.bazel b/src/google/protobuf/compiler/objectivec/BUILD.bazel index 30b6237a4b5a..d1cb4604f1c9 100644 --- a/src/google/protobuf/compiler/objectivec/BUILD.bazel +++ b/src/google/protobuf/compiler/objectivec/BUILD.bazel @@ -46,6 +46,7 @@ cc_library( deps = [ "//:protobuf", "//src/google/protobuf/compiler:code_generator", + "@com_google_absl//absl/strings", ], ) diff --git a/src/google/protobuf/compiler/objectivec/objectivec_enum.cc b/src/google/protobuf/compiler/objectivec/objectivec_enum.cc index b9e216ce9b74..49d6b38a7ea0 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_enum.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_enum.cc @@ -38,6 +38,8 @@ #include #include +#include "absl/strings/escaping.h" + namespace google { namespace protobuf { namespace compiler { @@ -47,9 +49,9 @@ std::string SafelyPrintIntToCode(int v) { if (v == std::numeric_limits::min()) { // Some compilers try to parse -2147483648 as two tokens and then get spicy // about the fact that +2147483648 cannot be represented as an int. - return StrCat(v + 1, " - 1"); + return absl::StrCat(v + 1, " - 1"); } else { - return StrCat(v); + return absl::StrCat(v); } } } // namespace @@ -207,7 +209,7 @@ void EnumGenerator::GenerateSource(io::Printer* printer) { for (int i = 0; i < text_blob.size(); i += kBytesPerLine) { printer->Print( "\n \"$data$\"", - "data", EscapeTrigraphs(CEscape(text_blob.substr(i, kBytesPerLine)))); + "data", EscapeTrigraphs(absl::CEscape(text_blob.substr(i, kBytesPerLine)))); } printer->Print( ";\n" @@ -237,7 +239,7 @@ void EnumGenerator::GenerateSource(io::Printer* printer) { " enumVerifier:$name$_IsValidValue\n" " extraTextFormatInfo:extraTextFormatInfo];\n", "name", name_, - "extraTextFormatInfo", CEscape(text_format_decode_data.Data())); + "extraTextFormatInfo", absl::CEscape(text_format_decode_data.Data())); } printer->Print( " GPBEnumDescriptor *expected = nil;\n" diff --git a/src/google/protobuf/compiler/objectivec/objectivec_extension.cc b/src/google/protobuf/compiler/objectivec/objectivec_extension.cc index 9cebcb22afa6..d3b7e0c0744d 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_extension.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_extension.cc @@ -86,7 +86,7 @@ void ExtensionGenerator::GenerateStaticVariablesInitialization( vars["root_class_and_method_name"] = root_class_and_method_name_; const std::string containing_type = ClassName(descriptor_->containing_type()); vars["extended_type"] = ObjCClass(containing_type); - vars["number"] = StrCat(descriptor_->number()); + vars["number"] = absl::StrCat(descriptor_->number()); std::vector options; if (descriptor_->is_repeated()) options.push_back("GPBExtensionRepeated"); diff --git a/src/google/protobuf/compiler/objectivec/objectivec_field.cc b/src/google/protobuf/compiler/objectivec/objectivec_field.cc index a9e651742929..df6bedf1588e 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_field.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_field.cc @@ -74,7 +74,7 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor, (*variables)["raw_field_name"] = raw_field_name; (*variables)["field_number_name"] = classname + "_FieldNumber_" + capitalized_name; - (*variables)["field_number"] = StrCat(descriptor->number()); + (*variables)["field_number"] = absl::StrCat(descriptor->number()); (*variables)["field_type"] = GetCapitalizedType(descriptor); (*variables)["deprecated_attribute"] = GetOptionalDeprecatedAttribute(descriptor); std::vector field_flags; @@ -224,7 +224,7 @@ void FieldGenerator::GenerateFieldDescription( } void FieldGenerator::SetRuntimeHasBit(int has_index) { - variables_["has_index"] = StrCat(has_index); + variables_["has_index"] = absl::StrCat(has_index); } void FieldGenerator::SetNoHasBit(void) { @@ -248,7 +248,7 @@ void FieldGenerator::SetOneofIndexBase(int index_base) { if (oneof != NULL) { int index = oneof->index() + index_base; // Flip the sign to mark it as a oneof. - variables_["has_index"] = StrCat(-index); + variables_["has_index"] = absl::StrCat(-index); } } diff --git a/src/google/protobuf/compiler/objectivec/objectivec_file.cc b/src/google/protobuf/compiler/objectivec/objectivec_file.cc index 53efded1c622..e601d7a40626 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_file.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_file.cc @@ -267,7 +267,7 @@ void FileGenerator::GenerateHeader(io::Printer* printer) { "#error This file was generated by an older version of protoc which is incompatible with your Protocol Buffer library sources.\n" "#endif\n" "\n", - "google_protobuf_objc_version", StrCat(GOOGLE_PROTOBUF_OBJC_VERSION)); + "google_protobuf_objc_version", absl::StrCat(GOOGLE_PROTOBUF_OBJC_VERSION)); // The bundled protos (WKTs) don't use of forward declarations. bool headers_use_forward_declarations = diff --git a/src/google/protobuf/compiler/objectivec/objectivec_generator.cc b/src/google/protobuf/compiler/objectivec/objectivec_generator.cc index d8e4cb5277c2..5a412a118ee5 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_generator.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_generator.cc @@ -51,7 +51,7 @@ namespace { // invalid, `result` is unchanged. bool StringToBool(const std::string& value, bool* result) { std::string upper_value(value); - UpperString(&upper_value); + ToUpper(&upper_value); if (upper_value == "NO") { *result = false; return true; @@ -122,7 +122,7 @@ bool ObjectiveCGenerator::GenerateAll( // A semicolon delimited string that lists the paths of .proto files to // exclude from the package prefix validations (expected_prefixes_path). // This is provided as an "out", to skip some files being checked. - for (StringPiece split_piece : Split( + for (absl::string_view split_piece : Split( options[i].second, ";", true)) { validation_options.expected_prefixes_suppressions.push_back( std::string(split_piece)); diff --git a/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc b/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc index 88208b591411..6a3e9457223c 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc @@ -53,6 +53,9 @@ #include #include +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" + // NOTE: src/google/protobuf/compiler/plugin.cc makes use of cerr for some // error cases, so it seems to be ok to use as a back door for errors. @@ -86,7 +89,7 @@ class SimpleLineCollector : public LineConsumer { explicit SimpleLineCollector(std::unordered_set* inout_set) : set_(inout_set) {} - virtual bool ConsumeLine(const StringPiece& line, std::string* out_error) override { + virtual bool ConsumeLine(const absl::string_view& line, std::string* out_error) override { set_->insert(std::string(line)); return true; } @@ -101,7 +104,7 @@ class PackageToPrefixesCollector : public LineConsumer { std::map* inout_package_to_prefix_map) : usage_(usage), prefix_map_(inout_package_to_prefix_map) {} - virtual bool ConsumeLine(const StringPiece& line, std::string* out_error) override; + virtual bool ConsumeLine(const absl::string_view& line, std::string* out_error) override; private: const std::string usage_; @@ -312,7 +315,7 @@ std::string UnderscoresToCamelCase(const std::string& input, bool last_char_was_upper = false; for (int i = 0; i < input.size(); i++) { char c = input[i]; - if (ascii_isdigit(c)) { + if (absl::ascii_isdigit(c)) { if (!last_char_was_number) { values.push_back(current); current = ""; @@ -320,7 +323,7 @@ std::string UnderscoresToCamelCase(const std::string& input, current += c; last_char_was_number = last_char_was_lower = last_char_was_upper = false; last_char_was_number = true; - } else if (ascii_islower(c)) { + } else if (absl::ascii_islower(c)) { // lowercase letter can follow a lowercase or uppercase letter if (!last_char_was_lower && !last_char_was_upper) { values.push_back(current); @@ -329,12 +332,12 @@ std::string UnderscoresToCamelCase(const std::string& input, current += c; // already lower last_char_was_number = last_char_was_lower = last_char_was_upper = false; last_char_was_lower = true; - } else if (ascii_isupper(c)) { + } else if (absl::ascii_isupper(c)) { if (!last_char_was_upper) { values.push_back(current); current = ""; } - current += ascii_tolower(c); + current += absl::ascii_tolower(c); last_char_was_number = last_char_was_lower = last_char_was_upper = false; last_char_was_upper = true; } else { @@ -354,7 +357,7 @@ std::string UnderscoresToCamelCase(const std::string& input, } for (int j = 0; j < value.length(); j++) { if (j == 0 || all_upper) { - value[j] = ascii_toupper(value[j]); + value[j] = absl::ascii_toupper(value[j]); } else { // Nothing, already in lower. } @@ -364,7 +367,7 @@ std::string UnderscoresToCamelCase(const std::string& input, if ((result.length() != 0) && !first_capitalized && !first_segment_forces_upper) { - result[0] = ascii_tolower(result[0]); + result[0] = absl::ascii_tolower(result[0]); } return result; } @@ -463,7 +466,7 @@ std::string SanitizeNameForObjC(const std::string& prefix, // b) Isn't equivalent to the prefix or // c) Has the prefix, but the letter after the prefix is lowercase if (HasPrefixString(input, prefix)) { - if (input.length() == prefix.length() || !ascii_isupper(input[prefix.length()])) { + if (input.length() == prefix.length() || !absl::ascii_isupper(input[prefix.length()])) { sanitized = prefix + input; } else { sanitized = input; @@ -518,7 +521,7 @@ bool IsSpecialName(const std::string& name, const std::string* special_names, // If name is longer than the retained_name[i] that it matches // the next character must be not lower case (newton vs newTon vs // new_ton). - return !ascii_islower(name[length]); + return !absl::ascii_islower(name[length]); } else { return true; } @@ -555,7 +558,7 @@ std::string GetEnumNameForFlagType(const FlagType flag_type) { } } -void MaybeUnQuote(StringPiece* input) { +void MaybeUnQuote(absl::string_view* input) { if ((input->length() >= 2) && ((*input->data() == '\'' || *input->data() == '"')) && ((*input)[input->length() - 1] == *input->data())) { @@ -571,11 +574,11 @@ std::string EscapeTrigraphs(const std::string& to_escape) { return StringReplace(to_escape, "?", "\\?", true); } -void TrimWhitespace(StringPiece* input) { - while (!input->empty() && ascii_isspace(*input->data())) { +void TrimWhitespace(absl::string_view* input) { + while (!input->empty() && absl::ascii_isspace(*input->data())) { input->remove_prefix(1); } - while (!input->empty() && ascii_isspace((*input)[input->length() - 1])) { + while (!input->empty() && absl::ascii_isspace((*input)[input->length() - 1])) { input->remove_suffix(1); } } @@ -767,10 +770,10 @@ std::string UnCamelCaseEnumShortName(const std::string& name) { std::string result; for (int i = 0; i < name.size(); i++) { char c = name[i]; - if (i > 0 && ascii_isupper(c)) { + if (i > 0 && absl::ascii_isupper(c)) { result += '_'; } - result += ascii_toupper(c); + result += absl::ascii_toupper(c); } return result; } @@ -801,7 +804,7 @@ std::string FieldNameCapitalized(const FieldDescriptor* field) { // name. std::string result = FieldName(field); if (result.length() > 0) { - result[0] = ascii_toupper(result[0]); + result[0] = absl::ascii_toupper(result[0]); } return result; } @@ -825,7 +828,7 @@ std::string OneofNameCapitalized(const OneofDescriptor* descriptor) { // Use the common handling and then up-case the first letter. std::string result = OneofName(descriptor); if (result.length() > 0) { - result[0] = ascii_toupper(result[0]); + result[0] = absl::ascii_toupper(result[0]); } return result; } @@ -848,8 +851,8 @@ std::string UnCamelCaseFieldName(const std::string& name, const FieldDescriptor* } if (field->type() == FieldDescriptor::TYPE_GROUP) { if (worker.length() > 0) { - if (ascii_islower(worker[0])) { - worker[0] = ascii_toupper(worker[0]); + if (absl::ascii_islower(worker[0])) { + worker[0] = absl::ascii_toupper(worker[0]); } } return worker; @@ -857,11 +860,11 @@ std::string UnCamelCaseFieldName(const std::string& name, const FieldDescriptor* std::string result; for (int i = 0; i < worker.size(); i++) { char c = worker[i]; - if (ascii_isupper(c)) { + if (absl::ascii_isupper(c)) { if (i > 0) { result += '_'; } - result += ascii_tolower(c); + result += absl::ascii_tolower(c); } else { result += c; } @@ -1060,17 +1063,17 @@ std::string DefaultValue(const FieldDescriptor* field) { if (field->default_value_int32() == INT_MIN) { return "-0x80000000"; } - return StrCat(field->default_value_int32()); + return absl::StrCat(field->default_value_int32()); case FieldDescriptor::CPPTYPE_UINT32: - return StrCat(field->default_value_uint32()) + "U"; + return absl::StrCat(field->default_value_uint32()) + "U"; case FieldDescriptor::CPPTYPE_INT64: // gcc and llvm reject the decimal form of kint32min and kint64min. if (field->default_value_int64() == LLONG_MIN) { return "-0x8000000000000000LL"; } - return StrCat(field->default_value_int64()) + "LL"; + return absl::StrCat(field->default_value_int64()) + "LL"; case FieldDescriptor::CPPTYPE_UINT64: - return StrCat(field->default_value_uint64()) + "ULL"; + return absl::StrCat(field->default_value_uint64()) + "ULL"; case FieldDescriptor::CPPTYPE_DOUBLE: return HandleExtremeFloatingPoint( SimpleDtoa(field->default_value_double()), false); @@ -1100,9 +1103,9 @@ std::string DefaultValue(const FieldDescriptor* field) { uint32_t length = ghtonl(default_string.length()); std::string bytes((const char*)&length, sizeof(length)); bytes.append(default_string); - return "(NSData*)\"" + EscapeTrigraphs(CEscape(bytes)) + "\""; + return "(NSData*)\"" + EscapeTrigraphs(absl::CEscape(bytes)) + "\""; } else { - return "@\"" + EscapeTrigraphs(CEscape(default_string)) + "\""; + return "@\"" + EscapeTrigraphs(absl::CEscape(default_string)) + "\""; } } case FieldDescriptor::CPPTYPE_ENUM: @@ -1266,21 +1269,21 @@ bool IsProtobufLibraryBundledProtoFile(const FileDescriptor* file) { return false; } -bool ReadLine(StringPiece* input, StringPiece* line) { +bool ReadLine(absl::string_view* input, absl::string_view* line) { for (int len = 0; len < input->size(); ++len) { if (ascii_isnewline((*input)[len])) { - *line = StringPiece(input->data(), len); + *line = absl::string_view(input->data(), len); ++len; // advance over the newline - *input = StringPiece(input->data() + len, input->size() - len); + *input = absl::string_view(input->data() + len, input->size() - len); return true; } } return false; // Ran out of input with no newline. } -void RemoveComment(StringPiece* input) { +void RemoveComment(absl::string_view* input) { int offset = input->find('#'); - if (offset != StringPiece::npos) { + if (offset != absl::string_view::npos) { input->remove_suffix(input->length() - offset); } } @@ -1288,14 +1291,14 @@ void RemoveComment(StringPiece* input) { namespace { bool PackageToPrefixesCollector::ConsumeLine( - const StringPiece& line, std::string* out_error) { + const absl::string_view& line, std::string* out_error) { int offset = line.find('='); - if (offset == StringPiece::npos) { - *out_error = usage_ + " file line without equal sign: '" + StrCat(line) + "'."; + if (offset == absl::string_view::npos) { + *out_error = usage_ + " file line without equal sign: '" + absl::StrCat(line) + "'."; return false; } - StringPiece package = line.substr(0, offset); - StringPiece prefix = line.substr(offset + 1); + absl::string_view package = line.substr(0, offset); + absl::string_view prefix = line.substr(offset + 1); TrimWhitespace(&package); TrimWhitespace(&prefix); MaybeUnQuote(&prefix); @@ -1417,7 +1420,7 @@ bool ValidateObjCClassPrefix( // Check: Warning - Make sure the prefix is is a reasonable value according // to Apple's rules (the checks above implicitly whitelist anything that // doesn't meet these rules). - if (!prefix.empty() && !ascii_isupper(prefix[0])) { + if (!prefix.empty() && !absl::ascii_isupper(prefix[0])) { std::cerr << "protoc:0: warning: Invalid 'option objc_class_prefix = \"" << prefix << "\";' in '" << file->name() << "';" @@ -1580,7 +1583,7 @@ class DecodeDataBuilder { void AddChar(const char desired) { ++segment_len_; - is_all_upper_ &= ascii_isupper(desired); + is_all_upper_ &= absl::ascii_isupper(desired); } void Push() { @@ -1595,9 +1598,9 @@ class DecodeDataBuilder { bool AddFirst(const char desired, const char input) { if (desired == input) { op_ = kOpAsIs; - } else if (desired == ascii_toupper(input)) { + } else if (desired == absl::ascii_toupper(input)) { op_ = kOpFirstUpper; - } else if (desired == ascii_tolower(input)) { + } else if (desired == absl::ascii_tolower(input)) { op_ = kOpFirstLower; } else { // Can't be transformed to match. @@ -1635,7 +1638,7 @@ bool DecodeDataBuilder::AddCharacter(const char desired, const char input) { if (desired == input) { // If we aren't transforming it, or we're upper casing it and it is // supposed to be uppercase; just add it to the segment. - if ((op_ != kOpAllUpper) || ascii_isupper(desired)) { + if ((op_ != kOpAllUpper) || absl::ascii_isupper(desired)) { AddChar(desired); return true; } @@ -1647,7 +1650,7 @@ bool DecodeDataBuilder::AddCharacter(const char desired, const char input) { // If we need to uppercase, and everything so far has been uppercase, // promote op to AllUpper. - if ((desired == ascii_toupper(input)) && is_all_upper_) { + if ((desired == absl::ascii_toupper(input)) && is_all_upper_) { op_ = kOpAllUpper; AddChar(desired); return true; @@ -1683,8 +1686,8 @@ std::string TextFormatDecodeData::DecodeDataForString( if ((input_for_decode.find('\0') != std::string::npos) || (desired_output.find('\0') != std::string::npos)) { std::cerr << "error: got a null char in a string for making TextFormat data," - << " input: \"" << CEscape(input_for_decode) << "\", desired: \"" - << CEscape(desired_output) << "\"." << std::endl; + << " input: \"" << absl::CEscape(input_for_decode) << "\", desired: \"" + << absl::CEscape(desired_output) << "\"." << std::endl; std::cerr.flush(); abort(); } @@ -1730,7 +1733,7 @@ class Parser { // Feeds in some input, parse what it can, returning success/failure. Calling // again after an error is undefined. - bool ParseChunk(StringPiece chunk, std::string* out_error); + bool ParseChunk(absl::string_view chunk, std::string* out_error); // Should be called to finish parsing (after all input has been provided via // successful calls to ParseChunk(), calling after a ParseChunk() failure is @@ -1745,16 +1748,16 @@ class Parser { std::string leftover_; }; -bool Parser::ParseChunk(StringPiece chunk, std::string* out_error) { - StringPiece full_chunk; +bool Parser::ParseChunk(absl::string_view chunk, std::string* out_error) { + absl::string_view full_chunk; if (!leftover_.empty()) { leftover_ += std::string(chunk); - full_chunk = StringPiece(leftover_); + full_chunk = absl::string_view(leftover_); } else { full_chunk = chunk; } - StringPiece line; + absl::string_view line; while (ReadLine(&full_chunk, &line)) { ++line_; RemoveComment(&line); @@ -1790,7 +1793,7 @@ bool Parser::Finish(std::string* out_error) { } std::string FullErrorString(const std::string& name, int line_num, const std::string& msg) { - return std::string("error: ") + name + " Line " + StrCat(line_num) + ", " + msg; + return std::string("error: ") + name + " Line " + absl::StrCat(line_num) + ", " + msg; } } // namespace @@ -1829,7 +1832,7 @@ bool ParseSimpleStream(io::ZeroCopyInputStream& input_stream, continue; } - if (!parser.ParseChunk(StringPiece(static_cast(buf), buf_len), + if (!parser.ParseChunk(absl::string_view(static_cast(buf), buf_len), &local_error)) { *out_error = FullErrorString(stream_name, parser.last_line(), local_error); return false; @@ -1996,26 +1999,26 @@ void ImportWriter::ParseFrameworkMappings() { } bool ImportWriter::ProtoFrameworkCollector::ConsumeLine( - const StringPiece& line, std::string* out_error) { + const absl::string_view& line, std::string* out_error) { int offset = line.find(':'); - if (offset == StringPiece::npos) { + if (offset == absl::string_view::npos) { *out_error = std::string("Framework/proto file mapping line without colon sign: '") + std::string(line) + "'."; return false; } - StringPiece framework_name = line.substr(0, offset); - StringPiece proto_file_list = line.substr(offset + 1); + absl::string_view framework_name = line.substr(0, offset); + absl::string_view proto_file_list = line.substr(offset + 1); TrimWhitespace(&framework_name); int start = 0; while (start < proto_file_list.length()) { offset = proto_file_list.find(',', start); - if (offset == StringPiece::npos) { + if (offset == absl::string_view::npos) { offset = proto_file_list.length(); } - StringPiece proto_file = proto_file_list.substr(start, offset - start); + absl::string_view proto_file = proto_file_list.substr(start, offset - start); TrimWhitespace(&proto_file); if (!proto_file.empty()) { std::map::iterator existing_entry = @@ -2028,7 +2031,7 @@ bool ImportWriter::ProtoFrameworkCollector::ConsumeLine( std::cerr.flush(); } - if (proto_file.find(' ') != StringPiece::npos) { + if (proto_file.find(' ') != absl::string_view::npos) { std::cerr << "note: framework mapping file had a proto file with a " "space in, hopefully that isn't a missing comma: '" << std::string(proto_file) << "'" << std::endl; diff --git a/src/google/protobuf/compiler/objectivec/objectivec_helpers.h b/src/google/protobuf/compiler/objectivec/objectivec_helpers.h index a1bcc4ba4a04..9a5c6e6656ab 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_helpers.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_helpers.h @@ -81,8 +81,8 @@ struct Options { // Escape C++ trigraphs by escaping question marks to "\?". std::string PROTOC_EXPORT EscapeTrigraphs(const std::string& to_escape); -// Remove white space from either end of a StringPiece. -void PROTOC_EXPORT TrimWhitespace(StringPiece* input); +// Remove white space from either end of a absl::string_view. +void PROTOC_EXPORT TrimWhitespace(absl::string_view* input); // Returns true if the name requires a ns_returns_not_retained attribute applied // to it. @@ -291,7 +291,7 @@ class PROTOC_EXPORT LineConsumer { public: LineConsumer(); virtual ~LineConsumer(); - virtual bool ConsumeLine(const StringPiece& line, std::string* out_error) = 0; + virtual bool ConsumeLine(const absl::string_view& line, std::string* out_error) = 0; }; bool PROTOC_EXPORT ParseSimpleFile(const std::string& path, @@ -327,7 +327,7 @@ class PROTOC_EXPORT ImportWriter { ProtoFrameworkCollector(std::map* inout_proto_file_to_framework_name) : map_(inout_proto_file_to_framework_name) {} - virtual bool ConsumeLine(const StringPiece& line, std::string* out_error) override; + virtual bool ConsumeLine(const absl::string_view& line, std::string* out_error) override; private: std::map* map_; diff --git a/src/google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc b/src/google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc index 736111dd68ae..200ca873880a 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc @@ -249,7 +249,7 @@ class TestLineCollector : public LineConsumer { bool skip_msg = false) : lines_(inout_lines), reject_(reject_line), skip_msg_(skip_msg) {} - bool ConsumeLine(const StringPiece& line, std::string* out_error) override { + bool ConsumeLine(const absl::string_view& line, std::string* out_error) override { if (reject_ && *reject_ == line) { if (!skip_msg_) { *out_error = std::string("Rejected '") + *reject_ + "'"; @@ -340,7 +340,7 @@ TEST(ObjCHelper, ParseSimple_RejectLines) { TestLineCollector collector(nullptr, &std::get<1>(test)); EXPECT_FALSE(ParseSimpleStream(input, "dummy", &collector, &err_str)); std::string expected_err = - StrCat("error: dummy Line ", std::get<2>(test), ", Rejected '", std::get<1>(test), "'"); + absl::StrCat("error: dummy Line ", std::get<2>(test), ", Rejected '", std::get<1>(test), "'"); EXPECT_EQ(err_str, expected_err); } } @@ -362,7 +362,7 @@ TEST(ObjCHelper, ParseSimple_RejectLinesNoMessage) { TestLineCollector collector(nullptr, &std::get<1>(test), true /* skip msg */); EXPECT_FALSE(ParseSimpleStream(input, "dummy", &collector, &err_str)); std::string expected_err = - StrCat("error: dummy Line ", std::get<2>(test), + absl::StrCat("error: dummy Line ", std::get<2>(test), ", ConsumeLine failed without setting an error."); EXPECT_EQ(err_str, expected_err); } diff --git a/src/google/protobuf/compiler/objectivec/objectivec_message.cc b/src/google/protobuf/compiler/objectivec/objectivec_message.cc index 4ebb75c2cdc8..14fbc574c7ab 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_message.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_message.cc @@ -43,6 +43,8 @@ #include #include +#include "absl/strings/escaping.h" + namespace google { namespace protobuf { namespace compiler { @@ -449,7 +451,7 @@ void MessageGenerator::GenerateSource(io::Printer* printer) { "typedef struct $classname$__storage_ {\n" " uint32_t _has_storage_[$sizeof_has_storage$];\n", "classname", class_name_, - "sizeof_has_storage", StrCat(sizeof_has_storage)); + "sizeof_has_storage", absl::StrCat(sizeof_has_storage)); printer->Indent(); for (int i = 0; i < descriptor_->field_count(); i++) { @@ -558,7 +560,7 @@ void MessageGenerator::GenerateSource(io::Printer* printer) { printer->Print( "\n \"$data$\"", "data", EscapeTrigraphs( - CEscape(text_format_data_str.substr(i, kBytesPerLine)))); + absl::CEscape(text_format_data_str.substr(i, kBytesPerLine)))); } printer->Print( ";\n" @@ -570,8 +572,8 @@ void MessageGenerator::GenerateSource(io::Printer* printer) { " static const GPBExtensionRange ranges[] = {\n"); for (int i = 0; i < sorted_extensions.size(); i++) { printer->Print(" { .start = $start$, .end = $end$ },\n", - "start", StrCat(sorted_extensions[i]->start), - "end", StrCat(sorted_extensions[i]->end)); + "start", absl::StrCat(sorted_extensions[i]->start), + "end", absl::StrCat(sorted_extensions[i]->end)); } printer->Print( " };\n" diff --git a/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc b/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc index 1bef293e2885..017d58ba56ad 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc @@ -46,7 +46,7 @@ OneofGenerator::OneofGenerator(const OneofDescriptor* descriptor) variables_["enum_name"] = OneofEnumName(descriptor_); variables_["name"] = OneofName(descriptor_); variables_["capitalized_name"] = OneofNameCapitalized(descriptor_); - variables_["raw_index"] = StrCat(descriptor_->index()); + variables_["raw_index"] = absl::StrCat(descriptor_->index()); const Descriptor* msg_descriptor = descriptor_->containing_type(); variables_["owning_message_class"] = ClassName(msg_descriptor); @@ -65,7 +65,7 @@ OneofGenerator::~OneofGenerator() {} void OneofGenerator::SetOneofIndexBase(int index_base) { int index = descriptor_->index() + index_base; // Flip the sign to mark it as a oneof. - variables_["index"] = StrCat(-index); + variables_["index"] = absl::StrCat(-index); } void OneofGenerator::GenerateCaseEnum(io::Printer* printer) { @@ -84,7 +84,7 @@ void OneofGenerator::GenerateCaseEnum(io::Printer* printer) { "$enum_name$_$field_name$ = $field_number$,\n", "enum_name", enum_name, "field_name", field_name, - "field_number", StrCat(field->number())); + "field_number", absl::StrCat(field->number())); } printer->Outdent(); printer->Print( diff --git a/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc b/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc index 1fefde5fe52a..d660563436ea 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc @@ -150,7 +150,7 @@ int PrimitiveFieldGenerator::ExtraRuntimeHasBitsNeeded(void) const { void PrimitiveFieldGenerator::SetExtraRuntimeHasBitsBase(int has_base) { if (GetObjectiveCType(descriptor_) == OBJECTIVECTYPE_BOOLEAN) { // Set into the offset the has bit to use for the actual value. - variables_["storage_offset_value"] = StrCat(has_base); + variables_["storage_offset_value"] = absl::StrCat(has_base); variables_["storage_offset_comment"] = " // Stored in _has_storage_ to save space."; } diff --git a/src/google/protobuf/compiler/parser.cc b/src/google/protobuf/compiler/parser.cc index f220fad8bee7..1df492017ae7 100644 --- a/src/google/protobuf/compiler/parser.cc +++ b/src/google/protobuf/compiler/parser.cc @@ -40,28 +40,30 @@ #include #include +#include #include #include -#include #include #include #include +#include "absl/base/casts.h" +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include #include #include +#include #include -#include -#include namespace google { namespace protobuf { namespace compiler { - -using internal::WireFormat; - namespace { +using ::google::protobuf::internal::DownCast; + typedef std::unordered_map TypeNameMap; const TypeNameMap& GetTypeNameTable() { @@ -1286,7 +1288,7 @@ bool Parser::ParseDefaultAssignment( DO(ConsumeInteger64(max_value, &value, "Expected integer for field default value.")); // And stringify it again. - default_value->append(StrCat(value)); + default_value->append(absl::StrCat(value)); break; } @@ -1309,7 +1311,7 @@ bool Parser::ParseDefaultAssignment( DO(ConsumeInteger64(max_value, &value, "Expected integer for field default value.")); // And stringify it again. - default_value->append(StrCat(value)); + default_value->append(absl::StrCat(value)); break; } @@ -1349,7 +1351,7 @@ bool Parser::ParseDefaultAssignment( case FieldDescriptorProto::TYPE_BYTES: DO(ConsumeString(default_value, "Expected string.")); - *default_value = CEscape(*default_value); + *default_value = absl::CEscape(*default_value); break; case FieldDescriptorProto::TYPE_ENUM: @@ -1477,7 +1479,7 @@ bool Parser::ParseOption(Message* options, } UninterpretedOption* uninterpreted_option = - down_cast(options->GetReflection()->AddMessage( + DownCast(options->GetReflection()->AddMessage( options, uninterpreted_option_field)); // Parse dot-separated name. @@ -2398,33 +2400,27 @@ bool SourceLocationTable::Find( const Message* descriptor, DescriptorPool::ErrorCollector::ErrorLocation location, int* line, int* column) const { - const std::pair* result = - FindOrNull(location_map_, std::make_pair(descriptor, location)); - if (result == nullptr) { + auto it = location_map_.find({descriptor, location}); + if (it == location_map_.end()) { *line = -1; *column = 0; return false; - } else { - *line = result->first; - *column = result->second; - return true; } + std::tie(*line, *column) = it->second; + return true; } bool SourceLocationTable::FindImport(const Message* descriptor, const std::string& name, int* line, int* column) const { - const std::pair* result = - FindOrNull(import_location_map_, std::make_pair(descriptor, name)); - if (result == nullptr) { + auto it = import_location_map_.find({descriptor, name}); + if (it == import_location_map_.end()) { *line = -1; *column = 0; return false; - } else { - *line = result->first; - *column = result->second; - return true; } + std::tie(*line, *column) = it->second; + return true; } void SourceLocationTable::Add( diff --git a/src/google/protobuf/compiler/parser_unittest.cc b/src/google/protobuf/compiler/parser_unittest.cc index 55ed7acb6fe0..8452b57a1564 100644 --- a/src/google/protobuf/compiler/parser_unittest.cc +++ b/src/google/protobuf/compiler/parser_unittest.cc @@ -50,8 +50,8 @@ #include #include #include +#include "absl/strings/str_join.h" #include -#include namespace google { namespace protobuf { @@ -2678,8 +2678,8 @@ class SourceInfoTest : public ParserTest { return true; } } else { - std::pair start_pos = FindOrDie(markers_, start_marker); - std::pair end_pos = FindOrDie(markers_, end_marker); + std::pair start_pos = markers_.at(start_marker); + std::pair end_pos = markers_.at(end_marker); RepeatedField expected_span; expected_span.Add(start_pos.first); @@ -2710,7 +2710,7 @@ class SourceInfoTest : public ParserTest { } else { EXPECT_EQ( expected_leading_detached_comments, - Join(iter->second->leading_detached_comments(), "\n")); + absl::StrJoin(iter->second->leading_detached_comments(), "\n")); } spans_.erase(iter); diff --git a/src/google/protobuf/compiler/php/BUILD.bazel b/src/google/protobuf/compiler/php/BUILD.bazel index 2ce166b31b21..5911760be9cc 100644 --- a/src/google/protobuf/compiler/php/BUILD.bazel +++ b/src/google/protobuf/compiler/php/BUILD.bazel @@ -19,6 +19,7 @@ cc_library( deps = [ "//:protobuf", "//src/google/protobuf/compiler:code_generator", + "@com_google_absl//absl/strings", ], ) diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index b2387039e888..0663e736b389 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -38,6 +38,8 @@ #include #include +#include "absl/strings/escaping.h" + #include const std::string kDescriptorFile = "google/protobuf/descriptor.proto"; @@ -914,7 +916,7 @@ void GenerateMessageToPool(const std::string& name_prefix, "field", field->name(), "key", ToUpper(key->type_name()), "value", ToUpper(val->type_name()), - "number", StrCat(field->number()), + "number", absl::StrCat(field->number()), "other", EnumOrMessageSuffix(val, true)); } else if (!field->real_containing_oneof()) { printer->Print( @@ -923,7 +925,7 @@ void GenerateMessageToPool(const std::string& name_prefix, "field", field->name(), "label", LabelForField(field), "type", ToUpper(field->type_name()), - "number", StrCat(field->number()), + "number", absl::StrCat(field->number()), "other", EnumOrMessageSuffix(field, true)); } } @@ -941,7 +943,7 @@ void GenerateMessageToPool(const std::string& name_prefix, "\\Google\\Protobuf\\Internal\\GPBType::^type^, ^number^^other^)\n", "field", field->name(), "type", ToUpper(field->type_name()), - "number", StrCat(field->number()), + "number", absl::StrCat(field->number()), "other", EnumOrMessageSuffix(field, true)); } printer->Print("->finish()\n"); @@ -2265,7 +2267,7 @@ void GenerateCWellKnownTypes(const std::vector& files, for (size_t i = 0; i < serialized.size();) { for (size_t j = 0; j < 25 && i < serialized.size(); ++i, ++j) { - printer.Print("'$ch$', ", "ch", CEscape(serialized.substr(i, 1))); + printer.Print("'$ch$', ", "ch", absl::CEscape(serialized.substr(i, 1))); } printer.Print("\n"); } diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index 32bfac19a256..e0b60572428c 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -198,7 +198,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fcompiler_2fplugin_2epro static const ::_pbi::DescriptorTable* const descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_deps[1] = { &::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, }; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto = { false, false, 773, descriptor_table_protodef_google_2fprotobuf_2fcompiler_2fplugin_2eproto, "google/protobuf/compiler/plugin.proto", diff --git a/src/google/protobuf/compiler/plugin.pb.h b/src/google/protobuf/compiler/plugin.pb.h index 94f81e7c7b59..81ffdc39e7f2 100644 --- a/src/google/protobuf/compiler/plugin.pb.h +++ b/src/google/protobuf/compiler/plugin.pb.h @@ -100,7 +100,7 @@ inline const std::string& CodeGeneratorResponse_Feature_Name(CodeGeneratorRespon (static_cast(value)); } inline bool CodeGeneratorResponse_Feature_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, CodeGeneratorResponse_Feature* value) { + ::absl::string_view name, CodeGeneratorResponse_Feature* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( CodeGeneratorResponse_Feature_descriptor(), name, value); } @@ -216,7 +216,7 @@ class PROTOC_EXPORT Version final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.compiler.Version"; } protected: @@ -426,7 +426,7 @@ class PROTOC_EXPORT CodeGeneratorRequest final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.compiler.CodeGeneratorRequest"; } protected: @@ -657,7 +657,7 @@ class PROTOC_EXPORT CodeGeneratorResponse_File final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.compiler.CodeGeneratorResponse.File"; } protected: @@ -882,7 +882,7 @@ class PROTOC_EXPORT CodeGeneratorResponse final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.compiler.CodeGeneratorResponse"; } protected: @@ -924,7 +924,7 @@ class PROTOC_EXPORT CodeGeneratorResponse final : "Incorrect type passed to function Feature_Name."); return CodeGeneratorResponse_Feature_Name(enum_t_value); } - static inline bool Feature_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, + static inline bool Feature_Parse(::absl::string_view name, Feature* value) { return CodeGeneratorResponse_Feature_Parse(name, value); } diff --git a/src/google/protobuf/compiler/python/generator.cc b/src/google/protobuf/compiler/python/generator.cc index 40c546b8b540..e174d6a99d43 100644 --- a/src/google/protobuf/compiler/python/generator.cc +++ b/src/google/protobuf/compiler/python/generator.cc @@ -55,6 +55,9 @@ #include #include #include +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include #include #include @@ -109,13 +112,13 @@ std::string StringifyDefaultValue(const FieldDescriptor& field) { switch (field.cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: - return StrCat(field.default_value_int32()); + return absl::StrCat(field.default_value_int32()); case FieldDescriptor::CPPTYPE_UINT32: - return StrCat(field.default_value_uint32()); + return absl::StrCat(field.default_value_uint32()); case FieldDescriptor::CPPTYPE_INT64: - return StrCat(field.default_value_int64()); + return absl::StrCat(field.default_value_int64()); case FieldDescriptor::CPPTYPE_UINT64: - return StrCat(field.default_value_uint64()); + return absl::StrCat(field.default_value_uint64()); case FieldDescriptor::CPPTYPE_DOUBLE: { double value = field.default_value_double(); if (value == std::numeric_limits::infinity()) { @@ -151,9 +154,9 @@ std::string StringifyDefaultValue(const FieldDescriptor& field) { case FieldDescriptor::CPPTYPE_BOOL: return field.default_value_bool() ? "True" : "False"; case FieldDescriptor::CPPTYPE_ENUM: - return StrCat(field.default_value_enum()->number()); + return absl::StrCat(field.default_value_enum()->number()); case FieldDescriptor::CPPTYPE_STRING: - return "b\"" + CEscape(field.default_value_string()) + + return "b\"" + absl::CEscape(field.default_value_string()) + (field.type() != FieldDescriptor::TYPE_STRING ? "\"" : "\".decode('utf-8')"); @@ -431,7 +434,7 @@ void Generator::PrintFileDescriptor() const { m["package"] = file_->package(); m["syntax"] = StringifySyntax(file_->syntax()); m["options"] = OptionsValue(file_->options().SerializeAsString()); - m["serialized_descriptor"] = strings::CHexEscape(file_descriptor_serialized_); + m["serialized_descriptor"] = absl::CHexEscape(file_descriptor_serialized_); if (GeneratingDescriptorProto()) { printer_->Print("if _descriptor._USE_C_DESCRIPTORS == False:\n"); printer_->Indent(); @@ -448,7 +451,7 @@ void Generator::PrintFileDescriptor() const { printer_->Print(m, file_descriptor_template); printer_->Indent(); printer_->Print("serialized_pb=b'$value$'\n", "value", - strings::CHexEscape(file_descriptor_serialized_)); + absl::CHexEscape(file_descriptor_serialized_)); if (file_->dependency_count() != 0) { printer_->Print(",\ndependencies=["); for (int i = 0; i < file_->dependency_count(); ++i) { @@ -683,8 +686,8 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { for (int i = 0; i < message_descriptor.extension_range_count(); ++i) { const Descriptor::ExtensionRange* range = message_descriptor.extension_range(i); - printer_->Print("($start$, $end$), ", "start", StrCat(range->start), - "end", StrCat(range->end)); + printer_->Print("($start$, $end$), ", "start", absl::StrCat(range->start), + "end", absl::StrCat(range->end)); } printer_->Print("],\n"); printer_->Print("oneofs=[\n"); @@ -694,7 +697,7 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { m.clear(); m["name"] = desc->name(); m["full_name"] = desc->full_name(); - m["index"] = StrCat(desc->index()); + m["index"] = absl::StrCat(desc->index()); options_string = OptionsValue(desc->options().SerializeAsString()); if (options_string == "None") { m["serialized_options"] = ""; @@ -1038,8 +1041,8 @@ void Generator::PrintEnumValueDescriptor( descriptor.options().SerializeToString(&options_string); std::map m; m["name"] = descriptor.name(); - m["index"] = StrCat(descriptor.index()); - m["number"] = StrCat(descriptor.number()); + m["index"] = absl::StrCat(descriptor.index()); + m["number"] = absl::StrCat(descriptor.number()); m["options"] = OptionsValue(options_string); printer_->Print(m, "_descriptor.EnumValueDescriptor(\n" @@ -1055,7 +1058,7 @@ std::string Generator::OptionsValue( if (serialized_options.length() == 0 || GeneratingDescriptorProto()) { return "None"; } else { - return "b'" + CEscape(serialized_options) + "'"; + return "b'" + absl::CEscape(serialized_options) + "'"; } } @@ -1067,11 +1070,11 @@ void Generator::PrintFieldDescriptor(const FieldDescriptor& field, std::map m; m["name"] = field.name(); m["full_name"] = field.full_name(); - m["index"] = StrCat(field.index()); - m["number"] = StrCat(field.number()); - m["type"] = StrCat(field.type()); - m["cpp_type"] = StrCat(field.cpp_type()); - m["label"] = StrCat(field.label()); + m["index"] = absl::StrCat(field.index()); + m["number"] = absl::StrCat(field.number()); + m["type"] = absl::StrCat(field.type()); + m["cpp_type"] = absl::StrCat(field.cpp_type()); + m["label"] = absl::StrCat(field.label()); m["has_default_value"] = field.has_default_value() ? "True" : "False"; m["default_value"] = StringifyDefaultValue(field); m["is_extension"] = is_extension ? "True" : "False"; @@ -1222,8 +1225,8 @@ void Generator::PrintSerializedPbInterval(const DescriptorT& descriptor, printer_->Print( "$name$._serialized_start=$serialized_start$\n" "$name$._serialized_end=$serialized_end$\n", - "name", name, "serialized_start", StrCat(offset), "serialized_end", - StrCat(offset + sp.size())); + "name", name, "serialized_start", absl::StrCat(offset), "serialized_end", + absl::StrCat(offset + sp.size())); } namespace { diff --git a/src/google/protobuf/compiler/python/helpers.cc b/src/google/protobuf/compiler/python/helpers.cc index 303072d521ab..5a17f83e18a2 100644 --- a/src/google/protobuf/compiler/python/helpers.cc +++ b/src/google/protobuf/compiler/python/helpers.cc @@ -33,6 +33,7 @@ #include #include +#include "absl/strings/escaping.h" #include #include #include @@ -103,7 +104,7 @@ bool HasGenericServices(const FileDescriptor* file) { std::string GeneratedCodeToBase64(const GeneratedCodeInfo& annotations) { std::string result; - Base64Escape(annotations.SerializeAsString(), &result); + absl::Base64Escape(annotations.SerializeAsString(), &result); return result; } diff --git a/src/google/protobuf/compiler/subprocess.cc b/src/google/protobuf/compiler/subprocess.cc index 6faab0513764..43be498e926f 100644 --- a/src/google/protobuf/compiler/subprocess.cc +++ b/src/google/protobuf/compiler/subprocess.cc @@ -45,6 +45,7 @@ #include #include +#include "absl/strings/escaping.h" #include #include #include @@ -278,7 +279,7 @@ bool Subprocess::Communicate(const Message& input, Message* output, } if (!output->ParseFromString(output_data)) { - *error = "Plugin output is unparseable: " + CEscape(output_data); + *error = "Plugin output is unparseable: " + absl::CEscape(output_data); return false; } @@ -484,7 +485,7 @@ bool Subprocess::Communicate(const Message& input, Message* output, } if (!output->ParseFromString(output_data)) { - *error = "Plugin output is unparseable: " + CEscape(output_data); + *error = "Plugin output is unparseable: " + absl::CEscape(output_data); return false; } diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index fb6185349c4d..a1af8d5d8d93 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -44,35 +44,34 @@ #include #include #include -#include -#include #include #include #include #include -#include -#include -#include -#include -#include -#include -#include +#include "absl/base/call_once.h" +#include "absl/base/casts.h" +#include "absl/base/dynamic_annotations.h" +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/hash/hash.h" +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include +#include "absl/strings/str_join.h" #include #include "absl/synchronization/mutex.h" +#include +#include #include #include #include #include +#include #include #include #include -#include -#include -#include - -#undef PACKAGE // autoheader #defines this. :( // Must be included last. @@ -80,18 +79,11 @@ namespace google { namespace protobuf { - namespace { -const int kPackageLimit = 100; +using ::google::protobuf::internal::DownCast; -// Note: I distrust ctype.h due to locales. -char ToUpper(char ch) { - return (ch >= 'a' && ch <= 'z') ? (ch - 'a' + 'A') : ch; -} +const int kPackageLimit = 100; -char ToLower(char ch) { - return (ch >= 'A' && ch <= 'Z') ? (ch - 'A' + 'a') : ch; -} std::string ToCamelCase(const std::string& input, bool lower_first) { bool capitalize_next = !lower_first; @@ -102,7 +94,7 @@ std::string ToCamelCase(const std::string& input, bool lower_first) { if (character == '_') { capitalize_next = true; } else if (capitalize_next) { - result.push_back(ToUpper(character)); + result.push_back(absl::ascii_toupper(character)); capitalize_next = false; } else { result.push_back(character); @@ -111,7 +103,7 @@ std::string ToCamelCase(const std::string& input, bool lower_first) { // Lower-case the first letter. if (lower_first && !result.empty()) { - result[0] = ToLower(result[0]); + result[0] = absl::ascii_tolower(result[0]); } return result; @@ -126,7 +118,7 @@ std::string ToJsonName(const std::string& input) { if (character == '_') { capitalize_next = true; } else if (capitalize_next) { - result.push_back(ToUpper(character)); + result.push_back(absl::ascii_toupper(character)); capitalize_next = false; } else { result.push_back(character); @@ -437,7 +429,7 @@ class FlatAllocatorImpl { std::string json_name = opt_json_name != nullptr ? *opt_json_name : ToJsonName(name); - StringPiece all_names[] = {name, lowercase_name, camelcase_name, + absl::string_view all_names[] = {name, lowercase_name, camelcase_name, json_name}; std::sort(all_names, all_names + 4); int unique = @@ -458,7 +450,7 @@ class FlatAllocatorImpl { GOOGLE_CHECK(has_allocated()); std::string full_name = - scope.empty() ? name : StrCat(scope, ".", name); + scope.empty() ? name : absl::StrCat(scope, ".", name); // Fast path for snake_case names, which follow the style guide. if (opt_json_name == nullptr) { @@ -576,7 +568,6 @@ class Symbol { METHOD, FULL_PACKAGE, SUB_PACKAGE, - QUERY_KEY }; Symbol() { @@ -642,35 +633,6 @@ class Symbol { : nullptr; } - // Not a real symbol. - // Only used for heterogeneous lookups and never actually inserted in the - // tables. - // TODO(b/215557658): If we templetize QueryKey on the expected object type we - // can skip the switches for the eq function altogether. - struct QueryKey : internal::SymbolBase { - StringPiece name; - const void* parent; - int field_number; - - // Adaptor functions to look like a Symbol to the comparators. - StringPiece full_name() const { return name; } - std::pair parent_number_key() const { - return {parent, field_number}; - } - std::pair parent_name_key() const { - return {parent, name}; - } - }; - // This constructor is implicit to allow for non-transparent lookups when - // necessary. - // For transparent lookup cases we query directly with the object without the - // type erasure layer. - Symbol(QueryKey& value) : ptr_(&value) { // NOLINT - value.symbol_type_ = QUERY_KEY; - } - const QueryKey* query_key() const { - return type() == QUERY_KEY ? static_cast(ptr_) : nullptr; - } #undef DEFINE_MEMBERS Type type() const { return static_cast(ptr_->symbol_type_); } @@ -708,7 +670,7 @@ class Symbol { } } - StringPiece full_name() const { + absl::string_view full_name() const { switch (type()) { case MESSAGE: return descriptor()->full_name(); @@ -727,17 +689,15 @@ class Symbol { case FULL_PACKAGE: return file_descriptor()->package(); case SUB_PACKAGE: - return StringPiece(sub_package_file_descriptor()->file->package()) + return absl::string_view(sub_package_file_descriptor()->file->package()) .substr(0, sub_package_file_descriptor()->name_size); - case QUERY_KEY: - return query_key()->full_name(); default: GOOGLE_CHECK(false); } return ""; } - std::pair parent_name_key() const { + std::pair parent_name_key() const { const auto or_file = [&](const void* p) { return p ? p : GetFile(); }; switch (type()) { case MESSAGE: @@ -764,24 +724,6 @@ class Symbol { return {GetFile(), service_descriptor()->name()}; case METHOD: return {method_descriptor()->service(), method_descriptor()->name()}; - case QUERY_KEY: - return query_key()->parent_name_key(); - default: - GOOGLE_CHECK(false); - } - return {}; - } - - std::pair parent_number_key() const { - switch (type()) { - case FIELD: - return {field_descriptor()->containing_type(), - field_descriptor()->number()}; - case ENUM_VALUE: - return {enum_value_descriptor()->type(), - enum_value_descriptor()->number()}; - case QUERY_KEY: - return query_key()->parent_number_key(); default: GOOGLE_CHECK(false); } @@ -895,9 +837,9 @@ std::string EnumValueToPascalCase(const std::string& input) { next_upper = true; } else { if (next_upper) { - result.push_back(ToUpper(character)); + result.push_back(absl::ascii_toupper(character)); } else { - result.push_back(ToLower(character)); + result.push_back(absl::ascii_tolower(character)); } next_upper = false; } @@ -909,18 +851,18 @@ std::string EnumValueToPascalCase(const std::string& input) { // Class to remove an enum prefix from enum values. class PrefixRemover { public: - PrefixRemover(StringPiece prefix) { + PrefixRemover(absl::string_view prefix) { // Strip underscores and lower-case the prefix. for (char character : prefix) { if (character != '_') { - prefix_ += ascii_tolower(character); + prefix_ += absl::ascii_tolower(character); } } } // Tries to remove the enum prefix from this enum value. // If this is not possible, returns the input verbatim. - std::string MaybeRemove(StringPiece str) { + std::string MaybeRemove(absl::string_view str) { // We can't just lowercase and strip str and look for a prefix. // We need to properly recognize the difference between: // @@ -939,7 +881,7 @@ class PrefixRemover { continue; } - if (ascii_tolower(str[i]) != prefix_[j++]) { + if (absl::ascii_tolower(str[i]) != prefix_[j++]) { return std::string(str); } } @@ -976,60 +918,16 @@ class PrefixRemover { // hash-maps for each object. // // The keys to these hash-maps are (parent, name) or (parent, number) pairs. - -typedef std::pair PointerStringPair; - -typedef std::pair DescriptorIntPair; - -#define HASH_MAP std::unordered_map -#define HASH_SET std::unordered_set -#define HASH_FXN hash - -template -struct PointerIntegerPairHash { - size_t operator()(const PairType& p) const { - static const size_t prime1 = 16777499; - static const size_t prime2 = 16777619; - return reinterpret_cast(p.first) * prime1 ^ - static_cast(p.second) * prime2; - } - -#ifdef _MSC_VER - // Used only by MSVC and platforms where hash_map is not available. - static const size_t bucket_size = 4; - static const size_t min_buckets = 8; -#endif - inline bool operator()(const PairType& a, const PairType& b) const { - return a < b; - } +struct FullNameQuery { + absl::string_view query; + absl::string_view full_name() const { return query; } }; - -struct PointerStringPairHash { - size_t operator()(const PointerStringPair& p) const { - static const size_t prime = 16777619; - hash string_hash; - return reinterpret_cast(p.first) * prime ^ - static_cast(string_hash(p.second)); - } - -#ifdef _MSC_VER - // Used only by MSVC and platforms where hash_map is not available. - static const size_t bucket_size = 4; - static const size_t min_buckets = 8; -#endif - inline bool operator()(const PointerStringPair& a, - const PointerStringPair& b) const { - return a < b; - } -}; - - struct SymbolByFullNameHash { using is_transparent = void; template size_t operator()(const T& s) const { - return HASH_FXN{}(s.full_name()); + return absl::HashOf(s.full_name()); } }; struct SymbolByFullNameEq { @@ -1041,14 +939,20 @@ struct SymbolByFullNameEq { } }; using SymbolsByNameSet = - HASH_SET; + absl::flat_hash_set; +struct ParentNameQuery { + std::pair query; + std::pair parent_name_key() const { + return query; + } +}; struct SymbolByParentHash { using is_transparent = void; template size_t operator()(const T& s) const { - return PointerStringPairHash{}(s.parent_name_key()); + return absl::HashOf(s.parent_name_key()); } }; struct SymbolByParentEq { @@ -1060,52 +964,94 @@ struct SymbolByParentEq { } }; using SymbolsByParentSet = - HASH_SET; + absl::flat_hash_set; + +struct FilesByNameHash { + using is_transparent = void; + + size_t operator()(absl::string_view name) const { return absl::HashOf(name); } + + size_t operator()(const FileDescriptor* file) const { + return absl::HashOf(file->name()); + } +}; -typedef HASH_MAP> - FilesByNameMap; +struct FilesByNameEq { + using is_transparent = void; -typedef HASH_MAP - FieldsByNameMap; + bool operator()(absl::string_view lhs, absl::string_view rhs) const { + return lhs == rhs; + } + bool operator()(absl::string_view lhs, const FileDescriptor* rhs) const { + return lhs == rhs->name(); + } + bool operator()(const FileDescriptor* lhs, absl::string_view rhs) const { + return lhs->name() == rhs; + } + bool operator()(const FileDescriptor* lhs, const FileDescriptor* rhs) const { + return lhs == rhs || lhs->name() == rhs->name(); + } +}; +using FilesByNameSet = + absl::flat_hash_set; + +using FieldsByNameMap = + absl::flat_hash_map, + const FieldDescriptor*>; -struct FieldsByNumberHash { +struct ParentNumberQuery { + std::pair query; +}; +std::pair ObjectToParentNumber(const FieldDescriptor* field) { + return {field->containing_type(), field->number()}; +} +std::pair ObjectToParentNumber( + const EnumValueDescriptor* enum_value) { + return {enum_value->type(), enum_value->number()}; +} +std::pair ObjectToParentNumber(ParentNumberQuery query) { + return query.query; +} +struct ParentNumberHash { using is_transparent = void; template - size_t operator()(const T& s) const { - return PointerIntegerPairHash>{}( - s.parent_number_key()); + size_t operator()(const T& t) const { + return absl::HashOf(ObjectToParentNumber(t)); } }; -struct FieldsByNumberEq { +struct ParentNumberEq { using is_transparent = void; template bool operator()(const T& a, const U& b) const { - return a.parent_number_key() == b.parent_number_key(); + return ObjectToParentNumber(a) == ObjectToParentNumber(b); } }; -using FieldsByNumberSet = - HASH_SET; -using EnumValuesByNumberSet = FieldsByNumberSet; +using FieldsByNumberSet = absl::flat_hash_set; +using EnumValuesByNumberSet = + absl::flat_hash_set; // This is a map rather than a hash-map, since we use it to iterate // through all the extensions that extend a given Descriptor, and an // ordered data structure that implements lower_bound is convenient // for that. -typedef std::map - ExtensionsGroupedByDescriptorMap; -typedef HASH_MAP - LocationsByPathMap; +using ExtensionsGroupedByDescriptorMap = + std::map, const FieldDescriptor*>; +using LocationsByPathMap = + absl::flat_hash_map; -std::set* NewAllowedProto3Extendee() { - auto allowed_proto3_extendees = new std::set; +absl::flat_hash_set* NewAllowedProto3Extendee() { const char* kOptionNames[] = { "FileOptions", "MessageOptions", "FieldOptions", "EnumOptions", "EnumValueOptions", "ServiceOptions", "MethodOptions", "OneofOptions", "ExtensionRangeOptions"}; + auto allowed_proto3_extendees = new absl::flat_hash_set(); + allowed_proto3_extendees->reserve(sizeof(kOptionNames) / + sizeof(kOptionNames[0])); + for (const char* option_name : kOptionNames) { // descriptor.proto has a different package name in opensource. We allow // both so the opensource protocol compiler can also compile internal @@ -1114,7 +1060,7 @@ std::set* NewAllowedProto3Extendee() { option_name); // Split the word to trick the opensource processing scripts so they // will keep the original package name. - allowed_proto3_extendees->insert(std::string("proto") + "2." + option_name); + allowed_proto3_extendees->insert(std::string("proto2.") + option_name); } return allowed_proto3_extendees; } @@ -1152,16 +1098,19 @@ class FileDescriptorTables { // Finding items. // Returns a null Symbol (symbol.IsNull() is true) if not found. + // TODO(sbenza): All callers to this function know the type they are looking + // for. If we propagate that information statically we can make the query + // faster. inline Symbol FindNestedSymbol(const void* parent, - StringPiece name) const; + absl::string_view name) const; // These return nullptr if not found. inline const FieldDescriptor* FindFieldByNumber(const Descriptor* parent, int number) const; inline const FieldDescriptor* FindFieldByLowercaseName( - const void* parent, StringPiece lowercase_name) const; + const void* parent, absl::string_view lowercase_name) const; inline const FieldDescriptor* FindFieldByCamelcaseName( - const void* parent, StringPiece camelcase_name) const; + const void* parent, absl::string_view camelcase_name) const; inline const EnumValueDescriptor* FindEnumValueByNumber( const EnumDescriptor* parent, int number) const; // This creates a new EnumValueDescriptor if not found, in a thread-safe way. @@ -1179,7 +1128,7 @@ class FileDescriptorTables { bool AddEnumValueByNumber(EnumValueDescriptor* value); // Populates p->first->locations_by_path_ from p->second. - // Unusual signature dictated by internal::call_once. + // Unusual signature dictated by absl::call_once. static void BuildLocationsByPath( std::pair* p); @@ -1204,8 +1153,8 @@ class FileDescriptorTables { void FieldsByCamelcaseNamesLazyInitInternal() const; SymbolsByParentSet symbols_by_parent_; - mutable internal::once_flag fields_by_lowercase_name_once_; - mutable internal::once_flag fields_by_camelcase_name_once_; + mutable absl::once_flag fields_by_lowercase_name_once_; + mutable absl::once_flag fields_by_camelcase_name_once_; // Make these fields atomic to avoid race conditions with // GetEstimatedOwnedMemoryBytesSize. Once the pointer is set the map won't // change anymore. @@ -1217,7 +1166,7 @@ class FileDescriptorTables { PROTOBUF_GUARDED_BY(unknown_enum_values_mu_); // Populated on first request to save space, hence constness games. - mutable internal::once_flag locations_by_path_once_; + mutable absl::once_flag locations_by_path_once_; mutable LocationsByPathMap locations_by_path_; // Mutex to protect the unknown-enum-value map due to dynamic @@ -1304,39 +1253,39 @@ class DescriptorPool::Tables { // execution of the current public API call, but for compatibility with // legacy clients, this is cleared at the beginning of each public API call. // Not used when fallback_database_ == nullptr. - HASH_SET known_bad_files_; + absl::flat_hash_set known_bad_files_; // A set of symbols which we have tried to load from the fallback database // and encountered errors. We will not attempt to load them again during // execution of the current public API call, but for compatibility with // legacy clients, this is cleared at the beginning of each public API call. - HASH_SET known_bad_symbols_; + absl::flat_hash_set known_bad_symbols_; // The set of descriptors for which we've already loaded the full // set of extensions numbers from fallback_database_. - HASH_SET extensions_loaded_from_db_; + absl::flat_hash_set extensions_loaded_from_db_; // Maps type name to Descriptor::WellKnownType. This is logically global // and const, but we make it a member here to simplify its construction and // destruction. This only has 20-ish entries and is one per DescriptorPool, // so the overhead is small. - HASH_MAP well_known_types_; + absl::flat_hash_map well_known_types_; // ----------------------------------------------------------------- // Finding items. // Find symbols. This returns a null Symbol (symbol.IsNull() is true) // if not found. - inline Symbol FindSymbol(StringPiece key) const; + inline Symbol FindSymbol(absl::string_view key) const; // This implements the body of DescriptorPool::Find*ByName(). It should // really be a private method of DescriptorPool, but that would require // declaring Symbol in descriptor.h, which would drag all kinds of other // stuff into the header. Yay C++. - Symbol FindByNameHelper(const DescriptorPool* pool, StringPiece name); + Symbol FindByNameHelper(const DescriptorPool* pool, absl::string_view name); // These return nullptr if not found. - inline const FileDescriptor* FindFile(StringPiece key) const; + inline const FileDescriptor* FindFile(absl::string_view key) const; inline const FieldDescriptor* FindExtension(const Descriptor* extendee, int number) const; inline void FindAllExtensions(const Descriptor* extendee, @@ -1395,7 +1344,7 @@ class DescriptorPool::Tables { flat_allocs_; SymbolsByNameSet symbols_by_name_; - FilesByNameMap files_by_name_; + FilesByNameSet files_by_name_; ExtensionsGroupedByDescriptorMap extensions_; struct CheckPoint { @@ -1419,7 +1368,7 @@ class DescriptorPool::Tables { std::vector checkpoints_; std::vector symbols_after_checkpoint_; std::vector files_after_checkpoint_; - std::vector extensions_after_checkpoint_; + std::vector> extensions_after_checkpoint_; }; DescriptorPool::Tables::Tables() { @@ -1484,7 +1433,7 @@ void DescriptorPool::Tables::RollbackToLastCheckpoint() { } for (size_t i = checkpoint.pending_files_before_checkpoint; i < files_after_checkpoint_.size(); i++) { - files_by_name_.erase(files_after_checkpoint_[i]->name()); + files_by_name_.erase(files_after_checkpoint_[i]); } for (size_t i = checkpoint.pending_extensions_before_checkpoint; i < extensions_after_checkpoint_.size(); i++) { @@ -1504,24 +1453,19 @@ void DescriptorPool::Tables::RollbackToLastCheckpoint() { // ------------------------------------------------------------------- -inline Symbol DescriptorPool::Tables::FindSymbol(StringPiece key) const { - Symbol::QueryKey name; - name.name = key; - auto it = symbols_by_name_.find(name); +inline Symbol DescriptorPool::Tables::FindSymbol(absl::string_view key) const { + auto it = symbols_by_name_.find(FullNameQuery{key}); return it == symbols_by_name_.end() ? Symbol() : *it; } inline Symbol FileDescriptorTables::FindNestedSymbol( - const void* parent, StringPiece name) const { - Symbol::QueryKey query; - query.name = name; - query.parent = parent; - auto it = symbols_by_parent_.find(query); + const void* parent, absl::string_view name) const { + auto it = symbols_by_parent_.find(ParentNameQuery{{parent, name}}); return it == symbols_by_parent_.end() ? Symbol() : *it; } Symbol DescriptorPool::Tables::FindByNameHelper(const DescriptorPool* pool, - StringPiece name) { + absl::string_view name) { if (pool->mutex_ != nullptr) { // Fast path: the Symbol is already cached. This is just a hash lookup. absl::ReaderMutexLock lock(pool->mutex_); @@ -1553,8 +1497,10 @@ Symbol DescriptorPool::Tables::FindByNameHelper(const DescriptorPool* pool, } inline const FileDescriptor* DescriptorPool::Tables::FindFile( - StringPiece key) const { - return FindPtrOrNull(files_by_name_, key); + absl::string_view key) const { + auto it = files_by_name_.find(key); + if (it == files_by_name_.end()) return nullptr; + return *it; } inline const FieldDescriptor* FileDescriptorTables::FindFieldByNumber( @@ -1566,12 +1512,8 @@ inline const FieldDescriptor* FileDescriptorTables::FindFieldByNumber( return parent->field(number - 1); } - Symbol::QueryKey query; - query.parent = parent; - query.field_number = number; - - auto it = fields_by_number_.find(query); - return it == fields_by_number_.end() ? nullptr : it->field_descriptor(); + auto it = fields_by_number_.find(ParentNumberQuery{{parent, number}}); + return it == fields_by_number_.end() ? nullptr : *it; } const void* FileDescriptorTables::FindParentForFieldsByMap( @@ -1604,13 +1546,15 @@ void FileDescriptorTables::FieldsByLowercaseNamesLazyInitInternal() const { } inline const FieldDescriptor* FileDescriptorTables::FindFieldByLowercaseName( - const void* parent, StringPiece lowercase_name) const { - internal::call_once( - fields_by_lowercase_name_once_, - &FileDescriptorTables::FieldsByLowercaseNamesLazyInitStatic, this); - return FindPtrOrNull( - *fields_by_lowercase_name_.load(std::memory_order_acquire), - PointerStringPair(parent, lowercase_name)); + const void* parent, absl::string_view lowercase_name) const { + absl::call_once(fields_by_lowercase_name_once_, + &FileDescriptorTables::FieldsByLowercaseNamesLazyInitStatic, + this); + const auto* fields = + fields_by_lowercase_name_.load(std::memory_order_acquire); + auto it = fields->find({parent, lowercase_name}); + if (it == fields->end()) return nullptr; + return it->second; } void FileDescriptorTables::FieldsByCamelcaseNamesLazyInitStatic( @@ -1630,13 +1574,14 @@ void FileDescriptorTables::FieldsByCamelcaseNamesLazyInitInternal() const { } inline const FieldDescriptor* FileDescriptorTables::FindFieldByCamelcaseName( - const void* parent, StringPiece camelcase_name) const { - internal::call_once( - fields_by_camelcase_name_once_, - FileDescriptorTables::FieldsByCamelcaseNamesLazyInitStatic, this); - return FindPtrOrNull( - *fields_by_camelcase_name_.load(std::memory_order_acquire), - PointerStringPair(parent, camelcase_name)); + const void* parent, absl::string_view camelcase_name) const { + absl::call_once(fields_by_camelcase_name_once_, + FileDescriptorTables::FieldsByCamelcaseNamesLazyInitStatic, + this); + auto* fields = fields_by_camelcase_name_.load(std::memory_order_acquire); + auto it = fields->find({parent, camelcase_name}); + if (it == fields->end()) return nullptr; + return it->second; } inline const EnumValueDescriptor* FileDescriptorTables::FindEnumValueByNumber( @@ -1649,13 +1594,8 @@ inline const EnumValueDescriptor* FileDescriptorTables::FindEnumValueByNumber( return parent->value(number - base); } - Symbol::QueryKey query; - query.parent = parent; - query.field_number = number; - - auto it = enum_values_by_number_.find(query); - return it == enum_values_by_number_.end() ? nullptr - : it->enum_value_descriptor(); + auto it = enum_values_by_number_.find(ParentNumberQuery{{parent, number}}); + return it == enum_values_by_number_.end() ? nullptr : *it; } inline const EnumValueDescriptor* @@ -1669,17 +1609,14 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( } } - Symbol::QueryKey query; - query.parent = parent; - query.field_number = number; + const ParentNumberQuery query{{parent, number}}; // Second try, with reader lock held on unknown enum values: common case. { absl::ReaderMutexLock l(&unknown_enum_values_mu_); auto it = unknown_enum_values_by_number_.find(query); - if (it != unknown_enum_values_by_number_.end() && - it->enum_value_descriptor() != nullptr) { - return it->enum_value_descriptor(); + if (it != unknown_enum_values_by_number_.end()) { + return *it; } } // If not found, try again with writer lock held, and create new descriptor if @@ -1687,9 +1624,8 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( { absl::WriterMutexLock l(&unknown_enum_values_mu_); auto it = unknown_enum_values_by_number_.find(query); - if (it != unknown_enum_values_by_number_.end() && - it->enum_value_descriptor() != nullptr) { - return it->enum_value_descriptor(); + if (it != unknown_enum_values_by_number_.end()) { + return *it; } // Create an EnumValueDescriptor dynamically. We don't insert it into the @@ -1712,18 +1648,20 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( EnumValueDescriptor* result = alloc.AllocateArray(1); result->all_names_ = alloc.AllocateStrings( enum_value_name, - StrCat(parent->full_name(), ".", enum_value_name)); + absl::StrCat(parent->full_name(), ".", enum_value_name)); result->number_ = number; result->type_ = parent; result->options_ = &EnumValueOptions::default_instance(); - unknown_enum_values_by_number_.insert(Symbol::EnumValue(result, 0)); + unknown_enum_values_by_number_.insert(result); return result; } } inline const FieldDescriptor* DescriptorPool::Tables::FindExtension( const Descriptor* extendee, int number) const { - return FindPtrOrNull(extensions_, std::make_pair(extendee, number)); + auto it = extensions_.find({extendee, number}); + if (it == extensions_.end()) return nullptr; + return it->second; } inline void DescriptorPool::Tables::FindAllExtensions( @@ -1758,7 +1696,7 @@ bool FileDescriptorTables::AddAliasUnderParent(const void* parent, } bool DescriptorPool::Tables::AddFile(const FileDescriptor* file) { - if (InsertIfNotPresent(&files_by_name_, file->name(), file)) { + if (files_by_name_.insert(file).second) { files_after_checkpoint_.push_back(file); return true; } else { @@ -1781,7 +1719,7 @@ bool FileDescriptorTables::AddFieldByNumber(FieldDescriptor* field) { return field->containing_type()->field(field->number() - 1) == field; } - return fields_by_number_.insert(Symbol(field)).second; + return fields_by_number_.insert(field).second; } bool FileDescriptorTables::AddEnumValueByNumber(EnumValueDescriptor* value) { @@ -1791,13 +1729,14 @@ bool FileDescriptorTables::AddEnumValueByNumber(EnumValueDescriptor* value) { value->number() <= static_cast(base) + value->type()->sequential_value_limit_) return true; - return enum_values_by_number_.insert(Symbol::EnumValue(value, 0)).second; + return enum_values_by_number_.insert(value).second; } bool DescriptorPool::Tables::AddExtension(const FieldDescriptor* field) { - DescriptorIntPair key(field->containing_type(), field->number()); - if (InsertIfNotPresent(&extensions_, key, field)) { - extensions_after_checkpoint_.push_back(key); + auto it_inserted = + extensions_.insert({{field->containing_type(), field->number()}, field}); + if (it_inserted.second) { + extensions_after_checkpoint_.push_back(it_inserted.first->first); return true; } else { return false; @@ -1843,7 +1782,7 @@ void FileDescriptorTables::BuildLocationsByPath( std::pair* p) { for (int i = 0, len = p->second->location_size(); i < len; ++i) { const SourceCodeInfo_Location* loc = &p->second->location().Get(i); - p->first->locations_by_path_[Join(loc->path(), ",")] = loc; + p->first->locations_by_path_[absl::StrJoin(loc->path(), ",")] = loc; } } @@ -1851,9 +1790,11 @@ const SourceCodeInfo_Location* FileDescriptorTables::GetSourceLocation( const std::vector& path, const SourceCodeInfo* info) const { std::pair p( std::make_pair(this, info)); - internal::call_once(locations_by_path_once_, - FileDescriptorTables::BuildLocationsByPath, &p); - return FindPtrOrNull(locations_by_path_, Join(path, ",")); + absl::call_once(locations_by_path_once_, + FileDescriptorTables::BuildLocationsByPath, &p); + auto it = locations_by_path_.find(absl::StrJoin(path, ",")); + if (it == locations_by_path_.end()) return nullptr; + return it->second; } // =================================================================== @@ -1909,7 +1850,7 @@ void DescriptorPool::InternalDontEnforceDependencies() { enforce_dependencies_ = false; } -void DescriptorPool::AddUnusedImportTrackFile(ConstStringParam file_name, +void DescriptorPool::AddUnusedImportTrackFile(absl::string_view file_name, bool is_error) { unused_import_track_files_[std::string(file_name)] = is_error; } @@ -1918,7 +1859,7 @@ void DescriptorPool::ClearUnusedImportTrackFiles() { unused_import_track_files_.clear(); } -bool DescriptorPool::InternalIsFileLoaded(ConstStringParam filename) const { +bool DescriptorPool::InternalIsFileLoaded(absl::string_view filename) const { absl::MutexLockMaybe lock(mutex_); return tables_->FindFile(filename) != nullptr; } @@ -1995,7 +1936,7 @@ void DescriptorPool::InternalAddGeneratedFile( // there's nothing more important to do (read: never). const FileDescriptor* DescriptorPool::FindFileByName( - ConstStringParam name) const { + absl::string_view name) const { absl::MutexLockMaybe lock(mutex_); if (fallback_database_ != nullptr) { tables_->known_bad_symbols_.clear(); @@ -2015,7 +1956,7 @@ const FileDescriptor* DescriptorPool::FindFileByName( } const FileDescriptor* DescriptorPool::FindFileContainingSymbol( - ConstStringParam symbol_name) const { + absl::string_view symbol_name) const { absl::MutexLockMaybe lock(mutex_); if (fallback_database_ != nullptr) { tables_->known_bad_symbols_.clear(); @@ -2036,12 +1977,12 @@ const FileDescriptor* DescriptorPool::FindFileContainingSymbol( } const Descriptor* DescriptorPool::FindMessageTypeByName( - ConstStringParam name) const { + absl::string_view name) const { return tables_->FindByNameHelper(this, name).descriptor(); } const FieldDescriptor* DescriptorPool::FindFieldByName( - ConstStringParam name) const { + absl::string_view name) const { if (const FieldDescriptor* field = tables_->FindByNameHelper(this, name).field_descriptor()) { if (!field->is_extension()) { @@ -2052,7 +1993,7 @@ const FieldDescriptor* DescriptorPool::FindFieldByName( } const FieldDescriptor* DescriptorPool::FindExtensionByName( - ConstStringParam name) const { + absl::string_view name) const { if (const FieldDescriptor* field = tables_->FindByNameHelper(this, name).field_descriptor()) { if (field->is_extension()) { @@ -2063,27 +2004,27 @@ const FieldDescriptor* DescriptorPool::FindExtensionByName( } const OneofDescriptor* DescriptorPool::FindOneofByName( - ConstStringParam name) const { + absl::string_view name) const { return tables_->FindByNameHelper(this, name).oneof_descriptor(); } const EnumDescriptor* DescriptorPool::FindEnumTypeByName( - ConstStringParam name) const { + absl::string_view name) const { return tables_->FindByNameHelper(this, name).enum_descriptor(); } const EnumValueDescriptor* DescriptorPool::FindEnumValueByName( - ConstStringParam name) const { + absl::string_view name) const { return tables_->FindByNameHelper(this, name).enum_value_descriptor(); } const ServiceDescriptor* DescriptorPool::FindServiceByName( - ConstStringParam name) const { + absl::string_view name) const { return tables_->FindByNameHelper(this, name).service_descriptor(); } const MethodDescriptor* DescriptorPool::FindMethodByName( - ConstStringParam name) const { + absl::string_view name) const { return tables_->FindByNameHelper(this, name).method_descriptor(); } @@ -2139,7 +2080,7 @@ const FieldDescriptor* DescriptorPool::InternalFindExtensionByNumberNoLock( } const FieldDescriptor* DescriptorPool::FindExtensionByPrintableName( - const Descriptor* extendee, ConstStringParam printable_name) const { + const Descriptor* extendee, absl::string_view printable_name) const { if (extendee->extension_range_count() == 0) return nullptr; const FieldDescriptor* result = FindExtensionByName(printable_name); if (result != nullptr && result->containing_type() == extendee) { @@ -2209,7 +2150,7 @@ const FieldDescriptor* Descriptor::FindFieldByNumber(int key) const { } const FieldDescriptor* Descriptor::FindFieldByLowercaseName( - ConstStringParam key) const { + absl::string_view key) const { const FieldDescriptor* result = file()->tables_->FindFieldByLowercaseName(this, key); if (result == nullptr || result->is_extension()) { @@ -2220,7 +2161,7 @@ const FieldDescriptor* Descriptor::FindFieldByLowercaseName( } const FieldDescriptor* Descriptor::FindFieldByCamelcaseName( - ConstStringParam key) const { + absl::string_view key) const { const FieldDescriptor* result = file()->tables_->FindFieldByCamelcaseName(this, key); if (result == nullptr || result->is_extension()) { @@ -2230,25 +2171,27 @@ const FieldDescriptor* Descriptor::FindFieldByCamelcaseName( } } -const FieldDescriptor* Descriptor::FindFieldByName(ConstStringParam key) const { +const FieldDescriptor* Descriptor::FindFieldByName( + absl::string_view key) const { const FieldDescriptor* field = file()->tables_->FindNestedSymbol(this, key).field_descriptor(); return field != nullptr && !field->is_extension() ? field : nullptr; } -const OneofDescriptor* Descriptor::FindOneofByName(ConstStringParam key) const { +const OneofDescriptor* Descriptor::FindOneofByName( + absl::string_view key) const { return file()->tables_->FindNestedSymbol(this, key).oneof_descriptor(); } const FieldDescriptor* Descriptor::FindExtensionByName( - ConstStringParam key) const { + absl::string_view key) const { const FieldDescriptor* field = file()->tables_->FindNestedSymbol(this, key).field_descriptor(); return field != nullptr && field->is_extension() ? field : nullptr; } const FieldDescriptor* Descriptor::FindExtensionByLowercaseName( - ConstStringParam key) const { + absl::string_view key) const { const FieldDescriptor* result = file()->tables_->FindFieldByLowercaseName(this, key); if (result == nullptr || !result->is_extension()) { @@ -2259,7 +2202,7 @@ const FieldDescriptor* Descriptor::FindExtensionByLowercaseName( } const FieldDescriptor* Descriptor::FindExtensionByCamelcaseName( - ConstStringParam key) const { + absl::string_view key) const { const FieldDescriptor* result = file()->tables_->FindFieldByCamelcaseName(this, key); if (result == nullptr || !result->is_extension()) { @@ -2269,17 +2212,18 @@ const FieldDescriptor* Descriptor::FindExtensionByCamelcaseName( } } -const Descriptor* Descriptor::FindNestedTypeByName(ConstStringParam key) const { +const Descriptor* Descriptor::FindNestedTypeByName( + absl::string_view key) const { return file()->tables_->FindNestedSymbol(this, key).descriptor(); } const EnumDescriptor* Descriptor::FindEnumTypeByName( - ConstStringParam key) const { + absl::string_view key) const { return file()->tables_->FindNestedSymbol(this, key).enum_descriptor(); } const EnumValueDescriptor* Descriptor::FindEnumValueByName( - ConstStringParam key) const { + absl::string_view key) const { return file()->tables_->FindNestedSymbol(this, key).enum_value_descriptor(); } @@ -2296,7 +2240,7 @@ const FieldDescriptor* Descriptor::map_value() const { } const EnumValueDescriptor* EnumDescriptor::FindValueByName( - ConstStringParam key) const { + absl::string_view key) const { return file()->tables_->FindNestedSymbol(this, key).enum_value_descriptor(); } @@ -2310,39 +2254,39 @@ const EnumValueDescriptor* EnumDescriptor::FindValueByNumberCreatingIfUnknown( } const MethodDescriptor* ServiceDescriptor::FindMethodByName( - ConstStringParam key) const { + absl::string_view key) const { return file()->tables_->FindNestedSymbol(this, key).method_descriptor(); } const Descriptor* FileDescriptor::FindMessageTypeByName( - ConstStringParam key) const { + absl::string_view key) const { return tables_->FindNestedSymbol(this, key).descriptor(); } const EnumDescriptor* FileDescriptor::FindEnumTypeByName( - ConstStringParam key) const { + absl::string_view key) const { return tables_->FindNestedSymbol(this, key).enum_descriptor(); } const EnumValueDescriptor* FileDescriptor::FindEnumValueByName( - ConstStringParam key) const { + absl::string_view key) const { return tables_->FindNestedSymbol(this, key).enum_value_descriptor(); } const ServiceDescriptor* FileDescriptor::FindServiceByName( - ConstStringParam key) const { + absl::string_view key) const { return tables_->FindNestedSymbol(this, key).service_descriptor(); } const FieldDescriptor* FileDescriptor::FindExtensionByName( - ConstStringParam key) const { + absl::string_view key) const { const FieldDescriptor* field = tables_->FindNestedSymbol(this, key).field_descriptor(); return field != nullptr && field->is_extension() ? field : nullptr; } const FieldDescriptor* FileDescriptor::FindExtensionByLowercaseName( - ConstStringParam key) const { + absl::string_view key) const { const FieldDescriptor* result = tables_->FindFieldByLowercaseName(this, key); if (result == nullptr || !result->is_extension()) { return nullptr; @@ -2352,7 +2296,7 @@ const FieldDescriptor* FileDescriptor::FindExtensionByLowercaseName( } const FieldDescriptor* FileDescriptor::FindExtensionByCamelcaseName( - ConstStringParam key) const { + absl::string_view key) const { const FieldDescriptor* result = tables_->FindFieldByCamelcaseName(this, key); if (result == nullptr || !result->is_extension()) { return nullptr; @@ -2409,7 +2353,7 @@ EnumDescriptor::FindReservedRangeContainingNumber(int number) const { // ------------------------------------------------------------------- bool DescriptorPool::TryFindFileInFallbackDatabase( - StringPiece name) const { + absl::string_view name) const { if (fallback_database_ == nullptr) return false; auto name_string = std::string(name); @@ -2424,7 +2368,7 @@ bool DescriptorPool::TryFindFileInFallbackDatabase( return true; } -bool DescriptorPool::IsSubSymbolOfBuiltType(StringPiece name) const { +bool DescriptorPool::IsSubSymbolOfBuiltType(absl::string_view name) const { auto prefix = std::string(name); for (;;) { std::string::size_type dot_pos = prefix.find_last_of('.'); @@ -2447,7 +2391,7 @@ bool DescriptorPool::IsSubSymbolOfBuiltType(StringPiece name) const { } bool DescriptorPool::TryFindSymbolInFallbackDatabase( - StringPiece name) const { + absl::string_view name) const { if (fallback_database_ == nullptr) return false; auto name_string = std::string(name); @@ -2524,13 +2468,13 @@ std::string FieldDescriptor::DefaultValueAsString( GOOGLE_CHECK(has_default_value()) << "No default value"; switch (cpp_type()) { case CPPTYPE_INT32: - return StrCat(default_value_int32_t()); + return absl::StrCat(default_value_int32_t()); case CPPTYPE_INT64: - return StrCat(default_value_int64_t()); + return absl::StrCat(default_value_int64_t()); case CPPTYPE_UINT32: - return StrCat(default_value_uint32_t()); + return absl::StrCat(default_value_uint32_t()); case CPPTYPE_UINT64: - return StrCat(default_value_uint64_t()); + return absl::StrCat(default_value_uint64_t()); case CPPTYPE_FLOAT: return SimpleFtoa(default_value_float()); case CPPTYPE_DOUBLE: @@ -2539,10 +2483,10 @@ std::string FieldDescriptor::DefaultValueAsString( return default_value_bool() ? "true" : "false"; case CPPTYPE_STRING: if (quote_string_type) { - return "\"" + CEscape(default_value_string()) + "\""; + return "\"" + absl::CEscape(default_value_string()) + "\""; } else { if (type() == TYPE_BYTES) { - return CEscape(default_value_string()); + return absl::CEscape(default_value_string()); } else { return default_value_string(); } @@ -2683,9 +2627,9 @@ void FieldDescriptor::CopyTo(FieldDescriptorProto* proto) const { // Some compilers do not allow static_cast directly between two enum types, // so we must cast to int first. proto->set_label(static_cast( - implicit_cast(label()))); + absl::implicit_cast(label()))); proto->set_type(static_cast( - implicit_cast(type()))); + absl::implicit_cast(type()))); if (is_extension()) { if (!containing_type()->is_unqualified_placeholder_) { @@ -2892,7 +2836,7 @@ bool FormatBracketedOptions(int depth, const Message& options, const DescriptorPool* pool, std::string* output) { std::vector all_options; if (RetrieveOptions(depth, options, pool, &all_options)) { - output->append(Join(all_options, ", ")); + output->append(absl::StrJoin(all_options, ", ")); } return !all_options.empty(); } @@ -2996,18 +2940,16 @@ std::string FileDescriptor::DebugStringWithOptions( SourceLocationCommentPrinter comment_printer(this, "", debug_string_options); comment_printer.AddPreComment(&contents); - std::set public_dependencies; - std::set weak_dependencies; - public_dependencies.insert(public_dependencies_, - public_dependencies_ + public_dependency_count_); - weak_dependencies.insert(weak_dependencies_, - weak_dependencies_ + weak_dependency_count_); + absl::flat_hash_set public_dependencies( + public_dependencies_, public_dependencies_ + public_dependency_count_); + absl::flat_hash_set weak_dependencies( + weak_dependencies_, weak_dependencies_ + weak_dependency_count_); for (int i = 0; i < dependency_count(); i++) { - if (public_dependencies.count(i) > 0) { + if (public_dependencies.contains(i)) { strings::SubstituteAndAppend(&contents, "import public \"$0\";\n", dependency(i)->name()); - } else if (weak_dependencies.count(i) > 0) { + } else if (weak_dependencies.contains(i)) { strings::SubstituteAndAppend(&contents, "import weak \"$0\";\n", dependency(i)->name()); } else { @@ -3037,7 +2979,7 @@ std::string FileDescriptor::DebugStringWithOptions( // Find all the 'group' type extensions; we will not output their nested // definitions (those will be done with their group field descriptor). - std::set groups; + absl::flat_hash_set groups; for (int i = 0; i < extension_count(); i++) { if (extension(i)->type() == FieldDescriptor::TYPE_GROUP) { groups.insert(extension(i)->message_type()); @@ -3045,7 +2987,7 @@ std::string FileDescriptor::DebugStringWithOptions( } for (int i = 0; i < message_type_count(); i++) { - if (groups.count(message_type(i)) == 0) { + if (!groups.contains(message_type(i))) { message_type(i)->DebugString(0, &contents, debug_string_options, /* include_opening_clause */ true); contents.append("\n"); @@ -3110,7 +3052,7 @@ void Descriptor::DebugString(int depth, std::string* contents, // Find all the 'group' types for fields and extensions; we will not output // their nested definitions (those will be done with their group field // descriptor). - std::set groups; + absl::flat_hash_set groups; for (int i = 0; i < field_count(); i++) { if (field(i)->type() == FieldDescriptor::TYPE_GROUP) { groups.insert(field(i)->message_type()); @@ -3123,7 +3065,7 @@ void Descriptor::DebugString(int depth, std::string* contents, } for (int i = 0; i < nested_type_count(); i++) { - if (groups.count(nested_type(i)) == 0) { + if (!groups.contains(nested_type(i))) { nested_type(i)->DebugString(depth, contents, debug_string_options, /* include_opening_clause */ true); } @@ -3181,7 +3123,7 @@ void Descriptor::DebugString(int depth, std::string* contents, strings::SubstituteAndAppend(contents, "$0 reserved ", prefix); for (int i = 0; i < reserved_name_count(); i++) { strings::SubstituteAndAppend(contents, "\"$0\", ", - CEscape(reserved_name(i))); + absl::CEscape(reserved_name(i))); } contents->replace(contents->size() - 2, 2, ";\n"); } @@ -3239,7 +3181,7 @@ void FieldDescriptor::DebugString( field_type = FieldTypeNameDebugString(); } - std::string label = StrCat(kLabelToName[this->label()], " "); + std::string label = absl::StrCat(kLabelToName[this->label()], " "); // Label is omitted for maps, oneof, and plain proto3 fields. if (is_map() || real_containing_oneof() || @@ -3269,7 +3211,7 @@ void FieldDescriptor::DebugString( contents->append(", "); } contents->append("json_name = \""); - contents->append(CEscape(json_name())); + contents->append(absl::CEscape(json_name())); contents->append("\""); } @@ -3386,7 +3328,7 @@ void EnumDescriptor::DebugString( strings::SubstituteAndAppend(contents, "$0 reserved ", prefix); for (int i = 0; i < reserved_name_count(); i++) { strings::SubstituteAndAppend(contents, "\"$0\", ", - CEscape(reserved_name(i))); + absl::CEscape(reserved_name(i))); } contents->replace(contents->size() - 2, 2, ";\n"); } @@ -3701,7 +3643,7 @@ class DescriptorBuilder { std::string filename_; FileDescriptor* file_; FileDescriptorTables* file_tables_; - std::set dependencies_; + absl::flat_hash_set dependencies_; struct MessageHints { int fields_to_suggest = 0; @@ -3724,11 +3666,11 @@ class DescriptorBuilder { } }; - std::unordered_map message_hints_; + absl::flat_hash_map message_hints_; // unused_dependency_ is used to record the unused imported files. // Note: public import is not considered. - std::set unused_dependency_; + absl::flat_hash_set unused_dependency_; // If LookupSymbol() finds a symbol that is in a file which is not a declared // dependency of this file, it will fail, but will set @@ -4308,7 +4250,7 @@ Symbol DescriptorBuilder::FindSymbolNotEnforcingDeps(const std::string& name, // Only find symbols which were defined in this file or one of its // dependencies. const FileDescriptor* file = result.GetFile(); - if (file == file_ || dependencies_.count(file) > 0) { + if (file == file_ || dependencies_.contains(file)) { unused_dependency_.erase(file); } return result; @@ -4327,7 +4269,7 @@ Symbol DescriptorBuilder::FindSymbol(const std::string& name, bool build_it) { // Only find symbols which were defined in this file or one of its // dependencies. const FileDescriptor* file = result.GetFile(); - if (file == file_ || dependencies_.count(file) > 0) { + if (file == file_ || dependencies_.contains(file)) { return result; } @@ -4340,11 +4282,9 @@ Symbol DescriptorBuilder::FindSymbol(const std::string& name, bool build_it) { // dependency also defines the same package. We can't really rule out this // symbol unless none of the dependencies define it. if (IsInPackage(file_, name)) return result; - for (std::set::const_iterator it = - dependencies_.begin(); - it != dependencies_.end(); ++it) { + for (const auto* dep : dependencies_) { // Note: A dependency may be nullptr if it was not found or had errors. - if (*it != nullptr && IsInPackage(*it, name)) return result; + if (dep != nullptr && IsInPackage(dep, name)) return result; } } @@ -4442,7 +4382,7 @@ Symbol DescriptorBuilder::LookupSymbol( return result; } -static bool ValidateQualifiedName(StringPiece name) { +static bool ValidateQualifiedName(absl::string_view name) { bool last_was_period = false; for (char character : name) { @@ -4462,20 +4402,20 @@ static bool ValidateQualifiedName(StringPiece name) { return !name.empty() && !last_was_period; } -Symbol DescriptorPool::NewPlaceholder(StringPiece name, +Symbol DescriptorPool::NewPlaceholder(absl::string_view name, PlaceholderType placeholder_type) const { absl::MutexLockMaybe lock(mutex_); return NewPlaceholderWithMutexHeld(name, placeholder_type); } Symbol DescriptorPool::NewPlaceholderWithMutexHeld( - StringPiece name, PlaceholderType placeholder_type) const { + absl::string_view name, PlaceholderType placeholder_type) const { if (mutex_) { mutex_->AssertHeld(); } // Compute names. - StringPiece placeholder_full_name; - StringPiece placeholder_name; + absl::string_view placeholder_full_name; + absl::string_view placeholder_name; const std::string* placeholder_package; if (!ValidateQualifiedName(name)) return Symbol(); @@ -4515,7 +4455,7 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( } FileDescriptor* placeholder_file = NewPlaceholderFileWithMutexHeld( - StrCat(placeholder_full_name, ".placeholder.proto"), alloc); + absl::StrCat(placeholder_full_name, ".placeholder.proto"), alloc); placeholder_file->package_ = placeholder_package; if (placeholder_type == PLACEHOLDER_ENUM) { @@ -4584,7 +4524,7 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( } FileDescriptor* DescriptorPool::NewPlaceholderFile( - StringPiece name) const { + absl::string_view name) const { absl::MutexLockMaybe lock(mutex_); internal::FlatAllocator alloc; alloc.PlanArray(1); @@ -4595,7 +4535,7 @@ FileDescriptor* DescriptorPool::NewPlaceholderFile( } FileDescriptor* DescriptorPool::NewPlaceholderFileWithMutexHeld( - StringPiece name, internal::FlatAllocator& alloc) const { + absl::string_view name, internal::FlatAllocator& alloc) const { if (mutex_) { mutex_->AssertHeld(); } @@ -5155,13 +5095,13 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( } // Make sure all dependencies are loaded. - std::set seen_dependencies; + absl::flat_hash_set seen_dependencies; result->dependency_count_ = proto.dependency_size(); result->dependencies_ = alloc.AllocateArray(proto.dependency_size()); result->dependencies_once_ = nullptr; unused_dependency_.clear(); - std::set weak_deps; + absl::flat_hash_set weak_deps; for (int i = 0; i < proto.weak_dependency_size(); ++i) { weak_deps.insert(proto.weak_dependency(i)); } @@ -5187,7 +5127,7 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( if (dependency == nullptr) { if (!pool_->lazily_build_dependencies_) { if (pool_->allow_unknown_ || - (!pool_->enforce_weak_ && weak_deps.find(i) != weak_deps.end())) { + (!pool_->enforce_weak_ && weak_deps.contains(i))) { internal::FlatAllocator lazy_dep_alloc; lazy_dep_alloc.PlanArray(1); lazy_dep_alloc.PlanArray(1); @@ -5224,8 +5164,8 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( } void* data = tables_->AllocateBytes( - static_cast(sizeof(internal::once_flag) + total_char_size)); - result->dependencies_once_ = ::new (data) internal::once_flag{}; + static_cast(sizeof(absl::once_flag)) + total_char_size); + result->dependencies_once_ = ::new (data) absl::once_flag{}; char* name_data = reinterpret_cast(result->dependencies_once_ + 1); for (int i = 0; i < proto.dependency_size(); i++) { @@ -5362,7 +5302,7 @@ const std::string* DescriptorBuilder::AllocateNameStrings( return alloc.AllocateStrings(proto_name, proto_name); } else { return alloc.AllocateStrings(proto_name, - StrCat(scope, ".", proto_name)); + absl::StrCat(scope, ".", proto_name)); } } @@ -5464,12 +5404,10 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, } } - HASH_SET reserved_name_set; + absl::flat_hash_set reserved_name_set; for (int i = 0; i < proto.reserved_name_size(); i++) { const std::string& name = proto.reserved_name(i); - if (reserved_name_set.find(name) == reserved_name_set.end()) { - reserved_name_set.insert(name); - } else { + if (!reserved_name_set.insert(name).second) { AddError(name, proto, DescriptorPool::ErrorCollector::NAME, strings::Substitute("Field name \"$0\" is reserved multiple times.", name)); @@ -5580,9 +5518,9 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, // Some compilers do not allow static_cast directly between two enum types, // so we must cast to int first. result->type_ = static_cast( - implicit_cast(proto.type())); + absl::implicit_cast(proto.type())); result->label_ = static_cast( - implicit_cast(proto.label())); + absl::implicit_cast(proto.label())); if (result->label_ == FieldDescriptor::LABEL_REQUIRED) { // An extension cannot have a required field (b/13365836). @@ -5677,8 +5615,9 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, break; case FieldDescriptor::CPPTYPE_STRING: if (result->type() == FieldDescriptor::TYPE_BYTES) { - result->default_value_string_ = alloc.AllocateStrings( - UnescapeCEscapeString(proto.default_value())); + std::string value; + absl::CUnescape(proto.default_value(), &value); + result->default_value_string_ = alloc.AllocateStrings(value); } else { result->default_value_string_ = alloc.AllocateStrings(proto.default_value()); @@ -6058,7 +5997,7 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, } } - HASH_SET reserved_name_set; + absl::flat_hash_set reserved_name_set; for (int i = 0; i < proto.reserved_name_size(); i++) { const std::string& name = proto.reserved_name(i); if (reserved_name_set.find(name) == reserved_name_set.end()) { @@ -6453,8 +6392,9 @@ void DescriptorBuilder::CrossLinkField(FieldDescriptor* field, int name_sizes = static_cast(name.size() + 1 + proto.default_value().size() + 1); - field->type_once_ = ::new (tables_->AllocateBytes(static_cast( - sizeof(internal::once_flag) + name_sizes))) internal::once_flag{}; + field->type_once_ = ::new (tables_->AllocateBytes( + static_cast(sizeof(absl::once_flag)) + name_sizes)) + absl::once_flag{}; char* names = reinterpret_cast(field->type_once_ + 1); memcpy(names, name.c_str(), name.size() + 1); @@ -6712,8 +6652,9 @@ void DescriptorBuilder::SuggestFieldNumbers(FileDescriptor* file, for (int message_index = 0; message_index < file->message_type_count(); message_index++) { const Descriptor* message = &file->message_types_[message_index]; - auto* hints = FindOrNull(message_hints_, message); - if (!hints) continue; + auto hints_it = message_hints_.find(message); + if (hints_it == message_hints_.end()) continue; + auto* hints = &hints_it->second; constexpr int kMaxSuggestions = 3; int fields_to_suggest = std::min(kMaxSuggestions, hints->fields_to_suggest); if (fields_to_suggest <= 0) continue; @@ -7313,7 +7254,7 @@ bool DescriptorBuilder::OptionInterpreter::InterpretOptions( *original_options, original_uninterpreted_options_field); for (int i = 0; i < num_uninterpreted_options; ++i) { src_path.push_back(i); - uninterpreted_option_ = down_cast( + uninterpreted_option_ = DownCast( &original_options->GetReflection()->GetRepeatedMessage( *original_options, original_uninterpreted_options_field, i)); if (!InterpretSingleOption(options, src_path, @@ -8158,22 +8099,20 @@ void DescriptorBuilder::LogUnusedDependency(const FileDescriptorProto& proto, auto itr = pool_->unused_import_track_files_.find(proto.name()); bool is_error = itr != pool_->unused_import_track_files_.end() && itr->second; - for (std::set::const_iterator it = - unused_dependency_.begin(); - it != unused_dependency_.end(); ++it) { - std::string error_message = "Import " + (*it)->name() + " is unused."; + for (const auto* unused : unused_dependency_) { + std::string error_message = "Import " + unused->name() + " is unused."; if (is_error) { - AddError((*it)->name(), proto, DescriptorPool::ErrorCollector::IMPORT, + AddError(unused->name(), proto, DescriptorPool::ErrorCollector::IMPORT, error_message); } else { - AddWarning((*it)->name(), proto, DescriptorPool::ErrorCollector::IMPORT, - error_message); + AddWarning(unused->name(), proto, + DescriptorPool::ErrorCollector::IMPORT, error_message); } } } } -Symbol DescriptorPool::CrossLinkOnDemandHelper(StringPiece name, +Symbol DescriptorPool::CrossLinkOnDemandHelper(absl::string_view name, bool expecting_enum) const { (void)expecting_enum; // Parameter is used by Google-internal code. auto lookup_name = std::string(name); @@ -8235,11 +8174,11 @@ void FieldDescriptor::TypeOnceInit(const FieldDescriptor* to_init) { } // message_type(), enum_type(), default_value_enum(), and type() -// all share the same internal::call_once init path to do lazy +// all share the same absl::call_once init path to do lazy // import building and cross linking of a field of a message. const Descriptor* FieldDescriptor::message_type() const { if (type_once_) { - internal::call_once(*type_once_, FieldDescriptor::TypeOnceInit, this); + absl::call_once(*type_once_, FieldDescriptor::TypeOnceInit, this); } return type_ == TYPE_MESSAGE || type_ == TYPE_GROUP ? type_descriptor_.message_type @@ -8248,14 +8187,14 @@ const Descriptor* FieldDescriptor::message_type() const { const EnumDescriptor* FieldDescriptor::enum_type() const { if (type_once_) { - internal::call_once(*type_once_, FieldDescriptor::TypeOnceInit, this); + absl::call_once(*type_once_, FieldDescriptor::TypeOnceInit, this); } return type_ == TYPE_ENUM ? type_descriptor_.enum_type : nullptr; } const EnumValueDescriptor* FieldDescriptor::default_value_enum() const { if (type_once_) { - internal::call_once(*type_once_, FieldDescriptor::TypeOnceInit, this); + absl::call_once(*type_once_, FieldDescriptor::TypeOnceInit, this); } return default_value_enum_; } @@ -8288,9 +8227,9 @@ void FileDescriptor::DependenciesOnceInit(const FileDescriptor* to_init) { const FileDescriptor* FileDescriptor::dependency(int index) const { if (dependencies_once_) { // Do once init for all indices, as it's unlikely only a single index would - // be called, and saves on internal::call_once allocations. - internal::call_once(*dependencies_once_, - FileDescriptor::DependenciesOnceInit, this); + // be called, and saves on absl::call_once allocations. + absl::call_once(*dependencies_once_, FileDescriptor::DependenciesOnceInit, + this); } return dependencies_[index]; } @@ -8309,7 +8248,7 @@ void LazyDescriptor::Set(const Descriptor* descriptor) { descriptor_ = descriptor; } -void LazyDescriptor::SetLazy(StringPiece name, +void LazyDescriptor::SetLazy(absl::string_view name, const FileDescriptor* file) { // verify Init() has been called and Set hasn't been called yet. GOOGLE_CHECK(!descriptor_); @@ -8318,7 +8257,7 @@ void LazyDescriptor::SetLazy(StringPiece name, GOOGLE_CHECK(file->pool_->lazily_build_dependencies_); GOOGLE_CHECK(!file->finished_building_); once_ = ::new (file->pool_->tables_->AllocateBytes(static_cast( - sizeof(internal::once_flag) + name.size() + 1))) internal::once_flag{}; + sizeof(absl::once_flag) + name.size() + 1))) absl::once_flag{}; char* lazy_name = reinterpret_cast(once_ + 1); memcpy(lazy_name, name.data(), name.size()); lazy_name[name.size()] = 0; @@ -8326,7 +8265,7 @@ void LazyDescriptor::SetLazy(StringPiece name, void LazyDescriptor::Once(const ServiceDescriptor* service) { if (once_) { - internal::call_once(*once_, [&] { + absl::call_once(*once_, [&] { auto* file = service->file(); GOOGLE_CHECK(file->finished_building_); const char* lazy_name = reinterpret_cast(once_ + 1); diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h index 1f79e293c80d..a14ded8c002d 100644 --- a/src/google/protobuf/descriptor.h +++ b/src/google/protobuf/descriptor.h @@ -65,9 +65,9 @@ #include #include -#include #include -#include +#include "absl/base/call_once.h" +#include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" @@ -193,7 +193,7 @@ namespace internal { // #if !defined(PROTOBUF_INTERNAL_CHECK_CLASS_SIZE) -#define PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(t, expected) static_assert(true, "") +#define PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(t, expected) #endif class FlatAllocator; @@ -218,7 +218,7 @@ class PROTOBUF_EXPORT LazyDescriptor { // build time if the symbol wasn't found and building of the file containing // that type is delayed because lazily_build_dependencies_ is set on the pool. // Should not be called after Set() has been called. - void SetLazy(StringPiece name, const FileDescriptor* file); + void SetLazy(absl::string_view name, const FileDescriptor* file); // Returns the current value of the descriptor, thread-safe. If SetLazy(...) // has been called, will do a one-time cross link of the type specified, @@ -233,7 +233,7 @@ class PROTOBUF_EXPORT LazyDescriptor { const Descriptor* descriptor_; // The once_ flag is followed by a NUL terminated string for the type name. - internal::once_flag* once_; + absl::once_flag* once_; }; class PROTOBUF_EXPORT SymbolBase { @@ -347,20 +347,20 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { // exists. const FieldDescriptor* FindFieldByNumber(int number) const; // Looks up a field by name. Returns nullptr if no such field exists. - const FieldDescriptor* FindFieldByName(ConstStringParam name) const; + const FieldDescriptor* FindFieldByName(absl::string_view name) const; // Looks up a field by lowercased name (as returned by lowercase_name()). // This lookup may be ambiguous if multiple field names differ only by case, // in which case the field returned is chosen arbitrarily from the matches. const FieldDescriptor* FindFieldByLowercaseName( - ConstStringParam lowercase_name) const; + absl::string_view lowercase_name) const; // Looks up a field by camel-case name (as returned by camelcase_name()). // This lookup may be ambiguous if multiple field names differ in a way that // leads them to have identical camel-case names, in which case the field // returned is chosen arbitrarily from the matches. const FieldDescriptor* FindFieldByCamelcaseName( - ConstStringParam camelcase_name) const; + absl::string_view camelcase_name) const; // The number of oneofs in this message type. int oneof_decl_count() const; @@ -373,7 +373,7 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { const OneofDescriptor* oneof_decl(int index) const; // Looks up a oneof by name. Returns nullptr if no such oneof exists. - const OneofDescriptor* FindOneofByName(ConstStringParam name) const; + const OneofDescriptor* FindOneofByName(absl::string_view name) const; // Nested type stuff ----------------------------------------------- @@ -385,7 +385,7 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { // Looks up a nested type by name. Returns nullptr if no such nested type // exists. - const Descriptor* FindNestedTypeByName(ConstStringParam name) const; + const Descriptor* FindNestedTypeByName(absl::string_view name) const; // Enum stuff ------------------------------------------------------ @@ -397,11 +397,11 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { // Looks up an enum type by name. Returns nullptr if no such enum type // exists. - const EnumDescriptor* FindEnumTypeByName(ConstStringParam name) const; + const EnumDescriptor* FindEnumTypeByName(absl::string_view name) const; // Looks up an enum value by name, among all enum types in this message. // Returns nullptr if no such value exists. - const EnumValueDescriptor* FindEnumValueByName(ConstStringParam name) const; + const EnumValueDescriptor* FindEnumValueByName(absl::string_view name) const; // Extensions ------------------------------------------------------ @@ -465,17 +465,17 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { // Looks up a named extension (which extends some *other* message type) // defined within this message type's scope. - const FieldDescriptor* FindExtensionByName(ConstStringParam name) const; + const FieldDescriptor* FindExtensionByName(absl::string_view name) const; // Similar to FindFieldByLowercaseName(), but finds extensions defined within // this message type's scope. const FieldDescriptor* FindExtensionByLowercaseName( - ConstStringParam name) const; + absl::string_view name) const; // Similar to FindFieldByCamelcaseName(), but finds extensions defined within // this message type's scope. const FieldDescriptor* FindExtensionByCamelcaseName( - ConstStringParam name) const; + absl::string_view name) const; // Reserved fields ------------------------------------------------- @@ -505,7 +505,7 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { const std::string& reserved_name(int index) const; // Returns true if the field name is reserved. - bool IsReservedName(ConstStringParam name) const; + bool IsReservedName(absl::string_view name) const; // Source Location --------------------------------------------------- @@ -939,7 +939,7 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase { // The once_flag is followed by a NUL terminated string for the type name and // enum default value (or empty string if no default enum). - internal::once_flag* type_once_; + absl::once_flag* type_once_; static void TypeOnceInit(const FieldDescriptor* to_init); void InternalTypeOnceInit() const; const Descriptor* containing_type_; @@ -1097,7 +1097,7 @@ class PROTOBUF_EXPORT EnumDescriptor : private internal::SymbolBase { const EnumValueDescriptor* value(int index) const; // Looks up a value by name. Returns nullptr if no such value exists. - const EnumValueDescriptor* FindValueByName(ConstStringParam name) const; + const EnumValueDescriptor* FindValueByName(absl::string_view name) const; // Looks up a value by number. Returns nullptr if no such value exists. If // multiple values have this number, the first one defined is returned. const EnumValueDescriptor* FindValueByNumber(int number) const; @@ -1155,7 +1155,7 @@ class PROTOBUF_EXPORT EnumDescriptor : private internal::SymbolBase { const std::string& reserved_name(int index) const; // Returns true if the field name is reserved. - bool IsReservedName(ConstStringParam name) const; + bool IsReservedName(absl::string_view name) const; // Source Location --------------------------------------------------- @@ -1355,7 +1355,7 @@ class PROTOBUF_EXPORT ServiceDescriptor : private internal::SymbolBase { const MethodDescriptor* method(int index) const; // Look up a MethodDescriptor by name. - const MethodDescriptor* FindMethodByName(ConstStringParam name) const; + const MethodDescriptor* FindMethodByName(absl::string_view name) const; // See Descriptor::CopyTo(). void CopyTo(ServiceDescriptorProto* proto) const; @@ -1586,25 +1586,25 @@ class PROTOBUF_EXPORT FileDescriptor : private internal::SymbolBase { // Find a top-level message type by name (not full_name). Returns nullptr if // not found. - const Descriptor* FindMessageTypeByName(ConstStringParam name) const; + const Descriptor* FindMessageTypeByName(absl::string_view name) const; // Find a top-level enum type by name. Returns nullptr if not found. - const EnumDescriptor* FindEnumTypeByName(ConstStringParam name) const; + const EnumDescriptor* FindEnumTypeByName(absl::string_view name) const; // Find an enum value defined in any top-level enum by name. Returns nullptr // if not found. - const EnumValueDescriptor* FindEnumValueByName(ConstStringParam name) const; + const EnumValueDescriptor* FindEnumValueByName(absl::string_view name) const; // Find a service definition by name. Returns nullptr if not found. - const ServiceDescriptor* FindServiceByName(ConstStringParam name) const; + const ServiceDescriptor* FindServiceByName(absl::string_view name) const; // Find a top-level extension definition by name. Returns nullptr if not // found. - const FieldDescriptor* FindExtensionByName(ConstStringParam name) const; + const FieldDescriptor* FindExtensionByName(absl::string_view name) const; // Similar to FindExtensionByName(), but searches by lowercased-name. See // Descriptor::FindFieldByLowercaseName(). const FieldDescriptor* FindExtensionByLowercaseName( - ConstStringParam name) const; + absl::string_view name) const; // Similar to FindExtensionByName(), but searches by camelcased-name. See // Descriptor::FindFieldByCamelcaseName(). const FieldDescriptor* FindExtensionByCamelcaseName( - ConstStringParam name) const; + absl::string_view name) const; // See Descriptor::CopyTo(). // Notes: @@ -1662,7 +1662,7 @@ class PROTOBUF_EXPORT FileDescriptor : private internal::SymbolBase { // dependencies_once_ contain a once_flag followed by N NUL terminated // strings. Dependencies that do not need to be loaded will be empty. ie just // {'\0'} - internal::once_flag* dependencies_once_; + absl::once_flag* dependencies_once_; static void DependenciesOnceInit(const FileDescriptor* to_init); void InternalDependenciesOnceInit() const; @@ -1774,28 +1774,28 @@ class PROTOBUF_EXPORT DescriptorPool { // Find a FileDescriptor in the pool by file name. Returns nullptr if not // found. - const FileDescriptor* FindFileByName(ConstStringParam name) const; + const FileDescriptor* FindFileByName(absl::string_view name) const; // Find the FileDescriptor in the pool which defines the given symbol. // If any of the Find*ByName() methods below would succeed, then this is // equivalent to calling that method and calling the result's file() method. // Otherwise this returns nullptr. const FileDescriptor* FindFileContainingSymbol( - ConstStringParam symbol_name) const; + absl::string_view symbol_name) const; // Looking up descriptors ------------------------------------------ // These find descriptors by fully-qualified name. These will find both // top-level descriptors and nested descriptors. They return nullptr if not // found. - const Descriptor* FindMessageTypeByName(ConstStringParam name) const; - const FieldDescriptor* FindFieldByName(ConstStringParam name) const; - const FieldDescriptor* FindExtensionByName(ConstStringParam name) const; - const OneofDescriptor* FindOneofByName(ConstStringParam name) const; - const EnumDescriptor* FindEnumTypeByName(ConstStringParam name) const; - const EnumValueDescriptor* FindEnumValueByName(ConstStringParam name) const; - const ServiceDescriptor* FindServiceByName(ConstStringParam name) const; - const MethodDescriptor* FindMethodByName(ConstStringParam name) const; + const Descriptor* FindMessageTypeByName(absl::string_view name) const; + const FieldDescriptor* FindFieldByName(absl::string_view name) const; + const FieldDescriptor* FindExtensionByName(absl::string_view name) const; + const OneofDescriptor* FindOneofByName(absl::string_view name) const; + const EnumDescriptor* FindEnumTypeByName(absl::string_view name) const; + const EnumValueDescriptor* FindEnumValueByName(absl::string_view name) const; + const ServiceDescriptor* FindServiceByName(absl::string_view name) const; + const MethodDescriptor* FindMethodByName(absl::string_view name) const; // Finds an extension of the given type by number. The extendee must be // a member of this DescriptorPool or one of its underlays. @@ -1808,7 +1808,7 @@ class PROTOBUF_EXPORT DescriptorPool { // or one of its underlays. Returns nullptr if there is no known message // extension with the given printable name. const FieldDescriptor* FindExtensionByPrintableName( - const Descriptor* extendee, ConstStringParam printable_name) const; + const Descriptor* extendee, absl::string_view printable_name) const; // Finds extensions of extendee. The extensions will be appended to // out in an undefined order. Only extensions defined directly in @@ -1981,11 +1981,11 @@ class PROTOBUF_EXPORT DescriptorPool { // For internal (unit test) use only: Returns true if a FileDescriptor has // been constructed for the given file, false otherwise. Useful for testing // lazy descriptor initialization behavior. - bool InternalIsFileLoaded(ConstStringParam filename) const; + bool InternalIsFileLoaded(absl::string_view filename) const; // Add a file to unused_import_track_files_. DescriptorBuilder will log // warnings or errors for those files if there is any unused import. - void AddUnusedImportTrackFile(ConstStringParam file_name, + void AddUnusedImportTrackFile(absl::string_view file_name, bool is_error = false); void ClearUnusedImportTrackFiles(); @@ -2003,14 +2003,14 @@ class PROTOBUF_EXPORT DescriptorPool { // Return true if the given name is a sub-symbol of any non-package // descriptor that already exists in the descriptor pool. (The full // definition of such types is already known.) - bool IsSubSymbolOfBuiltType(StringPiece name) const; + bool IsSubSymbolOfBuiltType(absl::string_view name) const; // Tries to find something in the fallback database and link in the // corresponding proto file. Returns true if successful, in which case // the caller should search for the thing again. These are declared // const because they are called by (semantically) const methods. - bool TryFindFileInFallbackDatabase(StringPiece name) const; - bool TryFindSymbolInFallbackDatabase(StringPiece name) const; + bool TryFindFileInFallbackDatabase(absl::string_view name) const; + bool TryFindSymbolInFallbackDatabase(absl::string_view name) const; bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type, int field_number) const; @@ -2031,13 +2031,13 @@ class PROTOBUF_EXPORT DescriptorPool { // symbol is defined if necessary. Will create a placeholder if the type // doesn't exist in the fallback database, or the file doesn't build // successfully. - Symbol CrossLinkOnDemandHelper(StringPiece name, + Symbol CrossLinkOnDemandHelper(absl::string_view name, bool expecting_enum) const; // Create a placeholder FileDescriptor of the specified name - FileDescriptor* NewPlaceholderFile(StringPiece name) const; + FileDescriptor* NewPlaceholderFile(absl::string_view name) const; FileDescriptor* NewPlaceholderFileWithMutexHeld( - StringPiece name, internal::FlatAllocator& alloc) const; + absl::string_view name, internal::FlatAllocator& alloc) const; enum PlaceholderType { PLACEHOLDER_MESSAGE, @@ -2045,9 +2045,9 @@ class PROTOBUF_EXPORT DescriptorPool { PLACEHOLDER_EXTENDABLE_MESSAGE }; // Create a placeholder Descriptor of the specified name - Symbol NewPlaceholder(StringPiece name, + Symbol NewPlaceholder(absl::string_view name, PlaceholderType placeholder_type) const; - Symbol NewPlaceholderWithMutexHeld(StringPiece name, + Symbol NewPlaceholderWithMutexHeld(absl::string_view name, PlaceholderType placeholder_type) const; // If fallback_database_ is nullptr, this is nullptr. Otherwise, this is a @@ -2221,9 +2221,9 @@ inline bool Descriptor::IsReservedNumber(int number) const { return FindReservedRangeContainingNumber(number) != nullptr; } -inline bool Descriptor::IsReservedName(ConstStringParam name) const { +inline bool Descriptor::IsReservedName(absl::string_view name) const { for (int i = 0; i < reserved_name_count(); i++) { - if (name == static_cast(reserved_name(i))) { + if (name == static_cast(reserved_name(i))) { return true; } } @@ -2240,9 +2240,9 @@ inline bool EnumDescriptor::IsReservedNumber(int number) const { return FindReservedRangeContainingNumber(number) != nullptr; } -inline bool EnumDescriptor::IsReservedName(ConstStringParam name) const { +inline bool EnumDescriptor::IsReservedName(absl::string_view name) const { for (int i = 0; i < reserved_name_count(); i++) { - if (name == static_cast(reserved_name(i))) { + if (name == static_cast(reserved_name(i))) { return true; } } @@ -2287,7 +2287,7 @@ inline FieldDescriptor::Label FieldDescriptor::label() const { inline FieldDescriptor::Type FieldDescriptor::type() const { if (type_once_) { - internal::call_once(*type_once_, &FieldDescriptor::TypeOnceInit, this); + absl::call_once(*type_once_, &FieldDescriptor::TypeOnceInit, this); } return static_cast(type_); } diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc index 7898fcbddf41..3bd5fe88944a 100644 --- a/src/google/protobuf/descriptor.pb.cc +++ b/src/google/protobuf/descriptor.pb.cc @@ -1199,7 +1199,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fdescriptor_2eproto[] PR "s/descriptorpb\370\001\001\242\002\003GPB\252\002\032Google.Protobu" "f.Reflection" ; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2fdescriptor_2eproto = { false, false, 6212, descriptor_table_protodef_google_2fprotobuf_2fdescriptor_2eproto, "google/protobuf/descriptor.proto", diff --git a/src/google/protobuf/descriptor.pb.h b/src/google/protobuf/descriptor.pb.h index 5f2179cc0a10..34e09e6e6b18 100644 --- a/src/google/protobuf/descriptor.pb.h +++ b/src/google/protobuf/descriptor.pb.h @@ -198,7 +198,7 @@ inline const std::string& FieldDescriptorProto_Type_Name(FieldDescriptorProto_Ty (static_cast(value)); } inline bool FieldDescriptorProto_Type_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, FieldDescriptorProto_Type* value) { + ::absl::string_view name, FieldDescriptorProto_Type* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( FieldDescriptorProto_Type_descriptor(), name, value); } @@ -226,7 +226,7 @@ inline const std::string& FieldDescriptorProto_Label_Name(FieldDescriptorProto_L (static_cast(value)); } inline bool FieldDescriptorProto_Label_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, FieldDescriptorProto_Label* value) { + ::absl::string_view name, FieldDescriptorProto_Label* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( FieldDescriptorProto_Label_descriptor(), name, value); } @@ -254,7 +254,7 @@ inline const std::string& FileOptions_OptimizeMode_Name(FileOptions_OptimizeMode (static_cast(value)); } inline bool FileOptions_OptimizeMode_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, FileOptions_OptimizeMode* value) { + ::absl::string_view name, FileOptions_OptimizeMode* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( FileOptions_OptimizeMode_descriptor(), name, value); } @@ -282,7 +282,7 @@ inline const std::string& FieldOptions_CType_Name(FieldOptions_CType value) { (static_cast(value)); } inline bool FieldOptions_CType_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, FieldOptions_CType* value) { + ::absl::string_view name, FieldOptions_CType* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( FieldOptions_CType_descriptor(), name, value); } @@ -310,7 +310,7 @@ inline const std::string& FieldOptions_JSType_Name(FieldOptions_JSType value) { (static_cast(value)); } inline bool FieldOptions_JSType_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, FieldOptions_JSType* value) { + ::absl::string_view name, FieldOptions_JSType* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( FieldOptions_JSType_descriptor(), name, value); } @@ -338,7 +338,7 @@ inline const std::string& MethodOptions_IdempotencyLevel_Name(MethodOptions_Idem (static_cast(value)); } inline bool MethodOptions_IdempotencyLevel_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, MethodOptions_IdempotencyLevel* value) { + ::absl::string_view name, MethodOptions_IdempotencyLevel* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( MethodOptions_IdempotencyLevel_descriptor(), name, value); } @@ -366,7 +366,7 @@ inline const std::string& GeneratedCodeInfo_Annotation_Semantic_Name(GeneratedCo (static_cast(value)); } inline bool GeneratedCodeInfo_Annotation_Semantic_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, GeneratedCodeInfo_Annotation_Semantic* value) { + ::absl::string_view name, GeneratedCodeInfo_Annotation_Semantic* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( GeneratedCodeInfo_Annotation_Semantic_descriptor(), name, value); } @@ -482,7 +482,7 @@ class PROTOBUF_EXPORT FileDescriptorSet final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.FileDescriptorSet"; } protected: @@ -646,7 +646,7 @@ class PROTOBUF_EXPORT FileDescriptorProto final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.FileDescriptorProto"; } protected: @@ -1065,7 +1065,7 @@ class PROTOBUF_EXPORT DescriptorProto_ExtensionRange final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.DescriptorProto.ExtensionRange"; } protected: @@ -1260,7 +1260,7 @@ class PROTOBUF_EXPORT DescriptorProto_ReservedRange final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.DescriptorProto.ReservedRange"; } protected: @@ -1435,7 +1435,7 @@ class PROTOBUF_EXPORT DescriptorProto final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.DescriptorProto"; } protected: @@ -1789,7 +1789,7 @@ class PROTOBUF_EXPORT ExtensionRangeOptions final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.ExtensionRangeOptions"; } protected: @@ -2145,7 +2145,7 @@ class PROTOBUF_EXPORT FieldDescriptorProto final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.FieldDescriptorProto"; } protected: @@ -2217,7 +2217,7 @@ class PROTOBUF_EXPORT FieldDescriptorProto final : "Incorrect type passed to function Type_Name."); return FieldDescriptorProto_Type_Name(enum_t_value); } - static inline bool Type_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, + static inline bool Type_Parse(::absl::string_view name, Type* value) { return FieldDescriptorProto_Type_Parse(name, value); } @@ -2249,7 +2249,7 @@ class PROTOBUF_EXPORT FieldDescriptorProto final : "Incorrect type passed to function Label_Name."); return FieldDescriptorProto_Label_Name(enum_t_value); } - static inline bool Label_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, + static inline bool Label_Parse(::absl::string_view name, Label* value) { return FieldDescriptorProto_Label_Parse(name, value); } @@ -2579,7 +2579,7 @@ class PROTOBUF_EXPORT OneofDescriptorProto final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.OneofDescriptorProto"; } protected: @@ -2764,7 +2764,7 @@ class PROTOBUF_EXPORT EnumDescriptorProto_EnumReservedRange final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.EnumDescriptorProto.EnumReservedRange"; } protected: @@ -2939,7 +2939,7 @@ class PROTOBUF_EXPORT EnumDescriptorProto final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.EnumDescriptorProto"; } protected: @@ -3192,7 +3192,7 @@ class PROTOBUF_EXPORT EnumValueDescriptorProto final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.EnumValueDescriptorProto"; } protected: @@ -3392,7 +3392,7 @@ class PROTOBUF_EXPORT ServiceDescriptorProto final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.ServiceDescriptorProto"; } protected: @@ -3597,7 +3597,7 @@ class PROTOBUF_EXPORT MethodDescriptorProto final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.MethodDescriptorProto"; } protected: @@ -3852,7 +3852,7 @@ class PROTOBUF_EXPORT FileOptions final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.FileOptions"; } protected: @@ -3894,7 +3894,7 @@ class PROTOBUF_EXPORT FileOptions final : "Incorrect type passed to function OptimizeMode_Name."); return FileOptions_OptimizeMode_Name(enum_t_value); } - static inline bool OptimizeMode_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, + static inline bool OptimizeMode_Parse(::absl::string_view name, OptimizeMode* value) { return FileOptions_OptimizeMode_Parse(name, value); } @@ -4591,7 +4591,7 @@ class PROTOBUF_EXPORT MessageOptions final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.MessageOptions"; } protected: @@ -5008,7 +5008,7 @@ class PROTOBUF_EXPORT FieldOptions final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.FieldOptions"; } protected: @@ -5050,7 +5050,7 @@ class PROTOBUF_EXPORT FieldOptions final : "Incorrect type passed to function CType_Name."); return FieldOptions_CType_Name(enum_t_value); } - static inline bool CType_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, + static inline bool CType_Parse(::absl::string_view name, CType* value) { return FieldOptions_CType_Parse(name, value); } @@ -5082,7 +5082,7 @@ class PROTOBUF_EXPORT FieldOptions final : "Incorrect type passed to function JSType_Name."); return FieldOptions_JSType_Name(enum_t_value); } - static inline bool JSType_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, + static inline bool JSType_Parse(::absl::string_view name, JSType* value) { return FieldOptions_JSType_Parse(name, value); } @@ -5534,7 +5534,7 @@ class PROTOBUF_EXPORT OneofOptions final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.OneofOptions"; } protected: @@ -5890,7 +5890,7 @@ class PROTOBUF_EXPORT EnumOptions final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.EnumOptions"; } protected: @@ -6277,7 +6277,7 @@ class PROTOBUF_EXPORT EnumValueOptions final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.EnumValueOptions"; } protected: @@ -6649,7 +6649,7 @@ class PROTOBUF_EXPORT ServiceOptions final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.ServiceOptions"; } protected: @@ -7021,7 +7021,7 @@ class PROTOBUF_EXPORT MethodOptions final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.MethodOptions"; } protected: @@ -7063,7 +7063,7 @@ class PROTOBUF_EXPORT MethodOptions final : "Incorrect type passed to function IdempotencyLevel_Name."); return MethodOptions_IdempotencyLevel_Name(enum_t_value); } - static inline bool IdempotencyLevel_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, + static inline bool IdempotencyLevel_Parse(::absl::string_view name, IdempotencyLevel* value) { return MethodOptions_IdempotencyLevel_Parse(name, value); } @@ -7440,7 +7440,7 @@ class PROTOBUF_EXPORT UninterpretedOption_NamePart final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.UninterpretedOption.NamePart"; } protected: @@ -7623,7 +7623,7 @@ class PROTOBUF_EXPORT UninterpretedOption final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.UninterpretedOption"; } protected: @@ -7895,7 +7895,7 @@ class PROTOBUF_EXPORT SourceCodeInfo_Location final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.SourceCodeInfo.Location"; } protected: @@ -8156,7 +8156,7 @@ class PROTOBUF_EXPORT SourceCodeInfo final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.SourceCodeInfo"; } protected: @@ -8322,7 +8322,7 @@ class PROTOBUF_EXPORT GeneratedCodeInfo_Annotation final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.GeneratedCodeInfo.Annotation"; } protected: @@ -8364,7 +8364,7 @@ class PROTOBUF_EXPORT GeneratedCodeInfo_Annotation final : "Incorrect type passed to function Semantic_Name."); return GeneratedCodeInfo_Annotation_Semantic_Name(enum_t_value); } - static inline bool Semantic_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, + static inline bool Semantic_Parse(::absl::string_view name, Semantic* value) { return GeneratedCodeInfo_Annotation_Semantic_Parse(name, value); } @@ -8589,7 +8589,7 @@ class PROTOBUF_EXPORT GeneratedCodeInfo final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.GeneratedCodeInfo"; } protected: diff --git a/src/google/protobuf/descriptor_database.cc b/src/google/protobuf/descriptor_database.cc index 203000d67d0b..2c849359bffd 100644 --- a/src/google/protobuf/descriptor_database.cc +++ b/src/google/protobuf/descriptor_database.cc @@ -36,9 +36,11 @@ #include #include +#include +#include "absl/strings/ascii.h" +#include #include -#include #include @@ -52,7 +54,7 @@ void RecordMessageNames(const DescriptorProto& desc_proto, GOOGLE_CHECK(desc_proto.has_name()); std::string full_name = prefix.empty() ? desc_proto.name() - : StrCat(prefix, ".", desc_proto.name()); + : absl::StrCat(prefix, ".", desc_proto.name()); output->insert(full_name); for (const auto& d : desc_proto.nested_type()) { @@ -117,7 +119,7 @@ SimpleDescriptorDatabase::~SimpleDescriptorDatabase() {} template bool SimpleDescriptorDatabase::DescriptorIndex::AddFile( const FileDescriptorProto& file, Value value) { - if (!InsertIfNotPresent(&by_name_, file.name(), value)) { + if (!by_name_.insert({file.name(), value}).second) { GOOGLE_LOG(ERROR) << "File already exists in database: " << file.name(); return false; } @@ -151,7 +153,7 @@ namespace { // Returns true if and only if all characters in the name are alphanumerics, // underscores, or periods. -bool ValidateSymbolName(StringPiece name) { +bool ValidateSymbolName(absl::string_view name) { for (char c : name) { // I don't trust ctype.h due to locales. :( if (c != '.' && c != '_' && (c < '0' || c > '9') && (c < 'A' || c > 'Z') && @@ -185,7 +187,7 @@ typename Container::const_iterator FindLastLessOrEqual( // True if either the arguments are equal or super_symbol identifies a // parent symbol of sub_symbol (e.g. "foo.bar" is a parent of // "foo.bar.baz", but not a parent of "foo.barbaz"). -bool IsSubSymbol(StringPiece sub_symbol, StringPiece super_symbol) { +bool IsSubSymbol(absl::string_view sub_symbol, absl::string_view super_symbol) { return sub_symbol == super_symbol || (HasPrefixString(super_symbol, sub_symbol) && super_symbol[sub_symbol.size()] == '.'); @@ -271,10 +273,11 @@ bool SimpleDescriptorDatabase::DescriptorIndex::AddExtension( if (!field.extendee().empty() && field.extendee()[0] == '.') { // The extension is fully-qualified. We can use it as a lookup key in // the by_symbol_ table. - if (!InsertIfNotPresent( - &by_extension_, - std::make_pair(field.extendee().substr(1), field.number()), - value)) { + if (!by_extension_ + .insert( + {std::make_pair(field.extendee().substr(1), field.number()), + value}) + .second) { GOOGLE_LOG(ERROR) << "Extension conflicts with extension already in database: " "extend " << field.extendee() << " { " << field.name() << " = " @@ -292,7 +295,9 @@ bool SimpleDescriptorDatabase::DescriptorIndex::AddExtension( template Value SimpleDescriptorDatabase::DescriptorIndex::FindFile( const std::string& filename) { - return FindWithDefault(by_name_, filename, Value()); + auto it = by_name_.find(filename); + if (it == by_name_.end()) return {}; + return it->second; } template @@ -308,8 +313,9 @@ Value SimpleDescriptorDatabase::DescriptorIndex::FindSymbol( template Value SimpleDescriptorDatabase::DescriptorIndex::FindExtension( const std::string& containing_type, int field_number) { - return FindWithDefault( - by_extension_, std::make_pair(containing_type, field_number), Value()); + auto it = by_extension_.find({containing_type, field_number}); + if (it == by_extension_.end()) return {}; + return it->second; } template @@ -352,6 +358,10 @@ bool SimpleDescriptorDatabase::AddAndOwn(const FileDescriptorProto* file) { return index_.AddFile(*file, file); } +bool SimpleDescriptorDatabase::AddUnowned(const FileDescriptorProto* file) { + return index_.AddFile(*file, file); +} + bool SimpleDescriptorDatabase::FindFileByName(const std::string& filename, FileDescriptorProto* output) { return MaybeCopy(index_.FindFile(filename), output); @@ -397,24 +407,24 @@ class EncodedDescriptorDatabase::DescriptorIndex { template bool AddFile(const FileProto& file, Value value); - Value FindFile(StringPiece filename); - Value FindSymbol(StringPiece name); - Value FindSymbolOnlyFlat(StringPiece name) const; - Value FindExtension(StringPiece containing_type, int field_number); - bool FindAllExtensionNumbers(StringPiece containing_type, + Value FindFile(absl::string_view filename); + Value FindSymbol(absl::string_view name); + Value FindSymbolOnlyFlat(absl::string_view name) const; + Value FindExtension(absl::string_view containing_type, int field_number); + bool FindAllExtensionNumbers(absl::string_view containing_type, std::vector* output); void FindAllFileNames(std::vector* output) const; private: friend class EncodedDescriptorDatabase; - bool AddSymbol(StringPiece symbol); + bool AddSymbol(absl::string_view symbol); template - bool AddNestedExtensions(StringPiece filename, + bool AddNestedExtensions(absl::string_view filename, const DescProto& message_type); template - bool AddExtension(StringPiece filename, const FieldProto& field); + bool AddExtension(absl::string_view filename, const FieldProto& field); // All the maps below have two representations: // - a std::set<> where we insert initially. @@ -427,8 +437,8 @@ class EncodedDescriptorDatabase::DescriptorIndex { using String = std::string; - String EncodeString(StringPiece str) const { return String(str); } - StringPiece DecodeString(const String& str, int) const { return str; } + String EncodeString(absl::string_view str) const { return String(str); } + absl::string_view DecodeString(const String& str, int) const { return str; } struct EncodedEntry { // Do not use `Value` here to avoid the padding of that object. @@ -445,7 +455,7 @@ class EncodedDescriptorDatabase::DescriptorIndex { int data_offset; String encoded_name; - StringPiece name(const DescriptorIndex& index) const { + absl::string_view name(const DescriptorIndex& index) const { return index.DecodeString(encoded_name, data_offset); } }; @@ -455,10 +465,10 @@ class EncodedDescriptorDatabase::DescriptorIndex { bool operator()(const FileEntry& a, const FileEntry& b) const { return a.name(index) < b.name(index); } - bool operator()(const FileEntry& a, StringPiece b) const { + bool operator()(const FileEntry& a, absl::string_view b) const { return a.name(index) < b; } - bool operator()(StringPiece a, const FileEntry& b) const { + bool operator()(absl::string_view a, const FileEntry& b) const { return a < b.name(index); } }; @@ -469,17 +479,17 @@ class EncodedDescriptorDatabase::DescriptorIndex { int data_offset; String encoded_symbol; - StringPiece package(const DescriptorIndex& index) const { + absl::string_view package(const DescriptorIndex& index) const { return index.DecodeString(index.all_values_[data_offset].encoded_package, data_offset); } - StringPiece symbol(const DescriptorIndex& index) const { + absl::string_view symbol(const DescriptorIndex& index) const { return index.DecodeString(encoded_symbol, data_offset); } std::string AsString(const DescriptorIndex& index) const { auto p = package(index); - return StrCat(p, p.empty() ? "" : ".", symbol(index)); + return absl::StrCat(p, p.empty() ? "" : ".", symbol(index)); } }; @@ -489,16 +499,16 @@ class EncodedDescriptorDatabase::DescriptorIndex { std::string AsString(const SymbolEntry& entry) const { return entry.AsString(index); } - static StringPiece AsString(StringPiece str) { return str; } + static absl::string_view AsString(absl::string_view str) { return str; } - std::pair GetParts( + std::pair GetParts( const SymbolEntry& entry) const { auto package = entry.package(index); - if (package.empty()) return {entry.symbol(index), StringPiece{}}; + if (package.empty()) return {entry.symbol(index), absl::string_view{}}; return {package, entry.symbol(index)}; } - std::pair GetParts( - StringPiece str) const { + std::pair GetParts( + absl::string_view str) const { return {str, {}}; } @@ -525,7 +535,7 @@ class EncodedDescriptorDatabase::DescriptorIndex { struct ExtensionEntry { int data_offset; String encoded_extendee; - StringPiece extendee(const DescriptorIndex& index) const { + absl::string_view extendee(const DescriptorIndex& index) const { return index.DecodeString(encoded_extendee, data_offset).substr(1); } int extension_number; @@ -538,10 +548,10 @@ class EncodedDescriptorDatabase::DescriptorIndex { std::make_tuple(b.extendee(index), b.extension_number); } bool operator()(const ExtensionEntry& a, - std::tuple b) const { + std::tuple b) const { return std::make_tuple(a.extendee(index), a.extension_number) < b; } - bool operator()(std::tuple a, + bool operator()(std::tuple a, const ExtensionEntry& b) const { return a < std::make_tuple(b.extendee(index), b.extension_number); } @@ -634,9 +644,10 @@ bool EncodedDescriptorDatabase::DescriptorIndex::AddFile(const FileProto& file, } all_values_.back().encoded_package = EncodeString(file.package()); - if (!InsertIfNotPresent( - &by_name_, FileEntry{static_cast(all_values_.size() - 1), - EncodeString(file.name())}) || + if (!by_name_ + .insert({static_cast(all_values_.size() - 1), + EncodeString(file.name())}) + .second || std::binary_search(by_name_flat_.begin(), by_name_flat_.end(), file.name(), by_name_.key_comp())) { GOOGLE_LOG(ERROR) << "File already exists in database: " << file.name(); @@ -662,7 +673,7 @@ bool EncodedDescriptorDatabase::DescriptorIndex::AddFile(const FileProto& file, } template -static bool CheckForMutualSubsymbols(StringPiece symbol_name, Iter* iter, +static bool CheckForMutualSubsymbols(absl::string_view symbol_name, Iter* iter, Iter2 end, const Index& index) { if (*iter != end) { if (IsSubSymbol((*iter)->AsString(index), symbol_name)) { @@ -690,7 +701,7 @@ static bool CheckForMutualSubsymbols(StringPiece symbol_name, Iter* iter, } bool EncodedDescriptorDatabase::DescriptorIndex::AddSymbol( - StringPiece symbol) { + absl::string_view symbol) { SymbolEntry entry = {static_cast(all_values_.size() - 1), EncodeString(symbol)}; std::string entry_as_string = entry.AsString(*this); @@ -730,7 +741,7 @@ bool EncodedDescriptorDatabase::DescriptorIndex::AddSymbol( template bool EncodedDescriptorDatabase::DescriptorIndex::AddNestedExtensions( - StringPiece filename, const DescProto& message_type) { + absl::string_view filename, const DescProto& message_type) { for (const auto& nested_type : message_type.nested_type()) { if (!AddNestedExtensions(filename, nested_type)) return false; } @@ -742,14 +753,14 @@ bool EncodedDescriptorDatabase::DescriptorIndex::AddNestedExtensions( template bool EncodedDescriptorDatabase::DescriptorIndex::AddExtension( - StringPiece filename, const FieldProto& field) { + absl::string_view filename, const FieldProto& field) { if (!field.extendee().empty() && field.extendee()[0] == '.') { // The extension is fully-qualified. We can use it as a lookup key in // the by_symbol_ table. - if (!InsertIfNotPresent( - &by_extension_, - ExtensionEntry{static_cast(all_values_.size() - 1), - EncodeString(field.extendee()), field.number()}) || + if (!by_extension_ + .insert({static_cast(all_values_.size() - 1), + EncodeString(field.extendee()), field.number()}) + .second || std::binary_search( by_extension_flat_.begin(), by_extension_flat_.end(), std::make_pair(field.extendee().substr(1), field.number()), @@ -769,14 +780,14 @@ bool EncodedDescriptorDatabase::DescriptorIndex::AddExtension( } std::pair -EncodedDescriptorDatabase::DescriptorIndex::FindSymbol(StringPiece name) { +EncodedDescriptorDatabase::DescriptorIndex::FindSymbol(absl::string_view name) { EnsureFlat(); return FindSymbolOnlyFlat(name); } std::pair EncodedDescriptorDatabase::DescriptorIndex::FindSymbolOnlyFlat( - StringPiece name) const { + absl::string_view name) const { auto iter = FindLastLessOrEqual(&by_symbol_flat_, name, by_symbol_.key_comp()); @@ -788,7 +799,7 @@ EncodedDescriptorDatabase::DescriptorIndex::FindSymbolOnlyFlat( std::pair EncodedDescriptorDatabase::DescriptorIndex::FindExtension( - StringPiece containing_type, int field_number) { + absl::string_view containing_type, int field_number) { EnsureFlat(); auto it = std::lower_bound( @@ -820,7 +831,7 @@ void EncodedDescriptorDatabase::DescriptorIndex::EnsureFlat() { } bool EncodedDescriptorDatabase::DescriptorIndex::FindAllExtensionNumbers( - StringPiece containing_type, std::vector* output) { + absl::string_view containing_type, std::vector* output) { EnsureFlat(); bool success = false; @@ -853,7 +864,7 @@ void EncodedDescriptorDatabase::DescriptorIndex::FindAllFileNames( std::pair EncodedDescriptorDatabase::DescriptorIndex::FindFile( - StringPiece filename) { + absl::string_view filename) { EnsureFlat(); auto it = std::lower_bound(by_name_flat_.begin(), by_name_flat_.end(), diff --git a/src/google/protobuf/descriptor_database.h b/src/google/protobuf/descriptor_database.h index f4f06bbfa67d..72084d9ec7d4 100644 --- a/src/google/protobuf/descriptor_database.h +++ b/src/google/protobuf/descriptor_database.h @@ -176,6 +176,10 @@ class PROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase { // Adds the FileDescriptorProto to the database and takes ownership of it. bool AddAndOwn(const FileDescriptorProto* file); + // Adds the FileDescriptorProto to the database and not take ownership of it. + // The owner must ensure file outlives the SimpleDescriptorDatabase. + bool AddUnowned(const FileDescriptorProto* file); + // implements DescriptorDatabase ----------------------------------- bool FindFileByName(const std::string& filename, FileDescriptorProto* output) override; diff --git a/src/google/protobuf/descriptor_database_unittest.cc b/src/google/protobuf/descriptor_database_unittest.cc index f58244808a1f..5948d1b86244 100644 --- a/src/google/protobuf/descriptor_database_unittest.cc +++ b/src/google/protobuf/descriptor_database_unittest.cc @@ -572,6 +572,29 @@ TEST(SimpleDescriptorDatabaseExtraTest, FindAllMessageNames) { EXPECT_THAT(messages, ::testing::UnorderedElementsAre("foo.Foo", "Bar")); } +TEST(SimpleDescriptorDatabaseExtraTest, AddUnowned) { + FileDescriptorProto f; + f.set_name("foo.proto"); + f.set_package("foo"); + f.add_message_type()->set_name("Foo"); + + FileDescriptorProto b; + b.set_name("bar.proto"); + b.set_package(""); + b.add_message_type()->set_name("Bar"); + + SimpleDescriptorDatabase db; + db.AddUnowned(&f); + db.AddUnowned(&b); + + std::vector packages; + EXPECT_TRUE(db.FindAllPackageNames(&packages)); + EXPECT_THAT(packages, ::testing::UnorderedElementsAre("foo", "")); + std::vector messages; + EXPECT_TRUE(db.FindAllMessageNames(&messages)); + EXPECT_THAT(messages, ::testing::UnorderedElementsAre("foo.Foo", "Bar")); +} + // =================================================================== class MergedDescriptorDatabaseTest : public testing::Test { diff --git a/src/google/protobuf/duration.pb.cc b/src/google/protobuf/duration.pb.cc index 94671ee6c900..5ca31452449b 100644 --- a/src/google/protobuf/duration.pb.cc +++ b/src/google/protobuf/duration.pb.cc @@ -68,7 +68,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fduration_2eproto[] PROT "uf/types/known/durationpb\370\001\001\242\002\003GPB\252\002\036Goo" "gle.Protobuf.WellKnownTypesb\006proto3" ; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2fduration_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2fduration_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2fduration_2eproto = { false, false, 235, descriptor_table_protodef_google_2fprotobuf_2fduration_2eproto, "google/protobuf/duration.proto", diff --git a/src/google/protobuf/duration.pb.h b/src/google/protobuf/duration.pb.h index 83e780e2b3b9..e33249099b34 100644 --- a/src/google/protobuf/duration.pb.h +++ b/src/google/protobuf/duration.pb.h @@ -159,7 +159,7 @@ class PROTOBUF_EXPORT Duration final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Duration"; } protected: diff --git a/src/google/protobuf/dynamic_message.cc b/src/google/protobuf/dynamic_message.cc index f839a491bab9..510d5a1aa5e8 100644 --- a/src/google/protobuf/dynamic_message.cc +++ b/src/google/protobuf/dynamic_message.cc @@ -75,7 +75,6 @@ #include #include #include -#include #include #include #include @@ -85,6 +84,7 @@ #include #include + // Must be included last. #include @@ -297,7 +297,21 @@ struct DynamicMessageFactory::TypeInfo { TypeInfo() : prototype(nullptr) {} - ~TypeInfo() { delete prototype; } + ~TypeInfo() { + delete prototype; + + // Scribble the payload to prevent unsanitized opt builds from silently + // allowing use-after-free bugs where the factory is destroyed but the + // DynamicMessage instances are still used. + // This is a common bug with DynamicMessageFactory. + // NOTE: This must happen after deleting the prototype. + if (offsets != nullptr) { + std::fill_n(offsets.get(), type->field_count(), 0xCDCDCDCDu); + } + if (has_bits_indices != nullptr) { + std::fill_n(has_bits_indices.get(), type->field_count(), 0xCDCDCDCDu); + } + } }; DynamicMessage::DynamicMessage(const DynamicMessageFactory::TypeInfo* type_info) diff --git a/src/google/protobuf/empty.pb.cc b/src/google/protobuf/empty.pb.cc index 8af459dbf840..f562c5939755 100644 --- a/src/google/protobuf/empty.pb.cc +++ b/src/google/protobuf/empty.pb.cc @@ -62,7 +62,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fempty_2eproto[] PROTOBU "/types/known/emptypb\370\001\001\242\002\003GPB\252\002\036Google.P" "rotobuf.WellKnownTypesb\006proto3" ; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2fempty_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2fempty_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2fempty_2eproto = { false, false, 190, descriptor_table_protodef_google_2fprotobuf_2fempty_2eproto, "google/protobuf/empty.proto", diff --git a/src/google/protobuf/empty.pb.h b/src/google/protobuf/empty.pb.h index ca821f91a88d..8e858d05129b 100644 --- a/src/google/protobuf/empty.pb.h +++ b/src/google/protobuf/empty.pb.h @@ -145,7 +145,7 @@ class PROTOBUF_EXPORT Empty final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Empty"; } protected: diff --git a/src/google/protobuf/extension_set.cc b/src/google/protobuf/extension_set.cc index fada4f5a9870..c0dc193eaf0b 100644 --- a/src/google/protobuf/extension_set.cc +++ b/src/google/protobuf/extension_set.cc @@ -35,29 +35,27 @@ #include #include -#include #include #include #include #include #include +#include "absl/container/flat_hash_set.h" +#include "absl/hash/hash.h" #include #include #include #include #include #include -#include -#include -// clang-format off -#include // must be last. -// clang-format on +// must be last. +#include + namespace google { namespace protobuf { namespace internal { - namespace { inline WireFormatLite::FieldType real_type(FieldType type) { @@ -71,8 +69,6 @@ inline WireFormatLite::CppType cpp_type(FieldType type) { // Registry stuff. -// Note that we cannot use hetererogeneous lookup for std containers since we -// need to support C++11. struct ExtensionEq { bool operator()(const ExtensionInfo& lhs, const ExtensionInfo& rhs) const { return lhs.message == rhs.message && lhs.number == rhs.number; @@ -81,13 +77,12 @@ struct ExtensionEq { struct ExtensionHasher { std::size_t operator()(const ExtensionInfo& info) const { - return std::hash{}(info.message) ^ - std::hash{}(info.number); + return absl::HashOf(info.message, info.number); } }; using ExtensionRegistry = - std::unordered_set; + absl::flat_hash_set; static const ExtensionRegistry* global_registry = nullptr; @@ -96,7 +91,7 @@ static const ExtensionRegistry* global_registry = nullptr; void Register(const ExtensionInfo& info) { static auto local_static_registry = OnShutdownDelete(new ExtensionRegistry); global_registry = local_static_registry; - if (!InsertIfNotPresent(local_static_registry, info)) { + if (!local_static_registry->insert(info).second) { GOOGLE_LOG(FATAL) << "Multiple extension registrations for type \"" << info.message->GetTypeName() << "\", field number " << info.number << "."; diff --git a/src/google/protobuf/extension_set_heavy.cc b/src/google/protobuf/extension_set_heavy.cc index a4bb94860cc9..086ab89f55cb 100644 --- a/src/google/protobuf/extension_set_heavy.cc +++ b/src/google/protobuf/extension_set_heavy.cc @@ -35,9 +35,9 @@ // Contains methods defined in extension_set.h which cannot be part of the // lite library because they use descriptors or reflection. -#include #include #include +#include "absl/base/casts.h" #include #include #include @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -413,7 +414,7 @@ size_t ExtensionSet::Extension::SpaceUsedExcludingSelfLong() const { if (is_lazy) { total_size += lazymessage_value->SpaceUsedLong(); } else { - total_size += down_cast(message_value)->SpaceUsedLong(); + total_size += DownCast(message_value)->SpaceUsedLong(); } break; default: diff --git a/src/google/protobuf/extension_set_unittest.cc b/src/google/protobuf/extension_set_unittest.cc index 207e10cfcfa4..065ab4dfdb20 100644 --- a/src/google/protobuf/extension_set_unittest.cc +++ b/src/google/protobuf/extension_set_unittest.cc @@ -32,8 +32,8 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include -#include +#include + #include #include #include @@ -44,15 +44,17 @@ #include #include #include -#include #include #include #include #include +#include "absl/base/casts.h" #include #include #include +#include + // Must be included last. #include @@ -62,6 +64,7 @@ namespace protobuf { namespace internal { namespace { +using ::google::protobuf::internal::DownCast; using TestUtil::EqualsToSerialized; // This test closely mirrors third_party/protobuf/compiler/cpp/unittest.cc diff --git a/src/google/protobuf/field_access_listener.h b/src/google/protobuf/field_access_listener.h index 47422e63c1c2..00436c33cd60 100644 --- a/src/google/protobuf/field_access_listener.h +++ b/src/google/protobuf/field_access_listener.h @@ -57,7 +57,7 @@ struct NoOpAccessListener { // to differentiate the protos during the runtime before the start of the // program, use this functor to get its name. We either way need it for // LITE_RUNTIME protos as they don't have descriptors at all. - explicit NoOpAccessListener(StringPiece (*name_extractor)()) {} + explicit NoOpAccessListener(absl::string_view (*name_extractor)()) {} // called repeatedly during serialization/deserialization/ByteSize of // Reflection as: // AccessListener::OnSerialize(this); diff --git a/src/google/protobuf/field_mask.pb.cc b/src/google/protobuf/field_mask.pb.cc index 8448013e6ff3..5f710927eacc 100644 --- a/src/google/protobuf/field_mask.pb.cc +++ b/src/google/protobuf/field_mask.pb.cc @@ -66,7 +66,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2ffield_5fmask_2eproto[] "n/fieldmaskpb\370\001\001\242\002\003GPB\252\002\036Google.Protobuf" ".WellKnownTypesb\006proto3" ; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto = { false, false, 223, descriptor_table_protodef_google_2fprotobuf_2ffield_5fmask_2eproto, "google/protobuf/field_mask.proto", diff --git a/src/google/protobuf/field_mask.pb.h b/src/google/protobuf/field_mask.pb.h index ff9424f0f49b..46723f9921c2 100644 --- a/src/google/protobuf/field_mask.pb.h +++ b/src/google/protobuf/field_mask.pb.h @@ -159,7 +159,7 @@ class PROTOBUF_EXPORT FieldMask final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.FieldMask"; } protected: diff --git a/src/google/protobuf/generated_enum_reflection.h b/src/google/protobuf/generated_enum_reflection.h index b96a9c61f98e..440a6fc2bb7e 100644 --- a/src/google/protobuf/generated_enum_reflection.h +++ b/src/google/protobuf/generated_enum_reflection.h @@ -43,7 +43,7 @@ #include #include -#include +#include "absl/strings/string_view.h" #include #ifdef SWIG @@ -74,10 +74,10 @@ namespace internal { // an enum name of the given type, returning true and filling in value on // success, or returning false and leaving value unchanged on failure. PROTOBUF_EXPORT bool ParseNamedEnum(const EnumDescriptor* descriptor, - ConstStringParam name, int* value); + absl::string_view name, int* value); template -bool ParseNamedEnum(const EnumDescriptor* descriptor, ConstStringParam name, +bool ParseNamedEnum(const EnumDescriptor* descriptor, absl::string_view name, EnumType* value) { int tmp; if (!ParseNamedEnum(descriptor, name, &tmp)) return false; diff --git a/src/google/protobuf/generated_enum_util.cc b/src/google/protobuf/generated_enum_util.cc index df7583e99984..f73e82c50a57 100644 --- a/src/google/protobuf/generated_enum_util.cc +++ b/src/google/protobuf/generated_enum_util.cc @@ -40,7 +40,7 @@ namespace internal { namespace { bool EnumCompareByName(const EnumEntry& a, const EnumEntry& b) { - return StringPiece(a.name) < StringPiece(b.name); + return absl::string_view(a.name) < absl::string_view(b.name); } // Gets the numeric value of the EnumEntry at the given index, but returns a @@ -57,7 +57,7 @@ int GetValue(const EnumEntry* enums, int i, int target) { } // namespace bool LookUpEnumValue(const EnumEntry* enums, size_t size, - StringPiece name, int* value) { + absl::string_view name, int* value) { EnumEntry target{name, 0}; auto it = std::lower_bound(enums, enums + size, target, EnumCompareByName); if (it != enums + size && it->name == name) { diff --git a/src/google/protobuf/generated_enum_util.h b/src/google/protobuf/generated_enum_util.h index 5d10ac010e76..de0bd8f216de 100644 --- a/src/google/protobuf/generated_enum_util.h +++ b/src/google/protobuf/generated_enum_util.h @@ -34,7 +34,7 @@ #include -#include +#include "absl/strings/string_view.h" #include // Must be included last. @@ -58,13 +58,13 @@ namespace internal { // protos. This struct and the following related functions should only be used // by protobuf generated code. struct EnumEntry { - StringPiece name; + absl::string_view name; int value; }; // Looks up a numeric enum value given the string name. PROTOBUF_EXPORT bool LookUpEnumValue(const EnumEntry* enums, size_t size, - StringPiece name, int* value); + absl::string_view name, int* value); // Looks up an enum name given the numeric value. PROTOBUF_EXPORT int LookUpEnumName(const EnumEntry* enums, diff --git a/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc index aec6e1a00c68..441a6e2ce93a 100644 --- a/src/google/protobuf/generated_message_reflection.cc +++ b/src/google/protobuf/generated_message_reflection.cc @@ -44,8 +44,8 @@ #include #include -#include -#include +#include "absl/base/casts.h" +#include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include #include @@ -101,7 +101,7 @@ Message* MaybeForceCopy(Arena* arena, Message* msg) { namespace internal { -bool ParseNamedEnum(const EnumDescriptor* descriptor, ConstStringParam name, +bool ParseNamedEnum(const EnumDescriptor* descriptor, absl::string_view name, int* value) { const EnumValueDescriptor* d = descriptor->FindValueByName(name); if (d == nullptr) return false; @@ -3536,9 +3536,8 @@ void RegisterAllTypesInternal(const Metadata* file_level_metadata, int size) { namespace internal { Metadata AssignDescriptors(const DescriptorTable* (*table)(), - internal::once_flag* once, - const Metadata& metadata) { - call_once(*once, [=] { + absl::once_flag* once, const Metadata& metadata) { + absl::call_once(*once, [=] { auto* t = table(); AssignDescriptorsImpl(t, t->is_eager); }); @@ -3548,7 +3547,7 @@ Metadata AssignDescriptors(const DescriptorTable* (*table)(), void AssignDescriptors(const DescriptorTable* table, bool eager) { if (!eager) eager = table->is_eager; - call_once(*table->once, AssignDescriptorsImpl, table, eager); + absl::call_once(*table->once, AssignDescriptorsImpl, table, eager); } AddDescriptorsRunner::AddDescriptorsRunner(const DescriptorTable* table) { diff --git a/src/google/protobuf/generated_message_reflection.h b/src/google/protobuf/generated_message_reflection.h index 007c46a38534..a67413562710 100644 --- a/src/google/protobuf/generated_message_reflection.h +++ b/src/google/protobuf/generated_message_reflection.h @@ -40,15 +40,15 @@ #include -#include #include -#include -#include +#include "absl/base/call_once.h" +#include "absl/base/casts.h" +#include "absl/strings/string_view.h" #include #include +#include #include - // Must be included last. #include @@ -331,7 +331,7 @@ struct PROTOBUF_EXPORT DescriptorTable { int size; // of serialized descriptor const char* descriptor; const char* filename; - once_flag* once; + absl::once_flag* once; const DescriptorTable* const* deps; int num_deps; int num_messages; @@ -357,7 +357,7 @@ void PROTOBUF_EXPORT AssignDescriptors(const DescriptorTable* table, // It takes a `Metadata` and returns it to allow for tail calls and reduce // binary size. Metadata PROTOBUF_EXPORT AssignDescriptors(const DescriptorTable* (*table)(), - internal::once_flag* once, + absl::once_flag* once, const Metadata& metadata); // These cannot be in lite so we put them in the reflection. diff --git a/src/google/protobuf/generated_message_tctable_full.cc b/src/google/protobuf/generated_message_tctable_full.cc index 1f887e9da670..780a13afcb22 100644 --- a/src/google/protobuf/generated_message_tctable_full.cc +++ b/src/google/protobuf/generated_message_tctable_full.cc @@ -34,16 +34,17 @@ #include #include #include +#include #include #include -// clang-format off +// must be last #include -// clang-format on namespace google { namespace protobuf { namespace internal { +using ::google::protobuf::internal::DownCast; const char* TcParser::GenericFallback(PROTOBUF_TC_PARAM_DECL) { return GenericFallbackImpl(PROTOBUF_TC_PARAM_PASS); @@ -57,7 +58,7 @@ const char* TcParser::ReflectionFallback(PROTOBUF_TC_PARAM_DECL) { return ptr; } - auto* full_msg = down_cast(msg); + auto* full_msg = DownCast(msg); auto* descriptor = full_msg->GetDescriptor(); auto* reflection = full_msg->GetReflection(); int field_number = WireFormatLite::GetTagFieldNumber(tag); @@ -81,7 +82,7 @@ const char* TcParser::ReflectionParseLoop(PROTOBUF_TC_PARAM_DECL) { (void)table; (void)hasbits; // Call into the wire format reflective parse loop. - return WireFormat::_InternalParse(down_cast(msg), ptr, ctx); + return WireFormat::_InternalParse(DownCast(msg), ptr, ctx); } } // namespace internal diff --git a/src/google/protobuf/generated_message_tctable_gen.cc b/src/google/protobuf/generated_message_tctable_gen.cc index 31c161e5fff9..ed28bebc3c22 100644 --- a/src/google/protobuf/generated_message_tctable_gen.cc +++ b/src/google/protobuf/generated_message_tctable_gen.cc @@ -394,7 +394,7 @@ std::vector GenerateFieldNames( std::string message_name = descriptor->full_name(); if (message_name.size() > kMaxNameLength) { static constexpr int kNameHalfLength = (kMaxNameLength - 3) / 2; - message_name = StrCat( + message_name = absl::StrCat( message_name.substr(0, kNameHalfLength), "...", message_name.substr(message_name.size() - kNameHalfLength)); } diff --git a/src/google/protobuf/generated_message_tctable_impl.h b/src/google/protobuf/generated_message_tctable_impl.h index 965f1ed0c36a..64ee8d714461 100644 --- a/src/google/protobuf/generated_message_tctable_impl.h +++ b/src/google/protobuf/generated_message_tctable_impl.h @@ -575,8 +575,8 @@ class PROTOBUF_EXPORT TcParser final { // Mini field lookup: static const TcParseTableBase::FieldEntry* FindFieldEntry( const TcParseTableBase* table, uint32_t field_num); - static StringPiece MessageName(const TcParseTableBase* table); - static StringPiece FieldName(const TcParseTableBase* table, + static absl::string_view MessageName(const TcParseTableBase* table); + static absl::string_view FieldName(const TcParseTableBase* table, const TcParseTableBase::FieldEntry*); static bool ChangeOneof(const TcParseTableBase* table, const TcParseTableBase::FieldEntry& entry, @@ -586,7 +586,7 @@ class PROTOBUF_EXPORT TcParser final { // UTF-8 validation: static void ReportFastUtf8Error(uint32_t decoded_tag, const TcParseTableBase* table); - static bool MpVerifyUtf8(StringPiece wire_bytes, + static bool MpVerifyUtf8(absl::string_view wire_bytes, const TcParseTableBase* table, const TcParseTableBase::FieldEntry& entry, uint16_t xform_val); diff --git a/src/google/protobuf/generated_message_tctable_lite.cc b/src/google/protobuf/generated_message_tctable_lite.cc index f9fb6cc4b915..c7b20b742301 100644 --- a/src/google/protobuf/generated_message_tctable_lite.cc +++ b/src/google/protobuf/generated_message_tctable_lite.cc @@ -225,7 +225,7 @@ const TcParseTableBase::FieldEntry* TcParser::FindFieldEntry( // This is designed to be compact but not particularly fast to retrieve. // In particular, it takes O(n) to retrieve the name of the n'th field, // which is usually fine because most protos have fewer than 10 fields. -static StringPiece FindName(const char* name_data, size_t entries, +static absl::string_view FindName(const char* name_data, size_t entries, size_t index) { // The compiler unrolls these... if this isn't fast enough, // there's an AVX version at https://godbolt.org/z/eojrjqzfr @@ -241,11 +241,11 @@ static StringPiece FindName(const char* name_data, size_t entries, return {start, size}; } -StringPiece TcParser::MessageName(const TcParseTableBase* table) { +absl::string_view TcParser::MessageName(const TcParseTableBase* table) { return FindName(table->name_data(), table->num_field_entries + 1, 0); } -StringPiece TcParser::FieldName(const TcParseTableBase* table, +absl::string_view TcParser::FieldName(const TcParseTableBase* table, const FieldEntry* field_entry) { const FieldEntry* const field_entries = table->field_entries_begin(); auto field_index = static_cast(field_entry - field_entries); @@ -1243,8 +1243,8 @@ const char* TcParser::FastEr1R2(PROTOBUF_TC_PARAM_DECL) { ////////////////////////////////////////////////////////////////////////////// // Defined in wire_format_lite.cc -void PrintUTF8ErrorLog(StringPiece message_name, - StringPiece field_name, const char* operation_str, +void PrintUTF8ErrorLog(absl::string_view message_name, + absl::string_view field_name, const char* operation_str, bool emit_stacktrace); void TcParser::ReportFastUtf8Error(uint32_t decoded_tag, @@ -1807,7 +1807,7 @@ const char* TcParser::MpPackedVarint(PROTOBUF_TC_PARAM_DECL) { return Error(PROTOBUF_TC_PARAM_PASS); } -bool TcParser::MpVerifyUtf8(StringPiece wire_bytes, +bool TcParser::MpVerifyUtf8(absl::string_view wire_bytes, const TcParseTableBase* table, const FieldEntry& entry, uint16_t xform_val) { if (xform_val == field_layout::kTvUtf8) { diff --git a/src/google/protobuf/generated_message_tctable_lite_test.cc b/src/google/protobuf/generated_message_tctable_lite_test.cc index 4310803a63d6..7cba5daec1bb 100644 --- a/src/google/protobuf/generated_message_tctable_lite_test.cc +++ b/src/google/protobuf/generated_message_tctable_lite_test.cc @@ -45,7 +45,7 @@ using ::testing::Eq; using ::testing::Not; MATCHER_P3(IsEntryForFieldNum, table, field_num, field_numbers_table, - StrCat(negation ? "isn't " : "", + absl::StrCat(negation ? "isn't " : "", "the field entry for field number ", field_num)) { if (arg == nullptr) { *result_listener << "which is nullptr"; @@ -108,7 +108,7 @@ class FindFieldEntryTest : public ::testing::Test { // Calls the private `FieldName` function. template - static StringPiece FieldName( + static absl::string_view FieldName( const TcParseTable& table, const TcParseTableBase::FieldEntry* entry) { @@ -118,7 +118,7 @@ class FindFieldEntryTest : public ::testing::Test { // Calls the private `MessageName` function. template - static StringPiece MessageName( + static absl::string_view MessageName( const TcParseTable& table) { return TcParser::MessageName(&table.header); @@ -300,10 +300,10 @@ TEST_F(FindFieldEntryTest, OutOfRange) { EXPECT_THAT(entry, IsEntryForFieldNum(&table, field_num, table_field_numbers)); - StringPiece name = FieldName(table, entry); + absl::string_view name = FieldName(table, entry); EXPECT_EQ(name.length(), field_num); while (name[0] == '0') name.remove_prefix(1); // strip leading zeores - EXPECT_EQ(name, StrCat(field_num)); + EXPECT_EQ(name, absl::StrCat(field_num)); } for (int field_num : {0, 4, 112, 500000000}) { EXPECT_THAT(FindFieldEntry(table, field_num), Eq(nullptr)); @@ -557,7 +557,7 @@ TEST_F(FindFieldEntryTest, BigMessage) { for (int field_num : {1, 12, 31, 42, 57, 68, 79, 90, 101, 119, 249, 402, 412}) { auto* entry = FindFieldEntry(test_all_types_table, field_num); - StringPiece name = FieldName(test_all_types_table, entry); + absl::string_view name = FieldName(test_all_types_table, entry); switch (field_num) { case 1: EXPECT_THAT(name, Eq("optional_int32")); diff --git a/src/google/protobuf/generated_message_util.h b/src/google/protobuf/generated_message_util.h index 149986d92a6a..3a1a6732cda3 100644 --- a/src/google/protobuf/generated_message_util.h +++ b/src/google/protobuf/generated_message_util.h @@ -43,19 +43,21 @@ #include #include #include +#include #include #include -#include // Add direct dep on port for pb.cc -#include -#include +#include "absl/base/call_once.h" +#include "absl/base/casts.h" +#include "absl/strings/string_view.h" #include #include #include #include +#include #include #include -#include + // Must be included last. #include @@ -76,15 +78,6 @@ class CodedInputStream; namespace internal { -template -inline To DownCast(From* f) { - return PROTOBUF_NAMESPACE_ID::internal::down_cast(f); -} -template -inline To DownCast(From& f) { - return PROTOBUF_NAMESPACE_ID::internal::down_cast(f); -} - // This fastpath inlines a single branch instead of having to make the // InitProtobufDefaults function call. diff --git a/src/google/protobuf/inlined_string_field.h b/src/google/protobuf/inlined_string_field.h index 71bd6a7eb423..1c391b39a695 100644 --- a/src/google/protobuf/inlined_string_field.h +++ b/src/google/protobuf/inlined_string_field.h @@ -37,7 +37,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" #include #include @@ -130,7 +130,7 @@ class PROTOBUF_EXPORT InlinedStringField { // `donated == ((donating_states & ~mask) != 0)` // // This method never changes the `donating_states`. - void Set(ConstStringParam value, Arena* arena, bool donated, + void Set(absl::string_view value, Arena* arena, bool donated, uint32_t* donating_states, uint32_t mask, MessageLite* msg); // Rvalue Set. If this field is donated, this method will undonate this field @@ -149,7 +149,7 @@ class PROTOBUF_EXPORT InlinedStringField { ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states, uint32_t mask, MessageLite* msg); - void SetBytes(ConstStringParam value, Arena* arena, bool donated, + void SetBytes(absl::string_view value, Arena* arena, bool donated, uint32_t* donating_states, uint32_t mask, MessageLite* msg); void SetBytes(std::string&& value, Arena* arena, bool donated, @@ -167,7 +167,7 @@ class PROTOBUF_EXPORT InlinedStringField { ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states, uint32_t mask, MessageLite* msg); - PROTOBUF_NDEBUG_INLINE void SetNoArena(StringPiece value); + PROTOBUF_NDEBUG_INLINE void SetNoArena(absl::string_view value); PROTOBUF_NDEBUG_INLINE void SetNoArena(std::string&& value); // Basic accessors. @@ -228,7 +228,7 @@ class PROTOBUF_EXPORT InlinedStringField { return Release(); } - void Set(const std::string*, ConstStringParam value, Arena* arena, + void Set(const std::string*, absl::string_view value, Arena* arena, bool donated, uint32_t* donating_states, uint32_t mask, MessageLite* msg) { Set(value, arena, donated, donating_states, mask, msg); @@ -261,7 +261,7 @@ class PROTOBUF_EXPORT InlinedStringField { Set(const_string_ref, arena, donated, donating_states, mask, msg); } - void SetBytes(const std::string*, ConstStringParam value, Arena* arena, + void SetBytes(const std::string*, absl::string_view value, Arena* arena, bool donated, uint32_t* donating_states, uint32_t mask, MessageLite* msg) { Set(value, arena, donated, donating_states, mask, msg); @@ -297,7 +297,7 @@ class PROTOBUF_EXPORT InlinedStringField { msg); } - void SetNoArena(const std::string*, StringPiece value) { + void SetNoArena(const std::string*, absl::string_view value) { SetNoArena(value); } void SetNoArena(const std::string*, std::string&& value) { @@ -413,7 +413,7 @@ inline void InlinedStringField::DestroyNoArena(const std::string*) { this->~InlinedStringField(); } -inline void InlinedStringField::SetNoArena(StringPiece value) { +inline void InlinedStringField::SetNoArena(absl::string_view value) { get_mutable()->assign(value.data(), value.length()); } @@ -445,7 +445,7 @@ inline PROTOBUF_NDEBUG_INLINE void InlinedStringField::InternalSwap( #endif } -inline void InlinedStringField::Set(ConstStringParam value, Arena* arena, +inline void InlinedStringField::Set(absl::string_view value, Arena* arena, bool donated, uint32_t* /*donating_states*/, uint32_t /*mask*/, MessageLite* /*msg*/) { (void)arena; @@ -456,17 +456,17 @@ inline void InlinedStringField::Set(ConstStringParam value, Arena* arena, inline void InlinedStringField::Set(const char* str, ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states, uint32_t mask, MessageLite* msg) { - Set(ConstStringParam(str), arena, donated, donating_states, mask, msg); + Set(absl::string_view(str), arena, donated, donating_states, mask, msg); } inline void InlinedStringField::Set(const char* str, size_t size, ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states, uint32_t mask, MessageLite* msg) { - Set(ConstStringParam{str, size}, arena, donated, donating_states, mask, msg); + Set(absl::string_view{str, size}, arena, donated, donating_states, mask, msg); } -inline void InlinedStringField::SetBytes(ConstStringParam value, Arena* arena, +inline void InlinedStringField::SetBytes(absl::string_view value, Arena* arena, bool donated, uint32_t* donating_states, uint32_t mask, MessageLite* msg) { diff --git a/src/google/protobuf/inlined_string_field_unittest.cc b/src/google/protobuf/inlined_string_field_unittest.cc index db9577e45b83..ed5f81a25789 100644 --- a/src/google/protobuf/inlined_string_field_unittest.cc +++ b/src/google/protobuf/inlined_string_field_unittest.cc @@ -44,7 +44,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" #include diff --git a/src/google/protobuf/io/coded_stream.h b/src/google/protobuf/io/coded_stream.h index e428f55b4b08..cc86652b1e04 100644 --- a/src/google/protobuf/io/coded_stream.h +++ b/src/google/protobuf/io/coded_stream.h @@ -129,7 +129,7 @@ #include #include -#include +#include "absl/strings/string_view.h" #include #include diff --git a/src/google/protobuf/io/coded_stream_unittest.cc b/src/google/protobuf/io/coded_stream_unittest.cc index b32bf45a9708..1ea43d982e20 100644 --- a/src/google/protobuf/io/coded_stream_unittest.cc +++ b/src/google/protobuf/io/coded_stream_unittest.cc @@ -45,16 +45,18 @@ #include #include +#include +#include "absl/base/casts.h" #include +#include "absl/strings/string_view.h" #include + #include -#include -#include + // Must be included last. #include - namespace google { namespace protobuf { namespace io { diff --git a/src/google/protobuf/io/tokenizer.cc b/src/google/protobuf/io/tokenizer.cc index 9614d015ce6f..f870a752a0a7 100644 --- a/src/google/protobuf/io/tokenizer.cc +++ b/src/google/protobuf/io/tokenizer.cc @@ -92,7 +92,7 @@ #include #include -#include +#include "absl/strings/escaping.h" #include #include #include @@ -1024,7 +1024,7 @@ double Tokenizer::ParseFloat(const std::string& text) { static_cast(end - start) != text.size() || *start == '-') << " Tokenizer::ParseFloat() passed text that could not have been" " tokenized as a float: " - << CEscape(text); + << absl::CEscape(text); return result; } @@ -1145,7 +1145,7 @@ void Tokenizer::ParseStringAppend(const std::string& text, if (text_size == 0) { GOOGLE_LOG(DFATAL) << " Tokenizer::ParseStringAppend() passed text that could not" " have been tokenized as a string: " - << CEscape(text); + << absl::CEscape(text); return; } diff --git a/src/google/protobuf/io/tokenizer_unittest.cc b/src/google/protobuf/io/tokenizer_unittest.cc index 6233d6ae8bfc..71ec3d458539 100644 --- a/src/google/protobuf/io/tokenizer_unittest.cc +++ b/src/google/protobuf/io/tokenizer_unittest.cc @@ -41,7 +41,7 @@ #include #include -#include +#include "absl/strings/escaping.h" #include #include #include @@ -201,7 +201,7 @@ struct SimpleTokenCase { inline std::ostream& operator<<(std::ostream& out, const SimpleTokenCase& test_case) { - return out << CEscape(test_case.input); + return out << absl::CEscape(test_case.input); } SimpleTokenCase kSimpleTokenCases[] = { @@ -370,7 +370,7 @@ struct MultiTokenCase { inline std::ostream& operator<<(std::ostream& out, const MultiTokenCase& test_case) { - return out << CEscape(test_case.input); + return out << absl::CEscape(test_case.input); } MultiTokenCase kMultiTokenCases[] = { @@ -641,7 +641,7 @@ struct DocCommentCase { inline std::ostream& operator<<(std::ostream& out, const DocCommentCase& test_case) { - return out << CEscape(test_case.input); + return out << absl::CEscape(test_case.input); } DocCommentCase kDocCommentCases[] = { @@ -1058,7 +1058,7 @@ struct ErrorCase { }; inline std::ostream& operator<<(std::ostream& out, const ErrorCase& test_case) { - return out << CEscape(test_case.input); + return out << absl::CEscape(test_case.input); } ErrorCase kErrorCases[] = { diff --git a/src/google/protobuf/io/zero_copy_sink_test.cc b/src/google/protobuf/io/zero_copy_sink_test.cc index da8fc05f23fe..b492ac34ae70 100644 --- a/src/google/protobuf/io/zero_copy_sink_test.cc +++ b/src/google/protobuf/io/zero_copy_sink_test.cc @@ -39,7 +39,9 @@ #include #include #include -#include +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" namespace google { namespace protobuf { @@ -49,11 +51,11 @@ namespace { class ChunkedString { public: - explicit ChunkedString(StringPiece data, size_t skipped_patterns) + explicit ChunkedString(absl::string_view data, size_t skipped_patterns) : data_(data), skipped_patterns_(skipped_patterns) {} // Returns the next chunk; empty if out of chunks. - StringPiece NextChunk() { + absl::string_view NextChunk() { if (pattern_bit_idx_ == data_.size()) { return ""; } @@ -83,11 +85,11 @@ class ChunkedString { for (size_t i = 0; i <= data_.size(); ++i) { if (i == data_.size() || (pattern_ >> start & 1) != 0) { if (!out.empty()) { - StrAppend(&out, " "); + absl::StrAppend(&out, " "); } - StrAppend( + absl::StrAppend( &out, "\"", - strings::CHexEscape(std::string{data_.substr(start, i - start)}), + absl::CHexEscape(std::string{data_.substr(start, i - start)}), "\""); start = i; } @@ -96,7 +98,7 @@ class ChunkedString { } private: - StringPiece data_; + absl::string_view data_; size_t skipped_patterns_; // pattern_ is a bitset indicating at which indices we insert a seam. uint64_t pattern_ = 0; @@ -108,7 +110,7 @@ class PatternedOutputStream : public io::ZeroCopyOutputStream { explicit PatternedOutputStream(ChunkedString data) : data_(data) {} bool Next(void** buffer, int* length) override { - StringPiece segment; + absl::string_view segment; if (!back_up_.empty()) { segment = back_up_.back(); back_up_.pop_back(); @@ -144,16 +146,16 @@ class PatternedOutputStream : public io::ZeroCopyOutputStream { private: ChunkedString data_; - StringPiece segment_; + absl::string_view segment_; - std::vector back_up_; + std::vector back_up_; int64_t byte_count_ = 0; }; class ZeroCopyStreamByteSinkTest : public testing::Test { protected: std::array output_{}; - StringPiece output_view_{output_.data(), output_.size()}; + absl::string_view output_view_{output_.data(), output_.size()}; ChunkedString output_chunks_{output_view_, 7}; }; @@ -168,7 +170,7 @@ TEST_F(ZeroCopyStreamByteSinkTest, WriteExact) { PatternedOutputStream output_stream(output_chunks_); ZeroCopyStreamByteSink byte_sink(&output_stream); SCOPED_TRACE(input.PatternAsQuotedString()); - StringPiece chunk; + absl::string_view chunk; while (!(chunk = input.NextChunk()).empty()) { byte_sink.Append(chunk.data(), chunk.size()); } @@ -189,13 +191,13 @@ TEST_F(ZeroCopyStreamByteSinkTest, WriteShort) { PatternedOutputStream output_stream(output_chunks_); ZeroCopyStreamByteSink byte_sink(&output_stream); SCOPED_TRACE(input.PatternAsQuotedString()); - StringPiece chunk; + absl::string_view chunk; while (!(chunk = input.NextChunk()).empty()) { byte_sink.Append(chunk.data(), chunk.size()); } } while (input.NextPattern()); - ASSERT_EQ(output_view_, StringPiece("012345678\0", 10)); + ASSERT_EQ(output_view_, absl::string_view("012345678\0", 10)); } while (output_chunks_.NextPattern()); } @@ -210,7 +212,7 @@ TEST_F(ZeroCopyStreamByteSinkTest, WriteLong) { PatternedOutputStream output_stream(output_chunks_); ZeroCopyStreamByteSink byte_sink(&output_stream); SCOPED_TRACE(input.PatternAsQuotedString()); - StringPiece chunk; + absl::string_view chunk; while (!(chunk = input.NextChunk()).empty()) { byte_sink.Append(chunk.data(), chunk.size()); } diff --git a/src/google/protobuf/io/zero_copy_stream_impl_lite.cc b/src/google/protobuf/io/zero_copy_stream_impl_lite.cc index bea35c123828..43ee26c2fd5b 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl_lite.cc +++ b/src/google/protobuf/io/zero_copy_stream_impl_lite.cc @@ -40,7 +40,7 @@ #include #include -#include +#include "absl/base/casts.h" #include // Must be included last @@ -187,7 +187,7 @@ int CopyingInputStream::Skip(int count) { int skipped = 0; while (skipped < count) { int bytes = Read(junk, std::min(count - skipped, - implicit_cast(sizeof(junk)))); + absl::implicit_cast(sizeof(junk)))); if (bytes <= 0) { // EOF or read error. return skipped; diff --git a/src/google/protobuf/lite_unittest.cc b/src/google/protobuf/lite_unittest.cc index 583d325e9b6e..993f39b1f812 100644 --- a/src/google/protobuf/lite_unittest.cc +++ b/src/google/protobuf/lite_unittest.cc @@ -40,6 +40,7 @@ #include #include #include +#include "absl/strings/string_view.h" #include #include #include diff --git a/src/google/protobuf/map.h b/src/google/protobuf/map.h index d19addacce9d..dc30ad35848b 100644 --- a/src/google/protobuf/map.h +++ b/src/google/protobuf/map.h @@ -60,7 +60,7 @@ #include #include #include -#include + #ifdef SWIG #error "You cannot SWIG proto headers" diff --git a/src/google/protobuf/map_entry_lite.h b/src/google/protobuf/map_entry_lite.h index 6b08cd92ba6e..c453a0057659 100644 --- a/src/google/protobuf/map_entry_lite.h +++ b/src/google/protobuf/map_entry_lite.h @@ -37,19 +37,20 @@ #include #include -#include #include #include -#include +#include "absl/base/casts.h" #include #include #include #include #include +#include #include // Must be included last. #include + #ifdef SWIG #error "You cannot SWIG proto headers" #endif diff --git a/src/google/protobuf/map_field.cc b/src/google/protobuf/map_field.cc index ed662dfc439a..6219d0fa344b 100644 --- a/src/google/protobuf/map_field.cc +++ b/src/google/protobuf/map_field.cc @@ -32,6 +32,7 @@ #include +#include #include // Must be included last. @@ -40,6 +41,7 @@ namespace google { namespace protobuf { namespace internal { +using ::google::protobuf::internal::DownCast; void MapFieldBase::Destruct() { if (arena_ == nullptr) { @@ -427,7 +429,7 @@ void DynamicMapField::MergeFrom(const MapFieldBase& other) { } void DynamicMapField::Swap(MapFieldBase* other) { - DynamicMapField* other_field = down_cast(other); + DynamicMapField* other_field = DownCast(other); std::swap(this->MapFieldBase::repeated_field_, other_field->repeated_field_); map_.swap(other_field->map_); // a relaxed swap of the atomic diff --git a/src/google/protobuf/map_field_inl.h b/src/google/protobuf/map_field_inl.h index 7c4c2323c411..379a81780972 100644 --- a/src/google/protobuf/map_field_inl.h +++ b/src/google/protobuf/map_field_inl.h @@ -33,10 +33,14 @@ #include -#include +#include "absl/base/casts.h" #include #include #include +#include + +// must be last +#include #ifdef SWIG #error "You cannot SWIG proto headers" @@ -278,7 +282,7 @@ template ::Swap( MapFieldBase* other) { MapFieldBase::Swap(other); - MapField* other_field = down_cast(other); + MapField* other_field = DownCast(other); impl_.Swap(&other_field->impl_); } @@ -287,7 +291,7 @@ template void MapField::UnsafeShallowSwap(MapFieldBase* other) { - InternalSwap(down_cast(other)); + InternalSwap(DownCast(other)); } template ::const_iterator it = map.begin(); it != map.end(); ++it) { EntryType* new_entry = - down_cast(default_entry->New(this->MapFieldBase::arena_)); + DownCast(default_entry->New(this->MapFieldBase::arena_)); repeated_field->AddAllocated(new_entry); (*new_entry->mutable_key()) = it->first; (*new_entry->mutable_value()) = it->second; @@ -372,4 +376,6 @@ size_t MapField + #endif // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__ diff --git a/src/google/protobuf/map_test.inc b/src/google/protobuf/map_test.inc index 1d55dd57e601..bffcca5ef4b9 100644 --- a/src/google/protobuf/map_test.inc +++ b/src/google/protobuf/map_test.inc @@ -53,7 +53,7 @@ #include #include #include -#include +#include "absl/base/casts.h" #include #include #include @@ -92,6 +92,7 @@ void MapTestForceDeterministic() { } namespace { +using internal::DownCast; // Map API Test ===================================================== @@ -1487,7 +1488,7 @@ TEST_F(MapImplTest, ConstInit) { static int Func(int i, int j) { return i * j; } -static std::string StrFunc(int i, int j) { return StrCat(Func(i, j)); } +static std::string StrFunc(int i, int j) { return absl::StrCat(Func(i, j)); } static int Int(const std::string& value) { int result = 0; @@ -1610,7 +1611,7 @@ TEST_F(MapFieldReflectionTest, RegularFields) { message_int32_message.GetReflection()->GetInt32( message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = - down_cast( + DownCast( message_int32_message.GetReflection()->GetMessage( message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); @@ -1647,7 +1648,7 @@ TEST_F(MapFieldReflectionTest, RegularFields) { message_int32_message.GetReflection()->GetInt32( message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = - down_cast( + DownCast( message_int32_message.GetReflection()->GetMessage( message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); @@ -1684,7 +1685,7 @@ TEST_F(MapFieldReflectionTest, RegularFields) { int32_t key_int32_message = message_int32_message->GetReflection()->GetInt32( *message_int32_message, fd_map_int32_foreign_message_key); - ForeignMessage* value_int32_message = down_cast( + ForeignMessage* value_int32_message = DownCast( message_int32_message->GetReflection()->MutableMessage( message_int32_message, fd_map_int32_foreign_message_value)); value_int32_message->set_c(Func(key_int32_message, -6)); @@ -1840,7 +1841,7 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { message_int32_message.GetReflection()->GetInt32( message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = - down_cast( + DownCast( message_int32_message.GetReflection()->GetMessage( message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); @@ -1881,7 +1882,7 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { message_int32_message.GetReflection()->GetInt32( message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = - down_cast( + DownCast( message_int32_message.GetReflection()->GetMessage( message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); @@ -2011,7 +2012,7 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { int32_t key = message.GetReflection()->GetInt32( message, fd_map_int32_foreign_message_key); const ForeignMessage& sub_message = - down_cast(message.GetReflection()->GetMessage( + DownCast(message.GetReflection()->GetMessage( message, fd_map_int32_foreign_message_value)); result[key].MergeFrom(sub_message); ++index; @@ -2166,13 +2167,13 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { const Message& message0a = mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get()); const ForeignMessage& sub_message0a = - down_cast(message0a.GetReflection()->GetMessage( + DownCast(message0a.GetReflection()->GetMessage( message0a, fd_map_int32_foreign_message_value)); int32_t int32_value0a = sub_message0a.c(); const Message& message9a = mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get()); const ForeignMessage& sub_message9a = - down_cast(message9a.GetReflection()->GetMessage( + DownCast(message9a.GetReflection()->GetMessage( message9a, fd_map_int32_foreign_message_value)); int32_t int32_value9a = sub_message9a.c(); @@ -2181,13 +2182,13 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { const Message& message0b = mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get()); const ForeignMessage& sub_message0b = - down_cast(message0b.GetReflection()->GetMessage( + DownCast(message0b.GetReflection()->GetMessage( message0b, fd_map_int32_foreign_message_value)); int32_t int32_value0b = sub_message0b.c(); const Message& message9b = mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get()); const ForeignMessage& sub_message9b = - down_cast(message9b.GetReflection()->GetMessage( + DownCast(message9b.GetReflection()->GetMessage( message9b, fd_map_int32_foreign_message_value)); int32_t int32_value9b = sub_message9b.c(); @@ -2525,7 +2526,7 @@ TEST(GeneratedMapFieldTest, UpcastCopyFrom) { MapTestUtil::SetMapFields(&message1); - const Message* source = implicit_cast(&message1); + const Message* source = absl::implicit_cast(&message1); message2.CopyFrom(*source); MapTestUtil::ExpectMapFieldsSet(message2); @@ -3032,7 +3033,7 @@ TEST(GeneratedMapFieldTest, MessagesMustMerge) { *p++ = 0; *p++ = WireFormatLite::MakeTag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED); *p++ = payload_size; - StrAppend(&s, dummy4_s, dummy5_s); + absl::StrAppend(&s, dummy4_s, dummy5_s); // Test key then value then value. int key = 0; @@ -3783,7 +3784,7 @@ static std::string DeterministicSerialization(const T& t) { static std::string ConstructKey(uint64_t n) { std::string s(n % static_cast(9), '\0'); if (s.empty()) { - return StrCat(n); + return absl::StrCat(n); } else { while (n != 0) { s[n % s.size()] = (n >> 10) & 0x7f; @@ -3889,7 +3890,6 @@ TEST(TextFormatMapTest, DynamicMessage) { "testdata/map_test_data.txt"), &expected_text, true)); - CleanStringLineEndings(&expected_text, false); std::string actual_text; TextFormat::PrintToString(*message, &actual_text); EXPECT_EQ(actual_text, expected_text); @@ -3906,7 +3906,6 @@ TEST(TextFormatMapTest, Sorted) { "testdata/map_test_data.txt"), &expected_text, true)); - CleanStringLineEndings(&expected_text, false); TextFormat::Printer printer; std::string actual_text; printer.PrintToString(message, &actual_text); diff --git a/src/google/protobuf/message.cc b/src/google/protobuf/message.cc index edd466624ef3..878269184b55 100644 --- a/src/google/protobuf/message.cc +++ b/src/google/protobuf/message.cc @@ -38,12 +38,15 @@ #include #include -#include #include #include #include #include -#include +#include "absl/base/casts.h" +#include "absl/container/flat_hash_map.h" +#include "absl/strings/str_join.h" +#include "absl/strings/string_view.h" +#include "absl/synchronization/mutex.h" #include #include #include @@ -59,14 +62,13 @@ #include #include #include -#include + // Must be included last. #include namespace google { namespace protobuf { - namespace internal { // TODO(gerbens) make this factorized better. This should not have to hop @@ -76,6 +78,7 @@ void RegisterFileLevelMetadata(const DescriptorTable* descriptor_table); } // namespace internal +using internal::DownCast; using internal::ReflectionOps; using internal::WireFormat; using internal::WireFormatLite; @@ -93,7 +96,7 @@ void Message::MergeFrom(const Message& from) { } void Message::CheckTypeAndMergeFrom(const MessageLite& other) { - MergeFrom(*down_cast(&other)); + MergeFrom(*DownCast(&other)); } void Message::CopyFrom(const Message& from) { @@ -153,7 +156,7 @@ void Message::FindInitializationErrors(std::vector* errors) const { std::string Message::InitializationErrorString() const { std::vector errors; FindInitializationErrors(&errors); - return Join(errors, ", "); + return absl::StrJoin(errors, ", "); } void Message::CheckInitialized() const { @@ -241,11 +244,6 @@ MessageFactory::~MessageFactory() {} namespace { - -#define HASH_MAP std::unordered_map -#define STR_HASH_FXN hash<::google::protobuf::StringPiece> - - class GeneratedMessageFactory final : public MessageFactory { public: static GeneratedMessageFactory* singleton(); @@ -258,13 +256,13 @@ class GeneratedMessageFactory final : public MessageFactory { private: // Only written at static init time, so does not require locking. - HASH_MAP + absl::flat_hash_map file_map_; absl::Mutex mutex_; - // Initialized lazily, so requires locking. - std::unordered_map type_map_; + absl::flat_hash_map type_map_ + ABSL_GUARDED_BY(mutex_); }; GeneratedMessageFactory* GeneratedMessageFactory::singleton() { @@ -309,7 +307,7 @@ const Message* GeneratedMessageFactory::GetPrototype(const Descriptor* type) { // Apparently the file hasn't been registered yet. Let's do that now. const internal::DescriptorTable* registration_data = - FindPtrOrNull(file_map_, type->file()->name().c_str()); + FindPtrOrNull(file_map_, type->file()->name()); if (registration_data == nullptr) { GOOGLE_LOG(DFATAL) << "File appears to be in generated pool but wasn't " "registered: " diff --git a/src/google/protobuf/message.h b/src/google/protobuf/message.h index 18d7fc5dc895..c401516f78b1 100644 --- a/src/google/protobuf/message.h +++ b/src/google/protobuf/message.h @@ -116,16 +116,19 @@ #include #include -#include #include #include #include +#include "absl/base/call_once.h" +#include "absl/base/casts.h" +#include "absl/strings/string_view.h" #include #include #include #include #include // TODO(b/211442718): cleanup #include +#include // Must be included last. @@ -1068,13 +1071,13 @@ class PROTOBUF_EXPORT Reflection final { // The table-driven parser table. // This table is generated on demand for Message types that did not override // _InternalParse. It uses the reflection information to do so. - mutable internal::once_flag tcparse_table_once_; + mutable absl::once_flag tcparse_table_once_; using TcParseTableBase = internal::TcParseTableBase; mutable const TcParseTableBase* tcparse_table_ = nullptr; const TcParseTableBase* GetTcParseTable() const { - internal::call_once(tcparse_table_once_, - [&] { tcparse_table_ = CreateTcParseTable(); }); + absl::call_once(tcparse_table_once_, + [&] { tcparse_table_ = CreateTcParseTable(); }); return tcparse_table_; } @@ -1432,7 +1435,7 @@ const T* DynamicCastToGenerated(const Message* from) { #else bool ok = from != nullptr && T::default_instance().GetReflection() == from->GetReflection(); - return ok ? down_cast(from) : nullptr; + return ok ? internal::DownCast(from) : nullptr; #endif } diff --git a/src/google/protobuf/message_lite.cc b/src/google/protobuf/message_lite.cc index b7e980bb3da4..f6f8a371d63d 100644 --- a/src/google/protobuf/message_lite.cc +++ b/src/google/protobuf/message_lite.cc @@ -48,7 +48,8 @@ #include #include #include "absl/base/dynamic_annotations.h" -#include +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include #include @@ -68,7 +69,7 @@ std::string MessageLite::InitializationErrorString() const { std::string MessageLite::DebugString() const { std::uintptr_t address = reinterpret_cast(this); - return StrCat("MessageLite at 0x", strings::Hex(address)); + return absl::StrCat("MessageLite at 0x", absl::Hex(address)); } namespace { @@ -115,8 +116,8 @@ std::string InitializationErrorMessage(const char* action, return result; } -inline StringPiece as_string_view(const void* data, int size) { - return StringPiece(static_cast(data), size); +inline absl::string_view as_string_view(const void* data, int size) { + return absl::string_view(static_cast(data), size); } // Returns true of all required fields are present / have values. @@ -139,7 +140,7 @@ void MessageLite::LogInitializationErrorMessage() const { namespace internal { template -bool MergeFromImpl(StringPiece input, MessageLite* msg, +bool MergeFromImpl(absl::string_view input, MessageLite* msg, MessageLite::ParseFlags parse_flags) { const char* ptr; internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(), @@ -181,9 +182,9 @@ bool MergeFromImpl(BoundedZCIS input, MessageLite* msg, return false; } -template bool MergeFromImpl(StringPiece input, MessageLite* msg, +template bool MergeFromImpl(absl::string_view input, MessageLite* msg, MessageLite::ParseFlags parse_flags); -template bool MergeFromImpl(StringPiece input, MessageLite* msg, +template bool MergeFromImpl(absl::string_view input, MessageLite* msg, MessageLite::ParseFlags parse_flags); template bool MergeFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg, @@ -308,11 +309,11 @@ bool MessageLite::ParsePartialFromBoundedZeroCopyStream( return ParseFrom(internal::BoundedZCIS{input, size}); } -bool MessageLite::ParseFromString(ConstStringParam data) { +bool MessageLite::ParseFromString(absl::string_view data) { return ParseFrom(data); } -bool MessageLite::ParsePartialFromString(ConstStringParam data) { +bool MessageLite::ParsePartialFromString(absl::string_view data) { return ParseFrom(data); } @@ -324,7 +325,7 @@ bool MessageLite::ParsePartialFromArray(const void* data, int size) { return ParseFrom(as_string_view(data, size)); } -bool MessageLite::MergeFromString(ConstStringParam data) { +bool MessageLite::MergeFromString(absl::string_view data) { return ParseFrom(data); } diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index 950ae1a126c8..a50121e5c785 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -47,12 +47,12 @@ #include #include #include -#include #include -#include +#include "absl/base/call_once.h" +#include "absl/strings/string_view.h" #include #include -#include // TODO(b/211442718): cleanup + // clang-format off #include @@ -277,11 +277,11 @@ class PROTOBUF_EXPORT MessageLite { // format, matching the encoding output by MessageLite::SerializeToString(). // If you'd like to convert a human-readable string into a protocol buffer // object, see google::protobuf::TextFormat::ParseFromString(). - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromString(ConstStringParam data); + PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromString(absl::string_view data); // Like ParseFromString(), but accepts messages that are missing // required fields. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromString( - ConstStringParam data); + absl::string_view data); // Parse a protocol buffer contained in an array of bytes. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromArray(const void* data, int size); @@ -312,7 +312,7 @@ class PROTOBUF_EXPORT MessageLite { bool MergePartialFromCodedStream(io::CodedInputStream* input); // Merge a protocol buffer contained in a string. - bool MergeFromString(ConstStringParam data); + bool MergeFromString(absl::string_view data); // Serialization --------------------------------------------------- @@ -499,12 +499,12 @@ class PROTOBUF_EXPORT MessageLite { namespace internal { template -bool MergeFromImpl(StringPiece input, MessageLite* msg, +bool MergeFromImpl(absl::string_view input, MessageLite* msg, MessageLite::ParseFlags parse_flags); -extern template bool MergeFromImpl(StringPiece input, +extern template bool MergeFromImpl(absl::string_view input, MessageLite* msg, MessageLite::ParseFlags parse_flags); -extern template bool MergeFromImpl(StringPiece input, +extern template bool MergeFromImpl(absl::string_view input, MessageLite* msg, MessageLite::ParseFlags parse_flags); diff --git a/src/google/protobuf/parse_context.cc b/src/google/protobuf/parse_context.cc index 4211babef1a0..91e10139742c 100644 --- a/src/google/protobuf/parse_context.cc +++ b/src/google/protobuf/parse_context.cc @@ -36,7 +36,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" #include // Must be included last. @@ -300,7 +300,7 @@ void WriteVarint(uint32_t num, uint64_t val, std::string* s) { WriteVarint(val, s); } -void WriteLengthDelimited(uint32_t num, StringPiece val, std::string* s) { +void WriteLengthDelimited(uint32_t num, absl::string_view val, std::string* s) { WriteVarint((num << 3) + 2, s); WriteVarint(val.size(), s); s->append(val.data(), val.size()); @@ -377,11 +377,11 @@ const char* StringParser(const char* begin, const char* end, void* object, } // Defined in wire_format_lite.cc -void PrintUTF8ErrorLog(StringPiece message_name, - StringPiece field_name, const char* operation_str, +void PrintUTF8ErrorLog(absl::string_view message_name, + absl::string_view field_name, const char* operation_str, bool emit_stacktrace); -bool VerifyUTF8(StringPiece str, const char* field_name) { +bool VerifyUTF8(absl::string_view str, const char* field_name) { if (!IsStructurallyValidUTF8(str)) { PrintUTF8ErrorLog("", field_name, "parsing", false); return false; diff --git a/src/google/protobuf/parse_context.h b/src/google/protobuf/parse_context.h index c72e19fe0d0e..cd8b3c788a14 100644 --- a/src/google/protobuf/parse_context.h +++ b/src/google/protobuf/parse_context.h @@ -40,7 +40,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" #include #include #include @@ -64,11 +64,11 @@ namespace internal { // Template code below needs to know about the existence of these functions. PROTOBUF_EXPORT void WriteVarint(uint32_t num, uint64_t val, std::string* s); -PROTOBUF_EXPORT void WriteLengthDelimited(uint32_t num, StringPiece val, +PROTOBUF_EXPORT void WriteLengthDelimited(uint32_t num, absl::string_view val, std::string* s); // Inline because it is just forwarding to s->WriteVarint inline void WriteVarint(uint32_t num, uint64_t val, UnknownFieldSet* s); -inline void WriteLengthDelimited(uint32_t num, StringPiece val, +inline void WriteLengthDelimited(uint32_t num, absl::string_view val, UnknownFieldSet* s); @@ -229,7 +229,7 @@ class PROTOBUF_EXPORT EpsCopyInputStream { return res.second; } - const char* InitFrom(StringPiece flat) { + const char* InitFrom(absl::string_view flat) { overall_limit_ = 0; if (flat.size() > kSlopBytes) { limit_ = kSlopBytes; @@ -909,7 +909,7 @@ const char* EpsCopyInputStream::ReadPackedVarint(const char* ptr, Add add) { // Helper for verification of utf8 PROTOBUF_EXPORT -bool VerifyUTF8(StringPiece s, const char* field_name); +bool VerifyUTF8(absl::string_view s, const char* field_name); inline bool VerifyUTF8(const std::string* s, const char* field_name) { return VerifyUTF8(*s, field_name); diff --git a/src/google/protobuf/port.h b/src/google/protobuf/port.h index 09e82c46ec18..9d39111e995d 100644 --- a/src/google/protobuf/port.h +++ b/src/google/protobuf/port.h @@ -38,11 +38,20 @@ #include #include +#include + + +// must be last +#include namespace google { namespace protobuf { + + namespace internal { + + inline void SizedDelete(void* p, size_t size) { #if defined(__cpp_sized_deallocation) ::operator delete(p, size); @@ -77,8 +86,35 @@ struct ArenaInitialized { explicit ArenaInitialized() = default; }; +template +inline To DownCast(From* f) { + static_assert( + std::is_base_of::type>::value, + "illegal DownCast"); + +#if PROTOBUF_RTTI + // RTTI: debug mode only! + assert(f == nullptr || dynamic_cast(f) != nullptr); +#endif + return static_cast(f); +} + +template +inline ToRef DownCast(From& f) { + using To = typename std::remove_reference::type; + static_assert(std::is_base_of::value, "illegal DownCast"); + +#if PROTOBUF_RTTI + // RTTI: debug mode only! + assert(dynamic_cast(&f) != nullptr); +#endif + return *static_cast(&f); +} + } // namespace internal } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_PORT_H__ diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index 5e034bae0fd8..98ce851e7b96 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -390,15 +390,6 @@ # define PROTOBUF_DEPRECATED_ENUM #endif -#ifdef PROTOBUF_FUNC_ALIGN -#error PROTOBUF_FUNC_ALIGN was previously defined -#endif -#if __has_attribute(aligned) || PROTOBUF_GNUC_MIN(4, 3) -#define PROTOBUF_FUNC_ALIGN(bytes) __attribute__((aligned(bytes))) -#else -#define PROTOBUF_FUNC_ALIGN(bytes) -#endif - #ifdef PROTOBUF_RETURNS_NONNULL #error PROTOBUF_RETURNS_NONNULL was previously defined #endif @@ -872,6 +863,13 @@ // ThreadSafeArenaz is turned off completely in opensource builds. +// autoheader defines this in some circumstances +#ifdef PACKAGE +#define PROTOBUF_DID_UNDEF_PACKAGE +#pragma push_macro("PACKAGE") +#undef PACKAGE +#endif + // Windows declares several inconvenient macro names. We #undef them and then // restore them in port_undef.inc. #ifdef _WIN32 @@ -893,6 +891,8 @@ #undef ERROR_NOT_FOUND #pragma push_macro("GetClassName") #undef GetClassName +#pragma push_macro("GetCurrentTime") +#undef GetCurrentTime #pragma push_macro("GetMessage") #undef GetMessage #pragma push_macro("GetObject") diff --git a/src/google/protobuf/port_undef.inc b/src/google/protobuf/port_undef.inc index 788743a34195..4dd6586811ae 100644 --- a/src/google/protobuf/port_undef.inc +++ b/src/google/protobuf/port_undef.inc @@ -55,7 +55,6 @@ #undef PROTOBUF_DEPRECATED #undef PROTOBUF_DEPRECATED_ENUM #undef PROTOBUF_DEPRECATED_MSG -#undef PROTOBUF_FUNC_ALIGN #undef PROTOBUF_RETURNS_NONNULL #undef PROTOBUF_ATTRIBUTE_REINITIALIZES #undef PROTOBUF_RTTI @@ -122,6 +121,11 @@ #undef PROTOBUF_FUTURE_FINAL +#ifdef PROTOBUF_DID_UNDEF_PACKAGE +#pragma pop_macro("PACKAGE") +#undef PROTOBUF_DID_UNDEF_PACKAGE +#endif + // Restore macro that may have been #undef'd in port_def.inc. #ifdef _WIN32 #pragma pop_macro("CREATE_NEW") @@ -133,6 +137,7 @@ #pragma pop_macro("ERROR_INSTALL_FAILED") #pragma pop_macro("ERROR_NOT_FOUND") #pragma pop_macro("GetClassName") +#pragma pop_macro("GetCurrentTime") #pragma pop_macro("GetMessage") #pragma pop_macro("GetObject") #pragma pop_macro("IGNORE") diff --git a/src/google/protobuf/reflection_ops.cc b/src/google/protobuf/reflection_ops.cc index 3a1972e2743b..a0c5231188f0 100644 --- a/src/google/protobuf/reflection_ops.cc +++ b/src/google/protobuf/reflection_ops.cc @@ -38,6 +38,7 @@ #include #include +#include "absl/strings/str_cat.h" #include #include #include @@ -374,7 +375,7 @@ static std::string SubMessagePrefix(const std::string& prefix, } if (index != -1) { result.append("["); - result.append(StrCat(index)); + result.append(absl::StrCat(index)); result.append("]"); } result.append("."); diff --git a/src/google/protobuf/reflection_ops_unittest.cc b/src/google/protobuf/reflection_ops_unittest.cc index 30f225c2c020..ab04b8006322 100644 --- a/src/google/protobuf/reflection_ops_unittest.cc +++ b/src/google/protobuf/reflection_ops_unittest.cc @@ -459,7 +459,7 @@ TEST(ReflectionOpsTest, OneofIsInitialized) { static std::string FindInitializationErrors(const Message& message) { std::vector errors; ReflectionOps::FindInitializationErrors(message, "", &errors); - return Join(errors, ","); + return absl::StrJoin(errors, ","); } TEST(ReflectionOpsTest, FindInitializationErrors) { diff --git a/src/google/protobuf/repeated_field_reflection_unittest.cc b/src/google/protobuf/repeated_field_reflection_unittest.cc index 25e82003f288..3512e5f48961 100644 --- a/src/google/protobuf/repeated_field_reflection_unittest.cc +++ b/src/google/protobuf/repeated_field_reflection_unittest.cc @@ -33,25 +33,26 @@ // Test reflection methods for aggregate access to Repeated[Ptr]Fields. // This test proto2 methods on a proto2 layout. -#include #include #include #include #include +#include "absl/base/casts.h" +#include #include namespace google { namespace protobuf { +namespace { +using internal::DownCast; using unittest::ForeignMessage; using unittest::TestAllExtensions; using unittest::TestAllTypes; -namespace { - static int Func(int i, int j) { return i * j; } -static std::string StrFunc(int i, int j) { return StrCat(Func(i, 4)); } +static std::string StrFunc(int i, int j) { return absl::StrCat(Func(i, 4)); } TEST(RepeatedFieldReflectionTest, RegularFields) { TestAllTypes message; @@ -113,7 +114,7 @@ TEST(RepeatedFieldReflectionTest, RegularFields) { EXPECT_EQ(rf_double.Get(i), Func(i, 2)); EXPECT_EQ(rpf_string.Get(i), StrFunc(i, 5)); EXPECT_EQ(rpf_foreign_message.Get(i).c(), Func(i, 6)); - EXPECT_EQ(down_cast(&rpf_message.Get(i))->c(), + EXPECT_EQ(DownCast(&rpf_message.Get(i))->c(), Func(i, 6)); // Check gets through mutable objects. @@ -121,7 +122,7 @@ TEST(RepeatedFieldReflectionTest, RegularFields) { EXPECT_EQ(mrf_double->Get(i), Func(i, 2)); EXPECT_EQ(mrpf_string->Get(i), StrFunc(i, 5)); EXPECT_EQ(mrpf_foreign_message->Get(i).c(), Func(i, 6)); - EXPECT_EQ(down_cast(&mrpf_message->Get(i))->c(), + EXPECT_EQ(DownCast(&mrpf_message->Get(i))->c(), Func(i, 6)); // Check sets through mutable objects. @@ -133,7 +134,7 @@ TEST(RepeatedFieldReflectionTest, RegularFields) { EXPECT_EQ(message.repeated_double(i), Func(i, -2)); EXPECT_EQ(message.repeated_string(i), StrFunc(i, -5)); EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, -6)); - down_cast(mrpf_message->Mutable(i))->set_c(Func(i, 7)); + DownCast(mrpf_message->Mutable(i))->set_c(Func(i, 7)); EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, 7)); } @@ -293,7 +294,7 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { ForeignMessage scratch_space; EXPECT_EQ(rf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6)); EXPECT_EQ( - down_cast(rf_message.Get(i, &scratch_space)).c(), + DownCast(rf_message.Get(i, &scratch_space)).c(), Func(i, 6)); // Check gets through mutable objects. @@ -302,8 +303,7 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { EXPECT_EQ(mrf_string.Get(i), StrFunc(i, 5)); EXPECT_EQ(mrf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6)); EXPECT_EQ( - down_cast(mrf_message.Get(i, &scratch_space)) - .c(), + DownCast(mrf_message.Get(i, &scratch_space)).c(), Func(i, 6)); // Check sets through mutable objects. diff --git a/src/google/protobuf/repeated_field_unittest.cc b/src/google/protobuf/repeated_field_unittest.cc index 0aa90f73bc7e..8a633393859f 100644 --- a/src/google/protobuf/repeated_field_unittest.cc +++ b/src/google/protobuf/repeated_field_unittest.cc @@ -53,6 +53,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include // Must be included last. @@ -2330,11 +2331,11 @@ TEST_F(RepeatedFieldInsertionIteratorsTest, TestAllTypes goldenproto; for (int i = 0; i < 10; ++i) { std::string* new_data = new std::string; - *new_data = "name-" + StrCat(i); + *new_data = "name-" + absl::StrCat(i); data.push_back(new_data); new_data = goldenproto.add_repeated_string(); - *new_data = "name-" + StrCat(i); + *new_data = "name-" + absl::StrCat(i); } TestAllTypes testproto; std::copy(data.begin(), data.end(), @@ -2367,7 +2368,7 @@ TEST_F(RepeatedFieldInsertionIteratorsTest, auto* goldenproto = Arena::CreateMessage(&arena); for (int i = 0; i < 10; ++i) { auto* new_data = goldenproto->add_repeated_string(); - *new_data = "name-" + StrCat(i); + *new_data = "name-" + absl::StrCat(i); data.push_back(new_data); } auto* testproto = Arena::CreateMessage(&arena); diff --git a/src/google/protobuf/source_context.pb.cc b/src/google/protobuf/source_context.pb.cc index 89118797ba42..41530f7798d4 100644 --- a/src/google/protobuf/source_context.pb.cc +++ b/src/google/protobuf/source_context.pb.cc @@ -66,7 +66,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fsource_5fcontext_2eprot "tobuf/types/known/sourcecontextpb\242\002\003GPB\252" "\002\036Google.Protobuf.WellKnownTypesb\006proto3" ; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto = { false, false, 240, descriptor_table_protodef_google_2fprotobuf_2fsource_5fcontext_2eproto, "google/protobuf/source_context.proto", diff --git a/src/google/protobuf/source_context.pb.h b/src/google/protobuf/source_context.pb.h index 784fe1cdf33d..e9a8f58ff750 100644 --- a/src/google/protobuf/source_context.pb.h +++ b/src/google/protobuf/source_context.pb.h @@ -159,7 +159,7 @@ class PROTOBUF_EXPORT SourceContext final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.SourceContext"; } protected: diff --git a/src/google/protobuf/struct.pb.cc b/src/google/protobuf/struct.pb.cc index f5b3468108b3..a95b4735e546 100644 --- a/src/google/protobuf/struct.pb.cc +++ b/src/google/protobuf/struct.pb.cc @@ -156,7 +156,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fstruct_2eproto[] PROTOB "rotobuf/types/known/structpb\370\001\001\242\002\003GPB\252\002\036" "Google.Protobuf.WellKnownTypesb\006proto3" ; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2fstruct_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2fstruct_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2fstruct_2eproto = { false, false, 638, descriptor_table_protodef_google_2fprotobuf_2fstruct_2eproto, "google/protobuf/struct.proto", diff --git a/src/google/protobuf/struct.pb.h b/src/google/protobuf/struct.pb.h index db305986749c..27dd45c9cd4d 100644 --- a/src/google/protobuf/struct.pb.h +++ b/src/google/protobuf/struct.pb.h @@ -94,7 +94,7 @@ inline const std::string& NullValue_Name(NullValue value) { (static_cast(value)); } inline bool NullValue_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, NullValue* value) { + ::absl::string_view name, NullValue* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( NullValue_descriptor(), name, value); } @@ -229,7 +229,7 @@ class PROTOBUF_EXPORT Struct final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Struct"; } protected: @@ -402,7 +402,7 @@ class PROTOBUF_EXPORT Value final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Value"; } protected: @@ -661,7 +661,7 @@ class PROTOBUF_EXPORT ListValue final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.ListValue"; } protected: diff --git a/src/google/protobuf/stubs/BUILD.bazel b/src/google/protobuf/stubs/BUILD.bazel index fbc19c282e2b..7ac9d02f8b56 100644 --- a/src/google/protobuf/stubs/BUILD.bazel +++ b/src/google/protobuf/stubs/BUILD.bazel @@ -15,42 +15,34 @@ cc_library( "bytestream.cc", "common.cc", "int128.cc", - "status.cc", - "statusor.cc", - "stringpiece.cc", "stringprintf.cc", "structurally_valid.cc", "strutil.cc", - "time.cc", ], hdrs = [ "bytestream.h", "callback.h", - "casts.h", "common.h", - "hash.h", "int128.h", "logging.h", "macros.h", "map_util.h", "mathutil.h", - "once.h", "platform_macros.h", "port.h", - "status.h", "status_macros.h", - "statusor.h", "stl_util.h", - "stringpiece.h", "stringprintf.h", "strutil.h", - "template_util.h", - "time.h", ], copts = COPTS, include_prefix = "google/protobuf/stubs", linkopts = LINK_OPTS, - deps = ["//src/google/protobuf:port_def"], + deps = [ + "//src/google/protobuf:port_def", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + ], ) cc_library( @@ -66,30 +58,25 @@ cc_library( textual_hdrs = [ "bytestream.h", "callback.h", - "casts.h", "common.h", - "hash.h", "int128.h", "logging.h", "macros.h", "map_util.h", "mathutil.h", - "once.h", "platform_macros.h", "port.h", - "status.h", "status_macros.h", - "statusor.h", "stl_util.h", - "stringpiece.h", "stringprintf.h", "strutil.h", - "template_util.h", - "time.h", ], deps = [ ":lite", "//src/google/protobuf:port_def", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", ], ) @@ -99,14 +86,9 @@ cc_test( "bytestream_unittest.cc", "common_unittest.cc", "int128_unittest.cc", - "status_test.cc", - "statusor_test.cc", - "stringpiece_unittest.cc", "stringprintf_unittest.cc", "structurally_valid_unittest.cc", "strutil_unittest.cc", - "template_util_unittest.cc", - "time_test.cc", ], copts = COPTS + select({ "//build_defs:config_msvc": [], diff --git a/src/google/protobuf/stubs/bytestream.cc b/src/google/protobuf/stubs/bytestream.cc index 980d6f6cfc56..c690d19db95b 100644 --- a/src/google/protobuf/stubs/bytestream.cc +++ b/src/google/protobuf/stubs/bytestream.cc @@ -41,7 +41,7 @@ namespace strings { void ByteSource::CopyTo(ByteSink* sink, size_t n) { while (n > 0) { - StringPiece fragment = Peek(); + absl::string_view fragment = Peek(); if (fragment.empty()) { GOOGLE_LOG(DFATAL) << "ByteSource::CopyTo() overran input."; break; @@ -149,9 +149,7 @@ size_t ArrayByteSource::Available() const { return input_.size(); } -StringPiece ArrayByteSource::Peek() { - return input_; -} +absl::string_view ArrayByteSource::Peek() { return input_; } void ArrayByteSource::Skip(size_t n) { GOOGLE_DCHECK_LE(n, input_.size()); @@ -172,9 +170,9 @@ size_t LimitByteSource::Available() const { return available; } -StringPiece LimitByteSource::Peek() { - StringPiece piece = source_->Peek(); - return StringPiece(piece.data(), std::min(piece.size(), limit_)); +absl::string_view LimitByteSource::Peek() { + absl::string_view piece = source_->Peek(); + return absl::string_view(piece.data(), std::min(piece.size(), limit_)); } void LimitByteSource::Skip(size_t n) { diff --git a/src/google/protobuf/stubs/bytestream.h b/src/google/protobuf/stubs/bytestream.h index c7a48dea544a..fa58ec25e554 100644 --- a/src/google/protobuf/stubs/bytestream.h +++ b/src/google/protobuf/stubs/bytestream.h @@ -44,19 +44,21 @@ // NullByteSink Consumes a never-ending stream of bytes // // ByteSource: -// ArrayByteSource Reads from an array or string/StringPiece +// ArrayByteSource Reads from an array or string // LimitedByteSource Limits the number of bytes read from an #ifndef GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_ #define GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_ +#include #include + #include -#include -#include +#include "absl/strings/string_view.h" -#include +// Must be last. +#include // NOLINT class CordByteSink; @@ -99,7 +101,7 @@ class PROTOBUF_EXPORT ByteSink { // // ByteSource* source = ... // while (source->Available() > 0) { -// StringPiece data = source->Peek(); +// absl::string_view data = source->Peek(); // ... do something with "data" ... // source->Skip(data.length()); // } @@ -118,17 +120,18 @@ class PROTOBUF_EXPORT ByteSource { // indicative of the fixed-size nature of a ByteSource. virtual size_t Available() const = 0; - // Returns a StringPiece of the next contiguous region of the source. Does not - // reposition the source. The returned region is empty iff Available() == 0. + // Returns an absl::string_view of the next contiguous region of the source. + // Does not reposition the source. The returned region is empty iff + // Available() == 0. // // The returned region is valid until the next call to Skip() or until this // object is destroyed, whichever occurs first. // - // The length of the returned StringPiece will be <= Available(). - virtual StringPiece Peek() = 0; + // The length of the returned absl::string_view will be <= Available(). + virtual absl::string_view Peek() = 0; - // Skips the next n bytes. Invalidates any StringPiece returned by a previous - // call to Peek(). + // Skips the next n bytes. Invalidates any absl::string_view returned by a + // previous call to Peek(). // // REQUIRES: Available() >= n virtual void Skip(size_t n) = 0; @@ -284,7 +287,7 @@ class PROTOBUF_EXPORT NullByteSink : public ByteSink { // Some commonly used implementations of ByteSource // -// Implementation of ByteSource that reads from a StringPiece. +// Implementation of ByteSource that reads from an absl::string_view. // // Example: // @@ -295,14 +298,14 @@ class PROTOBUF_EXPORT NullByteSink : public ByteSink { // class PROTOBUF_EXPORT ArrayByteSource : public ByteSource { public: - explicit ArrayByteSource(StringPiece s) : input_(s) {} + explicit ArrayByteSource(absl::string_view s) : input_(s) {} virtual size_t Available() const override; - virtual StringPiece Peek() override; + virtual absl::string_view Peek() override; virtual void Skip(size_t n) override; private: - StringPiece input_; + absl::string_view input_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayByteSource); }; @@ -330,7 +333,7 @@ class PROTOBUF_EXPORT LimitByteSource : public ByteSource { LimitByteSource(ByteSource* source, size_t limit); virtual size_t Available() const override; - virtual StringPiece Peek() override; + virtual absl::string_view Peek() override; virtual void Skip(size_t n) override; // We override CopyTo so that we can forward to the underlying source, in @@ -346,6 +349,6 @@ class PROTOBUF_EXPORT LimitByteSource : public ByteSource { } // namespace protobuf } // namespace google -#include +#include // NOLINT #endif // GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_ diff --git a/src/google/protobuf/stubs/bytestream_unittest.cc b/src/google/protobuf/stubs/bytestream_unittest.cc index cb11825ef72e..ccc845d9f361 100644 --- a/src/google/protobuf/stubs/bytestream_unittest.cc +++ b/src/google/protobuf/stubs/bytestream_unittest.cc @@ -47,22 +47,20 @@ namespace { // one fragment. class MockByteSource : public ByteSource { public: - MockByteSource(StringPiece data, int block_size) - : data_(data), block_size_(block_size) {} + MockByteSource(absl::string_view data, int block_size) + : data_(data), block_size_(block_size) {} size_t Available() const { return data_.size(); } - StringPiece Peek() { - return data_.substr(0, block_size_); - } + absl::string_view Peek() { return data_.substr(0, block_size_); } void Skip(size_t n) { data_.remove_prefix(n); } private: - StringPiece data_; + absl::string_view data_; int block_size_; }; TEST(ByteSourceTest, CopyTo) { - StringPiece data("Hello world!"); + absl::string_view data("Hello world!"); MockByteSource source(data, 3); std::string str; StringByteSink sink(&str); @@ -72,7 +70,7 @@ TEST(ByteSourceTest, CopyTo) { } TEST(ByteSourceTest, CopySubstringTo) { - StringPiece data("Hello world!"); + absl::string_view data("Hello world!"); MockByteSource source(data, 3); source.Skip(1); std::string str; @@ -84,7 +82,7 @@ TEST(ByteSourceTest, CopySubstringTo) { } TEST(ByteSourceTest, LimitByteSource) { - StringPiece data("Hello world!"); + absl::string_view data("Hello world!"); MockByteSource source(data, 3); LimitByteSource limit_source(&source, 6); EXPECT_EQ(6, limit_source.Available()); @@ -110,7 +108,7 @@ TEST(ByteSourceTest, LimitByteSource) { } TEST(ByteSourceTest, CopyToStringByteSink) { - StringPiece data("Hello world!"); + absl::string_view data("Hello world!"); MockByteSource source(data, 3); std::string str; StringByteSink sink(&str); diff --git a/src/google/protobuf/stubs/casts.h b/src/google/protobuf/stubs/casts.h deleted file mode 100644 index ad29dac1f876..000000000000 --- a/src/google/protobuf/stubs/casts.h +++ /dev/null @@ -1,138 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2014 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#ifndef GOOGLE_PROTOBUF_CASTS_H__ -#define GOOGLE_PROTOBUF_CASTS_H__ - -#include - -#include -#include - -namespace google { -namespace protobuf { -namespace internal { - -// Use implicit_cast as a safe version of static_cast or const_cast -// for upcasting in the type hierarchy (i.e. casting a pointer to Foo -// to a pointer to SuperclassOfFoo or casting a pointer to Foo to -// a const pointer to Foo). -// When you use implicit_cast, the compiler checks that the cast is safe. -// Such explicit implicit_casts are necessary in surprisingly many -// situations where C++ demands an exact type match instead of an -// argument type convertible to a target type. -// -// The From type can be inferred, so the preferred syntax for using -// implicit_cast is the same as for static_cast etc.: -// -// implicit_cast(expr) -// -// implicit_cast would have been part of the C++ standard library, -// but the proposal was submitted too late. It will probably make -// its way into the language in the future. -template -inline To implicit_cast(From const &f) { - return f; -} - -// When you upcast (that is, cast a pointer from type Foo to type -// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts -// always succeed. When you downcast (that is, cast a pointer from -// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because -// how do you know the pointer is really of type SubclassOfFoo? It -// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, -// when you downcast, you should use this macro. In debug mode, we -// use dynamic_cast<> to double-check the downcast is legal (we die -// if it's not). In normal mode, we do the efficient static_cast<> -// instead. Thus, it's important to test in debug mode to make sure -// the cast is legal! -// This is the only place in the code we should use dynamic_cast<>. -// In particular, you SHOULDN'T be using dynamic_cast<> in order to -// do RTTI (eg code like this: -// if (dynamic_cast(foo)) HandleASubclass1Object(foo); -// if (dynamic_cast(foo)) HandleASubclass2Object(foo); -// You should design the code some other way not to need this. - -template // use like this: down_cast(foo); -inline To down_cast(From* f) { // so we only accept pointers - // Ensures that To is a sub-type of From *. This test is here only - // for compile-time type checking, and has no overhead in an - // optimized build at run-time, as it will be optimized away - // completely. - if (false) { - implicit_cast(0); - } - -#if !defined(NDEBUG) && PROTOBUF_RTTI - assert(f == nullptr || dynamic_cast(f) != nullptr); // RTTI: debug mode only! -#endif - return static_cast(f); -} - -template // use like this: down_cast(foo); -inline To down_cast(From& f) { - typedef typename std::remove_reference::type* ToAsPointer; - // Ensures that To is a sub-type of From *. This test is here only - // for compile-time type checking, and has no overhead in an - // optimized build at run-time, as it will be optimized away - // completely. - if (false) { - implicit_cast(0); - } - -#if !defined(NDEBUG) && PROTOBUF_RTTI - // RTTI: debug mode only! - assert(dynamic_cast(&f) != nullptr); -#endif - return *static_cast(&f); -} - -template -inline To bit_cast(const From& from) { - static_assert(sizeof(From) == sizeof(To), "bit_cast_with_different_sizes"); - To dest; - memcpy(&dest, &from, sizeof(dest)); - return dest; -} - -} // namespace internal - -// We made these internal so that they would show up as such in the docs, -// but we don't want to stick "internal::" in front of them everywhere. -using internal::implicit_cast; -using internal::down_cast; -using internal::bit_cast; - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_CASTS_H__ diff --git a/src/google/protobuf/stubs/common.cc b/src/google/protobuf/stubs/common.cc index feff7a1ac015..8d7f00738c80 100644 --- a/src/google/protobuf/stubs/common.cc +++ b/src/google/protobuf/stubs/common.cc @@ -50,14 +50,15 @@ #endif #include +#include #include -#include -#include -#include #include -#include -#include +#include "absl/status/status.h" +#include "absl/strings/string_view.h" + +// Must be last. +#include // NOLINT namespace google { namespace protobuf { @@ -205,12 +206,12 @@ LogMessage& LogMessage::operator<<(const char* value) { return *this; } -LogMessage& LogMessage::operator<<(const StringPiece& value) { - message_ += value.ToString(); +LogMessage& LogMessage::operator<<(absl::string_view value) { + absl::StrAppend(&message_, value); return *this; } -LogMessage& LogMessage::operator<<(const util::Status& status) { +LogMessage& LogMessage::operator<<(const absl::Status& status) { message_ += status.ToString(); return *this; } @@ -223,18 +224,18 @@ LogMessage& LogMessage::operator<<(const uint128& value) { } LogMessage& LogMessage::operator<<(char value) { - return *this << StringPiece(&value, 1); + return *this << absl::string_view(&value, 1); } LogMessage& LogMessage::operator<<(void* value) { - StrAppend(&message_, strings::Hex(reinterpret_cast(value))); + absl::StrAppend(&message_, strings::Hex(reinterpret_cast(value))); return *this; } #undef DECLARE_STREAM_OPERATOR #define DECLARE_STREAM_OPERATOR(TYPE) \ LogMessage& LogMessage::operator<<(TYPE value) { \ - StrAppend(&message_, value); \ + absl::StrAppend(&message_, value); \ return *this; \ } @@ -336,4 +337,4 @@ const char* FatalException::what() const throw() { } // namespace protobuf } // namespace google -#include +#include // NOLINT diff --git a/src/google/protobuf/stubs/common.h b/src/google/protobuf/stubs/common.h index 892c66feb89f..d729739473f0 100644 --- a/src/google/protobuf/stubs/common.h +++ b/src/google/protobuf/stubs/common.h @@ -35,6 +35,10 @@ #ifndef GOOGLE_PROTOBUF_COMMON_H__ #define GOOGLE_PROTOBUF_COMMON_H__ +#include +#include +#include + #include #include #include @@ -43,10 +47,7 @@ #include #include -#include -#include -#include -#include +#include "absl/strings/string_view.h" #ifndef PROTOBUF_USE_EXCEPTIONS #if defined(_MSC_VER) && defined(_CPPUNWIND) @@ -134,12 +135,12 @@ namespace internal { // structurally_valid.cc. PROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len); -inline bool IsStructurallyValidUTF8(StringPiece str) { +inline bool IsStructurallyValidUTF8(absl::string_view str) { return IsStructurallyValidUTF8(str.data(), static_cast(str.length())); } // Returns initial number of bytes of structurally valid UTF-8. -PROTOBUF_EXPORT int UTF8SpnStructurallyValid(StringPiece str); +PROTOBUF_EXPORT int UTF8SpnStructurallyValid(absl::string_view str); // Coerce UTF-8 byte string in src_str to be // a structurally-valid equal-length string by selectively @@ -153,7 +154,8 @@ PROTOBUF_EXPORT int UTF8SpnStructurallyValid(StringPiece str); // // Optimized for: all structurally valid and no byte copying is done. // -PROTOBUF_EXPORT char* UTF8CoerceToStructurallyValid(StringPiece str, char* dst, +PROTOBUF_EXPORT char* UTF8CoerceToStructurallyValid(absl::string_view str, + char* dst, char replace_char); } // namespace internal diff --git a/src/google/protobuf/stubs/common_unittest.cc b/src/google/protobuf/stubs/common_unittest.cc index c55e45250574..7c93d5ac28e4 100644 --- a/src/google/protobuf/stubs/common_unittest.cc +++ b/src/google/protobuf/stubs/common_unittest.cc @@ -30,17 +30,18 @@ // Author: kenton@google.com (Kenton Varda) -#include #include -#include #include #include #include #include - #include #include +#include + +#include "absl/strings/ascii.h" + namespace google { namespace protobuf { namespace { @@ -56,7 +57,7 @@ TEST(VersionTest, VersionMatchesConfig) { std::string version = PACKAGE_VERSION; int pos = 0; while (pos < version.size() && - (ascii_isdigit(version[pos]) || version[pos] == '.')) { + (absl::ascii_isdigit(version[pos]) || version[pos] == '.')) { ++pos; } version.erase(pos); @@ -81,9 +82,8 @@ std::vector captured_messages_; void CaptureLog(LogLevel level, const char* filename, int line, const std::string& message) { - captured_messages_.push_back( - strings::Substitute("$0 $1:$2: $3", - implicit_cast(level), filename, line, message)); + captured_messages_.push_back(strings::Substitute( + "$0 $1:$2: $3", static_cast(level), filename, line, message)); } TEST(LoggingTest, DefaultLogging) { diff --git a/src/google/protobuf/stubs/hash.h b/src/google/protobuf/stubs/hash.h deleted file mode 100644 index a7ec06807484..000000000000 --- a/src/google/protobuf/stubs/hash.h +++ /dev/null @@ -1,114 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// Author: kenton@google.com (Kenton Varda) - -#ifndef GOOGLE_PROTOBUF_STUBS_HASH_H__ -#define GOOGLE_PROTOBUF_STUBS_HASH_H__ - -#include -#include -#include -#include - -# define GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START \ - namespace google { \ - namespace protobuf { -# define GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END }} - -namespace google { -namespace protobuf { - -template -struct hash : public std::hash {}; - -template -struct hash { - inline size_t operator()(const Key* key) const { - return reinterpret_cast(key); - } -}; - -// Unlike the old SGI version, the TR1 "hash" does not special-case char*. So, -// we go ahead and provide our own implementation. -template <> -struct hash { - inline size_t operator()(const char* str) const { - size_t result = 0; - for (; *str != '\0'; str++) { - result = 5 * result + static_cast(*str); - } - return result; - } -}; - -template<> -struct hash { - size_t operator()(bool x) const { - return static_cast(x); - } -}; - -template <> -struct hash { - inline size_t operator()(const std::string& key) const { - return hash()(key.c_str()); - } - - static const size_t bucket_size = 4; - static const size_t min_buckets = 8; - inline bool operator()(const std::string& a, const std::string& b) const { - return a < b; - } -}; - -template -struct hash > { - inline size_t operator()(const std::pair& key) const { - size_t first_hash = hash()(key.first); - size_t second_hash = hash()(key.second); - - // FIXME(kenton): What is the best way to compute this hash? I have - // no idea! This seems a bit better than an XOR. - return first_hash * ((1 << 16) - 1) + second_hash; - } - - static const size_t bucket_size = 4; - static const size_t min_buckets = 8; - inline bool operator()(const std::pair& a, - const std::pair& b) const { - return a < b; - } -}; - -} // namespace protobuf -} // namespace google - -#endif // GOOGLE_PROTOBUF_STUBS_HASH_H__ diff --git a/src/google/protobuf/stubs/logging.h b/src/google/protobuf/stubs/logging.h index 8ecc2fa0f14a..a2b71aa49bc9 100644 --- a/src/google/protobuf/stubs/logging.h +++ b/src/google/protobuf/stubs/logging.h @@ -33,10 +33,12 @@ #include #include -#include -#include -#include +#include "absl/status/status.h" +#include "absl/strings/string_view.h" + +// Must be last. +#include // NOLINT // =================================================================== // emulates google3/base/logging.h @@ -86,8 +88,8 @@ class PROTOBUF_EXPORT LogMessage { LogMessage& operator<<(unsigned long long value); LogMessage& operator<<(double value); LogMessage& operator<<(void* value); - LogMessage& operator<<(const StringPiece& value); - LogMessage& operator<<(const util::Status& status); + LogMessage& operator<<(absl::string_view value); + LogMessage& operator<<(const absl::Status& status); LogMessage& operator<<(const uint128& value); private: @@ -234,6 +236,6 @@ class PROTOBUF_EXPORT LogSilencer { } // namespace protobuf } // namespace google -#include +#include // NOLINT #endif // GOOGLE_PROTOBUF_STUBS_LOGGING_H_ diff --git a/src/google/protobuf/stubs/once.h b/src/google/protobuf/stubs/once.h deleted file mode 100644 index 070d36d19308..000000000000 --- a/src/google/protobuf/stubs/once.h +++ /dev/null @@ -1,55 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#ifndef GOOGLE_PROTOBUF_STUBS_ONCE_H__ -#define GOOGLE_PROTOBUF_STUBS_ONCE_H__ - -#include -#include - -#include - -namespace google { -namespace protobuf { -namespace internal { - -using once_flag = std::once_flag; -template -void call_once(Args&&... args ) { - std::call_once(std::forward(args)...); -} - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_ONCE_H__ diff --git a/src/google/protobuf/stubs/port.h b/src/google/protobuf/stubs/port.h index 954132bd5c04..e0d3ef776ec0 100644 --- a/src/google/protobuf/stubs/port.h +++ b/src/google/protobuf/stubs/port.h @@ -126,8 +126,6 @@ namespace google { namespace protobuf { -using ConstStringParam = const std::string &; - typedef unsigned int uint; typedef int8_t int8; diff --git a/src/google/protobuf/stubs/status.cc b/src/google/protobuf/stubs/status.cc deleted file mode 100644 index fc6c7bab0dc6..000000000000 --- a/src/google/protobuf/stubs/status.cc +++ /dev/null @@ -1,269 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include -#include -#include - -#include -#include -#include - -namespace google { -namespace protobuf { -namespace util { -namespace status_internal { -namespace { - -inline std::string StatusCodeToString(StatusCode code) { - switch (code) { - case StatusCode::kOk: - return "OK"; - case StatusCode::kCancelled: - return "CANCELLED"; - case StatusCode::kUnknown: - return "UNKNOWN"; - case StatusCode::kInvalidArgument: - return "INVALID_ARGUMENT"; - case StatusCode::kDeadlineExceeded: - return "DEADLINE_EXCEEDED"; - case StatusCode::kNotFound: - return "NOT_FOUND"; - case StatusCode::kAlreadyExists: - return "ALREADY_EXISTS"; - case StatusCode::kPermissionDenied: - return "PERMISSION_DENIED"; - case StatusCode::kUnauthenticated: - return "UNAUTHENTICATED"; - case StatusCode::kResourceExhausted: - return "RESOURCE_EXHAUSTED"; - case StatusCode::kFailedPrecondition: - return "FAILED_PRECONDITION"; - case StatusCode::kAborted: - return "ABORTED"; - case StatusCode::kOutOfRange: - return "OUT_OF_RANGE"; - case StatusCode::kUnimplemented: - return "UNIMPLEMENTED"; - case StatusCode::kInternal: - return "INTERNAL"; - case StatusCode::kUnavailable: - return "UNAVAILABLE"; - case StatusCode::kDataLoss: - return "DATA_LOSS"; - } - - // No default clause, clang will abort if a code is missing from - // above switch. - return "UNKNOWN"; -} - -} // namespace - -Status::Status() : error_code_(StatusCode::kOk) {} - -Status::Status(StatusCode error_code, StringPiece error_message) - : error_code_(error_code) { - if (error_code != StatusCode::kOk) { - error_message_ = error_message.ToString(); - } -} - -Status::Status(const Status& other) - : error_code_(other.error_code_), error_message_(other.error_message_) { -} - -Status& Status::operator=(const Status& other) { - error_code_ = other.error_code_; - error_message_ = other.error_message_; - return *this; -} - -bool Status::operator==(const Status& x) const { - return error_code_ == x.error_code_ && - error_message_ == x.error_message_; -} - -std::string Status::ToString() const { - if (error_code_ == StatusCode::kOk) { - return "OK"; - } else { - if (error_message_.empty()) { - return StatusCodeToString(error_code_); - } else { - return StatusCodeToString(error_code_) + ":" + error_message_; - } - } -} - -Status OkStatus() { return Status(); } - -std::ostream& operator<<(std::ostream& os, const Status& x) { - os << x.ToString(); - return os; -} - -bool IsAborted(const Status& status) { - return status.code() == StatusCode::kAborted; -} - -bool IsAlreadyExists(const Status& status) { - return status.code() == StatusCode::kAlreadyExists; -} - -bool IsCancelled(const Status& status) { - return status.code() == StatusCode::kCancelled; -} - -bool IsDataLoss(const Status& status) { - return status.code() == StatusCode::kDataLoss; -} - -bool IsDeadlineExceeded(const Status& status) { - return status.code() == StatusCode::kDeadlineExceeded; -} - -bool IsFailedPrecondition(const Status& status) { - return status.code() == StatusCode::kFailedPrecondition; -} - -bool IsInternal(const Status& status) { - return status.code() == StatusCode::kInternal; -} - -bool IsInvalidArgument(const Status& status) { - return status.code() == StatusCode::kInvalidArgument; -} - -bool IsNotFound(const Status& status) { - return status.code() == StatusCode::kNotFound; -} - -bool IsOutOfRange(const Status& status) { - return status.code() == StatusCode::kOutOfRange; -} - -bool IsPermissionDenied(const Status& status) { - return status.code() == StatusCode::kPermissionDenied; -} - -bool IsResourceExhausted(const Status& status) { - return status.code() == StatusCode::kResourceExhausted; -} - -bool IsUnauthenticated(const Status& status) { - return status.code() == StatusCode::kUnauthenticated; -} - -bool IsUnavailable(const Status& status) { - return status.code() == StatusCode::kUnavailable; -} - -bool IsUnimplemented(const Status& status) { - return status.code() == StatusCode::kUnimplemented; -} - -bool IsUnknown(const Status& status) { - return status.code() == StatusCode::kUnknown; -} - -Status AbortedError(StringPiece message) { - return Status(StatusCode::kAborted, message); -} - -Status AlreadyExistsError(StringPiece message) { - return Status(StatusCode::kAlreadyExists, message); -} - -Status CancelledError(StringPiece message) { - return Status(StatusCode::kCancelled, message); -} - -Status DataLossError(StringPiece message) { - return Status(StatusCode::kDataLoss, message); -} - -Status DeadlineExceededError(StringPiece message) { - return Status(StatusCode::kDeadlineExceeded, message); -} - -Status FailedPreconditionError(StringPiece message) { - return Status(StatusCode::kFailedPrecondition, message); -} - -Status InternalError(StringPiece message) { - return Status(StatusCode::kInternal, message); -} - -Status InvalidArgumentError(StringPiece message) { - return Status(StatusCode::kInvalidArgument, message); -} - -Status NotFoundError(StringPiece message) { - return Status(StatusCode::kNotFound, message); -} - -Status OutOfRangeError(StringPiece message) { - return Status(StatusCode::kOutOfRange, message); -} - -Status PermissionDeniedError(StringPiece message) { - return Status(StatusCode::kPermissionDenied, message); -} - -Status ResourceExhaustedError(StringPiece message) { - return Status(StatusCode::kResourceExhausted, message); -} - -Status UnauthenticatedError(StringPiece message) { - return Status(StatusCode::kUnauthenticated, message); -} - -Status UnavailableError(StringPiece message) { - return Status(StatusCode::kUnavailable, message); -} - -Status UnimplementedError(StringPiece message) { - return Status(StatusCode::kUnimplemented, message); -} - -Status UnknownError(StringPiece message) { - return Status(StatusCode::kUnknown, message); -} - -Status ErrnoToStatus(int error_number, StringPiece message) { - // We will take an Abseil dependency soon, so no reason to do anything - // elaborate here. - return InternalError(StrCat(message, ": ", strerror(error_number))); -} - -} // namespace status_internal -} // namespace util -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/stubs/status.h b/src/google/protobuf/stubs/status.h deleted file mode 100644 index 919e54ae2ea0..000000000000 --- a/src/google/protobuf/stubs/status.h +++ /dev/null @@ -1,202 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#ifndef GOOGLE_PROTOBUF_STUBS_STATUS_H_ -#define GOOGLE_PROTOBUF_STUBS_STATUS_H_ - -#include -#include - -#include - -// Must be included last. -#include - -namespace google { -namespace protobuf { -namespace util { -namespace status_internal { - -// These values must match error codes defined in google/rpc/code.proto. -enum class StatusCode : int { - kOk = 0, - kCancelled = 1, - kUnknown = 2, - kInvalidArgument = 3, - kDeadlineExceeded = 4, - kNotFound = 5, - kAlreadyExists = 6, - kPermissionDenied = 7, - kUnauthenticated = 16, - kResourceExhausted = 8, - kFailedPrecondition = 9, - kAborted = 10, - kOutOfRange = 11, - kUnimplemented = 12, - kInternal = 13, - kUnavailable = 14, - kDataLoss = 15, -}; - -class PROTOBUF_EXPORT Status { - public: - // Creates a "successful" status. - Status(); - - // Create a status in the canonical error space with the specified - // code, and error message. If "code == 0", error_message is - // ignored and a Status object identical to Status::kOk is - // constructed. - Status(StatusCode error_code, StringPiece error_message); - Status(const Status&); - Status& operator=(const Status& x); - ~Status() {} - - // Accessor - bool ok() const { return error_code_ == StatusCode::kOk; } - StatusCode code() const { return error_code_; } - StringPiece message() const { - return error_message_; - } - - bool operator==(const Status& x) const; - bool operator!=(const Status& x) const { - return !operator==(x); - } - - // Return a combination of the error code name and message. - std::string ToString() const; - - private: - StatusCode error_code_; - std::string error_message_; -}; - -// Returns an OK status, equivalent to a default constructed instance. Prefer -// usage of `OkStatus()` when constructing such an OK status. -PROTOBUF_EXPORT Status OkStatus(); - -// Prints a human-readable representation of 'x' to 'os'. -PROTOBUF_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x); - -// These convenience functions return `true` if a given status matches the -// `StatusCode` error code of its associated function. -PROTOBUF_EXPORT bool IsAborted(const Status& status); -PROTOBUF_EXPORT bool IsAlreadyExists(const Status& status); -PROTOBUF_EXPORT bool IsCancelled(const Status& status); -PROTOBUF_EXPORT bool IsDataLoss(const Status& status); -PROTOBUF_EXPORT bool IsDeadlineExceeded(const Status& status); -PROTOBUF_EXPORT bool IsFailedPrecondition(const Status& status); -PROTOBUF_EXPORT bool IsInternal(const Status& status); -PROTOBUF_EXPORT bool IsInvalidArgument(const Status& status); -PROTOBUF_EXPORT bool IsNotFound(const Status& status); -PROTOBUF_EXPORT bool IsOutOfRange(const Status& status); -PROTOBUF_EXPORT bool IsPermissionDenied(const Status& status); -PROTOBUF_EXPORT bool IsResourceExhausted(const Status& status); -PROTOBUF_EXPORT bool IsUnauthenticated(const Status& status); -PROTOBUF_EXPORT bool IsUnavailable(const Status& status); -PROTOBUF_EXPORT bool IsUnimplemented(const Status& status); -PROTOBUF_EXPORT bool IsUnknown(const Status& status); - -// These convenience functions create an `Status` object with an error code as -// indicated by the associated function name, using the error message passed in -// `message`. -// -// These functions are intentionally named `*Error` rather than `*Status` to -// match the names from Abseil: -// https://github.com/abseil/abseil-cpp/blob/2e9532cc6c701a8323d0cffb468999ab804095ab/absl/status/status.h#L716 -PROTOBUF_EXPORT Status AbortedError(StringPiece message); -PROTOBUF_EXPORT Status AlreadyExistsError(StringPiece message); -PROTOBUF_EXPORT Status CancelledError(StringPiece message); -PROTOBUF_EXPORT Status DataLossError(StringPiece message); -PROTOBUF_EXPORT Status DeadlineExceededError(StringPiece message); -PROTOBUF_EXPORT Status FailedPreconditionError(StringPiece message); -PROTOBUF_EXPORT Status InternalError(StringPiece message); -PROTOBUF_EXPORT Status InvalidArgumentError(StringPiece message); -PROTOBUF_EXPORT Status NotFoundError(StringPiece message); -PROTOBUF_EXPORT Status OutOfRangeError(StringPiece message); -PROTOBUF_EXPORT Status PermissionDeniedError(StringPiece message); -PROTOBUF_EXPORT Status ResourceExhaustedError(StringPiece message); -PROTOBUF_EXPORT Status UnauthenticatedError(StringPiece message); -PROTOBUF_EXPORT Status UnavailableError(StringPiece message); -PROTOBUF_EXPORT Status UnimplementedError(StringPiece message); -PROTOBUF_EXPORT Status UnknownError(StringPiece message); - -PROTOBUF_EXPORT Status ErrnoToStatus(int error_number, StringPiece message); - -} // namespace status_internal - -using ::google::protobuf::util::status_internal::Status; -using ::google::protobuf::util::status_internal::StatusCode; - -using ::google::protobuf::util::status_internal::IsAborted; -using ::google::protobuf::util::status_internal::IsAlreadyExists; -using ::google::protobuf::util::status_internal::IsCancelled; -using ::google::protobuf::util::status_internal::IsDataLoss; -using ::google::protobuf::util::status_internal::IsDeadlineExceeded; -using ::google::protobuf::util::status_internal::IsFailedPrecondition; -using ::google::protobuf::util::status_internal::IsInternal; -using ::google::protobuf::util::status_internal::IsInvalidArgument; -using ::google::protobuf::util::status_internal::IsNotFound; -using ::google::protobuf::util::status_internal::IsOutOfRange; -using ::google::protobuf::util::status_internal::IsPermissionDenied; -using ::google::protobuf::util::status_internal::IsResourceExhausted; -using ::google::protobuf::util::status_internal::IsUnauthenticated; -using ::google::protobuf::util::status_internal::IsUnavailable; -using ::google::protobuf::util::status_internal::IsUnimplemented; -using ::google::protobuf::util::status_internal::IsUnknown; - -using ::google::protobuf::util::status_internal::AbortedError; -using ::google::protobuf::util::status_internal::AlreadyExistsError; -using ::google::protobuf::util::status_internal::CancelledError; -using ::google::protobuf::util::status_internal::DataLossError; -using ::google::protobuf::util::status_internal::DeadlineExceededError; -using ::google::protobuf::util::status_internal::FailedPreconditionError; -using ::google::protobuf::util::status_internal::InternalError; -using ::google::protobuf::util::status_internal::InvalidArgumentError; -using ::google::protobuf::util::status_internal::NotFoundError; -using ::google::protobuf::util::status_internal::OkStatus; -using ::google::protobuf::util::status_internal::OutOfRangeError; -using ::google::protobuf::util::status_internal::PermissionDeniedError; -using ::google::protobuf::util::status_internal::ResourceExhaustedError; -using ::google::protobuf::util::status_internal::UnauthenticatedError; -using ::google::protobuf::util::status_internal::UnavailableError; -using ::google::protobuf::util::status_internal::UnimplementedError; -using ::google::protobuf::util::status_internal::UnknownError; - -using ::google::protobuf::util::status_internal::ErrnoToStatus; - -} // namespace util -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_ diff --git a/src/google/protobuf/stubs/status_macros.h b/src/google/protobuf/stubs/status_macros.h index 5a2cbf06ae03..2e7c0f54e9a6 100644 --- a/src/google/protobuf/stubs/status_macros.h +++ b/src/google/protobuf/stubs/status_macros.h @@ -34,17 +34,16 @@ #define GOOGLE_PROTOBUF_STUBS_STATUS_MACROS_H_ #include -#include -#include -#include +#include "absl/status/status.h" +#include "absl/status/statusor.h" + +// Needs to be last. +#include // NOLINT namespace google { namespace protobuf { namespace util { -namespace status_macros_internal { -using PROTOBUF_NAMESPACE_ID::util::Status; -} // namespace status_macros_internal // Run a command that returns a util::Status. If the called code returns an // error status, return that status up out of this method too. @@ -54,8 +53,7 @@ using PROTOBUF_NAMESPACE_ID::util::Status; #define RETURN_IF_ERROR(expr) \ do { \ /* Using _status below to avoid capture problems if expr is "status". */ \ - const ::google::protobuf::util::status_macros_internal::Status _status = \ - (expr); \ + const absl::Status _status = (expr); \ if (PROTOBUF_PREDICT_FALSE(!_status.ok())) return _status; \ } while (0) @@ -64,15 +62,15 @@ using PROTOBUF_NAMESPACE_ID::util::Status; #define STATUS_MACROS_CONCAT_NAME(x, y) STATUS_MACROS_CONCAT_NAME_INNER(x, y) template -Status DoAssignOrReturn(T& lhs, StatusOr result) { +absl::Status DoAssignOrReturn(T& lhs, absl::StatusOr result) { if (result.ok()) { lhs = result.value(); } return result.status(); } -#define ASSIGN_OR_RETURN_IMPL(status, lhs, rexpr) \ - Status status = DoAssignOrReturn(lhs, (rexpr)); \ +#define ASSIGN_OR_RETURN_IMPL(status, lhs, rexpr) \ + absl::Status status = DoAssignOrReturn(lhs, (rexpr)); \ if (PROTOBUF_PREDICT_FALSE(!status.ok())) return status; // Executes an expression that returns a util::StatusOr, extracting its value @@ -92,6 +90,6 @@ Status DoAssignOrReturn(T& lhs, StatusOr result) { } // namespace protobuf } // namespace google -#include +#include // NOLINT #endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_ diff --git a/src/google/protobuf/stubs/status_test.cc b/src/google/protobuf/stubs/status_test.cc deleted file mode 100644 index 9e9edf7e5fa7..000000000000 --- a/src/google/protobuf/stubs/status_test.cc +++ /dev/null @@ -1,278 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include - -#include - -#include -#include - -namespace google { -namespace protobuf { -namespace { - -TEST(Status, Constructor) { - EXPECT_EQ(util::StatusCode::kOk, - util::Status(util::StatusCode::kOk, "").code()); - EXPECT_EQ(util::StatusCode::kCancelled, - util::Status(util::StatusCode::kCancelled, "").code()); - EXPECT_EQ(util::StatusCode::kUnknown, - util::Status(util::StatusCode::kUnknown, "").code()); - EXPECT_EQ(util::StatusCode::kInvalidArgument, - util::Status(util::StatusCode::kInvalidArgument, "").code()); - EXPECT_EQ(util::StatusCode::kDeadlineExceeded, - util::Status(util::StatusCode::kDeadlineExceeded, "").code()); - EXPECT_EQ(util::StatusCode::kNotFound, - util::Status(util::StatusCode::kNotFound, "").code()); - EXPECT_EQ(util::StatusCode::kAlreadyExists, - util::Status(util::StatusCode::kAlreadyExists, "").code()); - EXPECT_EQ(util::StatusCode::kPermissionDenied, - util::Status(util::StatusCode::kPermissionDenied, "").code()); - EXPECT_EQ(util::StatusCode::kUnauthenticated, - util::Status(util::StatusCode::kUnauthenticated, "").code()); - EXPECT_EQ(util::StatusCode::kResourceExhausted, - util::Status(util::StatusCode::kResourceExhausted, "").code()); - EXPECT_EQ(util::StatusCode::kFailedPrecondition, - util::Status(util::StatusCode::kFailedPrecondition, "").code()); - EXPECT_EQ(util::StatusCode::kAborted, - util::Status(util::StatusCode::kAborted, "").code()); - EXPECT_EQ(util::StatusCode::kOutOfRange, - util::Status(util::StatusCode::kOutOfRange, "").code()); - EXPECT_EQ(util::StatusCode::kUnimplemented, - util::Status(util::StatusCode::kUnimplemented, "").code()); - EXPECT_EQ(util::StatusCode::kInternal, - util::Status(util::StatusCode::kInternal, "").code()); - EXPECT_EQ(util::StatusCode::kUnavailable, - util::Status(util::StatusCode::kUnavailable, "").code()); - EXPECT_EQ(util::StatusCode::kDataLoss, - util::Status(util::StatusCode::kDataLoss, "").code()); -} - -TEST(Status, ConstructorZero) { - util::Status status(util::StatusCode::kOk, "msg"); - EXPECT_TRUE(status.ok()); - EXPECT_EQ("OK", status.ToString()); - EXPECT_EQ(util::OkStatus(), status); -} - -TEST(Status, ConvenienceConstructors) { - EXPECT_EQ(util::StatusCode::kOk, util::OkStatus().code()); - EXPECT_EQ("", util::OkStatus().message()); - - EXPECT_EQ(util::StatusCode::kCancelled, util::CancelledError("").code()); - EXPECT_EQ("", util::CancelledError("").message()); - EXPECT_EQ("foo", util::CancelledError("foo").message()); - EXPECT_EQ("bar", util::CancelledError("bar").message()); - - EXPECT_EQ(util::StatusCode::kUnknown, util::UnknownError("").code()); - EXPECT_EQ("", util::UnknownError("").message()); - EXPECT_EQ("foo", util::UnknownError("foo").message()); - EXPECT_EQ("bar", util::UnknownError("bar").message()); - - EXPECT_EQ(util::StatusCode::kInvalidArgument, - util::InvalidArgumentError("").code()); - EXPECT_EQ("", util::InvalidArgumentError("").message()); - EXPECT_EQ("foo", util::InvalidArgumentError("foo").message()); - EXPECT_EQ("bar", util::InvalidArgumentError("bar").message()); - - EXPECT_EQ(util::StatusCode::kDeadlineExceeded, - util::DeadlineExceededError("").code()); - EXPECT_EQ("", util::DeadlineExceededError("").message()); - EXPECT_EQ("foo", util::DeadlineExceededError("foo").message()); - EXPECT_EQ("bar", util::DeadlineExceededError("bar").message()); - - EXPECT_EQ(util::StatusCode::kNotFound, util::NotFoundError("").code()); - EXPECT_EQ("", util::NotFoundError("").message()); - EXPECT_EQ("foo", util::NotFoundError("foo").message()); - EXPECT_EQ("bar", util::NotFoundError("bar").message()); - - EXPECT_EQ(util::StatusCode::kAlreadyExists, - util::AlreadyExistsError("").code()); - EXPECT_EQ("", util::AlreadyExistsError("").message()); - EXPECT_EQ("foo", util::AlreadyExistsError("foo").message()); - EXPECT_EQ("bar", util::AlreadyExistsError("bar").message()); - - EXPECT_EQ(util::StatusCode::kPermissionDenied, - util::PermissionDeniedError("").code()); - EXPECT_EQ("", util::PermissionDeniedError("").message()); - EXPECT_EQ("foo", util::PermissionDeniedError("foo").message()); - EXPECT_EQ("bar", util::PermissionDeniedError("bar").message()); - - EXPECT_EQ(util::StatusCode::kUnauthenticated, - util::UnauthenticatedError("").code()); - EXPECT_EQ("", util::UnauthenticatedError("").message()); - EXPECT_EQ("foo", util::UnauthenticatedError("foo").message()); - EXPECT_EQ("bar", util::UnauthenticatedError("bar").message()); - - EXPECT_EQ(util::StatusCode::kResourceExhausted, - util::ResourceExhaustedError("").code()); - EXPECT_EQ("", util::ResourceExhaustedError("").message()); - EXPECT_EQ("foo", util::ResourceExhaustedError("foo").message()); - EXPECT_EQ("bar", util::ResourceExhaustedError("bar").message()); - - EXPECT_EQ(util::StatusCode::kFailedPrecondition, - util::FailedPreconditionError("").code()); - EXPECT_EQ("", util::FailedPreconditionError("").message()); - EXPECT_EQ("foo", util::FailedPreconditionError("foo").message()); - EXPECT_EQ("bar", util::FailedPreconditionError("bar").message()); - - EXPECT_EQ(util::StatusCode::kAborted, util::AbortedError("").code()); - EXPECT_EQ("", util::AbortedError("").message()); - EXPECT_EQ("foo", util::AbortedError("foo").message()); - EXPECT_EQ("bar", util::AbortedError("bar").message()); - - EXPECT_EQ(util::StatusCode::kOutOfRange, util::OutOfRangeError("").code()); - EXPECT_EQ("", util::OutOfRangeError("").message()); - EXPECT_EQ("foo", util::OutOfRangeError("foo").message()); - EXPECT_EQ("bar", util::OutOfRangeError("bar").message()); - - EXPECT_EQ(util::StatusCode::kUnimplemented, - util::UnimplementedError("").code()); - EXPECT_EQ("", util::UnimplementedError("").message()); - EXPECT_EQ("foo", util::UnimplementedError("foo").message()); - EXPECT_EQ("bar", util::UnimplementedError("bar").message()); - - EXPECT_EQ(util::StatusCode::kInternal, util::InternalError("").code()); - EXPECT_EQ("", util::InternalError("").message()); - EXPECT_EQ("foo", util::InternalError("foo").message()); - EXPECT_EQ("bar", util::InternalError("bar").message()); - - EXPECT_EQ(util::StatusCode::kUnavailable, util::UnavailableError("").code()); - EXPECT_EQ("", util::UnavailableError("").message()); - EXPECT_EQ("foo", util::UnavailableError("foo").message()); - EXPECT_EQ("bar", util::UnavailableError("bar").message()); - - EXPECT_EQ(util::StatusCode::kDataLoss, util::DataLossError("").code()); - EXPECT_EQ("", util::DataLossError("").message()); - EXPECT_EQ("foo", util::DataLossError("foo").message()); - EXPECT_EQ("bar", util::DataLossError("bar").message()); -} - -TEST(Status, ConvenienceTests) { - EXPECT_TRUE(util::OkStatus().ok()); - EXPECT_TRUE(util::IsCancelled(util::CancelledError(""))); - EXPECT_TRUE(util::IsUnknown(util::UnknownError(""))); - EXPECT_TRUE(util::IsInvalidArgument(util::InvalidArgumentError(""))); - EXPECT_TRUE(util::IsDeadlineExceeded(util::DeadlineExceededError(""))); - EXPECT_TRUE(util::IsNotFound(util::NotFoundError(""))); - EXPECT_TRUE(util::IsAlreadyExists(util::AlreadyExistsError(""))); - EXPECT_TRUE(util::IsPermissionDenied(util::PermissionDeniedError(""))); - EXPECT_TRUE(util::IsUnauthenticated(util::UnauthenticatedError(""))); - EXPECT_TRUE(util::IsResourceExhausted(util::ResourceExhaustedError(""))); - EXPECT_TRUE(util::IsFailedPrecondition(util::FailedPreconditionError(""))); - EXPECT_TRUE(util::IsAborted(util::AbortedError(""))); - EXPECT_TRUE(util::IsOutOfRange(util::OutOfRangeError(""))); - EXPECT_TRUE(util::IsUnimplemented(util::UnimplementedError(""))); - EXPECT_TRUE(util::IsInternal(util::InternalError(""))); - EXPECT_TRUE(util::IsUnavailable(util::UnavailableError(""))); - EXPECT_TRUE(util::IsDataLoss(util::DataLossError(""))); -} - -TEST(Status, Empty) { - util::Status status; - EXPECT_TRUE(status.ok()); - EXPECT_EQ(util::OkStatus(), status); - EXPECT_EQ(util::StatusCode::kOk, status.code()); - EXPECT_EQ("OK", status.ToString()); -} - -TEST(Status, CheckOK) { - util::Status status; - GOOGLE_CHECK_OK(status); - GOOGLE_CHECK_OK(status) << "Failed"; - GOOGLE_DCHECK_OK(status) << "Failed"; -} - -TEST(Status, ErrorMessage) { - util::Status status = util::InvalidArgumentError(""); - EXPECT_FALSE(status.ok()); - EXPECT_EQ("", status.message().ToString()); - EXPECT_EQ("INVALID_ARGUMENT", status.ToString()); - status = util::InvalidArgumentError("msg"); - EXPECT_FALSE(status.ok()); - EXPECT_EQ("msg", status.message().ToString()); - EXPECT_EQ("INVALID_ARGUMENT:msg", status.ToString()); - status = util::Status(util::StatusCode::kOk, "msg"); - EXPECT_TRUE(status.ok()); - EXPECT_EQ("", status.message().ToString()); - EXPECT_EQ("OK", status.ToString()); -} - -TEST(Status, Copy) { - util::Status a = util::UnknownError("message"); - util::Status b(a); - ASSERT_EQ(a.ToString(), b.ToString()); -} - -TEST(Status, Assign) { - util::Status a = util::UnknownError("message"); - util::Status b; - b = a; - ASSERT_EQ(a.ToString(), b.ToString()); -} - -TEST(Status, AssignEmpty) { - util::Status a = util::UnknownError("message"); - util::Status b; - a = b; - ASSERT_EQ(std::string("OK"), a.ToString()); - ASSERT_TRUE(b.ok()); - ASSERT_TRUE(a.ok()); -} - -TEST(Status, EqualsOK) { ASSERT_EQ(util::OkStatus(), util::Status()); } - -TEST(Status, EqualsSame) { - const util::Status a = util::CancelledError("message"); - const util::Status b = util::CancelledError("message"); - ASSERT_EQ(a, b); -} - -TEST(Status, EqualsCopy) { - const util::Status a = util::CancelledError("message"); - const util::Status b = a; - ASSERT_EQ(a, b); -} - -TEST(Status, EqualsDifferentCode) { - const util::Status a = util::CancelledError("message"); - const util::Status b = util::UnknownError("message"); - ASSERT_NE(a, b); -} - -TEST(Status, EqualsDifferentMessage) { - const util::Status a = util::CancelledError("message"); - const util::Status b = util::CancelledError("another"); - ASSERT_NE(a, b); -} - -} // namespace -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/stubs/statusor.cc b/src/google/protobuf/stubs/statusor.cc deleted file mode 100644 index 9c0a1782a839..000000000000 --- a/src/google/protobuf/stubs/statusor.cc +++ /dev/null @@ -1,48 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#include - -#include - -namespace google { -namespace protobuf { -namespace util { -namespace statusor_internal { - -void StatusOrHelper::Crash(const Status& status) { - GOOGLE_LOG(FATAL) << "Attempting to fetch value instead of handling error " - << status.ToString(); -} - -} // namespace statusor_internal -} // namespace util -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/stubs/statusor.h b/src/google/protobuf/stubs/statusor.h deleted file mode 100644 index 1ec748d1fca8..000000000000 --- a/src/google/protobuf/stubs/statusor.h +++ /dev/null @@ -1,267 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// StatusOr is the union of a Status object and a T -// object. StatusOr models the concept of an object that is either a -// usable value, or an error Status explaining why such a value is -// not present. To this end, StatusOr does not allow its Status -// value to be OkStatus(). Further, StatusOr does not allow the -// contained pointer to be nullptr. -// -// The primary use-case for StatusOr is as the return value of a -// function which may fail. -// -// Example client usage for a StatusOr, where T is not a pointer: -// -// StatusOr result = DoBigCalculationThatCouldFail(); -// if (result.ok()) { -// float answer = result.value(); -// printf("Big calculation yielded: %f", answer); -// } else { -// LOG(ERROR) << result.status(); -// } -// -// Example client usage for a StatusOr: -// -// StatusOr result = FooFactory::MakeNewFoo(arg); -// if (result.ok()) { -// std::unique_ptr foo(result.value()); -// foo->DoSomethingCool(); -// } else { -// LOG(ERROR) << result.status(); -// } -// -// Example factory implementation returning StatusOr: -// -// StatusOr FooFactory::MakeNewFoo(int arg) { -// if (arg <= 0) { -// return InvalidArgumentError("Arg must be positive"); -// } else { -// return new Foo(arg); -// } -// } -// - -#ifndef GOOGLE_PROTOBUF_STUBS_STATUSOR_H_ -#define GOOGLE_PROTOBUF_STUBS_STATUSOR_H_ - -#include - -#include -#include -#include - -// Must be included last. -#include - -namespace google { -namespace protobuf { -namespace util { -namespace statusor_internal { - -template -class StatusOr { - template - friend class StatusOr; - - public: - using value_type = T; - - // Construct a new StatusOr with Status::UNKNOWN status. - // Construct a new StatusOr with UnknownError() status. - explicit StatusOr(); - - // Construct a new StatusOr with the given non-ok status. After calling - // this constructor, calls to value() will CHECK-fail. - // - // NOTE: Not explicit - we want to use StatusOr as a return - // value, so it is convenient and sensible to be able to do 'return - // Status()' when the return type is StatusOr. - // - // REQUIRES: status != OkStatus(). This requirement is DCHECKed. - // In optimized builds, passing OkStatus() here will have the effect - // of passing PosixErrorSpace::EINVAL as a fallback. - StatusOr(const Status& status); // NOLINT - - // Construct a new StatusOr with the given value. If T is a plain pointer, - // value must not be nullptr. After calling this constructor, calls to - // value() will succeed, and calls to status() will return OK. - // - // NOTE: Not explicit - we want to use StatusOr as a return type - // so it is convenient and sensible to be able to do 'return T()' - // when when the return type is StatusOr. - // - // REQUIRES: if T is a plain pointer, value != nullptr. This requirement is - // DCHECKed. In optimized builds, passing a null pointer here will have - // the effect of passing PosixErrorSpace::EINVAL as a fallback. - StatusOr(const T& value); // NOLINT - - // Copy constructor. - StatusOr(const StatusOr& other); - - // Conversion copy constructor, T must be copy constructible from U - template - StatusOr(const StatusOr& other); - - // Assignment operator. - StatusOr& operator=(const StatusOr& other); - - // Conversion assignment operator, T must be assignable from U - template - StatusOr& operator=(const StatusOr& other); - - // Returns a reference to our status. If this contains a T, then - // returns OkStatus(). - const Status& status() const; - - // Returns this->status().ok() - bool ok() const; - - // Returns a reference to our current value, or CHECK-fails if !this->ok(). - const T& value() const; - T& value(); - - // Returns a reference to our current value; UB if not OK. - const T& operator*() const { return value(); } - T& operator*() { return value(); } - const T* operator->() const { return &value(); } - T* operator->() { return &value(); } - - private: - Status status_; - T value_; -}; - -//////////////////////////////////////////////////////////////////////////////// -// Implementation details for StatusOr - -class PROTOBUF_EXPORT StatusOrHelper { - public: - // Move type-agnostic error handling to the .cc. - static void Crash(const util::Status& status); - - // Customized behavior for StatusOr vs. StatusOr - template - struct Specialize; -}; - -template -struct StatusOrHelper::Specialize { - // For non-pointer T, a reference can never be nullptr. - static inline bool IsValueNull(const T& /*t*/) { return false; } -}; - -template -struct StatusOrHelper::Specialize { - static inline bool IsValueNull(const T* t) { return t == nullptr; } -}; - -template -inline StatusOr::StatusOr() : status_(util::UnknownError("")) {} - -template -inline StatusOr::StatusOr(const Status& status) { - if (status.ok()) { - status_ = util::InternalError("OkStatus() is not a valid argument."); - } else { - status_ = status; - } -} - -template -inline StatusOr::StatusOr(const T& value) { - if (StatusOrHelper::Specialize::IsValueNull(value)) { - status_ = util::InternalError("nullptr is not a valid argument."); - } else { - status_ = util::OkStatus(); - value_ = value; - } -} - -template -inline StatusOr::StatusOr(const StatusOr& other) - : status_(other.status_), value_(other.value_) {} - -template -inline StatusOr& StatusOr::operator=(const StatusOr& other) { - status_ = other.status_; - value_ = other.value_; - return *this; -} - -template -template -inline StatusOr::StatusOr(const StatusOr& other) - : status_(other.status_), value_(other.status_.ok() ? other.value_ : T()) {} - -template -template -inline StatusOr& StatusOr::operator=(const StatusOr& other) { - status_ = other.status_; - if (status_.ok()) value_ = other.value_; - return *this; -} - -template -inline const Status& StatusOr::status() const { - return status_; -} - -template -inline bool StatusOr::ok() const { - return status().ok(); -} - -template -inline const T& StatusOr::value() const { - if (!status_.ok()) { - StatusOrHelper::Crash(status_); - } - return value_; -} - -template -inline T& StatusOr::value() { - if (!status_.ok()) { - StatusOrHelper::Crash(status_); - } - return value_; -} -} // namespace statusor_internal - -using ::google::protobuf::util::statusor_internal::StatusOr; - -} // namespace util -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_STATUSOR_H_ diff --git a/src/google/protobuf/stubs/statusor_test.cc b/src/google/protobuf/stubs/statusor_test.cc deleted file mode 100644 index 17c9a4dee27d..000000000000 --- a/src/google/protobuf/stubs/statusor_test.cc +++ /dev/null @@ -1,282 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#include - -#include -#include - -#include -#include - -namespace google { -namespace protobuf { -namespace util { -namespace { - -class Base1 { - public: - virtual ~Base1() {} - int pad; -}; - -class Base2 { - public: - virtual ~Base2() {} - int yetotherpad; -}; - -class Derived : public Base1, public Base2 { - public: - virtual ~Derived() {} - int evenmorepad; -}; - -class CopyNoAssign { - public: - explicit CopyNoAssign(int value) : foo(value) {} - CopyNoAssign(const CopyNoAssign& other) : foo(other.foo) {} - int foo; - private: - const CopyNoAssign& operator=(const CopyNoAssign&); -}; - -TEST(StatusOr, TestDefaultCtor) { - StatusOr thing; - EXPECT_FALSE(thing.ok()); - EXPECT_EQ(util::UnknownError(""), thing.status()); -} - -TEST(StatusOr, TestStatusCtor) { - StatusOr thing(util::CancelledError("")); - EXPECT_FALSE(thing.ok()); - EXPECT_EQ(util::CancelledError(""), thing.status()); -} - -TEST(StatusOr, TestValueCtor) { - const int kI = 4; - StatusOr thing(kI); - EXPECT_TRUE(thing.ok()); - EXPECT_EQ(kI, thing.value()); -} - -TEST(StatusOr, TestPtrOps) { - const int kI = 4; - StatusOr thing(kI); - EXPECT_TRUE(thing.ok()); - EXPECT_EQ(kI, *thing); - - StatusOr> thing2(thing); - EXPECT_EQ(kI, thing2->value()); -} - -TEST(StatusOr, TestCopyCtorStatusOk) { - const int kI = 4; - StatusOr original(kI); - StatusOr copy(original); - EXPECT_EQ(original.status(), copy.status()); - EXPECT_EQ(original.value(), copy.value()); -} - -TEST(StatusOr, TestCopyCtorStatusNotOk) { - StatusOr original(util::CancelledError("")); - StatusOr copy(original); - EXPECT_EQ(original.status(), copy.status()); -} - -TEST(StatusOr, TestCopyCtorStatusOKConverting) { - const int kI = 4; - StatusOr original(kI); - StatusOr copy(original); - EXPECT_EQ(original.status(), copy.status()); - EXPECT_EQ(original.value(), copy.value()); -} - -TEST(StatusOr, TestCopyCtorStatusNotOkConverting) { - StatusOr original(util::CancelledError("")); - StatusOr copy(original); - EXPECT_EQ(original.status(), copy.status()); -} - -TEST(StatusOr, TestAssignmentStatusOk) { - const int kI = 4; - StatusOr source(kI); - StatusOr target; - target = source; - EXPECT_EQ(source.status(), target.status()); - EXPECT_EQ(source.value(), target.value()); -} - -TEST(StatusOr, TestAssignmentStatusNotOk) { - StatusOr source(util::CancelledError("")); - StatusOr target; - target = source; - EXPECT_EQ(source.status(), target.status()); -} - -TEST(StatusOr, TestAssignmentStatusOKConverting) { - const int kI = 4; - StatusOr source(kI); - StatusOr target; - target = source; - EXPECT_EQ(source.status(), target.status()); - EXPECT_DOUBLE_EQ(source.value(), target.value()); -} - -TEST(StatusOr, TestAssignmentStatusNotOkConverting) { - StatusOr source(util::CancelledError("")); - StatusOr target; - target = source; - EXPECT_EQ(source.status(), target.status()); -} - -TEST(StatusOr, TestStatus) { - StatusOr good(4); - EXPECT_TRUE(good.ok()); - StatusOr bad(util::CancelledError("")); - EXPECT_FALSE(bad.ok()); - EXPECT_EQ(util::CancelledError(""), bad.status()); -} - -TEST(StatusOr, TestValue) { - const int kI = 4; - StatusOr thing(kI); - EXPECT_EQ(kI, thing.value()); -} - -TEST(StatusOr, TestValueConst) { - const int kI = 4; - const StatusOr thing(kI); - EXPECT_EQ(kI, thing.value()); -} - -TEST(StatusOr, TestPointerDefaultCtor) { - StatusOr thing; - EXPECT_FALSE(thing.ok()); - EXPECT_EQ(util::UnknownError(""), thing.status()); -} - -TEST(StatusOr, TestPointerStatusCtor) { - StatusOr thing(util::CancelledError("")); - EXPECT_FALSE(thing.ok()); - EXPECT_EQ(util::CancelledError(""), thing.status()); -} - -TEST(StatusOr, TestPointerValueCtor) { - const int kI = 4; - StatusOr thing(&kI); - EXPECT_TRUE(thing.ok()); - EXPECT_EQ(&kI, thing.value()); -} - -TEST(StatusOr, TestPointerCopyCtorStatusOk) { - const int kI = 0; - StatusOr original(&kI); - StatusOr copy(original); - EXPECT_EQ(original.status(), copy.status()); - EXPECT_EQ(original.value(), copy.value()); -} - -TEST(StatusOr, TestPointerCopyCtorStatusNotOk) { - StatusOr original(util::CancelledError("")); - StatusOr copy(original); - EXPECT_EQ(original.status(), copy.status()); -} - -TEST(StatusOr, TestPointerCopyCtorStatusOKConverting) { - Derived derived; - StatusOr original(&derived); - StatusOr copy(original); - EXPECT_EQ(original.status(), copy.status()); - EXPECT_EQ(static_cast(original.value()), copy.value()); -} - -TEST(StatusOr, TestPointerCopyCtorStatusNotOkConverting) { - StatusOr original(util::CancelledError("")); - StatusOr copy(original); - EXPECT_EQ(original.status(), copy.status()); -} - -TEST(StatusOr, TestPointerAssignmentStatusOk) { - const int kI = 0; - StatusOr source(&kI); - StatusOr target; - target = source; - EXPECT_EQ(source.status(), target.status()); - EXPECT_EQ(source.value(), target.value()); -} - -TEST(StatusOr, TestPointerAssignmentStatusNotOk) { - StatusOr source(util::CancelledError("")); - StatusOr target; - target = source; - EXPECT_EQ(source.status(), target.status()); -} - -TEST(StatusOr, TestPointerAssignmentStatusOKConverting) { - Derived derived; - StatusOr source(&derived); - StatusOr target; - target = source; - EXPECT_EQ(source.status(), target.status()); - EXPECT_EQ(static_cast(source.value()), target.value()); -} - -TEST(StatusOr, TestPointerAssignmentStatusNotOkConverting) { - StatusOr source(util::CancelledError("")); - StatusOr target; - target = source; - EXPECT_EQ(source.status(), target.status()); -} - -TEST(StatusOr, TestPointerStatus) { - const int kI = 0; - StatusOr good(&kI); - EXPECT_TRUE(good.ok()); - StatusOr bad(util::CancelledError("")); - EXPECT_EQ(util::CancelledError(""), bad.status()); -} - -TEST(StatusOr, TestPointerValue) { - const int kI = 0; - StatusOr thing(&kI); - EXPECT_EQ(&kI, thing.value()); -} - -TEST(StatusOr, TestPointerValueConst) { - const int kI = 0; - const StatusOr thing(&kI); - EXPECT_EQ(&kI, thing.value()); -} - -} // namespace -} // namespace util -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/stubs/stringpiece.cc b/src/google/protobuf/stubs/stringpiece.cc deleted file mode 100644 index 71880464c747..000000000000 --- a/src/google/protobuf/stubs/stringpiece.cc +++ /dev/null @@ -1,256 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include - -#include -#include -#include -#include -#include - -#include - -namespace google { -namespace protobuf { -namespace stringpiece_internal { - -std::ostream& operator<<(std::ostream& o, StringPiece piece) { - o.write(piece.data(), piece.size()); - return o; -} - -void StringPiece::LogFatalSizeTooBig(size_t size, const char* details) { - GOOGLE_LOG(FATAL) << "size too big: " << size << " details: " << details; -} - -void StringPiece::CopyToString(std::string* target) const { - target->assign(ptr_, length_); -} - -void StringPiece::AppendToString(std::string* target) const { - target->append(ptr_, length_); -} - -bool StringPiece::Consume(StringPiece x) { - if (starts_with(x)) { - ptr_ += x.length_; - length_ -= x.length_; - return true; - } - return false; -} - -bool StringPiece::ConsumeFromEnd(StringPiece x) { - if (ends_with(x)) { - length_ -= x.length_; - return true; - } - return false; -} - -StringPiece::size_type StringPiece::copy(char* buf, size_type n, - size_type pos) const { - size_type ret = std::min(length_ - pos, n); - memcpy(buf, ptr_ + pos, ret); - return ret; -} - -bool StringPiece::contains(StringPiece s) const { - return find(s, 0) != npos; -} - -StringPiece::size_type StringPiece::find(StringPiece s, size_type pos) const { - if (length_ <= 0 || pos > static_cast(length_)) { - if (length_ == 0 && pos == 0 && s.length_ == 0) return 0; - return npos; - } - const char *result = std::search(ptr_ + pos, ptr_ + length_, - s.ptr_, s.ptr_ + s.length_); - return result == ptr_ + length_ ? npos : result - ptr_; -} - -StringPiece::size_type StringPiece::find(char c, size_type pos) const { - if (length_ <= 0 || pos >= static_cast(length_)) { - return npos; - } - const char* result = static_cast( - memchr(ptr_ + pos, c, length_ - pos)); - return result != nullptr ? result - ptr_ : npos; -} - -StringPiece::size_type StringPiece::rfind(StringPiece s, size_type pos) const { - if (length_ < s.length_) return npos; - const size_t ulen = length_; - if (s.length_ == 0) return std::min(ulen, pos); - - const char* last = ptr_ + std::min(ulen - s.length_, pos) + s.length_; - const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); - return result != last ? result - ptr_ : npos; -} - -// Search range is [0..pos] inclusive. If pos == npos, search everything. -StringPiece::size_type StringPiece::rfind(char c, size_type pos) const { - // Note: memrchr() is not available on Windows. - if (empty()) return npos; - for (size_type i = std::min(pos, length_ - 1);; --i) { - if (ptr_[i] == c) { - return i; - } - if (i == 0) break; - } - return npos; -} - -// For each character in characters_wanted, sets the index corresponding -// to the ASCII code of that character to 1 in table. This is used by -// the find_.*_of methods below to tell whether or not a character is in -// the lookup table in constant time. -// The argument `table' must be an array that is large enough to hold all -// the possible values of an unsigned char. Thus it should be be declared -// as follows: -// bool table[UCHAR_MAX + 1] -static inline void BuildLookupTable(StringPiece characters_wanted, - bool* table) { - const StringPiece::size_type length = characters_wanted.length(); - const char* const data = characters_wanted.data(); - for (StringPiece::size_type i = 0; i < length; ++i) { - table[static_cast(data[i])] = true; - } -} - -StringPiece::size_type StringPiece::find_first_of(StringPiece s, - size_type pos) const { - if (empty() || s.empty()) { - return npos; - } - // Avoid the cost of BuildLookupTable() for a single-character search. - if (s.length_ == 1) return find_first_of(s.ptr_[0], pos); - - bool lookup[UCHAR_MAX + 1] = { false }; - BuildLookupTable(s, lookup); - for (size_type i = pos; i < length_; ++i) { - if (lookup[static_cast(ptr_[i])]) { - return i; - } - } - return npos; -} - -StringPiece::size_type StringPiece::find_first_not_of(StringPiece s, - size_type pos) const { - if (empty()) return npos; - if (s.empty()) return 0; - // Avoid the cost of BuildLookupTable() for a single-character search. - if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos); - - bool lookup[UCHAR_MAX + 1] = { false }; - BuildLookupTable(s, lookup); - for (size_type i = pos; i < length_; ++i) { - if (!lookup[static_cast(ptr_[i])]) { - return i; - } - } - return npos; -} - -StringPiece::size_type StringPiece::find_first_not_of(char c, - size_type pos) const { - if (empty()) return npos; - - for (; pos < static_cast(length_); ++pos) { - if (ptr_[pos] != c) { - return pos; - } - } - return npos; -} - -StringPiece::size_type StringPiece::find_last_of(StringPiece s, - size_type pos) const { - if (empty() || s.empty()) return npos; - // Avoid the cost of BuildLookupTable() for a single-character search. - if (s.length_ == 1) return find_last_of(s.ptr_[0], pos); - - bool lookup[UCHAR_MAX + 1] = { false }; - BuildLookupTable(s, lookup); - for (size_type i = std::min(pos, length_ - 1);; --i) { - if (lookup[static_cast(ptr_[i])]) { - return i; - } - if (i == 0) break; - } - return npos; -} - -StringPiece::size_type StringPiece::find_last_not_of(StringPiece s, - size_type pos) const { - if (empty()) return npos; - - size_type i = std::min(pos, length() - 1); - if (s.empty()) return i; - - // Avoid the cost of BuildLookupTable() for a single-character search. - if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos); - - bool lookup[UCHAR_MAX + 1] = { false }; - BuildLookupTable(s, lookup); - for (;; --i) { - if (!lookup[static_cast(ptr_[i])]) { - return i; - } - if (i == 0) break; - } - return npos; -} - -StringPiece::size_type StringPiece::find_last_not_of(char c, - size_type pos) const { - if (empty()) return npos; - size_type i = std::min(pos, length_ - 1); - for (;; --i) { - if (ptr_[i] != c) { - return i; - } - if (i == 0) break; - } - return npos; -} - -StringPiece StringPiece::substr(size_type pos, size_type n) const { - if (pos > length()) pos = length(); - if (n > length_ - pos) n = length() - pos; - return StringPiece(ptr_ + pos, n); -} - -const StringPiece::size_type StringPiece::npos = size_type(-1); - -} // namespace stringpiece_internal -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/stubs/stringpiece.h b/src/google/protobuf/stubs/stringpiece.h deleted file mode 100644 index c63e25b2549a..000000000000 --- a/src/google/protobuf/stubs/stringpiece.h +++ /dev/null @@ -1,402 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// A StringPiece points to part or all of a string, Cord, double-quoted string -// literal, or other string-like object. A StringPiece does *not* own the -// string to which it points. A StringPiece is not null-terminated. -// -// You can use StringPiece as a function or method parameter. A StringPiece -// parameter can receive a double-quoted string literal argument, a "const -// char*" argument, a string argument, or a StringPiece argument with no data -// copying. Systematic use of StringPiece for arguments reduces data -// copies and strlen() calls. -// -// Prefer passing StringPieces by value: -// void MyFunction(StringPiece arg); -// If circumstances require, you may also pass by const reference: -// void MyFunction(const StringPiece& arg); // not preferred -// Both of these have the same lifetime semantics. Passing by value -// generates slightly smaller code. For more discussion, see the thread -// go/stringpiecebyvalue on c-users. -// -// StringPiece is also suitable for local variables if you know that -// the lifetime of the underlying object is longer than the lifetime -// of your StringPiece variable. -// -// Beware of binding a StringPiece to a temporary: -// StringPiece sp = obj.MethodReturningString(); // BAD: lifetime problem -// -// This code is okay: -// string str = obj.MethodReturningString(); // str owns its contents -// StringPiece sp(str); // GOOD, because str outlives sp -// -// StringPiece is sometimes a poor choice for a return value and usually a poor -// choice for a data member. If you do use a StringPiece this way, it is your -// responsibility to ensure that the object pointed to by the StringPiece -// outlives the StringPiece. -// -// A StringPiece may represent just part of a string; thus the name "Piece". -// For example, when splitting a string, vector is a natural data -// type for the output. For another example, a Cord is a non-contiguous, -// potentially very long string-like object. The Cord class has an interface -// that iteratively provides StringPiece objects that point to the -// successive pieces of a Cord object. -// -// A StringPiece is not null-terminated. If you write code that scans a -// StringPiece, you must check its length before reading any characters. -// Common idioms that work on null-terminated strings do not work on -// StringPiece objects. -// -// There are several ways to create a null StringPiece: -// StringPiece() -// StringPiece(nullptr) -// StringPiece(nullptr, 0) -// For all of the above, sp.data() == nullptr, sp.length() == 0, -// and sp.empty() == true. Also, if you create a StringPiece with -// a non-null pointer then sp.data() != nullptr. Once created, -// sp.data() will stay either nullptr or not-nullptr, except if you call -// sp.clear() or sp.set(). -// -// Thus, you can use StringPiece(nullptr) to signal an out-of-band value -// that is different from other StringPiece values. This is similar -// to the way that const char* p1 = nullptr; is different from -// const char* p2 = "";. -// -// There are many ways to create an empty StringPiece: -// StringPiece() -// StringPiece(nullptr) -// StringPiece(nullptr, 0) -// StringPiece("") -// StringPiece("", 0) -// StringPiece("abcdef", 0) -// StringPiece("abcdef"+6, 0) -// For all of the above, sp.length() will be 0 and sp.empty() will be true. -// For some empty StringPiece values, sp.data() will be nullptr. -// For some empty StringPiece values, sp.data() will not be nullptr. -// -// Be careful not to confuse: null StringPiece and empty StringPiece. -// The set of empty StringPieces properly includes the set of null StringPieces. -// That is, every null StringPiece is an empty StringPiece, -// but some non-null StringPieces are empty Stringpieces too. -// -// All empty StringPiece values compare equal to each other. -// Even a null StringPieces compares equal to a non-null empty StringPiece: -// StringPiece() == StringPiece("", 0) -// StringPiece(nullptr) == StringPiece("abc", 0) -// StringPiece(nullptr, 0) == StringPiece("abcdef"+6, 0) -// -// Look carefully at this example: -// StringPiece("") == nullptr -// True or false? TRUE, because StringPiece::operator== converts -// the right-hand side from nullptr to StringPiece(nullptr), -// and then compares two zero-length spans of characters. -// However, we are working to make this example produce a compile error. -// -// Suppose you want to write: -// bool TestWhat?(StringPiece sp) { return sp == nullptr; } // BAD -// Do not do that. Write one of these instead: -// bool TestNull(StringPiece sp) { return sp.data() == nullptr; } -// bool TestEmpty(StringPiece sp) { return sp.empty(); } -// The intent of TestWhat? is unclear. Did you mean TestNull or TestEmpty? -// Right now, TestWhat? behaves likes TestEmpty. -// We are working to make TestWhat? produce a compile error. -// TestNull is good to test for an out-of-band signal. -// TestEmpty is good to test for an empty StringPiece. -// -// Caveats (again): -// (1) The lifetime of the pointed-to string (or piece of a string) -// must be longer than the lifetime of the StringPiece. -// (2) There may or may not be a '\0' character after the end of -// StringPiece data. -// (3) A null StringPiece is empty. -// An empty StringPiece may or may not be a null StringPiece. - -#ifndef GOOGLE_PROTOBUF_STUBS_STRINGPIECE_H_ -#define GOOGLE_PROTOBUF_STUBS_STRINGPIECE_H_ - -#include -#include -#include -#include -#include -#include - -#if defined(__cpp_lib_string_view) -#include -#endif - -#include - -#include - -namespace google { -namespace protobuf { -namespace stringpiece_internal { - -class PROTOBUF_EXPORT StringPiece { - public: - using traits_type = std::char_traits; - using value_type = char; - using pointer = char*; - using const_pointer = const char*; - using reference = char&; - using const_reference = const char&; - using const_iterator = const char*; - using iterator = const_iterator; - using const_reverse_iterator = std::reverse_iterator; - using reverse_iterator = const_reverse_iterator; - using size_type = size_t; - using difference_type = std::ptrdiff_t; - - private: - const char* ptr_; - size_type length_; - - static constexpr size_type kMaxSize = - (std::numeric_limits::max)(); - - static size_type CheckSize(size_type size) { -#if !defined(NDEBUG) || defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 - if (PROTOBUF_PREDICT_FALSE(size > kMaxSize)) { - // Some people grep for this message in logs - // so take care if you ever change it. - LogFatalSizeTooBig(size, "string length exceeds max size"); - } -#endif - return size; - } - - // Out-of-line error path. - static void LogFatalSizeTooBig(size_type size, const char* details); - - public: - // We provide non-explicit singleton constructors so users can pass - // in a "const char*" or a "string" wherever a "StringPiece" is - // expected. - // - // Style guide exception granted: - // http://goto/style-guide-exception-20978288 - StringPiece() : ptr_(nullptr), length_(0) {} - - StringPiece(const char* str) // NOLINT(runtime/explicit) - : ptr_(str), length_(0) { - if (str != nullptr) { - length_ = CheckSize(strlen(str)); - } - } - - template - StringPiece( // NOLINT(runtime/explicit) - const std::basic_string, Allocator>& str) - : ptr_(str.data()), length_(0) { - length_ = CheckSize(str.size()); - } - -#if defined(__cpp_lib_string_view) - StringPiece( // NOLINT(runtime/explicit) - std::string_view str) - : ptr_(str.data()), length_(0) { - length_ = CheckSize(str.size()); - } -#endif - - StringPiece(const char* offset, size_type len) - : ptr_(offset), length_(CheckSize(len)) {} - - // data() may return a pointer to a buffer with embedded NULs, and the - // returned buffer may or may not be null terminated. Therefore it is - // typically a mistake to pass data() to a routine that expects a NUL - // terminated string. - const_pointer data() const { return ptr_; } - size_type size() const { return length_; } - size_type length() const { return length_; } - bool empty() const { return length_ == 0; } - - char operator[](size_type i) const { - assert(i < length_); - return ptr_[i]; - } - - void remove_prefix(size_type n) { - assert(length_ >= n); - ptr_ += n; - length_ -= n; - } - - void remove_suffix(size_type n) { - assert(length_ >= n); - length_ -= n; - } - - // returns {-1, 0, 1} - int compare(StringPiece x) const { - size_type min_size = length_ < x.length_ ? length_ : x.length_; - int r = memcmp(ptr_, x.ptr_, static_cast(min_size)); - if (r < 0) return -1; - if (r > 0) return 1; - if (length_ < x.length_) return -1; - if (length_ > x.length_) return 1; - return 0; - } - - std::string as_string() const { return ToString(); } - // We also define ToString() here, since many other string-like - // interfaces name the routine that converts to a C++ string - // "ToString", and it's confusing to have the method that does that - // for a StringPiece be called "as_string()". We also leave the - // "as_string()" method defined here for existing code. - std::string ToString() const { - if (ptr_ == nullptr) return ""; - return std::string(data(), static_cast(size())); - } - - explicit operator std::string() const { return ToString(); } - - void CopyToString(std::string* target) const; - void AppendToString(std::string* target) const; - - bool starts_with(StringPiece x) const { - return (length_ >= x.length_) && - (memcmp(ptr_, x.ptr_, static_cast(x.length_)) == 0); - } - - bool ends_with(StringPiece x) const { - return ((length_ >= x.length_) && - (memcmp(ptr_ + (length_-x.length_), x.ptr_, - static_cast(x.length_)) == 0)); - } - - // Checks whether StringPiece starts with x and if so advances the beginning - // of it to past the match. It's basically a shortcut for starts_with - // followed by remove_prefix. - bool Consume(StringPiece x); - // Like above but for the end of the string. - bool ConsumeFromEnd(StringPiece x); - - // standard STL container boilerplate - static const size_type npos; - const_iterator begin() const { return ptr_; } - const_iterator end() const { return ptr_ + length_; } - const_reverse_iterator rbegin() const { - return const_reverse_iterator(ptr_ + length_); - } - const_reverse_iterator rend() const { - return const_reverse_iterator(ptr_); - } - size_type max_size() const { return length_; } - size_type capacity() const { return length_; } - - // cpplint.py emits a false positive [build/include_what_you_use] - size_type copy(char* buf, size_type n, size_type pos = 0) const; // NOLINT - - bool contains(StringPiece s) const; - - size_type find(StringPiece s, size_type pos = 0) const; - size_type find(char c, size_type pos = 0) const; - size_type rfind(StringPiece s, size_type pos = npos) const; - size_type rfind(char c, size_type pos = npos) const; - - size_type find_first_of(StringPiece s, size_type pos = 0) const; - size_type find_first_of(char c, size_type pos = 0) const { - return find(c, pos); - } - size_type find_first_not_of(StringPiece s, size_type pos = 0) const; - size_type find_first_not_of(char c, size_type pos = 0) const; - size_type find_last_of(StringPiece s, size_type pos = npos) const; - size_type find_last_of(char c, size_type pos = npos) const { - return rfind(c, pos); - } - size_type find_last_not_of(StringPiece s, size_type pos = npos) const; - size_type find_last_not_of(char c, size_type pos = npos) const; - - StringPiece substr(size_type pos, size_type n = npos) const; -}; - -// This large function is defined inline so that in a fairly common case where -// one of the arguments is a literal, the compiler can elide a lot of the -// following comparisons. -inline bool operator==(StringPiece x, StringPiece y) { - StringPiece::size_type len = x.size(); - if (len != y.size()) { - return false; - } - - return x.data() == y.data() || len <= 0 || - memcmp(x.data(), y.data(), static_cast(len)) == 0; -} - -inline bool operator!=(StringPiece x, StringPiece y) { - return !(x == y); -} - -inline bool operator<(StringPiece x, StringPiece y) { - const StringPiece::size_type min_size = - x.size() < y.size() ? x.size() : y.size(); - const int r = memcmp(x.data(), y.data(), static_cast(min_size)); - return (r < 0) || (r == 0 && x.size() < y.size()); -} - -inline bool operator>(StringPiece x, StringPiece y) { - return y < x; -} - -inline bool operator<=(StringPiece x, StringPiece y) { - return !(x > y); -} - -inline bool operator>=(StringPiece x, StringPiece y) { - return !(x < y); -} - -// allow StringPiece to be logged -extern std::ostream& operator<<(std::ostream& o, StringPiece piece); - -} // namespace stringpiece_internal - -using ::google::protobuf::stringpiece_internal::StringPiece; - -} // namespace protobuf -} // namespace google - -GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START -template<> struct hash { - size_t operator()(const StringPiece& s) const { - size_t result = 0; - for (const char *str = s.data(), *end = str + s.size(); str < end; str++) { - result = 5 * result + static_cast(*str); - } - return result; - } -}; -GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END - -#include - -#endif // STRINGS_STRINGPIECE_H_ diff --git a/src/google/protobuf/stubs/stringpiece_unittest.cc b/src/google/protobuf/stubs/stringpiece_unittest.cc deleted file mode 100644 index ba904cbf2cef..000000000000 --- a/src/google/protobuf/stubs/stringpiece_unittest.cc +++ /dev/null @@ -1,695 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include - -#include -#include -#include -#include -#include - -#include -#include -#include - -namespace google { -namespace protobuf { -namespace { -TEST(StringPiece, Ctor) { - { - // Null. - StringPiece s10; - EXPECT_TRUE(s10.data() == nullptr); - EXPECT_EQ(0, s10.length()); - } - - { - // const char* without length. - const char* hello = "hello"; - StringPiece s20(hello); - EXPECT_TRUE(s20.data() == hello); - EXPECT_EQ(5, s20.length()); - - // const char* with length. - StringPiece s21(hello, 4); - EXPECT_TRUE(s21.data() == hello); - EXPECT_EQ(4, s21.length()); - - // Not recommended, but valid C++ - StringPiece s22(hello, 6); - EXPECT_TRUE(s22.data() == hello); - EXPECT_EQ(6, s22.length()); - } - - { - // std::string. - std::string hola = "hola"; - StringPiece s30(hola); - EXPECT_TRUE(s30.data() == hola.data()); - EXPECT_EQ(4, s30.length()); - - // std::string with embedded '\0'. - hola.push_back('\0'); - hola.append("h2"); - hola.push_back('\0'); - StringPiece s31(hola); - EXPECT_TRUE(s31.data() == hola.data()); - EXPECT_EQ(8, s31.length()); - } - -#if defined(HAS_GLOBAL_STRING) - { - // ::string - std::string bonjour = "bonjour"; - StringPiece s40(bonjour); - EXPECT_TRUE(s40.data() == bonjour.data()); - EXPECT_EQ(7, s40.length()); - } -#endif - - // TODO(mec): StringPiece(StringPiece x, int pos); - // TODO(mec): StringPiece(StringPiece x, int pos, int len); - // TODO(mec): StringPiece(const StringPiece&); -} - -TEST(StringPiece, STLComparator) { - std::string s1("foo"); - std::string s2("bar"); - std::string s3("baz"); - - StringPiece p1(s1); - StringPiece p2(s2); - StringPiece p3(s3); - - typedef std::map TestMap; - TestMap map; - - map.insert(std::make_pair(p1, 0)); - map.insert(std::make_pair(p2, 1)); - map.insert(std::make_pair(p3, 2)); - EXPECT_EQ(map.size(), 3); - - TestMap::const_iterator iter = map.begin(); - EXPECT_EQ(iter->second, 1); - ++iter; - EXPECT_EQ(iter->second, 2); - ++iter; - EXPECT_EQ(iter->second, 0); - ++iter; - EXPECT_TRUE(iter == map.end()); - - TestMap::iterator new_iter = map.find("zot"); - EXPECT_TRUE(new_iter == map.end()); - - new_iter = map.find("bar"); - EXPECT_TRUE(new_iter != map.end()); - - map.erase(new_iter); - EXPECT_EQ(map.size(), 2); - - iter = map.begin(); - EXPECT_EQ(iter->second, 2); - ++iter; - EXPECT_EQ(iter->second, 0); - ++iter; - EXPECT_TRUE(iter == map.end()); -} - -TEST(StringPiece, ComparisonOperators) { -#define COMPARE(result, op, x, y) \ - EXPECT_EQ(result, StringPiece((x)) op StringPiece((y))); \ - EXPECT_EQ(result, StringPiece((x)).compare(StringPiece((y))) op 0) - - COMPARE(true, ==, "", ""); - COMPARE(true, ==, "", nullptr); - COMPARE(true, ==, nullptr, ""); - COMPARE(true, ==, "a", "a"); - COMPARE(true, ==, "aa", "aa"); - COMPARE(false, ==, "a", ""); - COMPARE(false, ==, "", "a"); - COMPARE(false, ==, "a", "b"); - COMPARE(false, ==, "a", "aa"); - COMPARE(false, ==, "aa", "a"); - - COMPARE(false, !=, "", ""); - COMPARE(false, !=, "a", "a"); - COMPARE(false, !=, "aa", "aa"); - COMPARE(true, !=, "a", ""); - COMPARE(true, !=, "", "a"); - COMPARE(true, !=, "a", "b"); - COMPARE(true, !=, "a", "aa"); - COMPARE(true, !=, "aa", "a"); - - COMPARE(true, <, "a", "b"); - COMPARE(true, <, "a", "aa"); - COMPARE(true, <, "aa", "b"); - COMPARE(true, <, "aa", "bb"); - COMPARE(false, <, "a", "a"); - COMPARE(false, <, "b", "a"); - COMPARE(false, <, "aa", "a"); - COMPARE(false, <, "b", "aa"); - COMPARE(false, <, "bb", "aa"); - - COMPARE(true, <=, "a", "a"); - COMPARE(true, <=, "a", "b"); - COMPARE(true, <=, "a", "aa"); - COMPARE(true, <=, "aa", "b"); - COMPARE(true, <=, "aa", "bb"); - COMPARE(false, <=, "b", "a"); - COMPARE(false, <=, "aa", "a"); - COMPARE(false, <=, "b", "aa"); - COMPARE(false, <=, "bb", "aa"); - - COMPARE(false, >=, "a", "b"); - COMPARE(false, >=, "a", "aa"); - COMPARE(false, >=, "aa", "b"); - COMPARE(false, >=, "aa", "bb"); - COMPARE(true, >=, "a", "a"); - COMPARE(true, >=, "b", "a"); - COMPARE(true, >=, "aa", "a"); - COMPARE(true, >=, "b", "aa"); - COMPARE(true, >=, "bb", "aa"); - - COMPARE(false, >, "a", "a"); - COMPARE(false, >, "a", "b"); - COMPARE(false, >, "a", "aa"); - COMPARE(false, >, "aa", "b"); - COMPARE(false, >, "aa", "bb"); - COMPARE(true, >, "b", "a"); - COMPARE(true, >, "aa", "a"); - COMPARE(true, >, "b", "aa"); - COMPARE(true, >, "bb", "aa"); - - std::string x; - for (int i = 0; i < 256; i++) { - x += 'a'; - std::string y = x; - COMPARE(true, ==, x, y); - for (int j = 0; j < i; j++) { - std::string z = x; - z[j] = 'b'; // Differs in position 'j' - COMPARE(false, ==, x, z); - COMPARE(true, <, x, z); - COMPARE(true, >, z, x); - if (j + 1 < i) { - z[j + 1] = 'A'; // Differs in position 'j+1' as well - COMPARE(false, ==, x, z); - COMPARE(true, <, x, z); - COMPARE(true, >, z, x); - z[j + 1] = 'z'; // Differs in position 'j+1' as well - COMPARE(false, ==, x, z); - COMPARE(true, <, x, z); - COMPARE(true, >, z, x); - } - } - } - -#undef COMPARE -} - -TEST(StringPiece, STL1) { - const StringPiece a("abcdefghijklmnopqrstuvwxyz"); - const StringPiece b("abc"); - const StringPiece c("xyz"); - const StringPiece d("foobar"); - const StringPiece e; - std::string temp("123"); - temp += '\0'; - temp += "456"; - const StringPiece f(temp); - - EXPECT_EQ(a[6], 'g'); - EXPECT_EQ(b[0], 'a'); - EXPECT_EQ(c[2], 'z'); - EXPECT_EQ(f[3], '\0'); - EXPECT_EQ(f[5], '5'); - - EXPECT_EQ(*d.data(), 'f'); - EXPECT_EQ(d.data()[5], 'r'); - EXPECT_TRUE(e.data() == nullptr); - - EXPECT_EQ(*a.begin(), 'a'); - EXPECT_EQ(*(b.begin() + 2), 'c'); - EXPECT_EQ(*(c.end() - 1), 'z'); - - EXPECT_EQ(*a.rbegin(), 'z'); - EXPECT_EQ(*(b.rbegin() + 2), 'a'); - EXPECT_EQ(*(c.rend() - 1), 'x'); - EXPECT_TRUE(a.rbegin() + 26 == a.rend()); - - EXPECT_EQ(a.size(), 26); - EXPECT_EQ(b.size(), 3); - EXPECT_EQ(c.size(), 3); - EXPECT_EQ(d.size(), 6); - EXPECT_EQ(e.size(), 0); - EXPECT_EQ(f.size(), 7); - - EXPECT_TRUE(!d.empty()); - EXPECT_TRUE(d.begin() != d.end()); - EXPECT_TRUE(d.begin() + 6 == d.end()); - - EXPECT_TRUE(e.empty()); - EXPECT_TRUE(e.begin() == e.end()); - - EXPECT_GE(a.max_size(), a.capacity()); - EXPECT_GE(a.capacity(), a.size()); - - char buf[4] = { '%', '%', '%', '%' }; - EXPECT_EQ(a.copy(buf, 4), 4); - EXPECT_EQ(buf[0], a[0]); - EXPECT_EQ(buf[1], a[1]); - EXPECT_EQ(buf[2], a[2]); - EXPECT_EQ(buf[3], a[3]); - EXPECT_EQ(a.copy(buf, 3, 7), 3); - EXPECT_EQ(buf[0], a[7]); - EXPECT_EQ(buf[1], a[8]); - EXPECT_EQ(buf[2], a[9]); - EXPECT_EQ(buf[3], a[3]); - EXPECT_EQ(c.copy(buf, 99), 3); - EXPECT_EQ(buf[0], c[0]); - EXPECT_EQ(buf[1], c[1]); - EXPECT_EQ(buf[2], c[2]); - EXPECT_EQ(buf[3], a[3]); -} - -// Separated from STL1() because some compilers produce an overly -// large stack frame for the combined function. -TEST(StringPiece, STL2) { - const StringPiece a("abcdefghijklmnopqrstuvwxyz"); - const StringPiece b("abc"); - const StringPiece c("xyz"); - const StringPiece e; - const StringPiece f("123" "\0" "456", 7); - - EXPECT_EQ(StringPiece::npos, std::string::npos); - - EXPECT_EQ(a.find(b), 0); - EXPECT_EQ(a.find(b, 1), StringPiece::npos); - EXPECT_EQ(a.find(c), 23); - EXPECT_EQ(a.find(c, 9), 23); - EXPECT_EQ(a.find(c, StringPiece::npos), StringPiece::npos); - EXPECT_EQ(b.find(c), StringPiece::npos); - EXPECT_EQ(b.find(c, StringPiece::npos), StringPiece::npos); - EXPECT_EQ(a.find(e), 0); - EXPECT_EQ(a.find(e, 17), 17); - StringPiece g("xx not found bb"); - EXPECT_EQ(a.find(g), StringPiece::npos); - // empty string nonsense - EXPECT_EQ(e.find(b), StringPiece::npos); - EXPECT_EQ(e.find(b, 7), StringPiece::npos); - - size_t empty_search_pos = std::string().find(std::string()); - EXPECT_EQ(e.find(e), empty_search_pos); - EXPECT_EQ(e.find(e, 4), std::string().find(std::string(), 4)); - - EXPECT_EQ(a.find('a'), 0); - EXPECT_EQ(a.find('c'), 2); - EXPECT_EQ(a.find('z'), 25); - EXPECT_EQ(a.find('$'), StringPiece::npos); - EXPECT_EQ(a.find('\0'), StringPiece::npos); - EXPECT_EQ(f.find('\0'), 3); - EXPECT_EQ(f.find('3'), 2); - EXPECT_EQ(f.find('5'), 5); - EXPECT_EQ(g.find('o'), 4); - EXPECT_EQ(g.find('o', 4), 4); - EXPECT_EQ(g.find('o', 5), 8); - EXPECT_EQ(a.find('b', 5), StringPiece::npos); - // empty string nonsense - EXPECT_EQ(e.find('\0'), StringPiece::npos); - EXPECT_EQ(e.find('\0', 7), StringPiece::npos); - EXPECT_EQ(e.find('x'), StringPiece::npos); - EXPECT_EQ(e.find('x', 7), StringPiece::npos); - - EXPECT_EQ(a.rfind(b), 0); - EXPECT_EQ(a.rfind(b, 1), 0); - EXPECT_EQ(a.rfind(c), 23); - EXPECT_EQ(a.rfind(c, 22), StringPiece::npos); - EXPECT_EQ(a.rfind(c, 1), StringPiece::npos); - EXPECT_EQ(a.rfind(c, 0), StringPiece::npos); - EXPECT_EQ(b.rfind(c), StringPiece::npos); - EXPECT_EQ(b.rfind(c, 0), StringPiece::npos); - EXPECT_EQ(a.rfind(e), a.as_string().rfind(std::string())); - EXPECT_EQ(a.rfind(e, 17), 17); - EXPECT_EQ(a.rfind(g), StringPiece::npos); - EXPECT_EQ(e.rfind(b), StringPiece::npos); - EXPECT_EQ(e.rfind(b, 7), StringPiece::npos); - // empty string nonsense - EXPECT_EQ(e.rfind(e, 7), std::string().rfind(std::string())); - EXPECT_EQ(e.rfind(e), std::string().rfind(std::string())); - - EXPECT_EQ(g.rfind('o'), 8); - EXPECT_EQ(g.rfind('q'), StringPiece::npos); - EXPECT_EQ(g.rfind('o', 8), 8); - EXPECT_EQ(g.rfind('o', 7), 4); - EXPECT_EQ(g.rfind('o', 3), StringPiece::npos); - EXPECT_EQ(f.rfind('\0'), 3); - EXPECT_EQ(f.rfind('\0', 12), 3); - EXPECT_EQ(f.rfind('3'), 2); - EXPECT_EQ(f.rfind('5'), 5); - // empty string nonsense - EXPECT_EQ(e.rfind('o'), StringPiece::npos); - EXPECT_EQ(e.rfind('o', 7), StringPiece::npos); - - EXPECT_EQ(a.find_first_of(b), 0); - EXPECT_EQ(a.find_first_of(b, 0), 0); - EXPECT_EQ(a.find_first_of(b, 1), 1); - EXPECT_EQ(a.find_first_of(b, 2), 2); - EXPECT_EQ(a.find_first_of(b, 3), StringPiece::npos); - EXPECT_EQ(a.find_first_of(c), 23); - EXPECT_EQ(a.find_first_of(c, 23), 23); - EXPECT_EQ(a.find_first_of(c, 24), 24); - EXPECT_EQ(a.find_first_of(c, 25), 25); - EXPECT_EQ(a.find_first_of(c, 26), StringPiece::npos); - EXPECT_EQ(g.find_first_of(b), 13); - EXPECT_EQ(g.find_first_of(c), 0); - EXPECT_EQ(a.find_first_of(f), StringPiece::npos); - EXPECT_EQ(f.find_first_of(a), StringPiece::npos); - // empty string nonsense - EXPECT_EQ(a.find_first_of(e), StringPiece::npos); - EXPECT_EQ(e.find_first_of(b), StringPiece::npos); - EXPECT_EQ(e.find_first_of(e), StringPiece::npos); - - EXPECT_EQ(a.find_first_not_of(b), 3); - EXPECT_EQ(a.find_first_not_of(c), 0); - EXPECT_EQ(b.find_first_not_of(a), StringPiece::npos); - EXPECT_EQ(c.find_first_not_of(a), StringPiece::npos); - EXPECT_EQ(f.find_first_not_of(a), 0); - EXPECT_EQ(a.find_first_not_of(f), 0); - EXPECT_EQ(a.find_first_not_of(e), 0); - // empty string nonsense - EXPECT_EQ(e.find_first_not_of(a), StringPiece::npos); - EXPECT_EQ(e.find_first_not_of(e), StringPiece::npos); - - StringPiece h("===="); - EXPECT_EQ(h.find_first_not_of('='), StringPiece::npos); - EXPECT_EQ(h.find_first_not_of('=', 3), StringPiece::npos); - EXPECT_EQ(h.find_first_not_of('\0'), 0); - EXPECT_EQ(g.find_first_not_of('x'), 2); - EXPECT_EQ(f.find_first_not_of('\0'), 0); - EXPECT_EQ(f.find_first_not_of('\0', 3), 4); - EXPECT_EQ(f.find_first_not_of('\0', 2), 2); - // empty string nonsense - EXPECT_EQ(e.find_first_not_of('x'), StringPiece::npos); - EXPECT_EQ(e.find_first_not_of('\0'), StringPiece::npos); - - // StringPiece g("xx not found bb"); - StringPiece i("56"); - EXPECT_EQ(h.find_last_of(a), StringPiece::npos); - EXPECT_EQ(g.find_last_of(a), g.size()-1); - EXPECT_EQ(a.find_last_of(b), 2); - EXPECT_EQ(a.find_last_of(c), a.size()-1); - EXPECT_EQ(f.find_last_of(i), 6); - EXPECT_EQ(a.find_last_of('a'), 0); - EXPECT_EQ(a.find_last_of('b'), 1); - EXPECT_EQ(a.find_last_of('z'), 25); - EXPECT_EQ(a.find_last_of('a', 5), 0); - EXPECT_EQ(a.find_last_of('b', 5), 1); - EXPECT_EQ(a.find_last_of('b', 0), StringPiece::npos); - EXPECT_EQ(a.find_last_of('z', 25), 25); - EXPECT_EQ(a.find_last_of('z', 24), StringPiece::npos); - EXPECT_EQ(f.find_last_of(i, 5), 5); - EXPECT_EQ(f.find_last_of(i, 6), 6); - EXPECT_EQ(f.find_last_of(a, 4), StringPiece::npos); - // empty string nonsense - EXPECT_EQ(f.find_last_of(e), StringPiece::npos); - EXPECT_EQ(f.find_last_of(e, 4), StringPiece::npos); - EXPECT_EQ(e.find_last_of(e), StringPiece::npos); - EXPECT_EQ(e.find_last_of(f), StringPiece::npos); - EXPECT_EQ(e.find_last_of(e, 4), StringPiece::npos); - EXPECT_EQ(e.find_last_of(f, 4), StringPiece::npos); - - EXPECT_EQ(a.find_last_not_of(b), a.size()-1); - EXPECT_EQ(a.find_last_not_of(c), 22); - EXPECT_EQ(b.find_last_not_of(a), StringPiece::npos); - EXPECT_EQ(b.find_last_not_of(b), StringPiece::npos); - EXPECT_EQ(f.find_last_not_of(i), 4); - EXPECT_EQ(a.find_last_not_of(c, 24), 22); - EXPECT_EQ(a.find_last_not_of(b, 3), 3); - EXPECT_EQ(a.find_last_not_of(b, 2), StringPiece::npos); - // empty string nonsense - EXPECT_EQ(f.find_last_not_of(e), f.size()-1); - EXPECT_EQ(f.find_last_not_of(e, 4), 4); - EXPECT_EQ(e.find_last_not_of(e), StringPiece::npos); - EXPECT_EQ(e.find_last_not_of(f), StringPiece::npos); - EXPECT_EQ(e.find_last_not_of(e, 4), StringPiece::npos); - EXPECT_EQ(e.find_last_not_of(f, 4), StringPiece::npos); - - EXPECT_EQ(h.find_last_not_of('x'), h.size() - 1); - EXPECT_EQ(h.find_last_not_of('='), StringPiece::npos); - EXPECT_EQ(b.find_last_not_of('c'), 1); - EXPECT_EQ(h.find_last_not_of('x', 2), 2); - EXPECT_EQ(h.find_last_not_of('=', 2), StringPiece::npos); - EXPECT_EQ(b.find_last_not_of('b', 1), 0); - // empty string nonsense - EXPECT_EQ(e.find_last_not_of('x'), StringPiece::npos); - EXPECT_EQ(e.find_last_not_of('\0'), StringPiece::npos); - - EXPECT_EQ(a.substr(0, 3), b); - EXPECT_EQ(a.substr(23), c); - EXPECT_EQ(a.substr(23, 3), c); - EXPECT_EQ(a.substr(23, 99), c); - EXPECT_EQ(a.substr(0), a); - EXPECT_EQ(a.substr(3, 2), "de"); - // empty string nonsense - EXPECT_EQ(a.substr(99, 2), e); - EXPECT_EQ(e.substr(99), e); - EXPECT_EQ(e.substr(0, 99), e); - EXPECT_EQ(e.substr(99, 99), e); - // use of npos - EXPECT_EQ(a.substr(0, StringPiece::npos), a); - EXPECT_EQ(a.substr(23, StringPiece::npos), c); - EXPECT_EQ(a.substr(StringPiece::npos, 0), e); - EXPECT_EQ(a.substr(StringPiece::npos, 1), e); - EXPECT_EQ(a.substr(StringPiece::npos, StringPiece::npos), e); -} - -TEST(StringPiece, Custom) { - StringPiece a("foobar"); - std::string s1("123"); - s1 += '\0'; - s1 += "456"; - StringPiece b(s1); - StringPiece e; - std::string s2; - - // CopyToString - a.CopyToString(&s2); - EXPECT_EQ(s2.size(), 6); - EXPECT_EQ(s2, "foobar"); - b.CopyToString(&s2); - EXPECT_EQ(s2.size(), 7); - EXPECT_EQ(s1, s2); - e.CopyToString(&s2); - EXPECT_TRUE(s2.empty()); - - // AppendToString - s2.erase(); - a.AppendToString(&s2); - EXPECT_EQ(s2.size(), 6); - EXPECT_EQ(s2, "foobar"); - a.AppendToString(&s2); - EXPECT_EQ(s2.size(), 12); - EXPECT_EQ(s2, "foobarfoobar"); - - // starts_with - EXPECT_TRUE(a.starts_with(a)); - EXPECT_TRUE(a.starts_with("foo")); - EXPECT_TRUE(a.starts_with(e)); - EXPECT_TRUE(b.starts_with(s1)); - EXPECT_TRUE(b.starts_with(b)); - EXPECT_TRUE(b.starts_with(e)); - EXPECT_TRUE(e.starts_with("")); - EXPECT_TRUE(!a.starts_with(b)); - EXPECT_TRUE(!b.starts_with(a)); - EXPECT_TRUE(!e.starts_with(a)); - - // ends with - EXPECT_TRUE(a.ends_with(a)); - EXPECT_TRUE(a.ends_with("bar")); - EXPECT_TRUE(a.ends_with(e)); - EXPECT_TRUE(b.ends_with(s1)); - EXPECT_TRUE(b.ends_with(b)); - EXPECT_TRUE(b.ends_with(e)); - EXPECT_TRUE(e.ends_with("")); - EXPECT_TRUE(!a.ends_with(b)); - EXPECT_TRUE(!b.ends_with(a)); - EXPECT_TRUE(!e.ends_with(a)); - - // remove_prefix - StringPiece c(a); - c.remove_prefix(3); - EXPECT_EQ(c, "bar"); - c = a; - c.remove_prefix(0); - EXPECT_EQ(c, a); - c.remove_prefix(c.size()); - EXPECT_EQ(c, e); - - // remove_suffix - c = a; - c.remove_suffix(3); - EXPECT_EQ(c, "foo"); - c = a; - c.remove_suffix(0); - EXPECT_EQ(c, a); - c.remove_suffix(c.size()); - EXPECT_EQ(c, e); - - c = StringPiece("foobar", 7); - - // as_string - std::string s3(a.as_string().c_str(), 7); - EXPECT_EQ(c, s3); - std::string s4(e.as_string()); - EXPECT_TRUE(s4.empty()); - - // ToString - { - std::string s5(a.ToString().c_str(), 7); - EXPECT_EQ(c, s5); - std::string s6(e.ToString()); - EXPECT_TRUE(s6.empty()); - } - - // Consume - { - StringPiece str("foobar"); - EXPECT_TRUE(str.Consume("foo")); - EXPECT_EQ(str, "bar"); - EXPECT_FALSE(str.Consume("foo")); - EXPECT_FALSE(str.Consume("barbar")); - EXPECT_FALSE(str.Consume("ar")); - EXPECT_EQ(str, "bar"); - } - - { - StringPiece str("foobar"); - EXPECT_TRUE(str.ConsumeFromEnd("bar")); - EXPECT_EQ(str, "foo"); - EXPECT_FALSE(str.ConsumeFromEnd("bar")); - EXPECT_FALSE(str.ConsumeFromEnd("foofoo")); - EXPECT_FALSE(str.ConsumeFromEnd("fo")); - EXPECT_EQ(str, "foo"); - } -} - -TEST(StringPiece, Contains) { - StringPiece a("abcdefg"); - StringPiece b("abcd"); - StringPiece c("efg"); - StringPiece d("gh"); - EXPECT_TRUE(a.contains(b)); - EXPECT_TRUE(a.contains(c)); - EXPECT_TRUE(!a.contains(d)); -} - -TEST(StringPiece, NullInput) { - // we used to crash here, but now we don't. - StringPiece s(nullptr); - EXPECT_EQ(s.data(), (const char*)nullptr); - EXPECT_EQ(s.size(), 0); - - // .ToString() on a StringPiece with nullptr should produce the empty string. - EXPECT_EQ("", s.ToString()); - EXPECT_EQ("", s.as_string()); -} - -TEST(StringPiece, Comparisons2) { - StringPiece abc("abcdefghijklmnopqrstuvwxyz"); - - // check comparison operations on strings longer than 4 bytes. - EXPECT_EQ(abc, StringPiece("abcdefghijklmnopqrstuvwxyz")); - EXPECT_EQ(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxyz")), 0); - - EXPECT_LT(abc, StringPiece("abcdefghijklmnopqrstuvwxzz")); - EXPECT_LT(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxzz")), 0); - - EXPECT_GT(abc, StringPiece("abcdefghijklmnopqrstuvwxyy")); - EXPECT_GT(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxyy")), 0); - - // starts_with - EXPECT_TRUE(abc.starts_with(abc)); - EXPECT_TRUE(abc.starts_with("abcdefghijklm")); - EXPECT_TRUE(!abc.starts_with("abcdefguvwxyz")); - - // ends_with - EXPECT_TRUE(abc.ends_with(abc)); - EXPECT_TRUE(!abc.ends_with("abcdefguvwxyz")); - EXPECT_TRUE(abc.ends_with("nopqrstuvwxyz")); -} - -TEST(ComparisonOpsTest, StringCompareNotAmbiguous) { - EXPECT_EQ("hello", std::string("hello")); - EXPECT_LT("hello", std::string("world")); -} - -TEST(ComparisonOpsTest, HeterogenousStringPieceEquals) { - EXPECT_EQ(StringPiece("hello"), std::string("hello")); - EXPECT_EQ("hello", StringPiece("hello")); -} - -TEST(FindOneCharTest, EdgeCases) { - StringPiece a("xxyyyxx"); - - // Set a = "xyyyx". - a.remove_prefix(1); - a.remove_suffix(1); - - EXPECT_EQ(0, a.find('x')); - EXPECT_EQ(0, a.find('x', 0)); - EXPECT_EQ(4, a.find('x', 1)); - EXPECT_EQ(4, a.find('x', 4)); - EXPECT_EQ(StringPiece::npos, a.find('x', 5)); - - EXPECT_EQ(4, a.rfind('x')); - EXPECT_EQ(4, a.rfind('x', 5)); - EXPECT_EQ(4, a.rfind('x', 4)); - EXPECT_EQ(0, a.rfind('x', 3)); - EXPECT_EQ(0, a.rfind('x', 0)); - - // Set a = "yyy". - a.remove_prefix(1); - a.remove_suffix(1); - - EXPECT_EQ(StringPiece::npos, a.find('x')); - EXPECT_EQ(StringPiece::npos, a.rfind('x')); -} - -#ifdef PROTOBUF_HAS_DEATH_TEST -#ifndef NDEBUG -TEST(NonNegativeLenTest, NonNegativeLen) { - EXPECT_DEATH(StringPiece("xyz", -1), "string length exceeds max size"); -} -#endif // ndef DEBUG -#endif // PROTOBUF_HAS_DEATH_TEST - -} // namespace -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/stubs/structurally_valid.cc b/src/google/protobuf/stubs/structurally_valid.cc index a53573658312..136bb0c787ed 100644 --- a/src/google/protobuf/stubs/structurally_valid.cc +++ b/src/google/protobuf/stubs/structurally_valid.cc @@ -32,7 +32,7 @@ #include -#include +#include "absl/strings/string_view.h" namespace google { namespace protobuf { @@ -563,7 +563,7 @@ bool IsStructurallyValidUTF8(const char* buf, int len) { return (bytes_consumed == len); } -int UTF8SpnStructurallyValid(StringPiece str) { +int UTF8SpnStructurallyValid(absl::string_view str) { if (!module_initialized_) return str.size(); int bytes_consumed = 0; @@ -584,7 +584,7 @@ int UTF8SpnStructurallyValid(StringPiece str) { // // Fast case: all is structurally valid and no byte copying is done. // -char* UTF8CoerceToStructurallyValid(StringPiece src_str, char* idst, +char* UTF8CoerceToStructurallyValid(absl::string_view src_str, char* idst, const char replace_char) { const char* isrc = src_str.data(); const int len = src_str.length(); @@ -602,7 +602,7 @@ char* UTF8CoerceToStructurallyValid(StringPiece src_str, char* idst, dst[0] = replace_char; // replace one bad byte src++; dst++; - StringPiece str2(src, srclimit - src); + absl::string_view str2(src, srclimit - src); n = UTF8SpnStructurallyValid(str2); // scan the remainder memmove(dst, src, n); // copy next good chunk src += n; diff --git a/src/google/protobuf/stubs/strutil.cc b/src/google/protobuf/stubs/strutil.cc index 8f94486469bf..529e50f4e534 100644 --- a/src/google/protobuf/stubs/strutil.cc +++ b/src/google/protobuf/stubs/strutil.cc @@ -30,18 +30,20 @@ // from google3/strings/strutil.cc -#include - #include -#include // FLT_DIG and DBL_DIG +#include // FLT_DIG and DBL_DIG +#include +#include +#include #include #include + #include #include #include -#include -#include +#include "absl/strings/ascii.h" +#include "absl/strings/string_view.h" #ifdef _WIN32 // MSVC has only _snprintf, not snprintf. @@ -99,7 +101,7 @@ void StripWhitespace(std::string *str) { // Strip off leading whitespace. int first = 0; - while (first < str_length && ascii_isspace(str->at(first))) { + while (first < str_length && absl::ascii_isspace(str->at(first))) { ++first; } // If entire string is white space. @@ -114,7 +116,7 @@ void StripWhitespace(std::string *str) { // Strip off trailing whitespace. int last = str_length - 1; - while (last >= 0 && ascii_isspace(str->at(last))) { + while (last >= 0 && absl::ascii_isspace(str->at(last))) { --last; } if (last != (str_length - 1) && last >= 0) { @@ -176,7 +178,7 @@ std::string StringReplace(const std::string &s, const std::string &oldsub, // the characters in the string, not the entire string as a single delimiter. // ---------------------------------------------------------------------- template -static inline void SplitStringToIteratorUsing(StringPiece full, +static inline void SplitStringToIteratorUsing(absl::string_view full, const char *delim, ITR &result) { // Optimize the common case where delim is a single character. if (delim[0] != '\0' && delim[1] == '\0') { @@ -209,7 +211,7 @@ static inline void SplitStringToIteratorUsing(StringPiece full, } } -void SplitStringUsing(StringPiece full, const char *delim, +void SplitStringUsing(absl::string_view full, const char *delim, std::vector *result) { std::back_insert_iterator > it(*result); SplitStringToIteratorUsing(full, delim, it); @@ -227,7 +229,7 @@ void SplitStringUsing(StringPiece full, const char *delim, // If "pieces" is negative for some reason, it returns the whole string // ---------------------------------------------------------------------- template -static inline void SplitStringToIteratorAllowEmpty(StringPiece full, +static inline void SplitStringToIteratorAllowEmpty(absl::string_view full, const char *delim, int pieces, ITR &result) { std::string::size_type begin_index, end_index; @@ -246,381 +248,12 @@ static inline void SplitStringToIteratorAllowEmpty(StringPiece full, *result++ = std::string(full.substr(begin_index)); } -void SplitStringAllowEmpty(StringPiece full, const char *delim, +void SplitStringAllowEmpty(absl::string_view full, const char *delim, std::vector *result) { std::back_insert_iterator > it(*result); SplitStringToIteratorAllowEmpty(full, delim, 0, it); } -// ---------------------------------------------------------------------- -// JoinStrings() -// This merges a vector of string components with delim inserted -// as separaters between components. -// -// ---------------------------------------------------------------------- -template -static void JoinStringsIterator(const ITERATOR &start, const ITERATOR &end, - const char *delim, std::string *result) { - GOOGLE_CHECK(result != nullptr); - result->clear(); - int delim_length = strlen(delim); - - // Precompute resulting length so we can reserve() memory in one shot. - int length = 0; - for (ITERATOR iter = start; iter != end; ++iter) { - if (iter != start) { - length += delim_length; - } - length += iter->size(); - } - result->reserve(length); - - // Now combine everything. - for (ITERATOR iter = start; iter != end; ++iter) { - if (iter != start) { - result->append(delim, delim_length); - } - result->append(iter->data(), iter->size()); - } -} - -void JoinStrings(const std::vector &components, const char *delim, - std::string *result) { - JoinStringsIterator(components.begin(), components.end(), delim, result); -} - -// ---------------------------------------------------------------------- -// UnescapeCEscapeSequences() -// This does all the unescaping that C does: \ooo, \r, \n, etc -// Returns length of resulting string. -// The implementation of \x parses any positive number of hex digits, -// but it is an error if the value requires more than 8 bits, and the -// result is truncated to 8 bits. -// -// The second call stores its errors in a supplied string vector. -// If the string vector pointer is nullptr, it reports the errors with LOG(). -// ---------------------------------------------------------------------- - -#define IS_OCTAL_DIGIT(c) (((c) >= '0') && ((c) <= '7')) - -// Protocol buffers doesn't ever care about errors, but I don't want to remove -// the code. -#define LOG_STRING(LEVEL, VECTOR) GOOGLE_LOG_IF(LEVEL, false) - -int UnescapeCEscapeSequences(const char* source, char* dest) { - return UnescapeCEscapeSequences(source, dest, nullptr); -} - -int UnescapeCEscapeSequences(const char *source, char *dest, - std::vector *errors) { - GOOGLE_DCHECK(errors == nullptr) << "Error reporting not implemented."; - - char* d = dest; - const char* p = source; - - // Small optimization for case where source = dest and there's no escaping - while ( p == d && *p != '\0' && *p != '\\' ) - p++, d++; - - while (*p != '\0') { - if (*p != '\\') { - *d++ = *p++; - } else { - switch ( *++p ) { // skip past the '\\' - case '\0': - LOG_STRING(ERROR, errors) << "String cannot end with \\"; - *d = '\0'; - return d - dest; // we're done with p - case 'a': *d++ = '\a'; break; - case 'b': *d++ = '\b'; break; - case 'f': *d++ = '\f'; break; - case 'n': *d++ = '\n'; break; - case 'r': *d++ = '\r'; break; - case 't': *d++ = '\t'; break; - case 'v': *d++ = '\v'; break; - case '\\': *d++ = '\\'; break; - case '?': *d++ = '\?'; break; // \? Who knew? - case '\'': *d++ = '\''; break; - case '"': *d++ = '\"'; break; - case '0': case '1': case '2': case '3': // octal digit: 1 to 3 digits - case '4': case '5': case '6': case '7': { - char ch = *p - '0'; - if ( IS_OCTAL_DIGIT(p[1]) ) - ch = ch * 8 + *++p - '0'; - if ( IS_OCTAL_DIGIT(p[1]) ) // safe (and easy) to do this twice - ch = ch * 8 + *++p - '0'; // now points at last digit - *d++ = ch; - break; - } - case 'x': case 'X': { - if (!isxdigit(p[1])) { - if (p[1] == '\0') { - LOG_STRING(ERROR, errors) << "String cannot end with \\x"; - } else { - LOG_STRING(ERROR, errors) << - "\\x cannot be followed by non-hex digit: \\" << *p << p[1]; - } - break; - } - unsigned int ch = 0; - const char *hex_start = p; - while (isxdigit(p[1])) // arbitrarily many hex digits - ch = (ch << 4) + hex_digit_to_int(*++p); - if (ch > 0xFF) - LOG_STRING(ERROR, errors) - << "Value of " - << "\\" << std::string(hex_start, p + 1 - hex_start) - << " exceeds 8 bits"; - *d++ = ch; - break; - } -#if 0 // TODO(kenton): Support \u and \U? Requires runetochar(). - case 'u': { - // \uhhhh => convert 4 hex digits to UTF-8 - char32 rune = 0; - const char *hex_start = p; - for (int i = 0; i < 4; ++i) { - if (isxdigit(p[1])) { // Look one char ahead. - rune = (rune << 4) + hex_digit_to_int(*++p); // Advance p. - } else { - LOG_STRING(ERROR, errors) - << "\\u must be followed by 4 hex digits: \\" - << std::string(hex_start, p+1-hex_start); - break; - } - } - d += runetochar(d, &rune); - break; - } - case 'U': { - // \Uhhhhhhhh => convert 8 hex digits to UTF-8 - char32 rune = 0; - const char *hex_start = p; - for (int i = 0; i < 8; ++i) { - if (isxdigit(p[1])) { // Look one char ahead. - // Don't change rune until we're sure this - // is within the Unicode limit, but do advance p. - char32 newrune = (rune << 4) + hex_digit_to_int(*++p); - if (newrune > 0x10FFFF) { - LOG_STRING(ERROR, errors) - << "Value of \\" - << std::string(hex_start, p + 1 - hex_start) - << " exceeds Unicode limit (0x10FFFF)"; - break; - } else { - rune = newrune; - } - } else { - LOG_STRING(ERROR, errors) - << "\\U must be followed by 8 hex digits: \\" - << std::string(hex_start, p+1-hex_start); - break; - } - } - d += runetochar(d, &rune); - break; - } -#endif - default: - LOG_STRING(ERROR, errors) << "Unknown escape sequence: \\" << *p; - } - p++; // read past letter we escaped - } - } - *d = '\0'; - return d - dest; -} - -// ---------------------------------------------------------------------- -// UnescapeCEscapeString() -// This does the same thing as UnescapeCEscapeSequences, but creates -// a new string. The caller does not need to worry about allocating -// a dest buffer. This should be used for non performance critical -// tasks such as printing debug messages. It is safe for src and dest -// to be the same. -// -// The second call stores its errors in a supplied string vector. -// If the string vector pointer is nullptr, it reports the errors with LOG(). -// -// In the first and second calls, the length of dest is returned. In the -// the third call, the new string is returned. -// ---------------------------------------------------------------------- -int UnescapeCEscapeString(const std::string &src, std::string *dest) { - return UnescapeCEscapeString(src, dest, nullptr); -} - -int UnescapeCEscapeString(const std::string &src, std::string *dest, - std::vector *errors) { - std::unique_ptr unescaped(new char[src.size() + 1]); - int len = UnescapeCEscapeSequences(src.c_str(), unescaped.get(), errors); - GOOGLE_CHECK(dest); - dest->assign(unescaped.get(), len); - return len; -} - -std::string UnescapeCEscapeString(const std::string &src) { - std::unique_ptr unescaped(new char[src.size() + 1]); - int len = UnescapeCEscapeSequences(src.c_str(), unescaped.get(), nullptr); - return std::string(unescaped.get(), len); -} - -// ---------------------------------------------------------------------- -// CEscapeString() -// CHexEscapeString() -// Copies 'src' to 'dest', escaping dangerous characters using -// C-style escape sequences. This is very useful for preparing query -// flags. 'src' and 'dest' should not overlap. The 'Hex' version uses -// hexadecimal rather than octal sequences. -// Returns the number of bytes written to 'dest' (not including the \0) -// or -1 if there was insufficient space. -// -// Currently only \n, \r, \t, ", ', \ and !isprint() chars are escaped. -// ---------------------------------------------------------------------- -int CEscapeInternal(const char* src, int src_len, char* dest, - int dest_len, bool use_hex, bool utf8_safe) { - const char* src_end = src + src_len; - int used = 0; - bool last_hex_escape = false; // true if last output char was \xNN - - for (; src < src_end; src++) { - if (dest_len - used < 2) // Need space for two letter escape - return -1; - - bool is_hex_escape = false; - switch (*src) { - case '\n': dest[used++] = '\\'; dest[used++] = 'n'; break; - case '\r': dest[used++] = '\\'; dest[used++] = 'r'; break; - case '\t': dest[used++] = '\\'; dest[used++] = 't'; break; - case '\"': dest[used++] = '\\'; dest[used++] = '\"'; break; - case '\'': dest[used++] = '\\'; dest[used++] = '\''; break; - case '\\': dest[used++] = '\\'; dest[used++] = '\\'; break; - default: - // Note that if we emit \xNN and the src character after that is a hex - // digit then that digit must be escaped too to prevent it being - // interpreted as part of the character code by C. - if ((!utf8_safe || static_cast(*src) < 0x80) && - (!isprint(*src) || - (last_hex_escape && isxdigit(*src)))) { - // need space for 4 letter escape and the trailing '\0' to - // be written by snprintf. - if (dest_len - used < 5) return -1; - snprintf(dest + used, 5, (use_hex ? "\\x%02x" : "\\%03o"), - static_cast(*src)); - is_hex_escape = use_hex; - used += 4; - } else { - dest[used++] = *src; break; - } - } - last_hex_escape = is_hex_escape; - } - - if (dest_len - used < 1) // make sure that there is room for \0 - return -1; - - dest[used] = '\0'; // doesn't count towards return value though - return used; -} - -// Calculates the length of the C-style escaped version of 'src'. -// Assumes that non-printable characters are escaped using octal sequences, and -// that UTF-8 bytes are not handled specially. -static inline size_t CEscapedLength(StringPiece src) { - static char c_escaped_len[256] = { - 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 2, 4, 4, // \t, \n, \r - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // ", ' - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // '0'..'9' - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 'A'..'O' - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, // 'P'..'Z', '\' - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 'a'..'o' - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, // 'p'..'z', DEL - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - }; - - size_t escaped_len = 0; - for (StringPiece::size_type i = 0; i < src.size(); ++i) { - unsigned char c = static_cast(src[i]); - escaped_len += c_escaped_len[c]; - } - return escaped_len; -} - -// ---------------------------------------------------------------------- -// Escapes 'src' using C-style escape sequences, and appends the escaped string -// to 'dest'. This version is faster than calling CEscapeInternal as it computes -// the required space using a lookup table, and also does not do any special -// handling for Hex or UTF-8 characters. -// ---------------------------------------------------------------------- -void CEscapeAndAppend(StringPiece src, std::string *dest) { - size_t escaped_len = CEscapedLength(src); - if (escaped_len == src.size()) { - dest->append(src.data(), src.size()); - return; - } - - size_t cur_dest_len = dest->size(); - dest->resize(cur_dest_len + escaped_len); - char* append_ptr = &(*dest)[cur_dest_len]; - - for (StringPiece::size_type i = 0; i < src.size(); ++i) { - unsigned char c = static_cast(src[i]); - switch (c) { - case '\n': *append_ptr++ = '\\'; *append_ptr++ = 'n'; break; - case '\r': *append_ptr++ = '\\'; *append_ptr++ = 'r'; break; - case '\t': *append_ptr++ = '\\'; *append_ptr++ = 't'; break; - case '\"': *append_ptr++ = '\\'; *append_ptr++ = '\"'; break; - case '\'': *append_ptr++ = '\\'; *append_ptr++ = '\''; break; - case '\\': *append_ptr++ = '\\'; *append_ptr++ = '\\'; break; - default: - if (!isprint(c)) { - *append_ptr++ = '\\'; - *append_ptr++ = '0' + c / 64; - *append_ptr++ = '0' + (c % 64) / 8; - *append_ptr++ = '0' + c % 8; - } else { - *append_ptr++ = c; - } - break; - } - } -} - -std::string CEscape(const std::string &src) { - std::string dest; - CEscapeAndAppend(src, &dest); - return dest; -} - -namespace strings { - -std::string Utf8SafeCEscape(const std::string &src) { - const int dest_length = src.size() * 4 + 1; // Maximum possible expansion - std::unique_ptr dest(new char[dest_length]); - const int len = CEscapeInternal(src.data(), src.size(), - dest.get(), dest_length, false, true); - GOOGLE_DCHECK_GE(len, 0); - return std::string(dest.get(), len); -} - -std::string CHexEscape(const std::string &src) { - const int dest_length = src.size() * 4 + 1; // Maximum possible expansion - std::unique_ptr dest(new char[dest_length]); - const int len = CEscapeInternal(src.data(), src.size(), - dest.get(), dest_length, true, false); - GOOGLE_DCHECK_GE(len, 0); - return std::string(dest.get(), len); -} - -} // namespace strings - // ---------------------------------------------------------------------- // strto32_adaptor() // strtou32_adaptor() @@ -1290,19 +923,21 @@ static int memcasecmp(const char *s1, const char *s2, size_t len) { for (size_t i = 0; i < len; i++) { const int diff = - static_cast(static_cast(ascii_tolower(us1[i]))) - - static_cast(static_cast(ascii_tolower(us2[i]))); + static_cast( + static_cast(absl::ascii_tolower(us1[i]))) - + static_cast( + static_cast(absl::ascii_tolower(us2[i]))); if (diff != 0) return diff; } return 0; } -inline bool CaseEqual(StringPiece s1, StringPiece s2) { +inline bool CaseEqual(absl::string_view s1, absl::string_view s2) { if (s1.size() != s2.size()) return false; return memcasecmp(s1.data(), s2.data(), s1.size()) == 0; } -bool safe_strtob(StringPiece str, bool* value) { +bool safe_strtob(absl::string_view str, bool *value) { GOOGLE_CHECK(value != nullptr) << "nullptr output boolean given."; if (CaseEqual(str, "true") || CaseEqual(str, "t") || CaseEqual(str, "yes") || CaseEqual(str, "y") || @@ -1334,7 +969,7 @@ bool safe_strtod(const char* str, double* value) { char* endptr; *value = internal::NoLocaleStrtod(str, &endptr); if (endptr != str) { - while (ascii_isspace(*endptr)) ++endptr; + while (absl::ascii_isspace(*endptr)) ++endptr; } // Ignore range errors from strtod. The values it // returns on underflow and overflow are the right @@ -1396,223 +1031,6 @@ char* FloatToBuffer(float value, char* buffer) { return buffer; } -namespace strings { - -AlphaNum::AlphaNum(strings::Hex hex) { - char *const end = &digits[kFastToBufferSize]; - char *writer = end; - uint64_t value = hex.value; - uint64_t width = hex.spec; - // We accomplish minimum width by OR'ing in 0x10000 to the user's value, - // where 0x10000 is the smallest hex number that is as wide as the user - // asked for. - uint64_t mask = (static_cast(1) << ((width - 1) * 4)) | value; - static const char hexdigits[] = "0123456789abcdef"; - do { - *--writer = hexdigits[value & 0xF]; - value >>= 4; - mask >>= 4; - } while (mask != 0); - piece_data_ = writer; - piece_size_ = end - writer; -} - -} // namespace strings - -// ---------------------------------------------------------------------- -// StrCat() -// This merges the given strings or integers, with no delimiter. This -// is designed to be the fastest possible way to construct a string out -// of a mix of raw C strings, C++ strings, and integer values. -// ---------------------------------------------------------------------- - -// Append is merely a version of memcpy that returns the address of the byte -// after the area just overwritten. It comes in multiple flavors to minimize -// call overhead. -static char *Append1(char *out, const AlphaNum &x) { - if (x.size() > 0) { - memcpy(out, x.data(), x.size()); - out += x.size(); - } - return out; -} - -static char *Append2(char *out, const AlphaNum &x1, const AlphaNum &x2) { - if (x1.size() > 0) { - memcpy(out, x1.data(), x1.size()); - out += x1.size(); - } - if (x2.size() > 0) { - memcpy(out, x2.data(), x2.size()); - out += x2.size(); - } - return out; -} - -static char *Append4(char *out, const AlphaNum &x1, const AlphaNum &x2, - const AlphaNum &x3, const AlphaNum &x4) { - if (x1.size() > 0) { - memcpy(out, x1.data(), x1.size()); - out += x1.size(); - } - if (x2.size() > 0) { - memcpy(out, x2.data(), x2.size()); - out += x2.size(); - } - if (x3.size() > 0) { - memcpy(out, x3.data(), x3.size()); - out += x3.size(); - } - if (x4.size() > 0) { - memcpy(out, x4.data(), x4.size()); - out += x4.size(); - } - return out; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b) { - std::string result; - result.resize(a.size() + b.size()); - char *const begin = &*result.begin(); - char *out = Append2(begin, a, b); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c) { - std::string result; - result.resize(a.size() + b.size() + c.size()); - char *const begin = &*result.begin(); - char *out = Append2(begin, a, b); - out = Append1(out, c); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d, const AlphaNum &e) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size() + e.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - out = Append1(out, e); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d, const AlphaNum &e, const AlphaNum &f) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size() + e.size() + - f.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - out = Append2(out, e, f); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d, const AlphaNum &e, const AlphaNum &f, - const AlphaNum &g) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size() + e.size() + - f.size() + g.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - out = Append2(out, e, f); - out = Append1(out, g); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d, const AlphaNum &e, const AlphaNum &f, - const AlphaNum &g, const AlphaNum &h) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size() + e.size() + - f.size() + g.size() + h.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - out = Append4(out, e, f, g, h); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d, const AlphaNum &e, const AlphaNum &f, - const AlphaNum &g, const AlphaNum &h, const AlphaNum &i) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size() + e.size() + - f.size() + g.size() + h.size() + i.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - out = Append4(out, e, f, g, h); - out = Append1(out, i); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -// It's possible to call StrAppend with a char * pointer that is partway into -// the string we're appending to. However the results of this are random. -// Therefore, check for this in debug mode. Use unsigned math so we only have -// to do one comparison. -#define GOOGLE_DCHECK_NO_OVERLAP(dest, src) \ - GOOGLE_DCHECK(((src).size() == 0) || \ - uintptr_t((src).data() - (dest).data()) > \ - uintptr_t((dest).size())) - -void StrAppend(std::string *result, const AlphaNum &a) { - GOOGLE_DCHECK_NO_OVERLAP(*result, a); - result->append(a.data(), a.size()); -} - -void StrAppend(std::string *result, const AlphaNum &a, const AlphaNum &b) { - GOOGLE_DCHECK_NO_OVERLAP(*result, a); - GOOGLE_DCHECK_NO_OVERLAP(*result, b); - std::string::size_type old_size = result->size(); - result->resize(old_size + a.size() + b.size()); - char *const begin = &*result->begin(); - char *out = Append2(begin + old_size, a, b); - GOOGLE_DCHECK_EQ(out, begin + result->size()); -} - -void StrAppend(std::string *result, const AlphaNum &a, const AlphaNum &b, - const AlphaNum &c) { - GOOGLE_DCHECK_NO_OVERLAP(*result, a); - GOOGLE_DCHECK_NO_OVERLAP(*result, b); - GOOGLE_DCHECK_NO_OVERLAP(*result, c); - std::string::size_type old_size = result->size(); - result->resize(old_size + a.size() + b.size() + c.size()); - char *const begin = &*result->begin(); - char *out = Append2(begin + old_size, a, b); - out = Append1(out, c); - GOOGLE_DCHECK_EQ(out, begin + result->size()); -} - -void StrAppend(std::string *result, const AlphaNum &a, const AlphaNum &b, - const AlphaNum &c, const AlphaNum &d) { - GOOGLE_DCHECK_NO_OVERLAP(*result, a); - GOOGLE_DCHECK_NO_OVERLAP(*result, b); - GOOGLE_DCHECK_NO_OVERLAP(*result, c); - GOOGLE_DCHECK_NO_OVERLAP(*result, d); - std::string::size_type old_size = result->size(); - result->resize(old_size + a.size() + b.size() + c.size() + d.size()); - char *const begin = &*result->begin(); - char *out = Append4(begin + old_size, a, b, c, d); - GOOGLE_DCHECK_EQ(out, begin + result->size()); -} - int GlobalReplaceSubstring(const std::string &substring, const std::string &replacement, std::string *s) { GOOGLE_CHECK(s != nullptr); @@ -1621,11 +1039,11 @@ int GlobalReplaceSubstring(const std::string &substring, std::string tmp; int num_replacements = 0; int pos = 0; - for (StringPiece::size_type match_pos = + for (absl::string_view::size_type match_pos = s->find(substring.data(), pos, substring.length()); match_pos != std::string::npos; pos = match_pos + substring.length(), - match_pos = s->find(substring.data(), pos, - substring.length())) { + match_pos = s->find(substring.data(), pos, + substring.length())) { ++num_replacements; // Append the original content before the match. tmp.append(*s, pos, match_pos - pos); @@ -1641,6 +1059,7 @@ int GlobalReplaceSubstring(const std::string &substring, return num_replacements; } +namespace { int CalculateBase64EscapedLen(int input_len, bool do_padding) { // Base64 encodes three bytes of input at a time. If the input is not // divisible by three, we pad as appropriate. @@ -1655,7 +1074,6 @@ int CalculateBase64EscapedLen(int input_len, bool do_padding) { // 64 input is an integral number of octets, only the following cases // can arise: - // Base64 encodes each three bytes of input into four bytes of output. int len = (input_len / 3) * 4; @@ -1693,403 +1111,8 @@ int CalculateBase64EscapedLen(int input_len) { return CalculateBase64EscapedLen(input_len, true); } -// ---------------------------------------------------------------------- -// int Base64Unescape() - base64 decoder -// int Base64Escape() - base64 encoder -// int WebSafeBase64Unescape() - Google's variation of base64 decoder -// int WebSafeBase64Escape() - Google's variation of base64 encoder -// -// Check out -// http://tools.ietf.org/html/rfc2045 for formal description, but what we -// care about is that... -// Take the encoded stuff in groups of 4 characters and turn each -// character into a code 0 to 63 thus: -// A-Z map to 0 to 25 -// a-z map to 26 to 51 -// 0-9 map to 52 to 61 -// +(- for WebSafe) maps to 62 -// /(_ for WebSafe) maps to 63 -// There will be four numbers, all less than 64 which can be represented -// by a 6 digit binary number (aaaaaa, bbbbbb, cccccc, dddddd respectively). -// Arrange the 6 digit binary numbers into three bytes as such: -// aaaaaabb bbbbcccc ccdddddd -// Equals signs (one or two) are used at the end of the encoded block to -// indicate that the text was not an integer multiple of three bytes long. -// ---------------------------------------------------------------------- - -int Base64UnescapeInternal(const char *src_param, int szsrc, - char *dest, int szdest, - const signed char* unbase64) { - static const char kPad64Equals = '='; - static const char kPad64Dot = '.'; - - int decode = 0; - int destidx = 0; - int state = 0; - unsigned int ch = 0; - unsigned int temp = 0; - - // If "char" is signed by default, using *src as an array index results in - // accessing negative array elements. Treat the input as a pointer to - // unsigned char to avoid this. - const unsigned char *src = reinterpret_cast(src_param); - - // The GET_INPUT macro gets the next input character, skipping - // over any whitespace, and stopping when we reach the end of the - // string or when we read any non-data character. The arguments are - // an arbitrary identifier (used as a label for goto) and the number - // of data bytes that must remain in the input to avoid aborting the - // loop. -#define GET_INPUT(label, remain) \ - label: \ - --szsrc; \ - ch = *src++; \ - decode = unbase64[ch]; \ - if (decode < 0) { \ - if (ascii_isspace(ch) && szsrc >= remain) \ - goto label; \ - state = 4 - remain; \ - break; \ - } - - // if dest is null, we're just checking to see if it's legal input - // rather than producing output. (I suspect this could just be done - // with a regexp...). We duplicate the loop so this test can be - // outside it instead of in every iteration. - - if (dest) { - // This loop consumes 4 input bytes and produces 3 output bytes - // per iteration. We can't know at the start that there is enough - // data left in the string for a full iteration, so the loop may - // break out in the middle; if so 'state' will be set to the - // number of input bytes read. - - while (szsrc >= 4) { - // We'll start by optimistically assuming that the next four - // bytes of the string (src[0..3]) are four good data bytes - // (that is, no nulls, whitespace, padding chars, or illegal - // chars). We need to test src[0..2] for nulls individually - // before constructing temp to preserve the property that we - // never read past a null in the string (no matter how long - // szsrc claims the string is). - - if (!src[0] || !src[1] || !src[2] || - (temp = ((unsigned(unbase64[src[0]]) << 18) | - (unsigned(unbase64[src[1]]) << 12) | - (unsigned(unbase64[src[2]]) << 6) | - (unsigned(unbase64[src[3]])))) & 0x80000000) { - // Iff any of those four characters was bad (null, illegal, - // whitespace, padding), then temp's high bit will be set - // (because unbase64[] is -1 for all bad characters). - // - // We'll back up and resort to the slower decoder, which knows - // how to handle those cases. - - GET_INPUT(first, 4); - temp = decode; - GET_INPUT(second, 3); - temp = (temp << 6) | decode; - GET_INPUT(third, 2); - temp = (temp << 6) | decode; - GET_INPUT(fourth, 1); - temp = (temp << 6) | decode; - } else { - // We really did have four good data bytes, so advance four - // characters in the string. - - szsrc -= 4; - src += 4; - decode = -1; - ch = '\0'; - } - - // temp has 24 bits of input, so write that out as three bytes. - - if (destidx+3 > szdest) return -1; - dest[destidx+2] = temp; - temp >>= 8; - dest[destidx+1] = temp; - temp >>= 8; - dest[destidx] = temp; - destidx += 3; - } - } else { - while (szsrc >= 4) { - if (!src[0] || !src[1] || !src[2] || - (temp = ((unsigned(unbase64[src[0]]) << 18) | - (unsigned(unbase64[src[1]]) << 12) | - (unsigned(unbase64[src[2]]) << 6) | - (unsigned(unbase64[src[3]])))) & 0x80000000) { - GET_INPUT(first_no_dest, 4); - GET_INPUT(second_no_dest, 3); - GET_INPUT(third_no_dest, 2); - GET_INPUT(fourth_no_dest, 1); - } else { - szsrc -= 4; - src += 4; - decode = -1; - ch = '\0'; - } - destidx += 3; - } - } - -#undef GET_INPUT - - // if the loop terminated because we read a bad character, return - // now. - if (decode < 0 && ch != '\0' && - ch != kPad64Equals && ch != kPad64Dot && !ascii_isspace(ch)) - return -1; - - if (ch == kPad64Equals || ch == kPad64Dot) { - // if we stopped by hitting an '=' or '.', un-read that character -- we'll - // look at it again when we count to check for the proper number of - // equals signs at the end. - ++szsrc; - --src; - } else { - // This loop consumes 1 input byte per iteration. It's used to - // clean up the 0-3 input bytes remaining when the first, faster - // loop finishes. 'temp' contains the data from 'state' input - // characters read by the first loop. - while (szsrc > 0) { - --szsrc; - ch = *src++; - decode = unbase64[ch]; - if (decode < 0) { - if (ascii_isspace(ch)) { - continue; - } else if (ch == '\0') { - break; - } else if (ch == kPad64Equals || ch == kPad64Dot) { - // back up one character; we'll read it again when we check - // for the correct number of pad characters at the end. - ++szsrc; - --src; - break; - } else { - return -1; - } - } - - // Each input character gives us six bits of output. - temp = (temp << 6) | decode; - ++state; - if (state == 4) { - // If we've accumulated 24 bits of output, write that out as - // three bytes. - if (dest) { - if (destidx+3 > szdest) return -1; - dest[destidx+2] = temp; - temp >>= 8; - dest[destidx+1] = temp; - temp >>= 8; - dest[destidx] = temp; - } - destidx += 3; - state = 0; - temp = 0; - } - } - } - - // Process the leftover data contained in 'temp' at the end of the input. - int expected_equals = 0; - switch (state) { - case 0: - // Nothing left over; output is a multiple of 3 bytes. - break; - - case 1: - // Bad input; we have 6 bits left over. - return -1; - - case 2: - // Produce one more output byte from the 12 input bits we have left. - if (dest) { - if (destidx+1 > szdest) return -1; - temp >>= 4; - dest[destidx] = temp; - } - ++destidx; - expected_equals = 2; - break; - - case 3: - // Produce two more output bytes from the 18 input bits we have left. - if (dest) { - if (destidx+2 > szdest) return -1; - temp >>= 2; - dest[destidx+1] = temp; - temp >>= 8; - dest[destidx] = temp; - } - destidx += 2; - expected_equals = 1; - break; - - default: - // state should have no other values at this point. - GOOGLE_LOG(FATAL) << "This can't happen; base64 decoder state = " << state; - } - - // The remainder of the string should be all whitespace, mixed with - // exactly 0 equals signs, or exactly 'expected_equals' equals - // signs. (Always accepting 0 equals signs is a google extension - // not covered in the RFC, as is accepting dot as the pad character.) - - int equals = 0; - while (szsrc > 0 && *src) { - if (*src == kPad64Equals || *src == kPad64Dot) - ++equals; - else if (!ascii_isspace(*src)) - return -1; - --szsrc; - ++src; - } - - return (equals == 0 || equals == expected_equals) ? destidx : -1; -} - -// The arrays below were generated by the following code -// #include -// #include -// #include -// #include -// main() -// { -// static const char Base64[] = -// "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -// const char *pos; -// int idx, i, j; -// printf(" "); -// for (i = 0; i < 255; i += 8) { -// for (j = i; j < i + 8; j++) { -// pos = strchr(Base64, j); -// if ((pos == nullptr) || (j == 0)) -// idx = -1; -// else -// idx = pos - Base64; -// if (idx == -1) -// printf(" %2d, ", idx); -// else -// printf(" %2d/""*%c*""/,", idx, j); -// } -// printf("\n "); -// } -// } -// -// where the value of "Base64[]" was replaced by one of the base-64 conversion -// tables from the functions below. -static const signed char kUnBase64[] = { - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, 62/*+*/, -1, -1, -1, 63/*/ */, - 52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/, - 60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1, - -1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/, - 7/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/, - 15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/, - 23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, -1, - -1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/, - 33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/, - 41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/, - 49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1 -}; -static const signed char kUnWebSafeBase64[] = { - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, 62/*-*/, -1, -1, - 52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/, - 60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1, - -1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/, - 7/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/, - 15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/, - 23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, 63/*_*/, - -1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/, - 33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/, - 41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/, - 49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1 -}; - -int WebSafeBase64Unescape(const char *src, int szsrc, char *dest, int szdest) { - return Base64UnescapeInternal(src, szsrc, dest, szdest, kUnWebSafeBase64); -} - -static bool Base64UnescapeInternal(const char *src, int slen, std::string *dest, - const signed char *unbase64) { - // Determine the size of the output string. Base64 encodes every 3 bytes into - // 4 characters. any leftover chars are added directly for good measure. - // This is documented in the base64 RFC: http://tools.ietf.org/html/rfc3548 - const int dest_len = 3 * (slen / 4) + (slen % 4); - - dest->resize(dest_len); - - // We are getting the destination buffer by getting the beginning of the - // string and converting it into a char *. - const int len = Base64UnescapeInternal(src, slen, string_as_array(dest), - dest_len, unbase64); - if (len < 0) { - dest->clear(); - return false; - } - - // could be shorter if there was padding - GOOGLE_DCHECK_LE(len, dest_len); - dest->erase(len); - - return true; -} - -bool Base64Unescape(StringPiece src, std::string *dest) { - return Base64UnescapeInternal(src.data(), src.size(), dest, kUnBase64); -} - -bool WebSafeBase64Unescape(StringPiece src, std::string *dest) { - return Base64UnescapeInternal(src.data(), src.size(), dest, kUnWebSafeBase64); -} - -int Base64EscapeInternal(const unsigned char *src, int szsrc, - char *dest, int szdest, const char *base64, +int Base64EscapeInternal(const unsigned char *src, int szsrc, char *dest, + int szdest, const absl::string_view base64, bool do_padding) { static const char kPad64 = '='; @@ -2188,61 +1211,42 @@ int Base64EscapeInternal(const unsigned char *src, int szsrc, return (cur_dest - dest); } -static const char kBase64Chars[] = -"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -static const char kWebSafeBase64Chars[] = -"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; - -int Base64Escape(const unsigned char *src, int szsrc, char *dest, int szdest) { - return Base64EscapeInternal(src, szsrc, dest, szdest, kBase64Chars, true); -} -int WebSafeBase64Escape(const unsigned char *src, int szsrc, char *dest, - int szdest, bool do_padding) { - return Base64EscapeInternal(src, szsrc, dest, szdest, - kWebSafeBase64Chars, do_padding); -} - void Base64EscapeInternal(const unsigned char *src, int szsrc, std::string *dest, bool do_padding, - const char *base64_chars) { - const int calc_escaped_size = - CalculateBase64EscapedLen(szsrc, do_padding); + const absl::string_view base64_chars) { + const int calc_escaped_size = CalculateBase64EscapedLen(szsrc, do_padding); dest->resize(calc_escaped_size); - const int escaped_len = Base64EscapeInternal(src, szsrc, - string_as_array(dest), - dest->size(), - base64_chars, - do_padding); + const int escaped_len = + Base64EscapeInternal(src, szsrc, string_as_array(dest), dest->size(), + base64_chars, do_padding); GOOGLE_DCHECK_EQ(calc_escaped_size, escaped_len); dest->erase(escaped_len); } -void Base64Escape(const unsigned char *src, int szsrc, std::string *dest, - bool do_padding) { - Base64EscapeInternal(src, szsrc, dest, do_padding, kBase64Chars); -} +static constexpr absl::string_view kBase64Chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -void WebSafeBase64Escape(const unsigned char *src, int szsrc, std::string *dest, - bool do_padding) { - Base64EscapeInternal(src, szsrc, dest, do_padding, kWebSafeBase64Chars); -} +static constexpr absl::string_view kWebSafeBase64Chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; -void Base64Escape(StringPiece src, std::string *dest) { - Base64Escape(reinterpret_cast(src.data()), - src.size(), dest, true); -} +} // namespace + +namespace strings { -void WebSafeBase64Escape(StringPiece src, std::string *dest) { - WebSafeBase64Escape(reinterpret_cast(src.data()), - src.size(), dest, false); +void LegacyBase64EscapeWithoutPadding(absl::string_view src, + std::string *dest) { + Base64EscapeInternal(reinterpret_cast(src.data()), + src.size(), dest, /*do_padding=*/false, kBase64Chars); } -void WebSafeBase64EscapeWithPadding(StringPiece src, std::string *dest) { - WebSafeBase64Escape(reinterpret_cast(src.data()), - src.size(), dest, true); +void WebSafeBase64EscapeWithPadding(absl::string_view src, std::string *dest) { + Base64EscapeInternal(reinterpret_cast(src.data()), + src.size(), dest, + /*do_padding=*/true, kWebSafeBase64Chars); } +} // namespace strings + // Helper to append a Unicode code point to a string as UTF8, without bringing // in any external dependencies. int EncodeAsUTF8Char(uint32_t code_point, char* output) { @@ -2301,111 +1305,6 @@ int UTF8FirstLetterNumBytes(const char* src, int len) { return kUTF8LenTbl[*reinterpret_cast(src)]; } -// ---------------------------------------------------------------------- -// CleanStringLineEndings() -// Clean up a multi-line string to conform to Unix line endings. -// Reads from src and appends to dst, so usually dst should be empty. -// -// If there is no line ending at the end of a non-empty string, it can -// be added automatically. -// -// Four different types of input are correctly handled: -// -// - Unix/Linux files: line ending is LF: pass through unchanged -// -// - DOS/Windows files: line ending is CRLF: convert to LF -// -// - Legacy Mac files: line ending is CR: convert to LF -// -// - Garbled files: random line endings: convert gracefully -// lonely CR, lonely LF, CRLF: convert to LF -// -// @param src The multi-line string to convert -// @param dst The converted string is appended to this string -// @param auto_end_last_line Automatically terminate the last line -// -// Limitations: -// -// This does not do the right thing for CRCRLF files created by -// broken programs that do another Unix->DOS conversion on files -// that are already in CRLF format. For this, a two-pass approach -// brute-force would be needed that -// -// (1) determines the presence of LF (first one is ok) -// (2) if yes, removes any CR, else convert every CR to LF - -void CleanStringLineEndings(const std::string &src, std::string *dst, - bool auto_end_last_line) { - if (dst->empty()) { - dst->append(src); - CleanStringLineEndings(dst, auto_end_last_line); - } else { - std::string tmp = src; - CleanStringLineEndings(&tmp, auto_end_last_line); - dst->append(tmp); - } -} - -void CleanStringLineEndings(std::string *str, bool auto_end_last_line) { - ptrdiff_t output_pos = 0; - bool r_seen = false; - ptrdiff_t len = str->size(); - - char *p = &(*str)[0]; - - for (ptrdiff_t input_pos = 0; input_pos < len;) { - if (!r_seen && input_pos + 8 < len) { - uint64_t v = GOOGLE_UNALIGNED_LOAD64(p + input_pos); - // Loop over groups of 8 bytes at a time until we come across - // a word that has a byte whose value is less than or equal to - // '\r' (i.e. could contain a \n (0x0a) or a \r (0x0d) ). - // - // We use a has_less macro that quickly tests a whole 64-bit - // word to see if any of the bytes has a value < N. - // - // For more details, see: - // http://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord -#define has_less(x, n) (((x) - ~0ULL / 255 * (n)) & ~(x) & ~0ULL / 255 * 128) - if (!has_less(v, '\r' + 1)) { -#undef has_less - // No byte in this word has a value that could be a \r or a \n - if (output_pos != input_pos) { - GOOGLE_UNALIGNED_STORE64(p + output_pos, v); - } - input_pos += 8; - output_pos += 8; - continue; - } - } - std::string::const_reference in = p[input_pos]; - if (in == '\r') { - if (r_seen) p[output_pos++] = '\n'; - r_seen = true; - } else if (in == '\n') { - if (input_pos != output_pos) - p[output_pos++] = '\n'; - else - output_pos++; - r_seen = false; - } else { - if (r_seen) p[output_pos++] = '\n'; - r_seen = false; - if (input_pos != output_pos) - p[output_pos++] = in; - else - output_pos++; - } - input_pos++; - } - if (r_seen || - (auto_end_last_line && output_pos > 0 && p[output_pos - 1] != '\n')) { - str->resize(output_pos + 1); - str->operator[](output_pos) = '\n'; - } else if (output_pos < len) { - str->resize(output_pos); - } -} - namespace internal { // ---------------------------------------------------------------------- diff --git a/src/google/protobuf/stubs/strutil.h b/src/google/protobuf/stubs/strutil.h index 9658abf9080f..853a570eb977 100644 --- a/src/google/protobuf/stubs/strutil.h +++ b/src/google/protobuf/stubs/strutil.h @@ -34,13 +34,16 @@ #define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__ #include -#include #include #include #include #include +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/string_view.h" + namespace google { namespace protobuf { @@ -53,49 +56,6 @@ namespace protobuf { #define strtoull strtoul #endif -// ---------------------------------------------------------------------- -// ascii_isalnum() -// Check if an ASCII character is alphanumeric. We can't use ctype's -// isalnum() because it is affected by locale. This function is applied -// to identifiers in the protocol buffer language, not to natural-language -// strings, so locale should not be taken into account. -// ascii_isdigit() -// Like above, but only accepts digits. -// ascii_isspace() -// Check if the character is a space character. -// ---------------------------------------------------------------------- - -inline bool ascii_isalnum(char c) { - return ('a' <= c && c <= 'z') || - ('A' <= c && c <= 'Z') || - ('0' <= c && c <= '9'); -} - -inline bool ascii_isdigit(char c) { - return ('0' <= c && c <= '9'); -} - -inline bool ascii_isspace(char c) { - return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || - c == '\r'; -} - -inline bool ascii_isupper(char c) { - return c >= 'A' && c <= 'Z'; -} - -inline bool ascii_islower(char c) { - return c >= 'a' && c <= 'z'; -} - -inline char ascii_toupper(char c) { - return ascii_islower(c) ? c - ('a' - 'A') : c; -} - -inline char ascii_tolower(char c) { - return ascii_isupper(c) ? c + ('a' - 'A') : c; -} - inline int hex_digit_to_int(char c) { /* Assume ASCII. */ int x = static_cast(c); @@ -113,7 +73,7 @@ inline int hex_digit_to_int(char c) { // prefix string if the prefix matches, otherwise the original // string. // ---------------------------------------------------------------------- -inline bool HasPrefixString(StringPiece str, StringPiece prefix) { +inline bool HasPrefixString(absl::string_view str, absl::string_view prefix) { return str.size() >= prefix.size() && memcmp(str.data(), prefix.data(), prefix.size()) == 0; } @@ -135,7 +95,7 @@ inline std::string StripPrefixString(const std::string& str, // suffix string if the suffix matches, otherwise the original // string. // ---------------------------------------------------------------------- -inline bool HasSuffixString(StringPiece str, StringPiece suffix) { +inline bool HasSuffixString(absl::string_view str, absl::string_view suffix) { return str.size() >= suffix.size() && memcmp(str.data() + str.size() - suffix.size(), suffix.data(), suffix.size()) == 0; @@ -217,7 +177,7 @@ PROTOBUF_EXPORT std::string StringReplace(const std::string& s, // to 'result'. If there are consecutive delimiters, this function skips // over all of them. // ---------------------------------------------------------------------- -PROTOBUF_EXPORT void SplitStringUsing(StringPiece full, const char* delim, +PROTOBUF_EXPORT void SplitStringUsing(absl::string_view full, const char* delim, std::vector* res); // Split a string using one or more byte delimiters, presented @@ -228,14 +188,15 @@ PROTOBUF_EXPORT void SplitStringUsing(StringPiece full, const char* delim, // // If "full" is the empty string, yields an empty string as the only value. // ---------------------------------------------------------------------- -PROTOBUF_EXPORT void SplitStringAllowEmpty(StringPiece full, const char* delim, +PROTOBUF_EXPORT void SplitStringAllowEmpty(absl::string_view full, + const char* delim, std::vector* result); // ---------------------------------------------------------------------- // Split() // Split a string using a character delimiter. // ---------------------------------------------------------------------- -inline std::vector Split(StringPiece full, const char* delim, +inline std::vector Split(absl::string_view full, const char* delim, bool skip_empty = true) { std::vector result; if (skip_empty) { @@ -246,105 +207,6 @@ inline std::vector Split(StringPiece full, const char* delim, return result; } -// ---------------------------------------------------------------------- -// JoinStrings() -// These methods concatenate a vector of strings into a C++ string, using -// the C-string "delim" as a separator between components. There are two -// flavors of the function, one flavor returns the concatenated string, -// another takes a pointer to the target string. In the latter case the -// target string is cleared and overwritten. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT void JoinStrings(const std::vector& components, - const char* delim, std::string* result); - -inline std::string JoinStrings(const std::vector& components, - const char* delim) { - std::string result; - JoinStrings(components, delim, &result); - return result; -} - -// ---------------------------------------------------------------------- -// UnescapeCEscapeSequences() -// Copies "source" to "dest", rewriting C-style escape sequences -// -- '\n', '\r', '\\', '\ooo', etc -- to their ASCII -// equivalents. "dest" must be sufficiently large to hold all -// the characters in the rewritten string (i.e. at least as large -// as strlen(source) + 1 should be safe, since the replacements -// are always shorter than the original escaped sequences). It's -// safe for source and dest to be the same. RETURNS the length -// of dest. -// -// It allows hex sequences \xhh, or generally \xhhhhh with an -// arbitrary number of hex digits, but all of them together must -// specify a value of a single byte (e.g. \x0045 is equivalent -// to \x45, and \x1234 is erroneous). -// -// It also allows escape sequences of the form \uhhhh (exactly four -// hex digits, upper or lower case) or \Uhhhhhhhh (exactly eight -// hex digits, upper or lower case) to specify a Unicode code -// point. The dest array will contain the UTF8-encoded version of -// that code-point (e.g., if source contains \u2019, then dest will -// contain the three bytes 0xE2, 0x80, and 0x99). -// -// Errors: In the first form of the call, errors are reported with -// LOG(ERROR). The same is true for the second form of the call if -// the pointer to the string std::vector is nullptr; otherwise, error -// messages are stored in the std::vector. In either case, the effect on -// the dest array is not defined, but rest of the source will be -// processed. -// ---------------------------------------------------------------------- - -PROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest); -PROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest, - std::vector* errors); - -// ---------------------------------------------------------------------- -// UnescapeCEscapeString() -// This does the same thing as UnescapeCEscapeSequences, but creates -// a new string. The caller does not need to worry about allocating -// a dest buffer. This should be used for non performance critical -// tasks such as printing debug messages. It is safe for src and dest -// to be the same. -// -// The second call stores its errors in a supplied string vector. -// If the string vector pointer is nullptr, it reports the errors with LOG(). -// -// In the first and second calls, the length of dest is returned. In the -// the third call, the new string is returned. -// ---------------------------------------------------------------------- - -PROTOBUF_EXPORT int UnescapeCEscapeString(const std::string& src, - std::string* dest); -PROTOBUF_EXPORT int UnescapeCEscapeString(const std::string& src, - std::string* dest, - std::vector* errors); -PROTOBUF_EXPORT std::string UnescapeCEscapeString(const std::string& src); - -// ---------------------------------------------------------------------- -// CEscape() -// Escapes 'src' using C-style escape sequences and returns the resulting -// string. -// -// Escaped chars: \n, \r, \t, ", ', \, and !isprint(). -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT std::string CEscape(const std::string& src); - -// ---------------------------------------------------------------------- -// CEscapeAndAppend() -// Escapes 'src' using C-style escape sequences, and appends the escaped -// string to 'dest'. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT void CEscapeAndAppend(StringPiece src, std::string* dest); - -namespace strings { -// Like CEscape() but does not escape bytes with the upper bit set. -PROTOBUF_EXPORT std::string Utf8SafeCEscape(const std::string& src); - -// Like CEscape() but uses hex (\x) escapes instead of octals. -PROTOBUF_EXPORT std::string CHexEscape(const std::string& src); -} // namespace strings - // ---------------------------------------------------------------------- // strto32() // strtou32() @@ -397,21 +259,21 @@ inline uint64_t strtou64(const char *nptr, char **endptr, int base) { // safe_strtof() // safe_strtod() // ---------------------------------------------------------------------- -PROTOBUF_EXPORT bool safe_strtob(StringPiece str, bool* value); +PROTOBUF_EXPORT bool safe_strtob(absl::string_view str, bool* value); PROTOBUF_EXPORT bool safe_strto32(const std::string& str, int32_t* value); PROTOBUF_EXPORT bool safe_strtou32(const std::string& str, uint32_t* value); inline bool safe_strto32(const char* str, int32_t* value) { return safe_strto32(std::string(str), value); } -inline bool safe_strto32(StringPiece str, int32_t* value) { - return safe_strto32(str.ToString(), value); +inline bool safe_strto32(absl::string_view str, int32_t* value) { + return safe_strto32(std::string(str), value); } inline bool safe_strtou32(const char* str, uint32_t* value) { return safe_strtou32(std::string(str), value); } -inline bool safe_strtou32(StringPiece str, uint32_t* value) { - return safe_strtou32(str.ToString(), value); +inline bool safe_strtou32(absl::string_view str, uint32_t* value) { + return safe_strtou32(std::string(str), value); } PROTOBUF_EXPORT bool safe_strto64(const std::string& str, int64_t* value); @@ -419,14 +281,14 @@ PROTOBUF_EXPORT bool safe_strtou64(const std::string& str, uint64_t* value); inline bool safe_strto64(const char* str, int64_t* value) { return safe_strto64(std::string(str), value); } -inline bool safe_strto64(StringPiece str, int64_t* value) { - return safe_strto64(str.ToString(), value); +inline bool safe_strto64(absl::string_view str, int64_t* value) { + return safe_strto64(std::string(str), value); } inline bool safe_strtou64(const char* str, uint64_t* value) { return safe_strtou64(std::string(str), value); } -inline bool safe_strtou64(StringPiece str, uint64_t* value) { - return safe_strtou64(str.ToString(), value); +inline bool safe_strtou64(absl::string_view str, uint64_t* value) { + return safe_strtou64(std::string(str), value); } PROTOBUF_EXPORT bool safe_strtof(const char* str, float* value); @@ -437,11 +299,11 @@ inline bool safe_strtof(const std::string& str, float* value) { inline bool safe_strtod(const std::string& str, double* value) { return safe_strtod(str.c_str(), value); } -inline bool safe_strtof(StringPiece str, float* value) { - return safe_strtof(str.ToString(), value); +inline bool safe_strtof(absl::string_view str, float* value) { + return safe_strtof(std::string(str), value); } -inline bool safe_strtod(StringPiece str, double* value) { - return safe_strtod(str.ToString(), value); +inline bool safe_strtod(absl::string_view str, double* value) { + return safe_strtod(std::string(str), value); } // ---------------------------------------------------------------------- @@ -575,216 +437,10 @@ static const int kFloatToBufferSize = 24; namespace strings { -enum PadSpec { - NO_PAD = 1, - ZERO_PAD_2, - ZERO_PAD_3, - ZERO_PAD_4, - ZERO_PAD_5, - ZERO_PAD_6, - ZERO_PAD_7, - ZERO_PAD_8, - ZERO_PAD_9, - ZERO_PAD_10, - ZERO_PAD_11, - ZERO_PAD_12, - ZERO_PAD_13, - ZERO_PAD_14, - ZERO_PAD_15, - ZERO_PAD_16, -}; - -struct Hex { - uint64_t value; - enum PadSpec spec; - template - explicit Hex(Int v, PadSpec s = NO_PAD) - : spec(s) { - // Prevent sign-extension by casting integers to - // their unsigned counterparts. -#ifdef LANG_CXX11 - static_assert( - sizeof(v) == 1 || sizeof(v) == 2 || sizeof(v) == 4 || sizeof(v) == 8, - "Unknown integer type"); -#endif - value = sizeof(v) == 1 ? static_cast(v) - : sizeof(v) == 2 ? static_cast(v) - : sizeof(v) == 4 ? static_cast(v) - : static_cast(v); - } -}; - -struct PROTOBUF_EXPORT AlphaNum { - const char *piece_data_; // move these to string_ref eventually - size_t piece_size_; // move these to string_ref eventually - - char digits[kFastToBufferSize]; - - // No bool ctor -- bools convert to an integral type. - // A bool ctor would also convert incoming pointers (bletch). - - AlphaNum(int i32) - : piece_data_(digits), - piece_size_(FastInt32ToBufferLeft(i32, digits) - &digits[0]) {} - AlphaNum(unsigned int u32) - : piece_data_(digits), - piece_size_(FastUInt32ToBufferLeft(u32, digits) - &digits[0]) {} - AlphaNum(long long i64) - : piece_data_(digits), - piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {} - AlphaNum(unsigned long long u64) - : piece_data_(digits), - piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {} - - // Note: on some architectures, "long" is only 32 bits, not 64, but the - // performance hit of using FastInt64ToBufferLeft to handle 32-bit values - // is quite minor. - AlphaNum(long i64) - : piece_data_(digits), - piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {} - AlphaNum(unsigned long u64) - : piece_data_(digits), - piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {} - - AlphaNum(float f) - : piece_data_(digits), piece_size_(strlen(FloatToBuffer(f, digits))) {} - AlphaNum(double f) - : piece_data_(digits), piece_size_(strlen(DoubleToBuffer(f, digits))) {} - - AlphaNum(Hex hex); - - AlphaNum(const char* c_str) - : piece_data_(c_str), piece_size_(strlen(c_str)) {} - // TODO: Add a string_ref constructor, eventually - // AlphaNum(const StringPiece &pc) : piece(pc) {} - - AlphaNum(const std::string& str) - : piece_data_(str.data()), piece_size_(str.size()) {} - - AlphaNum(StringPiece str) - : piece_data_(str.data()), piece_size_(str.size()) {} - - size_t size() const { return piece_size_; } - const char *data() const { return piece_data_; } - - private: - // Use ":" not ':' - AlphaNum(char c); // NOLINT(runtime/explicit) - - // Disallow copy and assign. - AlphaNum(const AlphaNum&); - void operator=(const AlphaNum&); -}; +using Hex = absl::Hex; } // namespace strings -using strings::AlphaNum; - -// ---------------------------------------------------------------------- -// StrCat() -// This merges the given strings or numbers, with no delimiter. This -// is designed to be the fastest possible way to construct a string out -// of a mix of raw C strings, strings, bool values, -// and numeric values. -// -// Don't use this for user-visible strings. The localization process -// works poorly on strings built up out of fragments. -// -// For clarity and performance, don't use StrCat when appending to a -// string. In particular, avoid using any of these (anti-)patterns: -// str.append(StrCat(...) -// str += StrCat(...) -// str = StrCat(str, ...) -// where the last is the worse, with the potential to change a loop -// from a linear time operation with O(1) dynamic allocations into a -// quadratic time operation with O(n) dynamic allocations. StrAppend -// is a better choice than any of the above, subject to the restriction -// of StrAppend(&str, a, b, c, ...) that none of the a, b, c, ... may -// be a reference into str. -// ---------------------------------------------------------------------- - -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, const AlphaNum& f); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, const AlphaNum& f, - const AlphaNum& g); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, const AlphaNum& f, - const AlphaNum& g, const AlphaNum& h); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, const AlphaNum& f, - const AlphaNum& g, const AlphaNum& h, - const AlphaNum& i); - -inline std::string StrCat(const AlphaNum& a) { - return std::string(a.data(), a.size()); -} - -// ---------------------------------------------------------------------- -// StrAppend() -// Same as above, but adds the output to the given string. -// WARNING: For speed, StrAppend does not try to check each of its input -// arguments to be sure that they are not a subset of the string being -// appended to. That is, while this will work: -// -// string s = "foo"; -// s += s; -// -// This will not (necessarily) work: -// -// string s = "foo"; -// StrAppend(&s, s); -// -// Note: while StrCat supports appending up to 9 arguments, StrAppend -// is currently limited to 4. That's rarely an issue except when -// automatically transforming StrCat to StrAppend, and can easily be -// worked around as consecutive calls to StrAppend are quite efficient. -// ---------------------------------------------------------------------- - -PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a); -PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a, - const AlphaNum& b); -PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a, - const AlphaNum& b, const AlphaNum& c); -PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a, - const AlphaNum& b, const AlphaNum& c, - const AlphaNum& d); - -// ---------------------------------------------------------------------- -// Join() -// These methods concatenate a range of components into a C++ string, using -// the C-string "delim" as a separator between components. -// ---------------------------------------------------------------------- -template -void Join(Iterator start, Iterator end, const char* delim, - std::string* result) { - for (Iterator it = start; it != end; ++it) { - if (it != start) { - result->append(delim); - } - StrAppend(result, *it); - } -} - -template -std::string Join(const Range& components, const char* delim) { - std::string result; - Join(components.begin(), components.end(), delim, &result); - return result; -} - // ---------------------------------------------------------------------- // ToHex() // Return a lower-case hex string representation of the given integer. @@ -802,66 +458,13 @@ PROTOBUF_EXPORT int GlobalReplaceSubstring(const std::string& substring, const std::string& replacement, std::string* s); -// ---------------------------------------------------------------------- -// Base64Unescape() -// Converts "src" which is encoded in Base64 to its binary equivalent and -// writes it to "dest". If src contains invalid characters, dest is cleared -// and the function returns false. Returns true on success. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT bool Base64Unescape(StringPiece src, std::string* dest); - -// ---------------------------------------------------------------------- -// WebSafeBase64Unescape() -// This is a variation of Base64Unescape which uses '-' instead of '+', and -// '_' instead of '/'. src is not null terminated, instead specify len. I -// recommend that slen(base64_tests[i].plaintext); - - StringPiece plaintext(base64_tests[i].plaintext, - base64_tests[i].plain_length); - - cipher_length = strlen(base64_tests[i].ciphertext); - - // The basic escape function: - memset(encode_buffer, 0, sizeof(encode_buffer)); - encode_length = Base64Escape(unsigned_plaintext, - base64_tests[i].plain_length, - encode_buffer, - sizeof(encode_buffer)); - // Is it of the expected length? - EXPECT_EQ(encode_length, cipher_length); - // Would it have been okay to allocate only CalculateBase64EscapeLen()? - EXPECT_EQ(CalculateBase64EscapedLen(base64_tests[i].plain_length), - encode_length); - - // Is it the expected encoded value? - ASSERT_STREQ(encode_buffer, base64_tests[i].ciphertext); - - // If we encode it into a buffer of exactly the right length... - memset(encode_buffer, 0, sizeof(encode_buffer)); - encode_length = - Base64Escape(unsigned_plaintext, base64_tests[i].plain_length, - encode_buffer, cipher_length); - // Is it still of the expected length? - EXPECT_EQ(encode_length, cipher_length); - - // And is the value still correct? (i.e., not losing the last byte) - EXPECT_STREQ(encode_buffer, base64_tests[i].ciphertext); - - // If we decode it back: - decode_str.clear(); - EXPECT_TRUE( - Base64Unescape(StringPiece(encode_buffer, cipher_length), &decode_str)); - - // Is it of the expected length? - EXPECT_EQ(base64_tests[i].plain_length, decode_str.length()); - - // Is it the expected decoded value? - EXPECT_EQ(plaintext, decode_str); - - // Let's try with a pre-populated string. - std::string encoded("this junk should be ignored"); - Base64Escape( - std::string(base64_tests[i].plaintext, base64_tests[i].plain_length), - &encoded); - EXPECT_EQ(encoded, std::string(encode_buffer, cipher_length)); - - std::string decoded("this junk should be ignored"); - EXPECT_TRUE( - Base64Unescape(StringPiece(encode_buffer, cipher_length), &decoded)); - EXPECT_EQ(decoded.size(), base64_tests[i].plain_length); - EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i); - - // Our decoder treats the padding '=' characters at the end as - // optional (but if there are any, there must be the correct - // number of them.) If encode_buffer has any, run some additional - // tests that fiddle with them. - char* first_equals = strchr(encode_buffer, '='); - if (first_equals) { - // How many equals signs does the string start with? - int equals = (*(first_equals+1) == '=') ? 2 : 1; - - // Try chopping off the equals sign(s) entirely. The decoder - // should still be okay with this. - std::string decoded2("this junk should also be ignored"); - *first_equals = '\0'; - EXPECT_TRUE(Base64Unescape( - StringPiece(encode_buffer, first_equals - encode_buffer), &decoded2)); - EXPECT_EQ(decoded.size(), base64_tests[i].plain_length); - EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i); - - // Now test chopping off the equals sign(s) and adding - // whitespace. Our decoder should still accept this. - decoded2.assign("this junk should be ignored"); - *first_equals = ' '; - *(first_equals+1) = '\0'; - EXPECT_TRUE(Base64Unescape( - StringPiece(encode_buffer, first_equals - encode_buffer + 1), - &decoded2)); - EXPECT_EQ(decoded.size(), base64_tests[i].plain_length); - EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i); - - // Now stick a bad character at the end of the string. The decoder - // should refuse this string. - decoded2.assign("this junk should be ignored"); - *first_equals = '?'; - *(first_equals+1) = '\0'; - EXPECT_TRUE( - !Base64Unescape( - StringPiece(encode_buffer, first_equals - encode_buffer + 1), - &decoded2)); - - int len; - - // Test whitespace mixed with the padding. (eg "AA = = ") The - // decoder should accept this. - if (equals == 2) { - snprintf(first_equals, 6, " = = "); - len = first_equals - encode_buffer + 5; - } else { - snprintf(first_equals, 6, " = "); - len = first_equals - encode_buffer + 3; - } - decoded2.assign("this junk should be ignored"); - EXPECT_TRUE( - Base64Unescape(StringPiece(encode_buffer, len), &decoded2)); - EXPECT_EQ(decoded.size(), base64_tests[i].plain_length); - EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i); - - // Test whitespace mixed with the padding, but with the wrong - // number of equals signs (eg "AA = "). The decoder should - // refuse these strings. - if (equals == 1) { - snprintf(first_equals, 6, " = = "); - len = first_equals - encode_buffer + 5; - } else { - snprintf(first_equals, 6, " = "); - len = first_equals - encode_buffer + 3; - } - EXPECT_TRUE( - !Base64Unescape(StringPiece(encode_buffer, len), &decoded2)); - } - - // Cool! the basic Base64 encoder/decoder works. - // Let's try the alternate alphabet: tr -- '+/' '-_' - - char websafe[100]; - memset(websafe, 0, sizeof(websafe)); - strncpy(websafe, base64_tests[i].ciphertext, cipher_length); - for (int c = 0; c < sizeof(websafe); ++c) { - if ('+' == websafe[c]) { websafe[c] = '-'; } - if ('/' == websafe[c]) { websafe[c] = '_'; } - } - - // The websafe escape function: - memset(encode_buffer, 0, sizeof(encode_buffer)); - encode_length = WebSafeBase64Escape(unsigned_plaintext, - base64_tests[i].plain_length, - encode_buffer, - sizeof(encode_buffer), - true); - // Is it of the expected length? - EXPECT_EQ(encode_length, cipher_length); - EXPECT_EQ( - CalculateBase64EscapedLen(base64_tests[i].plain_length, true), - encode_length); - - // Is it the expected encoded value? - EXPECT_STREQ(encode_buffer, websafe); - - // If we encode it into a buffer of exactly the right length... - memset(encode_buffer, 0, sizeof(encode_buffer)); - encode_length = - WebSafeBase64Escape(unsigned_plaintext, base64_tests[i].plain_length, - encode_buffer, cipher_length, true); - // Is it still of the expected length? - EXPECT_EQ(encode_length, cipher_length); - - // And is the value still correct? (i.e., not losing the last byte) - EXPECT_STREQ(encode_buffer, websafe); - - // Let's try the string version of the encoder - encoded = "this junk should be ignored"; - WebSafeBase64Escape( - unsigned_plaintext, base64_tests[i].plain_length, - &encoded, true); - EXPECT_EQ(encoded.size(), cipher_length); - EXPECT_STREQ(encoded.c_str(), websafe); - - // If we decode it back: - memset(decode_buffer, 0, sizeof(decode_buffer)); - decode_length = WebSafeBase64Unescape(encode_buffer, cipher_length, - decode_buffer, sizeof(decode_buffer)); - - // Is it of the expected length? - EXPECT_EQ(decode_length, base64_tests[i].plain_length); - - // Is it the expected decoded value? - EXPECT_EQ(0, - memcmp(decode_buffer, base64_tests[i].plaintext, decode_length)); - - // If we decode it into a buffer of exactly the right length... - memset(decode_buffer, 0, sizeof(decode_buffer)); - decode_length = WebSafeBase64Unescape(encode_buffer, cipher_length, - decode_buffer, decode_length); - - // Is it still of the expected length? - EXPECT_EQ(decode_length, base64_tests[i].plain_length); - - // And is it the expected decoded value? - EXPECT_EQ(0, - memcmp(decode_buffer, base64_tests[i].plaintext, decode_length)); - - // Try using '.' for the pad character. - for (int c = cipher_length - 1; c >= 0 && '=' == encode_buffer[c]; --c) { - encode_buffer[c] = '.'; - } - - // If we decode it back: - memset(decode_buffer, 0, sizeof(decode_buffer)); - decode_length = WebSafeBase64Unescape(encode_buffer, cipher_length, - decode_buffer, sizeof(decode_buffer)); - - // Is it of the expected length? - EXPECT_EQ(decode_length, base64_tests[i].plain_length); - - // Is it the expected decoded value? - EXPECT_EQ(0, - memcmp(decode_buffer, base64_tests[i].plaintext, decode_length)); - - // If we decode it into a buffer of exactly the right length... - memset(decode_buffer, 0, sizeof(decode_buffer)); - decode_length = WebSafeBase64Unescape(encode_buffer, cipher_length, - decode_buffer, decode_length); - - // Is it still of the expected length? - EXPECT_EQ(decode_length, base64_tests[i].plain_length); - - // And is it the expected decoded value? - EXPECT_EQ(0, - memcmp(decode_buffer, base64_tests[i].plaintext, decode_length)); - - // Let's try the string version of the decoder - decoded = "this junk should be ignored"; - EXPECT_TRUE(WebSafeBase64Unescape(StringPiece(encode_buffer, cipher_length), - &decoded)); - EXPECT_EQ(decoded.size(), base64_tests[i].plain_length); - EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i); - - // Okay! the websafe Base64 encoder/decoder works. - // Let's try the unpadded version - - for (int c = 0; c < sizeof(websafe); ++c) { - if ('=' == websafe[c]) { - websafe[c] = '\0'; - cipher_length = c; - break; - } - } - - // The websafe escape function: - memset(encode_buffer, 0, sizeof(encode_buffer)); - encode_length = WebSafeBase64Escape(unsigned_plaintext, - base64_tests[i].plain_length, - encode_buffer, - sizeof(encode_buffer), - false); - // Is it of the expected length? - EXPECT_EQ(encode_length, cipher_length); - EXPECT_EQ( - CalculateBase64EscapedLen(base64_tests[i].plain_length, false), - encode_length); - - // Is it the expected encoded value? - EXPECT_STREQ(encode_buffer, websafe); - - // If we encode it into a buffer of exactly the right length... - memset(encode_buffer, 0, sizeof(encode_buffer)); - encode_length = - WebSafeBase64Escape(unsigned_plaintext, base64_tests[i].plain_length, - encode_buffer, cipher_length, false); - // Is it still of the expected length? - EXPECT_EQ(encode_length, cipher_length); - - // And is the value still correct? (i.e., not losing the last byte) - EXPECT_STREQ(encode_buffer, websafe); - - // Let's try the (other) string version of the encoder - std::string plain(base64_tests[i].plaintext, base64_tests[i].plain_length); - encoded = "this junk should be ignored"; - WebSafeBase64Escape(plain, &encoded); - EXPECT_EQ(encoded.size(), cipher_length); - EXPECT_STREQ(encoded.c_str(), websafe); - - // If we decode it back: - memset(decode_buffer, 0, sizeof(decode_buffer)); - decode_length = WebSafeBase64Unescape(encode_buffer, cipher_length, - decode_buffer, sizeof(decode_buffer)); - - // Is it of the expected length? - EXPECT_EQ(decode_length, base64_tests[i].plain_length); - - // Is it the expected decoded value? - EXPECT_EQ(0, - memcmp(decode_buffer, base64_tests[i].plaintext, decode_length)); - - // If we decode it into a buffer of exactly the right length... - memset(decode_buffer, 0, sizeof(decode_buffer)); - decode_length = WebSafeBase64Unescape(encode_buffer, cipher_length, - decode_buffer, decode_length); - - // Is it still of the expected length? - EXPECT_EQ(decode_length, base64_tests[i].plain_length); - - // And is it the expected decoded value? - EXPECT_EQ(0, - memcmp(decode_buffer, base64_tests[i].plaintext, decode_length)); - - - // Let's try the string version of the decoder - decoded = "this junk should be ignored"; - EXPECT_TRUE(WebSafeBase64Unescape(StringPiece(encode_buffer, cipher_length), - &decoded)); - EXPECT_EQ(decoded.size(), base64_tests[i].plain_length); - EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i); - - // This value works. Try the next. - } - - // Now try the long strings, this tests the streaming - for (int i = 0; i < sizeof(base64_strings) / sizeof(base64_strings[0]); - ++i) { - const unsigned char* unsigned_plaintext = - reinterpret_cast(base64_strings[i].plaintext); - int plain_length = strlen(base64_strings[i].plaintext); - int cipher_length = strlen(base64_strings[i].ciphertext); - std::vector buffer(cipher_length + 1); - int encode_length = WebSafeBase64Escape(unsigned_plaintext, - plain_length, - &buffer[0], - buffer.size(), - false); - EXPECT_EQ(cipher_length, encode_length); - EXPECT_EQ( - CalculateBase64EscapedLen(plain_length, false), encode_length); - buffer[ encode_length ] = '\0'; - EXPECT_STREQ(base64_strings[i].ciphertext, &buffer[0]); - } - - // Verify the behavior when decoding bad data - { - const char* bad_data = "ab-/"; - std::string buf; - EXPECT_FALSE(Base64Unescape(StringPiece(bad_data), &buf)); - EXPECT_TRUE(!WebSafeBase64Unescape(bad_data, &buf)); - EXPECT_TRUE(buf.empty()); - } -} - -// Test StrCat of ints and longs of various sizes and signdedness. -TEST(StrCat, Ints) { - const short s = -1; // NOLINT(runtime/int) - const uint16_t us = 2; - const int i = -3; - const unsigned int ui = 4; - const long l = -5; // NOLINT(runtime/int) - const unsigned long ul = 6; // NOLINT(runtime/int) - const long long ll = -7; // NOLINT(runtime/int) - const unsigned long long ull = 8; // NOLINT(runtime/int) - const ptrdiff_t ptrdiff = -9; - const size_t size = 10; - const intptr_t intptr = -12; - const uintptr_t uintptr = 13; - std::string answer; - answer = StrCat(s, us); - EXPECT_EQ(answer, "-12"); - answer = StrCat(i, ui); - EXPECT_EQ(answer, "-34"); - answer = StrCat(l, ul); - EXPECT_EQ(answer, "-56"); - answer = StrCat(ll, ull); - EXPECT_EQ(answer, "-78"); - answer = StrCat(ptrdiff, size); - EXPECT_EQ(answer, "-910"); - answer = StrCat(ptrdiff, intptr); - EXPECT_EQ(answer, "-9-12"); - answer = StrCat(uintptr, 0); - EXPECT_EQ(answer, "130"); -} - class ReplaceChars : public ::testing::TestWithParam< std::tuple> {}; diff --git a/src/google/protobuf/stubs/substitute.cc b/src/google/protobuf/stubs/substitute.cc index d301682ee337..733cfc314ecb 100644 --- a/src/google/protobuf/stubs/substitute.cc +++ b/src/google/protobuf/stubs/substitute.cc @@ -30,11 +30,13 @@ // Author: kenton@google.com (Kenton Varda) -#include - #include -#include #include +#include +#include + +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" namespace google { namespace protobuf { @@ -78,25 +80,24 @@ void SubstituteAndAppend(std::string* output, const char* format, int size = 0; for (int i = 0; format[i] != '\0'; i++) { if (format[i] == '$') { - if (ascii_isdigit(format[i+1])) { + if (absl::ascii_isdigit(format[i + 1])) { int index = format[i+1] - '0'; if (args_array[index]->size() == -1) { GOOGLE_LOG(DFATAL) - << "strings::Substitute format string invalid: asked for \"$" - << index << "\", but only " << CountSubstituteArgs(args_array) - << " args were given. Full format string was: \"" - << CEscape(format) << "\"."; + << "strings::Substitute format string invalid: asked for \"$" + << index << "\", but only " << CountSubstituteArgs(args_array) + << " args were given. Full format string was: \"" + << absl::CEscape(format) << "\"."; return; } size += args_array[index]->size(); ++i; // Skip next char. - } else if (format[i+1] == '$') { + } else if (format[i + 1] == '$') { ++size; ++i; // Skip next char. } else { - GOOGLE_LOG(DFATAL) - << "Invalid strings::Substitute() format string: \"" - << CEscape(format) << "\"."; + GOOGLE_LOG(DFATAL) << "Invalid strings::Substitute() format string: \"" + << absl::CEscape(format) << "\"."; return; } } else { @@ -112,14 +113,14 @@ void SubstituteAndAppend(std::string* output, const char* format, char* target = string_as_array(output) + original_size; for (int i = 0; format[i] != '\0'; i++) { if (format[i] == '$') { - if (ascii_isdigit(format[i+1])) { + if (absl::ascii_isdigit(format[i + 1])) { unsigned int index = format[i+1] - '0'; assert(index < 10); const SubstituteArg* src = args_array[index]; memcpy(target, src->data(), src->size()); target += src->size(); ++i; // Skip next char. - } else if (format[i+1] == '$') { + } else if (format[i + 1] == '$') { *target++ = '$'; ++i; // Skip next char. } diff --git a/src/google/protobuf/stubs/substitute.h b/src/google/protobuf/stubs/substitute.h index 0f851de096ed..3cefbcafaab5 100644 --- a/src/google/protobuf/stubs/substitute.h +++ b/src/google/protobuf/stubs/substitute.h @@ -31,16 +31,18 @@ // Author: kenton@google.com (Kenton Varda) // from google3/strings/substitute.h +#ifndef GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_ +#define GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_ + #include -#include #include -#include +#include // NOLINT -#ifndef GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_ -#define GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_ +#include "absl/strings/string_view.h" -#include +// Must be last. +#include // NOLINT namespace google { namespace protobuf { @@ -94,7 +96,7 @@ class SubstituteArg { : text_(value), size_(strlen(text_)) {} inline SubstituteArg(const std::string& value) : text_(value.data()), size_(value.size()) {} - inline SubstituteArg(const StringPiece value) + inline SubstituteArg(const absl::string_view value) : text_(value.data()), size_(value.size()) {} // Indicates that no argument was given. @@ -173,6 +175,6 @@ PROTOBUF_EXPORT void SubstituteAndAppend( } // namespace protobuf } // namespace google -#include +#include // NOLINT #endif // GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_ diff --git a/src/google/protobuf/stubs/template_util.h b/src/google/protobuf/stubs/template_util.h deleted file mode 100644 index feef904beaa5..000000000000 --- a/src/google/protobuf/stubs/template_util.h +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright 2005 Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// ---- -// Author: lar@google.com (Laramie Leavitt) -// -// Template metaprogramming utility functions. -// -// This code is compiled directly on many platforms, including client -// platforms like Windows, Mac, and embedded systems. Before making -// any changes here, make sure that you're not breaking any platforms. -// -// -// The names chosen here reflect those used in tr1 and the boost::mpl -// library, there are similar operations used in the Loki library as -// well. I prefer the boost names for 2 reasons: -// 1. I think that portions of the Boost libraries are more likely to -// be included in the c++ standard. -// 2. It is not impossible that some of the boost libraries will be -// included in our own build in the future. -// Both of these outcomes means that we may be able to directly replace -// some of these with boost equivalents. -// -#ifndef GOOGLE_PROTOBUF_TEMPLATE_UTIL_H_ -#define GOOGLE_PROTOBUF_TEMPLATE_UTIL_H_ - -namespace google { -namespace protobuf { -namespace internal { - -// Types small_ and big_ are guaranteed such that sizeof(small_) < -// sizeof(big_) -typedef char small_; - -struct big_ { - char dummy[2]; -}; - -// Identity metafunction. -template -struct identity_ { - typedef T type; -}; - -// integral_constant, defined in tr1, is a wrapper for an integer -// value. We don't really need this generality; we could get away -// with hardcoding the integer type to bool. We use the fully -// general integer_constant for compatibility with tr1. - -template -struct integral_constant { - static const T value = v; - typedef T value_type; - typedef integral_constant type; -}; - -template const T integral_constant::value; - - -// Abbreviations: true_type and false_type are structs that represent boolean -// true and false values. Also define the boost::mpl versions of those names, -// true_ and false_. -typedef integral_constant true_type; -typedef integral_constant false_type; -typedef true_type true_; -typedef false_type false_; - -// if_ is a templatized conditional statement. -// if_ is a compile time evaluation of cond. -// if_<>::type contains A if cond is true, B otherwise. -template -struct if_{ - typedef A type; -}; - -template -struct if_ { - typedef B type; -}; - - -// type_equals_ is a template type comparator, similar to Loki IsSameType. -// type_equals_::value is true iff "A" is the same type as "B". -// -// New code should prefer base::is_same, defined in base/type_traits.h. -// It is functionally identical, but is_same is the standard spelling. -template -struct type_equals_ : public false_ { -}; - -template -struct type_equals_ : public true_ { -}; - -// and_ is a template && operator. -// and_::value evaluates "A::value && B::value". -template -struct and_ : public integral_constant { -}; - -// or_ is a template || operator. -// or_::value evaluates "A::value || B::value". -template -struct or_ : public integral_constant { -}; - - -} // namespace internal -} // namespace protobuf -} // namespace google - -#endif // GOOGLE_PROTOBUF_TEMPLATE_UTIL_H_ diff --git a/src/google/protobuf/stubs/template_util_unittest.cc b/src/google/protobuf/stubs/template_util_unittest.cc deleted file mode 100644 index b1745e2bf7c8..000000000000 --- a/src/google/protobuf/stubs/template_util_unittest.cc +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright 2005 Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// ---- -// Author: lar@google.com (Laramie Leavitt) -// -// These tests are really compile time tests. -// If you try to step through this in a debugger -// you will not see any evaluations, merely that -// value is assigned true or false sequentially. - -#include - -#include -#include - -namespace GOOGLE_NAMESPACE = google::protobuf::internal; - -namespace google { -namespace protobuf { -namespace internal { -namespace { - -TEST(TemplateUtilTest, TestSize) { - EXPECT_GT(sizeof(GOOGLE_NAMESPACE::big_), sizeof(GOOGLE_NAMESPACE::small_)); -} - -TEST(TemplateUtilTest, TestIntegralConstants) { - // test the built-in types. - EXPECT_TRUE(true_type::value); - EXPECT_FALSE(false_type::value); - - typedef integral_constant one_type; - EXPECT_EQ(1, one_type::value); -} - -TEST(TemplateUtilTest, TestTemplateIf) { - typedef if_::type if_true; - EXPECT_TRUE(if_true::value); - - typedef if_::type if_false; - EXPECT_FALSE(if_false::value); -} - -TEST(TemplateUtilTest, TestTemplateTypeEquals) { - // Check that the TemplateTypeEquals works correctly. - bool value = false; - - // Test the same type is true. - value = type_equals_::value; - EXPECT_TRUE(value); - - // Test different types are false. - value = type_equals_::value; - EXPECT_FALSE(value); - - // Test type aliasing. - typedef const int foo; - value = type_equals_::value; - EXPECT_TRUE(value); -} - -TEST(TemplateUtilTest, TestTemplateAndOr) { - // Check that the TemplateTypeEquals works correctly. - bool value = false; - - // Yes && Yes == true. - value = and_::value; - EXPECT_TRUE(value); - // Yes && No == false. - value = and_::value; - EXPECT_FALSE(value); - // No && Yes == false. - value = and_::value; - EXPECT_FALSE(value); - // No && No == false. - value = and_::value; - EXPECT_FALSE(value); - - // Yes || Yes == true. - value = or_::value; - EXPECT_TRUE(value); - // Yes || No == true. - value = or_::value; - EXPECT_TRUE(value); - // No || Yes == true. - value = or_::value; - EXPECT_TRUE(value); - // No || No == false. - value = or_::value; - EXPECT_FALSE(value); -} - -TEST(TemplateUtilTest, TestIdentity) { - EXPECT_TRUE( - (type_equals_::type, int>::value)); - EXPECT_TRUE( - (type_equals_::type, void>::value)); -} - -} // anonymous namespace -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/stubs/time.cc b/src/google/protobuf/stubs/time.cc deleted file mode 100644 index 692cb822878a..000000000000 --- a/src/google/protobuf/stubs/time.cc +++ /dev/null @@ -1,365 +0,0 @@ -#include - -#include - -#include -#include - -namespace google { -namespace protobuf { -namespace internal { - -namespace { -static const int64_t kSecondsPerMinute = 60; -static const int64_t kSecondsPerHour = 3600; -static const int64_t kSecondsPerDay = kSecondsPerHour * 24; -static const int64_t kSecondsPer400Years = - kSecondsPerDay * (400 * 365 + 400 / 4 - 3); -// Seconds from 0001-01-01T00:00:00 to 1970-01-01T:00:00:00 -static const int64_t kSecondsFromEraToEpoch = 62135596800LL; -// The range of timestamp values we support. -static const int64_t kMinTime = -62135596800LL; // 0001-01-01T00:00:00 -static const int64_t kMaxTime = 253402300799LL; // 9999-12-31T23:59:59 - -static const int kNanosPerMillisecond = 1000000; -static const int kNanosPerMicrosecond = 1000; - -// Count the seconds from the given year (start at Jan 1, 00:00) to 100 years -// after. -int64_t SecondsPer100Years(int year) { - if (year % 400 == 0 || year % 400 > 300) { - return kSecondsPerDay * (100 * 365 + 100 / 4); - } else { - return kSecondsPerDay * (100 * 365 + 100 / 4 - 1); - } -} - -// Count the seconds from the given year (start at Jan 1, 00:00) to 4 years -// after. -int64_t SecondsPer4Years(int year) { - if ((year % 100 == 0 || year % 100 > 96) && - !(year % 400 == 0 || year % 400 > 396)) { - // No leap years. - return kSecondsPerDay * (4 * 365); - } else { - // One leap years. - return kSecondsPerDay * (4 * 365 + 1); - } -} - -bool IsLeapYear(int year) { - return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); -} - -int64_t SecondsPerYear(int year) { - return kSecondsPerDay * (IsLeapYear(year) ? 366 : 365); -} - -static const int kDaysInMonth[13] = { - 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 -}; - -int64_t SecondsPerMonth(int month, bool leap) { - if (month == 2 && leap) { - return kSecondsPerDay * (kDaysInMonth[month] + 1); - } - return kSecondsPerDay * kDaysInMonth[month]; -} - -static const int kDaysSinceJan[13] = { - 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, -}; - -bool ValidateDateTime(const DateTime& time) { - if (time.year < 1 || time.year > 9999 || - time.month < 1 || time.month > 12 || - time.day < 1 || time.day > 31 || - time.hour < 0 || time.hour > 23 || - time.minute < 0 || time.minute > 59 || - time.second < 0 || time.second > 59) { - return false; - } - if (time.month == 2 && IsLeapYear(time.year)) { - return time.day <= kDaysInMonth[time.month] + 1; - } else { - return time.day <= kDaysInMonth[time.month]; - } -} - -// Count the number of seconds elapsed from 0001-01-01T00:00:00 to the given -// time. -int64_t SecondsSinceCommonEra(const DateTime& time) { - int64_t result = 0; - // Years should be between 1 and 9999. - assert(time.year >= 1 && time.year <= 9999); - int year = 1; - if ((time.year - year) >= 400) { - int count_400years = (time.year - year) / 400; - result += kSecondsPer400Years * count_400years; - year += count_400years * 400; - } - while ((time.year - year) >= 100) { - result += SecondsPer100Years(year); - year += 100; - } - while ((time.year - year) >= 4) { - result += SecondsPer4Years(year); - year += 4; - } - while (time.year > year) { - result += SecondsPerYear(year); - ++year; - } - // Months should be between 1 and 12. - assert(time.month >= 1 && time.month <= 12); - int month = time.month; - result += kSecondsPerDay * kDaysSinceJan[month]; - if (month > 2 && IsLeapYear(year)) { - result += kSecondsPerDay; - } - assert(time.day >= 1 && - time.day <= (month == 2 && IsLeapYear(year) - ? kDaysInMonth[month] + 1 - : kDaysInMonth[month])); - result += kSecondsPerDay * (time.day - 1); - result += kSecondsPerHour * time.hour + - kSecondsPerMinute * time.minute + - time.second; - return result; -} - -// Format nanoseconds with either 3, 6, or 9 digits depending on the required -// precision to represent the exact value. -std::string FormatNanos(int32_t nanos) { - if (nanos % kNanosPerMillisecond == 0) { - return StringPrintf("%03d", nanos / kNanosPerMillisecond); - } else if (nanos % kNanosPerMicrosecond == 0) { - return StringPrintf("%06d", nanos / kNanosPerMicrosecond); - } else { - return StringPrintf("%09d", nanos); - } -} - -// Parses an integer from a null-terminated char sequence. The method -// consumes at most "width" chars. Returns a pointer after the consumed -// integer, or nullptr if the data does not start with an integer or the -// integer value does not fall in the range of [min_value, max_value]. -const char* ParseInt(const char* data, int width, int min_value, - int max_value, int* result) { - if (!ascii_isdigit(*data)) { - return nullptr; - } - int value = 0; - for (int i = 0; i < width; ++i, ++data) { - if (ascii_isdigit(*data)) { - value = value * 10 + (*data - '0'); - } else { - break; - } - } - if (value >= min_value && value <= max_value) { - *result = value; - return data; - } else { - return nullptr; - } -} - -// Consumes the fractional parts of a second into nanos. For example, -// "010" will be parsed to 10000000 nanos. -const char* ParseNanos(const char* data, int32_t* nanos) { - if (!ascii_isdigit(*data)) { - return nullptr; - } - int value = 0; - int len = 0; - // Consume as many digits as there are but only take the first 9 into - // account. - while (ascii_isdigit(*data)) { - if (len < 9) { - value = value * 10 + *data - '0'; - } - ++len; - ++data; - } - while (len < 9) { - value = value * 10; - ++len; - } - *nanos = value; - return data; -} - -const char* ParseTimezoneOffset(const char* data, int64_t* offset) { - // Accept format "HH:MM". E.g., "08:00" - int hour; - if ((data = ParseInt(data, 2, 0, 23, &hour)) == nullptr) { - return nullptr; - } - if (*data++ != ':') { - return nullptr; - } - int minute; - if ((data = ParseInt(data, 2, 0, 59, &minute)) == nullptr) { - return nullptr; - } - *offset = (hour * 60 + minute) * 60; - return data; -} -} // namespace - -bool SecondsToDateTime(int64_t seconds, DateTime* time) { - if (seconds < kMinTime || seconds > kMaxTime) { - return false; - } - // It's easier to calculate the DateTime starting from 0001-01-01T00:00:00 - seconds = seconds + kSecondsFromEraToEpoch; - int year = 1; - if (seconds >= kSecondsPer400Years) { - int count_400years = seconds / kSecondsPer400Years; - year += 400 * count_400years; - seconds %= kSecondsPer400Years; - } - while (seconds >= SecondsPer100Years(year)) { - seconds -= SecondsPer100Years(year); - year += 100; - } - while (seconds >= SecondsPer4Years(year)) { - seconds -= SecondsPer4Years(year); - year += 4; - } - while (seconds >= SecondsPerYear(year)) { - seconds -= SecondsPerYear(year); - year += 1; - } - bool leap = IsLeapYear(year); - int month = 1; - while (seconds >= SecondsPerMonth(month, leap)) { - seconds -= SecondsPerMonth(month, leap); - ++month; - } - int day = 1 + seconds / kSecondsPerDay; - seconds %= kSecondsPerDay; - int hour = seconds / kSecondsPerHour; - seconds %= kSecondsPerHour; - int minute = seconds / kSecondsPerMinute; - seconds %= kSecondsPerMinute; - time->year = year; - time->month = month; - time->day = day; - time->hour = hour; - time->minute = minute; - time->second = static_cast(seconds); - return true; -} - -bool DateTimeToSeconds(const DateTime& time, int64_t* seconds) { - if (!ValidateDateTime(time)) { - return false; - } - *seconds = SecondsSinceCommonEra(time) - kSecondsFromEraToEpoch; - return true; -} - -void GetCurrentTime(int64_t* seconds, int32_t* nanos) { - // TODO(xiaofeng): Improve the accuracy of this implementation (or just - // remove this method from protobuf). - *seconds = time(nullptr); - *nanos = 0; -} - -std::string FormatTime(int64_t seconds, int32_t nanos) { - DateTime time; - if (nanos < 0 || nanos > 999999999 || !SecondsToDateTime(seconds, &time)) { - return "InvalidTime"; - } - std::string result = - StringPrintf("%04d-%02d-%02dT%02d:%02d:%02d", time.year, time.month, - time.day, time.hour, time.minute, time.second); - if (nanos != 0) { - result += "." + FormatNanos(nanos); - } - return result + "Z"; -} - -bool ParseTime(const std::string& value, int64_t* seconds, int32_t* nanos) { - DateTime time; - const char* data = value.c_str(); - // We only accept: - // Z-normalized: 2015-05-20T13:29:35.120Z - // With UTC offset: 2015-05-20T13:29:35.120-08:00 - - // Parse year - if ((data = ParseInt(data, 4, 1, 9999, &time.year)) == nullptr) { - return false; - } - // Expect '-' - if (*data++ != '-') return false; - // Parse month - if ((data = ParseInt(data, 2, 1, 12, &time.month)) == nullptr) { - return false; - } - // Expect '-' - if (*data++ != '-') return false; - // Parse day - if ((data = ParseInt(data, 2, 1, 31, &time.day)) == nullptr) { - return false; - } - // Expect 'T' - if (*data++ != 'T') return false; - // Parse hour - if ((data = ParseInt(data, 2, 0, 23, &time.hour)) == nullptr) { - return false; - } - // Expect ':' - if (*data++ != ':') return false; - // Parse minute - if ((data = ParseInt(data, 2, 0, 59, &time.minute)) == nullptr) { - return false; - } - // Expect ':' - if (*data++ != ':') return false; - // Parse second - if ((data = ParseInt(data, 2, 0, 59, &time.second)) == nullptr) { - return false; - } - if (!DateTimeToSeconds(time, seconds)) { - return false; - } - // Parse nanoseconds. - if (*data == '.') { - ++data; - // Parse nanoseconds. - if ((data = ParseNanos(data, nanos)) == nullptr) { - return false; - } - } else { - *nanos = 0; - } - // Parse UTC offsets. - if (*data == 'Z') { - ++data; - } else if (*data == '+') { - ++data; - int64_t offset; - if ((data = ParseTimezoneOffset(data, &offset)) == nullptr) { - return false; - } - *seconds -= offset; - } else if (*data == '-') { - ++data; - int64_t offset; - if ((data = ParseTimezoneOffset(data, &offset)) == nullptr) { - return false; - } - *seconds += offset; - } else { - return false; - } - // Done with parsing. - return *data == 0; -} - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/stubs/time.h b/src/google/protobuf/stubs/time.h deleted file mode 100644 index 8b6e56214f70..000000000000 --- a/src/google/protobuf/stubs/time.h +++ /dev/null @@ -1,82 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#ifndef GOOGLE_PROTOBUF_STUBS_TIME_H_ -#define GOOGLE_PROTOBUF_STUBS_TIME_H_ - -#include - -#include - -#include - -namespace google { -namespace protobuf { -namespace internal { - -struct DateTime { - int year; - int month; - int day; - int hour; - int minute; - int second; -}; - -// Converts a timestamp (seconds elapsed since 1970-01-01T00:00:00, could be -// negative to represent time before 1970-01-01) to DateTime. Returns false -// if the timestamp is not in the range between 0001-01-01T00:00:00 and -// 9999-12-31T23:59:59. -bool PROTOBUF_EXPORT SecondsToDateTime(int64_t seconds, DateTime* time); -// Converts DateTime to a timestamp (seconds since 1970-01-01T00:00:00). -// Returns false if the DateTime is not valid or is not in the valid range. -bool PROTOBUF_EXPORT DateTimeToSeconds(const DateTime& time, int64_t* seconds); - -void PROTOBUF_EXPORT GetCurrentTime(int64_t* seconds, int32_t* nanos); - -// Formats a time string in RFC3339 format. -// -// For example, "2015-05-20T13:29:35.120Z". For nanos, 0, 3, 6 or 9 fractional -// digits will be used depending on how many are required to represent the exact -// value. -// -// Note that "nanos" must in the range of [0, 999999999]. -std::string PROTOBUF_EXPORT FormatTime(int64_t seconds, int32_t nanos); -// Parses a time string. This method accepts RFC3339 date/time string with UTC -// offset. For example, "2015-05-20T13:29:35.120-08:00". -bool PROTOBUF_EXPORT ParseTime(const std::string& value, int64_t* seconds, - int32_t* nanos); - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_TIME_H_ diff --git a/src/google/protobuf/stubs/time_test.cc b/src/google/protobuf/stubs/time_test.cc deleted file mode 100644 index 1ce0a1c130f7..000000000000 --- a/src/google/protobuf/stubs/time_test.cc +++ /dev/null @@ -1,261 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include - -#include -#include - -namespace google { -namespace protobuf { -namespace internal { -namespace { -static const int64 kSecondsPerDay = 3600 * 24; - -// For DateTime, tests will mostly focus on the date part because that's -// the tricky one. -int64 CreateTimestamp(int year, int month, int day) { - DateTime time; - time.year = year; - time.month = month; - time.day = day; - time.hour = time.minute = time.second = 0; - int64 result; - GOOGLE_CHECK(DateTimeToSeconds(time, &result)); - // Check that a roundtrip produces the same result. - GOOGLE_CHECK(SecondsToDateTime(result, &time)); - GOOGLE_CHECK(time.year == year); - GOOGLE_CHECK(time.month == month); - GOOGLE_CHECK(time.day == day); - return result; -} - -TEST(DateTimeTest, SimpleTime) { - DateTime time; - ASSERT_TRUE(SecondsToDateTime(1, &time)); - EXPECT_EQ(1970, time.year); - EXPECT_EQ(1, time.month); - EXPECT_EQ(1, time.day); - EXPECT_EQ(0, time.hour); - EXPECT_EQ(0, time.minute); - EXPECT_EQ(1, time.second); - int64 seconds; - ASSERT_TRUE(DateTimeToSeconds(time, &seconds)); - EXPECT_EQ(1, seconds); - - ASSERT_TRUE(SecondsToDateTime(-1, &time)); - EXPECT_EQ(1969, time.year); - EXPECT_EQ(12, time.month); - EXPECT_EQ(31, time.day); - EXPECT_EQ(23, time.hour); - EXPECT_EQ(59, time.minute); - EXPECT_EQ(59, time.second); - ASSERT_TRUE(DateTimeToSeconds(time, &seconds)); - EXPECT_EQ(-1, seconds); - - DateTime start, end; - start.year = 1; - start.month = 1; - start.day = 1; - start.hour = 0; - start.minute = 0; - start.second = 0; - end.year = 9999; - end.month = 12; - end.day = 31; - end.hour = 23; - end.minute = 59; - end.second = 59; - int64 start_time, end_time; - ASSERT_TRUE(DateTimeToSeconds(start, &start_time)); - ASSERT_TRUE(DateTimeToSeconds(end, &end_time)); - EXPECT_EQ(315537897599LL, end_time - start_time); - ASSERT_TRUE(SecondsToDateTime(start_time, &time)); - ASSERT_TRUE(DateTimeToSeconds(time, &seconds)); - EXPECT_EQ(start_time, seconds); - ASSERT_TRUE(SecondsToDateTime(end_time, &time)); - ASSERT_TRUE(DateTimeToSeconds(time, &seconds)); - EXPECT_EQ(end_time, seconds); -} - -TEST(DateTimeTest, DayInMonths) { - // Check that month boundaries are handled correctly. - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 1, 1) - CreateTimestamp(2014, 12, 31)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 2, 1) - CreateTimestamp(2015, 1, 31)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 3, 1) - CreateTimestamp(2015, 2, 28)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 4, 1) - CreateTimestamp(2015, 3, 31)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 5, 1) - CreateTimestamp(2015, 4, 30)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 6, 1) - CreateTimestamp(2015, 5, 31)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 7, 1) - CreateTimestamp(2015, 6, 30)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 8, 1) - CreateTimestamp(2015, 7, 31)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 9, 1) - CreateTimestamp(2015, 8, 31)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 10, 1) - CreateTimestamp(2015, 9, 30)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 11, 1) - CreateTimestamp(2015, 10, 31)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 12, 1) - CreateTimestamp(2015, 11, 30)); - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2016, 1, 1) - CreateTimestamp(2015, 12, 31)); -} - -TEST(DateTimeTest, LeapYear) { - // Non-leap year. - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2015, 3, 1) - CreateTimestamp(2015, 2, 28)); - // Leap year. - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2016, 3, 1) - CreateTimestamp(2016, 2, 29)); - // Non-leap year. - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2100, 3, 1) - CreateTimestamp(2100, 2, 28)); - // Leap year. - EXPECT_EQ(kSecondsPerDay, - CreateTimestamp(2400, 3, 1) - CreateTimestamp(2400, 2, 29)); -} - -TEST(DateTimeTest, WrongDays) { - int64 seconds; - DateTime time; - time.hour = 0; - time.minute = 0; - time.second = 0; - time.month = 2; - - // Non-leap year. - time.year = 2015; - time.day = 29; - ASSERT_FALSE(DateTimeToSeconds(time, &seconds)); - - // Leap year. - time.year = 2016; - time.day = 29; - ASSERT_TRUE(DateTimeToSeconds(time, &seconds)); - time.day = 30; - ASSERT_FALSE(DateTimeToSeconds(time, &seconds)); - - // Non-leap year. - time.year = 2100; - time.day = 29; - ASSERT_FALSE(DateTimeToSeconds(time, &seconds)); - - // Leap year. - time.year = 2400; - time.day = 29; - ASSERT_TRUE(DateTimeToSeconds(time, &seconds)); - time.day = 30; - ASSERT_FALSE(DateTimeToSeconds(time, &seconds)); - - // Non-february - time.year = 2015; - time.month = 1; - time.day = 0; - ASSERT_FALSE(DateTimeToSeconds(time, &seconds)); - time.day = 1; - ASSERT_TRUE(DateTimeToSeconds(time, &seconds)); - time.day = 31; - ASSERT_TRUE(DateTimeToSeconds(time, &seconds)); - time.day = 32; - ASSERT_FALSE(DateTimeToSeconds(time, &seconds)); - - // Bad month - time.year = 2015; - time.month = 0; - time.day = 1; - ASSERT_FALSE(DateTimeToSeconds(time, &seconds)); - time.month = 13; - ASSERT_FALSE(DateTimeToSeconds(time, &seconds)); -} - -TEST(DateTimeTest, StringFormat) { - DateTime start, end; - start.year = 1; - start.month = 1; - start.day = 1; - start.hour = 0; - start.minute = 0; - start.second = 0; - end.year = 9999; - end.month = 12; - end.day = 31; - end.hour = 23; - end.minute = 59; - end.second = 59; - int64 start_time, end_time; - ASSERT_TRUE(DateTimeToSeconds(start, &start_time)); - ASSERT_TRUE(DateTimeToSeconds(end, &end_time)); - - EXPECT_EQ("0001-01-01T00:00:00Z", FormatTime(start_time, 0)); - EXPECT_EQ("9999-12-31T23:59:59Z", FormatTime(end_time, 0)); - - // Make sure the nanoseconds part is formatted correctly. - EXPECT_EQ("1970-01-01T00:00:00.010Z", FormatTime(0, 10000000)); - EXPECT_EQ("1970-01-01T00:00:00.000010Z", FormatTime(0, 10000)); - EXPECT_EQ("1970-01-01T00:00:00.000000010Z", FormatTime(0, 10)); -} - -TEST(DateTimeTest, ParseString) { - int64 seconds; - int32 nanos; - ASSERT_TRUE(ParseTime("0001-01-01T00:00:00Z", &seconds, &nanos)); - EXPECT_EQ("0001-01-01T00:00:00Z", FormatTime(seconds, nanos)); - ASSERT_TRUE(ParseTime("9999-12-31T23:59:59.999999999Z", &seconds, &nanos)); - EXPECT_EQ("9999-12-31T23:59:59.999999999Z", FormatTime(seconds, nanos)); - - // Test time zone offsets. - ASSERT_TRUE(ParseTime("1970-01-01T00:00:00-08:00", &seconds, &nanos)); - EXPECT_EQ("1970-01-01T08:00:00Z", FormatTime(seconds, nanos)); - ASSERT_TRUE(ParseTime("1970-01-01T00:00:00+08:00", &seconds, &nanos)); - EXPECT_EQ("1969-12-31T16:00:00Z", FormatTime(seconds, nanos)); - - // Test nanoseconds. - ASSERT_TRUE(ParseTime("1970-01-01T00:00:00.01Z", &seconds, &nanos)); - EXPECT_EQ("1970-01-01T00:00:00.010Z", FormatTime(seconds, nanos)); - ASSERT_TRUE(ParseTime("1970-01-01T00:00:00.00001-08:00", &seconds, &nanos)); - EXPECT_EQ("1970-01-01T08:00:00.000010Z", FormatTime(seconds, nanos)); - ASSERT_TRUE(ParseTime("1970-01-01T00:00:00.00000001+08:00", &seconds, &nanos)); - EXPECT_EQ("1969-12-31T16:00:00.000000010Z", FormatTime(seconds, nanos)); - // Fractional parts less than 1 nanosecond will be ignored. - ASSERT_TRUE(ParseTime("1970-01-01T00:00:00.0123456789Z", &seconds, &nanos)); - EXPECT_EQ("1970-01-01T00:00:00.012345678Z", FormatTime(seconds, nanos)); -} - -} // namespace -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/test_util_lite.cc b/src/google/protobuf/test_util_lite.cc index 628f1a029499..5a09aeecf837 100644 --- a/src/google/protobuf/test_util_lite.cc +++ b/src/google/protobuf/test_util_lite.cc @@ -37,7 +37,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/text_format.cc b/src/google/protobuf/text_format.cc index 8efa9cf4f261..6c77d019dde4 100644 --- a/src/google/protobuf/text_format.cc +++ b/src/google/protobuf/text_format.cc @@ -51,6 +51,11 @@ #include #include #include +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/string_view.h" #include #include #include @@ -61,7 +66,6 @@ #include #include #include -#include #include // Must be included last. @@ -176,14 +180,13 @@ TextFormat::ParseLocationRange TextFormat::ParseInfoTree::GetLocationRange( index = 0; } - const std::vector* locations = - FindOrNull(locations_, field); - if (locations == nullptr || - index >= static_cast(locations->size())) { + auto it = locations_.find(field); + if (it == locations_.end() || + index >= static_cast(it->second.size())) { return TextFormat::ParseLocationRange(); } - return (*locations)[index]; + return it->second[static_cast(index)]; } TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::GetTreeForNested( @@ -198,7 +201,7 @@ TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::GetTreeForNested( return nullptr; } - return it->second[index].get(); + return it->second[static_cast(index)].get(); } namespace { @@ -430,7 +433,7 @@ class TextFormat::Parser::ParserImpl { std::string full_type_name, prefix; DO(ConsumeAnyTypeUrl(&full_type_name, &prefix)); std::string prefix_and_full_type_name = - StrCat(prefix, full_type_name); + absl::StrCat(prefix, full_type_name); DO(ConsumeBeforeWhitespace("]")); TryConsumeWhitespace(); // ':' is optional between message labels and values. @@ -696,7 +699,7 @@ class TextFormat::Parser::ParserImpl { const FieldDescriptor* field) { if (--recursion_limit_ < 0) { ReportError( - StrCat("Message is too deep, the parser exceeded the " + absl::StrCat("Message is too deep, the parser exceeded the " "configured recursion limit of ", initial_recursion_limit_, ".")); return false; @@ -732,7 +735,7 @@ class TextFormat::Parser::ParserImpl { bool SkipFieldMessage() { if (--recursion_limit_ < 0) { ReportError( - StrCat("Message is too deep, the parser exceeded the " + absl::StrCat("Message is too deep, the parser exceeded the " "configured recursion limit of ", initial_recursion_limit_, ".")); return false; @@ -846,7 +849,7 @@ class TextFormat::Parser::ParserImpl { } else if (LookingAt("-") || LookingAtType(io::Tokenizer::TYPE_INTEGER)) { DO(ConsumeSignedInteger(&int_value, kint32max)); - value = StrCat(int_value); // for error reporting + value = absl::StrCat(int_value); // for error reporting enum_value = enum_type->FindValueByNumber(int_value); } else { ReportError("Expected integer or identifier, got: " + @@ -892,7 +895,7 @@ class TextFormat::Parser::ParserImpl { bool SkipFieldValue() { if (--recursion_limit_ < 0) { ReportError( - StrCat("Message is too deep, the parser exceeded the " + absl::StrCat("Message is too deep, the parser exceeded the " "configured recursion limit of ", initial_recursion_limit_, ".")); return false; @@ -1287,7 +1290,7 @@ class TextFormat::Parser::ParserImpl { had_silent_marker_ = false; if (LookingAtType(io::Tokenizer::TYPE_WHITESPACE)) { if (tokenizer_.current().text == - StrCat(" ", internal::kDebugStringSilentMarkerForDetection)) { + absl::StrCat(" ", internal::kDebugStringSilentMarkerForDetection)) { had_silent_marker_ = true; } tokenizer_.Next(); @@ -1424,15 +1427,15 @@ class TextFormat::Printer::TextGenerator // error.) bool failed() const { return failed_; } - void PrintMaybeWithMarker(StringPiece text) { + void PrintMaybeWithMarker(absl::string_view text) { Print(text.data(), text.size()); if (ConsumeInsertSilentMarker()) { PrintLiteral(internal::kDebugStringSilentMarker); } } - void PrintMaybeWithMarker(StringPiece text_head, - StringPiece text_tail) { + void PrintMaybeWithMarker(absl::string_view text_head, + absl::string_view text_tail) { Print(text_head.data(), text_head.size()); if (ConsumeInsertSilentMarker()) { PrintLiteral(internal::kDebugStringSilentMarker); @@ -1552,7 +1555,7 @@ class TextFormat::Printer::FastFieldValuePrinterUtf8Escaping void PrintString(const std::string& val, TextFormat::BaseTextGenerator* generator) const override { generator->PrintLiteral("\""); - generator->PrintString(strings::Utf8SafeCEscape(val)); + generator->PrintString(absl::Utf8SafeCEscape(val)); generator->PrintLiteral("\""); } void PrintBytes(const std::string& val, @@ -1606,12 +1609,12 @@ TextFormat::Parser::~Parser() {} namespace { -bool CheckParseInputSize(StringPiece input, +bool CheckParseInputSize(absl::string_view input, io::ErrorCollector* error_collector) { if (input.size() > INT_MAX) { error_collector->AddError( -1, 0, - StrCat( + absl::StrCat( "Input size too large: ", static_cast(input.size()), " bytes", " > ", INT_MAX, " bytes.")); return false; @@ -1638,7 +1641,7 @@ bool TextFormat::Parser::Parse(io::ZeroCopyInputStream* input, return MergeUsingImpl(input, output, &parser); } -bool TextFormat::Parser::ParseFromString(ConstStringParam input, +bool TextFormat::Parser::ParseFromString(absl::string_view input, Message* output) { DO(CheckParseInputSize(input, error_collector_)); io::ArrayInputStream input_stream(input.data(), input.size()); @@ -1656,7 +1659,7 @@ bool TextFormat::Parser::Merge(io::ZeroCopyInputStream* input, return MergeUsingImpl(input, output, &parser); } -bool TextFormat::Parser::MergeFromString(ConstStringParam input, +bool TextFormat::Parser::MergeFromString(absl::string_view input, Message* output) { DO(CheckParseInputSize(input, error_collector_)); io::ArrayInputStream input_stream(input.data(), input.size()); @@ -1672,7 +1675,7 @@ bool TextFormat::Parser::MergeUsingImpl(io::ZeroCopyInputStream* /* input */, output->FindInitializationErrors(&missing_fields); parser_impl->ReportError(-1, 0, "Message missing required fields: " + - Join(missing_fields, ", ")); + absl::StrJoin(missing_fields, ", ")); return false; } return true; @@ -1701,12 +1704,12 @@ bool TextFormat::Parser::ParseFieldValueFromString(const std::string& input, return Parser().Merge(input, output); } -/* static */ bool TextFormat::ParseFromString(ConstStringParam input, +/* static */ bool TextFormat::ParseFromString(absl::string_view input, Message* output) { return Parser().ParseFromString(input, output); } -/* static */ bool TextFormat::MergeFromString(ConstStringParam input, +/* static */ bool TextFormat::MergeFromString(absl::string_view input, Message* output) { return Parser().MergeFromString(input, output); } @@ -1822,19 +1825,19 @@ void TextFormat::FastFieldValuePrinter::PrintBool( } void TextFormat::FastFieldValuePrinter::PrintInt32( int32_t val, BaseTextGenerator* generator) const { - generator->PrintString(StrCat(val)); + generator->PrintString(absl::StrCat(val)); } void TextFormat::FastFieldValuePrinter::PrintUInt32( uint32_t val, BaseTextGenerator* generator) const { - generator->PrintString(StrCat(val)); + generator->PrintString(absl::StrCat(val)); } void TextFormat::FastFieldValuePrinter::PrintInt64( int64_t val, BaseTextGenerator* generator) const { - generator->PrintString(StrCat(val)); + generator->PrintString(absl::StrCat(val)); } void TextFormat::FastFieldValuePrinter::PrintUInt64( uint64_t val, BaseTextGenerator* generator) const { - generator->PrintString(StrCat(val)); + generator->PrintString(absl::StrCat(val)); } void TextFormat::FastFieldValuePrinter::PrintFloat( float val, BaseTextGenerator* generator) const { @@ -1853,7 +1856,7 @@ void TextFormat::FastFieldValuePrinter::PrintEnum( void TextFormat::FastFieldValuePrinter::PrintString( const std::string& val, BaseTextGenerator* generator) const { generator->PrintLiteral("\""); - generator->PrintString(CEscape(val)); + generator->PrintString(absl::CEscape(val)); generator->PrintLiteral("\""); } void TextFormat::FastFieldValuePrinter::PrintBytes( @@ -2513,7 +2516,7 @@ void TextFormat::Printer::PrintFieldName(const Message& message, // if use_field_number_ is true, prints field number instead // of field name. if (use_field_number_) { - generator->PrintString(StrCat(field->number())); + generator->PrintString(absl::StrCat(field->number())); return; } @@ -2592,7 +2595,7 @@ void TextFormat::Printer::PrintFieldValue(const Message& message, // it is possible for the user to force an unknown integer value. So we // simply use the integer value itself as the enum value name in this // case. - printer->PrintEnum(enum_value, StrCat(enum_value), generator); + printer->PrintEnum(enum_value, absl::StrCat(enum_value), generator); } break; } @@ -2642,13 +2645,13 @@ void TextFormat::Printer::PrintUnknownFields( int recursion_budget) const { for (int i = 0; i < unknown_fields.field_count(); i++) { const UnknownField& field = unknown_fields.field(i); - std::string field_number = StrCat(field.number()); + std::string field_number = absl::StrCat(field.number()); switch (field.type()) { case UnknownField::TYPE_VARINT: generator->PrintString(field_number); generator->PrintMaybeWithMarker(": "); - generator->PrintString(StrCat(field.varint())); + generator->PrintString(absl::StrCat(field.varint())); if (single_line_mode_) { generator->PrintLiteral(" "); } else { @@ -2659,7 +2662,7 @@ void TextFormat::Printer::PrintUnknownFields( generator->PrintString(field_number); generator->PrintMaybeWithMarker(": ", "0x"); generator->PrintString( - StrCat(strings::Hex(field.fixed32(), strings::ZERO_PAD_8))); + absl::StrCat(absl::Hex(field.fixed32(), absl::kZeroPad8))); if (single_line_mode_) { generator->PrintLiteral(" "); } else { @@ -2671,7 +2674,7 @@ void TextFormat::Printer::PrintUnknownFields( generator->PrintString(field_number); generator->PrintMaybeWithMarker(": ", "0x"); generator->PrintString( - StrCat(strings::Hex(field.fixed64(), strings::ZERO_PAD_16))); + absl::StrCat(absl::Hex(field.fixed64(), absl::kZeroPad16))); if (single_line_mode_) { generator->PrintLiteral(" "); } else { @@ -2711,7 +2714,7 @@ void TextFormat::Printer::PrintUnknownFields( // This field is not parseable as a Message (or we ran out of // recursion budget). So it is probably just a plain string. generator->PrintMaybeWithMarker(": ", "\""); - generator->PrintString(CEscape(value)); + generator->PrintString(absl::CEscape(value)); if (single_line_mode_) { generator->PrintLiteral("\" "); } else { diff --git a/src/google/protobuf/text_format.h b/src/google/protobuf/text_format.h index e10bef7ddf8d..18d3e57fb0a8 100644 --- a/src/google/protobuf/text_format.h +++ b/src/google/protobuf/text_format.h @@ -46,6 +46,7 @@ #include #include +#include "absl/strings/string_view.h" #include #include #include @@ -476,13 +477,13 @@ class PROTOBUF_EXPORT TextFormat { // google::protobuf::MessageLite::ParseFromString(). static bool Parse(io::ZeroCopyInputStream* input, Message* output); // Like Parse(), but reads directly from a string. - static bool ParseFromString(ConstStringParam input, Message* output); + static bool ParseFromString(absl::string_view input, Message* output); // Like Parse(), but the data is merged into the given message, as if // using Message::MergeFrom(). static bool Merge(io::ZeroCopyInputStream* input, Message* output); // Like Merge(), but reads directly from a string. - static bool MergeFromString(ConstStringParam input, Message* output); + static bool MergeFromString(absl::string_view input, Message* output); // Parse the given text as a single field value and store it into the // given field of the given message. If the field is a repeated field, @@ -571,11 +572,11 @@ class PROTOBUF_EXPORT TextFormat { // Like TextFormat::Parse(). bool Parse(io::ZeroCopyInputStream* input, Message* output); // Like TextFormat::ParseFromString(). - bool ParseFromString(ConstStringParam input, Message* output); + bool ParseFromString(absl::string_view input, Message* output); // Like TextFormat::Merge(). bool Merge(io::ZeroCopyInputStream* input, Message* output); // Like TextFormat::MergeFromString(). - bool MergeFromString(ConstStringParam input, Message* output); + bool MergeFromString(absl::string_view input, Message* output); // Set where to report parse errors. If nullptr (the default), errors will // be printed to stderr. diff --git a/src/google/protobuf/text_format_unittest.cc b/src/google/protobuf/text_format_unittest.cc index 7d0dfdb344cf..419ca9d73518 100644 --- a/src/google/protobuf/text_format_unittest.cc +++ b/src/google/protobuf/text_format_unittest.cc @@ -58,6 +58,8 @@ #include #include #include +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include #include #include @@ -97,7 +99,6 @@ class TextFormatTest : public testing::Test { "third_party/protobuf/" "testdata/text_format_unittest_data_oneof_implemented.txt"), &static_proto_text_format_, true)); - CleanStringLineEndings(&static_proto_text_format_, false); } TextFormatTest() : proto_text_format_(static_proto_text_format_) {} @@ -119,7 +120,6 @@ class TextFormatExtensionsTest : public testing::Test { TestUtil::GetTestDataPath("third_party/protobuf/testdata/" "text_format_unittest_extensions_data.txt"), &static_proto_text_format_, true)); - CleanStringLineEndings(&static_proto_text_format_, false); } TextFormatExtensionsTest() : proto_text_format_(static_proto_text_format_) {} @@ -155,7 +155,7 @@ TEST_F(TextFormatTest, ShortDebugString) { proto_.mutable_optional_foreign_message(); EXPECT_EQ(proto_.ShortDebugString(), - StrCat("optional_int32: ", kDebugStringSilentMarker, + absl::StrCat("optional_int32: ", kDebugStringSilentMarker, "1 " "optional_string: \"hello\" " "optional_nested_message { bb: 2 } " @@ -229,7 +229,7 @@ TEST_F(TextFormatTest, StringEscape) { // Hardcode a correct value to test against. std::string correct_string = - StrCat("optional_string: ", kDebugStringSilentMarker, + absl::StrCat("optional_string: ", kDebugStringSilentMarker, kEscapeTestStringEscaped, "\n"); // Compare. @@ -238,7 +238,7 @@ TEST_F(TextFormatTest, StringEscape) { // the protocol buffer contains no UTF-8 text. EXPECT_EQ(correct_string, utf8_debug_string); - std::string expected_short_debug_string = StrCat( + std::string expected_short_debug_string = absl::StrCat( "optional_string: ", kDebugStringSilentMarker, kEscapeTestStringEscaped); EXPECT_EQ(expected_short_debug_string, proto_.ShortDebugString()); } @@ -254,11 +254,11 @@ TEST_F(TextFormatTest, Utf8DebugString) { // Hardcode a correct value to test against. std::string correct_utf8_string = - StrCat("optional_string: ", kDebugStringSilentMarker, + absl::StrCat("optional_string: ", kDebugStringSilentMarker, "\"\350\260\267\346\255\214\"\n" "optional_bytes: \"\\350\\260\\267\\346\\255\\214\"\n"); std::string correct_string = - StrCat("optional_string: ", kDebugStringSilentMarker, + absl::StrCat("optional_string: ", kDebugStringSilentMarker, "\"\\350\\260\\267\\346\\255\\214\"\n" "optional_bytes: \"\\350\\260\\267\\346\\255\\214\"\n"); @@ -283,7 +283,7 @@ TEST_F(TextFormatTest, PrintUnknownFields) { unknown_fields->AddVarint(8, 2); unknown_fields->AddVarint(8, 3); - EXPECT_EQ(StrCat("5: ", kDebugStringSilentMarker, + EXPECT_EQ(absl::StrCat("5: ", kDebugStringSilentMarker, "1\n" "5: 0x00000002\n" "5: 0x0000000000000003\n" @@ -459,7 +459,7 @@ TEST_F(TextFormatTest, PrintBufferTooSmall) { class CustomUInt32FieldValuePrinter : public TextFormat::FieldValuePrinter { public: std::string PrintUInt32(uint32_t val) const override { - return StrCat(FieldValuePrinter::PrintUInt32(val), "u"); + return absl::StrCat(FieldValuePrinter::PrintUInt32(val), "u"); } }; @@ -483,7 +483,7 @@ TEST_F(TextFormatTest, DefaultCustomFieldPrinter) { class CustomInt32FieldValuePrinter : public TextFormat::FieldValuePrinter { public: std::string PrintInt32(int32_t val) const override { - return StrCat("value-is(", FieldValuePrinter::PrintInt32(val), ")"); + return absl::StrCat("value-is(", FieldValuePrinter::PrintInt32(val), ")"); } }; @@ -534,8 +534,8 @@ TEST_F(TextFormatTest, ErrorCasesRegisteringFieldValuePrinterShouldFail) { class CustomMessageFieldValuePrinter : public TextFormat::FieldValuePrinter { public: std::string PrintInt32(int32_t v) const override { - return StrCat(FieldValuePrinter::PrintInt32(v), " # x", - strings::Hex(v)); + return absl::StrCat(FieldValuePrinter::PrintInt32(v), " # x", + absl::Hex(v)); } std::string PrintMessageStart(const Message& message, int field_index, @@ -544,7 +544,7 @@ class CustomMessageFieldValuePrinter : public TextFormat::FieldValuePrinter { if (single_line_mode) { return " { "; } - return StrCat(" { # ", message.GetDescriptor()->name(), ": ", + return absl::StrCat(" { # ", message.GetDescriptor()->name(), ": ", field_index, "\n"); } }; @@ -634,7 +634,7 @@ class CustomMultilineCommentPrinter : public TextFormat::FieldValuePrinter { std::string PrintMessageStart(const Message& message, int field_index, int field_count, bool single_line_comment) const override { - return StrCat(" { # 1\n", " # 2\n"); + return absl::StrCat(" { # 1\n", " # 2\n"); } }; @@ -845,7 +845,7 @@ TEST_F(TextFormatTest, PrintUnknownEnumFieldProto3) { proto.add_repeated_nested_enum( static_cast(-2147483648)); - EXPECT_EQ(StrCat("repeated_nested_enum: ", kDebugStringSilentMarker, + EXPECT_EQ(absl::StrCat("repeated_nested_enum: ", kDebugStringSilentMarker, "10\n" "repeated_nested_enum: -10\n" "repeated_nested_enum: 2147483647\n" @@ -1116,7 +1116,7 @@ TEST_F(TextFormatTest, PrintExotic) { // have this problem, so we switched to that instead. EXPECT_EQ( - StrCat("repeated_int64: ", kDebugStringSilentMarker, + absl::StrCat("repeated_int64: ", kDebugStringSilentMarker, "-9223372036854775808\n" "repeated_uint64: 18446744073709551615\n" "repeated_double: 123.456\n" @@ -1176,7 +1176,7 @@ TEST_F(TextFormatTest, PrintFloatPrecision) { message.add_repeated_double(1.2345678987654e100); message.add_repeated_double(1.23456789876543e100); - EXPECT_EQ(StrCat("repeated_float: ", kDebugStringSilentMarker, + EXPECT_EQ(absl::StrCat("repeated_float: ", kDebugStringSilentMarker, "1\n" "repeated_float: 1.2\n" "repeated_float: 1.23\n" @@ -1414,7 +1414,7 @@ class TextFormatParserTest : public testing::Test { parser_.RecordErrorsTo(&error_collector); EXPECT_EQ(expected_result, parser_.ParseFromString(input, proto)) << input << " -> " << proto->DebugString(); - EXPECT_EQ(StrCat(line, ":", col, ": ", message, "\n"), + EXPECT_EQ(absl::StrCat(line, ":", col, ": ", message, "\n"), error_collector.text_); parser_.RecordErrorsTo(nullptr); } @@ -1649,7 +1649,7 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { // enum EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAR, "BAR"); EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAZ, - StrCat(unittest::TestAllTypes::BAZ)); + absl::StrCat(unittest::TestAllTypes::BAZ)); EXPECT_INVALID(nested_enum, "FOOBAR"); // message @@ -1937,7 +1937,7 @@ TEST_F(TextFormatParserTest, SetRecursionLimitUnknownFieldValue) { const char* format = "[$0]"; std::string input = "\"test_value\""; for (int i = 0; i < 99; ++i) input = strings::Substitute(format, input); - std::string not_deep_input = StrCat("unknown_nested_array: ", input); + std::string not_deep_input = absl::StrCat("unknown_nested_array: ", input); parser_.AllowUnknownField(true); parser_.SetRecursionLimit(100); @@ -1946,7 +1946,7 @@ TEST_F(TextFormatParserTest, SetRecursionLimitUnknownFieldValue) { ExpectSuccessAndTree(not_deep_input, &message, nullptr); input = strings::Substitute(format, input); - std::string deep_input = StrCat("unknown_nested_array: ", input); + std::string deep_input = absl::StrCat("unknown_nested_array: ", input); ExpectMessage( deep_input, "WARNING:Message type \"protobuf_unittest.NestedTestAllTypes\" has no " diff --git a/src/google/protobuf/timestamp.pb.cc b/src/google/protobuf/timestamp.pb.cc index 09b1b176d6b0..84a04a54cc6c 100644 --- a/src/google/protobuf/timestamp.pb.cc +++ b/src/google/protobuf/timestamp.pb.cc @@ -68,7 +68,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2ftimestamp_2eproto[] PRO "tobuf/types/known/timestamppb\370\001\001\242\002\003GPB\252\002" "\036Google.Protobuf.WellKnownTypesb\006proto3" ; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2ftimestamp_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2ftimestamp_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2ftimestamp_2eproto = { false, false, 239, descriptor_table_protodef_google_2fprotobuf_2ftimestamp_2eproto, "google/protobuf/timestamp.proto", diff --git a/src/google/protobuf/timestamp.pb.h b/src/google/protobuf/timestamp.pb.h index 14efcf185f39..a53224fc3bc9 100644 --- a/src/google/protobuf/timestamp.pb.h +++ b/src/google/protobuf/timestamp.pb.h @@ -159,7 +159,7 @@ class PROTOBUF_EXPORT Timestamp final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Timestamp"; } protected: diff --git a/src/google/protobuf/type.pb.cc b/src/google/protobuf/type.pb.cc index cc11f4ee5c9a..abf51746afd9 100644 --- a/src/google/protobuf/type.pb.cc +++ b/src/google/protobuf/type.pb.cc @@ -242,7 +242,7 @@ static const ::_pbi::DescriptorTable* const descriptor_table_google_2fprotobuf_2 &::descriptor_table_google_2fprotobuf_2fany_2eproto, &::descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto, }; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2ftype_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2ftype_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2ftype_2eproto = { false, false, 1592, descriptor_table_protodef_google_2fprotobuf_2ftype_2eproto, "google/protobuf/type.proto", diff --git a/src/google/protobuf/type.pb.h b/src/google/protobuf/type.pb.h index c64f5cb7db05..218a0e5cb170 100644 --- a/src/google/protobuf/type.pb.h +++ b/src/google/protobuf/type.pb.h @@ -115,7 +115,7 @@ inline const std::string& Field_Kind_Name(Field_Kind value) { (static_cast(value)); } inline bool Field_Kind_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, Field_Kind* value) { + ::absl::string_view name, Field_Kind* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( Field_Kind_descriptor(), name, value); } @@ -146,7 +146,7 @@ inline const std::string& Field_Cardinality_Name(Field_Cardinality value) { (static_cast(value)); } inline bool Field_Cardinality_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, Field_Cardinality* value) { + ::absl::string_view name, Field_Cardinality* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( Field_Cardinality_descriptor(), name, value); } @@ -175,7 +175,7 @@ inline const std::string& Syntax_Name(Syntax value) { (static_cast(value)); } inline bool Syntax_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, Syntax* value) { + ::absl::string_view name, Syntax* value) { return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( Syntax_descriptor(), name, value); } @@ -284,7 +284,7 @@ class PROTOBUF_EXPORT Type final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Type"; } protected: @@ -534,7 +534,7 @@ class PROTOBUF_EXPORT Field final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Field"; } protected: @@ -608,7 +608,7 @@ class PROTOBUF_EXPORT Field final : "Incorrect type passed to function Kind_Name."); return Field_Kind_Name(enum_t_value); } - static inline bool Kind_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, + static inline bool Kind_Parse(::absl::string_view name, Kind* value) { return Field_Kind_Parse(name, value); } @@ -642,7 +642,7 @@ class PROTOBUF_EXPORT Field final : "Incorrect type passed to function Cardinality_Name."); return Field_Cardinality_Name(enum_t_value); } - static inline bool Cardinality_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, + static inline bool Cardinality_Parse(::absl::string_view name, Cardinality* value) { return Field_Cardinality_Parse(name, value); } @@ -908,7 +908,7 @@ class PROTOBUF_EXPORT Enum final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Enum"; } protected: @@ -1132,7 +1132,7 @@ class PROTOBUF_EXPORT EnumValue final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.EnumValue"; } protected: @@ -1316,7 +1316,7 @@ class PROTOBUF_EXPORT Option final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Option"; } protected: diff --git a/src/google/protobuf/unknown_field_set.h b/src/google/protobuf/unknown_field_set.h index 9aa2cbbe47b2..598ae7d82ab4 100644 --- a/src/google/protobuf/unknown_field_set.h +++ b/src/google/protobuf/unknown_field_set.h @@ -217,7 +217,7 @@ namespace internal { inline void WriteVarint(uint32_t num, uint64_t val, UnknownFieldSet* unknown) { unknown->AddVarint(num, val); } -inline void WriteLengthDelimited(uint32_t num, StringPiece val, +inline void WriteLengthDelimited(uint32_t num, absl::string_view val, UnknownFieldSet* unknown) { unknown->AddLengthDelimited(num)->assign(val.data(), val.size()); } diff --git a/src/google/protobuf/unknown_field_set_unittest.cc b/src/google/protobuf/unknown_field_set_unittest.cc index 6996d392212a..6c62570e5819 100644 --- a/src/google/protobuf/unknown_field_set_unittest.cc +++ b/src/google/protobuf/unknown_field_set_unittest.cc @@ -55,7 +55,8 @@ #include #include #include "absl/synchronization/mutex.h" -#include +#include "absl/time/clock.h" +#include "absl/time/time.h" #include #include diff --git a/src/google/protobuf/util/BUILD.bazel b/src/google/protobuf/util/BUILD.bazel index 833cebdea5bd..e30abefe769a 100644 --- a/src/google/protobuf/util/BUILD.bazel +++ b/src/google/protobuf/util/BUILD.bazel @@ -132,6 +132,7 @@ cc_library( "//src/google/protobuf/util/internal:json", "//src/google/protobuf/util/internal:protostream", "//src/google/protobuf/util/internal:utility", + "@com_google_absl//absl/status", ], ) @@ -151,6 +152,7 @@ cc_test( "//src/google/protobuf:test_util", "//src/google/protobuf/testing", "//src/google/protobuf/util/internal:maps_cc_proto", + "@com_google_absl//absl/status", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", ], @@ -195,6 +197,7 @@ cc_library( "//src/google/protobuf/io", "//src/google/protobuf/stubs", "//src/google/protobuf/util/internal:utility", + "@com_google_absl//absl/status", ], ) diff --git a/src/google/protobuf/util/field_mask_util.cc b/src/google/protobuf/util/field_mask_util.cc index 700e59004a08..8247f28304e1 100644 --- a/src/google/protobuf/util/field_mask_util.cc +++ b/src/google/protobuf/util/field_mask_util.cc @@ -33,6 +33,7 @@ #include #include +#include "absl/strings/str_join.h" #include #include @@ -46,10 +47,10 @@ namespace util { using google::protobuf::FieldMask; std::string FieldMaskUtil::ToString(const FieldMask& mask) { - return Join(mask.paths(), ","); + return absl::StrJoin(mask.paths(), ","); } -void FieldMaskUtil::FromString(StringPiece str, FieldMask* out) { +void FieldMaskUtil::FromString(absl::string_view str, FieldMask* out) { out->Clear(); std::vector paths = Split(str, ","); for (const std::string& path : paths) { @@ -58,7 +59,7 @@ void FieldMaskUtil::FromString(StringPiece str, FieldMask* out) { } } -bool FieldMaskUtil::SnakeCaseToCamelCase(StringPiece input, +bool FieldMaskUtil::SnakeCaseToCamelCase(absl::string_view input, std::string* output) { output->clear(); bool after_underscore = false; @@ -88,7 +89,7 @@ bool FieldMaskUtil::SnakeCaseToCamelCase(StringPiece input, return true; } -bool FieldMaskUtil::CamelCaseToSnakeCase(StringPiece input, +bool FieldMaskUtil::CamelCaseToSnakeCase(absl::string_view input, std::string* output) { output->clear(); for (const char c : input) { @@ -122,7 +123,7 @@ bool FieldMaskUtil::ToJsonString(const FieldMask& mask, std::string* out) { return true; } -bool FieldMaskUtil::FromJsonString(StringPiece str, FieldMask* out) { +bool FieldMaskUtil::FromJsonString(absl::string_view str, FieldMask* out) { out->Clear(); std::vector paths = Split(str, ","); for (const std::string& path : paths) { @@ -137,7 +138,7 @@ bool FieldMaskUtil::FromJsonString(StringPiece str, FieldMask* out) { } bool FieldMaskUtil::GetFieldDescriptors( - const Descriptor* descriptor, StringPiece path, + const Descriptor* descriptor, absl::string_view path, std::vector* field_descriptors) { if (field_descriptors != nullptr) { field_descriptors->clear(); @@ -665,7 +666,7 @@ void FieldMaskUtil::Subtract(const Descriptor* descriptor, tree.MergeToFieldMask(out); } -bool FieldMaskUtil::IsPathInFieldMask(StringPiece path, +bool FieldMaskUtil::IsPathInFieldMask(absl::string_view path, const FieldMask& mask) { for (int i = 0; i < mask.paths_size(); ++i) { const std::string& mask_path = mask.paths(i); diff --git a/src/google/protobuf/util/field_mask_util.h b/src/google/protobuf/util/field_mask_util.h index d51a332c8b4d..eef39836fced 100644 --- a/src/google/protobuf/util/field_mask_util.h +++ b/src/google/protobuf/util/field_mask_util.h @@ -37,7 +37,7 @@ #include #include -#include +#include "absl/strings/string_view.h" #include // Must be included last. @@ -54,7 +54,7 @@ class PROTOBUF_EXPORT FieldMaskUtil { // Converts FieldMask to/from string, formatted by separating each path // with a comma (e.g., "foo_bar,baz.quz"). static std::string ToString(const FieldMask& mask); - static void FromString(StringPiece str, FieldMask* out); + static void FromString(absl::string_view str, FieldMask* out); // Populates the FieldMask with the paths corresponding to the fields with the // given numbers, after checking that all field numbers are valid. @@ -76,19 +76,19 @@ class PROTOBUF_EXPORT FieldMaskUtil { // style conforming (i.e., not snake_case when converted to string, or not // camelCase when converted from string), the conversion will fail. static bool ToJsonString(const FieldMask& mask, std::string* out); - static bool FromJsonString(StringPiece str, FieldMask* out); + static bool FromJsonString(absl::string_view str, FieldMask* out); // Get the descriptors of the fields which the given path from the message // descriptor traverses, if field_descriptors is not null. // Return false if the path is not valid, and the content of field_descriptors // is unspecified. static bool GetFieldDescriptors( - const Descriptor* descriptor, StringPiece path, + const Descriptor* descriptor, absl::string_view path, std::vector* field_descriptors); // Checks whether the given path is valid for type T. template - static bool IsValidPath(StringPiece path) { + static bool IsValidPath(absl::string_view path) { return GetFieldDescriptors(T::descriptor(), path, nullptr); } @@ -106,7 +106,7 @@ class PROTOBUF_EXPORT FieldMaskUtil { // Adds a path to FieldMask after checking whether the given path is valid. // This method check-fails if the path is not a valid path for type T. template - static void AddPathToFieldMask(StringPiece path, FieldMask* mask) { + static void AddPathToFieldMask(absl::string_view path, FieldMask* mask) { GOOGLE_CHECK(IsValidPath(path)) << path; mask->add_paths(std::string(path)); } @@ -159,7 +159,7 @@ class PROTOBUF_EXPORT FieldMaskUtil { // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc. // Also note that parent paths are not covered by explicit child path, i.e. // "foo.bar" does NOT cover "foo", even if "bar" is the only child. - static bool IsPathInFieldMask(StringPiece path, const FieldMask& mask); + static bool IsPathInFieldMask(absl::string_view path, const FieldMask& mask); class MergeOptions; // Merges fields specified in a FieldMask into another message. @@ -194,7 +194,7 @@ class PROTOBUF_EXPORT FieldMaskUtil { // Note that the input can contain characters not allowed in C identifiers. // For example, "foo_bar,baz_quz" will be converted to "fooBar,bazQuz" // successfully. - static bool SnakeCaseToCamelCase(StringPiece input, + static bool SnakeCaseToCamelCase(absl::string_view input, std::string* output); // Converts a field name from camelCase to snake_case: // 1. Every uppercase letter is converted to lowercase with an additional @@ -208,7 +208,7 @@ class PROTOBUF_EXPORT FieldMaskUtil { // Note that the input can contain characters not allowed in C identifiers. // For example, "fooBar,bazQuz" will be converted to "foo_bar,baz_quz" // successfully. - static bool CamelCaseToSnakeCase(StringPiece input, + static bool CamelCaseToSnakeCase(absl::string_view input, std::string* output); }; diff --git a/src/google/protobuf/util/internal/BUILD.bazel b/src/google/protobuf/util/internal/BUILD.bazel index b1cdc69909d5..a982f165bb08 100644 --- a/src/google/protobuf/util/internal/BUILD.bazel +++ b/src/google/protobuf/util/internal/BUILD.bazel @@ -9,8 +9,8 @@ load("//build_defs:cpp_opts.bzl", "COPTS") package( default_visibility = [ - "//src/google/protobuf/util:__pkg__", "//pkg:__pkg__", + "//src/google/protobuf/util:__pkg__", ], ) @@ -34,6 +34,7 @@ cc_library( ":utility", "//src/google/protobuf", "//src/google/protobuf/stubs", + "@com_google_absl//absl/strings", ], ) @@ -105,6 +106,8 @@ cc_library( "//src/google/protobuf", "//src/google/protobuf/io", "//src/google/protobuf/stubs", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", ], ) @@ -209,6 +212,7 @@ cc_library( "//src/google/protobuf/io", "//src/google/protobuf/stubs", "//src/google/protobuf/util:type_resolver_util", + "@com_google_absl//absl/strings", ], ) @@ -265,6 +269,7 @@ cc_library( ":constants", "//src/google/protobuf", "//src/google/protobuf/stubs", + "@com_google_absl//absl/strings", ], ) diff --git a/src/google/protobuf/util/internal/datapiece.cc b/src/google/protobuf/util/internal/datapiece.cc index 63c1ac2c7d1a..4428b2d8b036 100644 --- a/src/google/protobuf/util/internal/datapiece.cc +++ b/src/google/protobuf/util/internal/datapiece.cc @@ -37,9 +37,12 @@ #include #include #include -#include -#include +#include "absl/status/status.h" +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include +#include #include namespace google { @@ -47,17 +50,17 @@ namespace protobuf { namespace util { namespace converter { -using util::Status; +using ::absl::Status; namespace { template -util::StatusOr ValidateNumberConversion(To after, From before) { +absl::StatusOr ValidateNumberConversion(To after, From before) { if (after == before && MathUtil::Sign(before) == MathUtil::Sign(after)) { return after; } else { - return util::InvalidArgumentError( + return absl::InvalidArgumentError( std::is_integral::value ? ValueAsString(before) : std::is_same::value ? DoubleAsString(before) : FloatAsString(before)); @@ -68,7 +71,7 @@ util::StatusOr ValidateNumberConversion(To after, From before) { // int32, int64, uint32, uint64, double and float // except conversion between double and float. template -util::StatusOr NumberConvertAndCheck(From before) { +absl::StatusOr NumberConvertAndCheck(From before) { if (std::is_same::value) return before; To after = static_cast(before); @@ -78,7 +81,7 @@ util::StatusOr NumberConvertAndCheck(From before) { // For conversion to integer types (int32, int64, uint32, uint64) from floating // point types (double, float) only. template -util::StatusOr FloatingPointToIntConvertAndCheck(From before) { +absl::StatusOr FloatingPointToIntConvertAndCheck(From before) { if (std::is_same::value) return before; To after = static_cast(before); @@ -86,13 +89,13 @@ util::StatusOr FloatingPointToIntConvertAndCheck(From before) { } // For conversion between double and float only. -util::StatusOr FloatToDouble(float before) { +absl::StatusOr FloatToDouble(float before) { // Casting float to double should just work as double has more precision // than float. return static_cast(before); } -util::StatusOr DoubleToFloat(double before) { +absl::StatusOr DoubleToFloat(double before) { if (std::isnan(before)) { return std::numeric_limits::quiet_NaN(); } else if (!std::isfinite(before)) { @@ -119,7 +122,7 @@ util::StatusOr DoubleToFloat(double before) { } } // Double value outside of the range of float. - return util::InvalidArgumentError(DoubleAsString(before)); + return absl::InvalidArgumentError(DoubleAsString(before)); } else { return static_cast(before); } @@ -127,7 +130,7 @@ util::StatusOr DoubleToFloat(double before) { } // namespace -util::StatusOr DataPiece::ToInt32() const { +absl::StatusOr DataPiece::ToInt32() const { if (type_ == TYPE_STRING) return StringToNumber(safe_strto32); @@ -140,7 +143,7 @@ util::StatusOr DataPiece::ToInt32() const { return GenericConvert(); } -util::StatusOr DataPiece::ToUint32() const { +absl::StatusOr DataPiece::ToUint32() const { if (type_ == TYPE_STRING) return StringToNumber(safe_strtou32); @@ -153,7 +156,7 @@ util::StatusOr DataPiece::ToUint32() const { return GenericConvert(); } -util::StatusOr DataPiece::ToInt64() const { +absl::StatusOr DataPiece::ToInt64() const { if (type_ == TYPE_STRING) return StringToNumber(safe_strto64); @@ -166,7 +169,7 @@ util::StatusOr DataPiece::ToInt64() const { return GenericConvert(); } -util::StatusOr DataPiece::ToUint64() const { +absl::StatusOr DataPiece::ToUint64() const { if (type_ == TYPE_STRING) return StringToNumber(safe_strtou64); @@ -179,7 +182,7 @@ util::StatusOr DataPiece::ToUint64() const { return GenericConvert(); } -util::StatusOr DataPiece::ToDouble() const { +absl::StatusOr DataPiece::ToDouble() const { if (type_ == TYPE_FLOAT) { return FloatToDouble(float_); } @@ -187,11 +190,11 @@ util::StatusOr DataPiece::ToDouble() const { if (str_ == "Infinity") return std::numeric_limits::infinity(); if (str_ == "-Infinity") return -std::numeric_limits::infinity(); if (str_ == "NaN") return std::numeric_limits::quiet_NaN(); - util::StatusOr value = StringToNumber(safe_strtod); + absl::StatusOr value = StringToNumber(safe_strtod); if (value.ok() && !std::isfinite(value.value())) { // safe_strtod converts out-of-range values to +inf/-inf, but we want // to report them as errors. - return util::InvalidArgumentError(StrCat("\"", str_, "\"")); + return absl::InvalidArgumentError(absl::StrCat("\"", str_, "\"")); } else { return value; } @@ -199,7 +202,7 @@ util::StatusOr DataPiece::ToDouble() const { return GenericConvert(); } -util::StatusOr DataPiece::ToFloat() const { +absl::StatusOr DataPiece::ToFloat() const { if (type_ == TYPE_DOUBLE) { return DoubleToFloat(double_); } @@ -214,7 +217,7 @@ util::StatusOr DataPiece::ToFloat() const { return GenericConvert(); } -util::StatusOr DataPiece::ToBool() const { +absl::StatusOr DataPiece::ToBool() const { switch (type_) { case TYPE_BOOL: return bool_; @@ -223,36 +226,36 @@ util::StatusOr DataPiece::ToBool() const { default: break; } - return util::InvalidArgumentError( + return absl::InvalidArgumentError( ValueAsStringOrDefault("Wrong type. Cannot convert to Bool.")); } -util::StatusOr DataPiece::ToString() const { +absl::StatusOr DataPiece::ToString() const { switch (type_) { case TYPE_STRING: return std::string(str_); case TYPE_BYTES: { std::string base64; - Base64Escape(str_, &base64); + absl::Base64Escape(str_, &base64); return base64; } default: - return util::InvalidArgumentError( + return absl::InvalidArgumentError( ValueAsStringOrDefault("Cannot convert to string.")); } } std::string DataPiece::ValueAsStringOrDefault( - StringPiece default_string) const { + absl::string_view default_string) const { switch (type_) { case TYPE_INT32: - return StrCat(i32_); + return absl::StrCat(i32_); case TYPE_INT64: - return StrCat(i64_); + return absl::StrCat(i64_); case TYPE_UINT32: - return StrCat(u32_); + return absl::StrCat(u32_); case TYPE_UINT64: - return StrCat(u64_); + return absl::StrCat(u64_); case TYPE_DOUBLE: return DoubleAsString(double_); case TYPE_FLOAT: @@ -260,11 +263,11 @@ std::string DataPiece::ValueAsStringOrDefault( case TYPE_BOOL: return SimpleBtoa(bool_); case TYPE_STRING: - return StrCat("\"", str_.ToString(), "\""); + return absl::StrCat("\"", str_, "\""); case TYPE_BYTES: { std::string base64; - WebSafeBase64Escape(str_, &base64); - return StrCat("\"", base64, "\""); + absl::WebSafeBase64Escape(str_, &base64); + return absl::StrCat("\"", base64, "\""); } case TYPE_NULL: return "null"; @@ -273,22 +276,22 @@ std::string DataPiece::ValueAsStringOrDefault( } } -util::StatusOr DataPiece::ToBytes() const { - if (type_ == TYPE_BYTES) return str_.ToString(); +absl::StatusOr DataPiece::ToBytes() const { + if (type_ == TYPE_BYTES) return std::string(str_); if (type_ == TYPE_STRING) { std::string decoded; if (!DecodeBase64(str_, &decoded)) { - return util::InvalidArgumentError( + return absl::InvalidArgumentError( ValueAsStringOrDefault("Invalid data in input.")); } return decoded; } else { - return util::InvalidArgumentError(ValueAsStringOrDefault( + return absl::InvalidArgumentError(ValueAsStringOrDefault( "Wrong type. Only String or Bytes can be converted to Bytes.")); } } -util::StatusOr DataPiece::ToEnum(const google::protobuf::Enum* enum_type, +absl::StatusOr DataPiece::ToEnum(const google::protobuf::Enum* enum_type, bool use_lower_camel_for_enums, bool case_insensitive_enum_parsing, bool ignore_unknown_enum_values, @@ -303,7 +306,7 @@ util::StatusOr DataPiece::ToEnum(const google::protobuf::Enum* enum_type, if (value != nullptr) return value->number(); // Check if int version of enum is sent as string. - util::StatusOr int_value = ToInt32(); + absl::StatusOr int_value = ToInt32(); if (int_value.ok()) { if (const google::protobuf::EnumValue* enum_value = FindEnumValueByNumberOrNull(enum_type, int_value.value())) { @@ -317,7 +320,7 @@ util::StatusOr DataPiece::ToEnum(const google::protobuf::Enum* enum_type, if (should_normalize_enum) { for (std::string::iterator it = enum_name.begin(); it != enum_name.end(); ++it) { - *it = *it == '-' ? '_' : ascii_toupper(*it); + *it = *it == '-' ? '_' : absl::ascii_toupper(*it); } value = FindEnumValueByNameOrNull(enum_type, enum_name); if (value != nullptr) return value->number(); @@ -343,12 +346,12 @@ util::StatusOr DataPiece::ToEnum(const google::protobuf::Enum* enum_type, // enum because we preserve unknown enum values as well. return ToInt32(); } - return util::InvalidArgumentError( + return absl::InvalidArgumentError( ValueAsStringOrDefault("Cannot find enum with given value.")); } template -util::StatusOr DataPiece::GenericConvert() const { +absl::StatusOr DataPiece::GenericConvert() const { switch (type_) { case TYPE_INT32: return NumberConvertAndCheck(i32_); @@ -363,35 +366,35 @@ util::StatusOr DataPiece::GenericConvert() const { case TYPE_FLOAT: return NumberConvertAndCheck(float_); default: // TYPE_ENUM, TYPE_STRING, TYPE_CORD, TYPE_BOOL - return util::InvalidArgumentError(ValueAsStringOrDefault( + return absl::InvalidArgumentError(ValueAsStringOrDefault( "Wrong type. Bool, Enum, String and Cord not supported in " "GenericConvert.")); } } template -util::StatusOr DataPiece::StringToNumber(bool (*func)(StringPiece, +absl::StatusOr DataPiece::StringToNumber(bool (*func)(absl::string_view, To*)) const { if (str_.size() > 0 && (str_[0] == ' ' || str_[str_.size() - 1] == ' ')) { - return util::InvalidArgumentError(StrCat("\"", str_, "\"")); + return absl::InvalidArgumentError(absl::StrCat("\"", str_, "\"")); } To result; if (func(str_, &result)) return result; - return util::InvalidArgumentError( - StrCat("\"", std::string(str_), "\"")); + return absl::InvalidArgumentError( + absl::StrCat("\"", std::string(str_), "\"")); } -bool DataPiece::DecodeBase64(StringPiece src, std::string* dest) const { +bool DataPiece::DecodeBase64(absl::string_view src, std::string* dest) const { // Try web-safe decode first, if it fails, try the non-web-safe decode. - if (WebSafeBase64Unescape(src, dest)) { + if (absl::WebSafeBase64Unescape(src, dest)) { if (use_strict_base64_decoding_) { // In strict mode, check if the escaped version gives us the same value as // unescaped. std::string encoded; // WebSafeBase64Escape does no padding by default. - WebSafeBase64Escape(*dest, &encoded); + absl::WebSafeBase64Escape(*dest, &encoded); // Remove trailing padding '=' characters before comparison. - StringPiece src_no_padding = StringPiece(src).substr( + absl::string_view src_no_padding = absl::string_view(src).substr( 0, HasSuffixString(src, "=") ? src.find_last_not_of('=') + 1 : src.length()); return encoded == src_no_padding; @@ -399,12 +402,11 @@ bool DataPiece::DecodeBase64(StringPiece src, std::string* dest) const { return true; } - if (Base64Unescape(src, dest)) { + if (absl::Base64Unescape(src, dest)) { if (use_strict_base64_decoding_) { std::string encoded; - Base64Escape(reinterpret_cast(dest->data()), - dest->length(), &encoded, false); - StringPiece src_no_padding = StringPiece(src).substr( + strings::LegacyBase64EscapeWithoutPadding(*dest, &encoded); + absl::string_view src_no_padding = absl::string_view(src).substr( 0, HasSuffixString(src, "=") ? src.find_last_not_of('=') + 1 : src.length()); return encoded == src_no_padding; diff --git a/src/google/protobuf/util/internal/datapiece.h b/src/google/protobuf/util/internal/datapiece.h index 6d08349acd3b..8dd36a7ba3c5 100644 --- a/src/google/protobuf/util/internal/datapiece.h +++ b/src/google/protobuf/util/internal/datapiece.h @@ -37,8 +37,8 @@ #include #include #include -#include -#include +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" // Must be included last. #include @@ -54,10 +54,11 @@ class ProtoWriter; // For primitive types (int32, int64, uint32, uint64, double, float, bool), // the data is stored by value. // -// For string, a StringPiece is stored. For Cord, a pointer to Cord is stored. -// Just like StringPiece, the DataPiece class does not own the storage for -// the actual string or Cord, so it is the user's responsibility to guarantee -// that the underlying storage is still valid when the DataPiece is accessed. +// For string, an absl::string_view is stored. For Cord, a pointer to Cord is +// stored. Just like absl::string_view, the DataPiece class does not own the +// storage for the actual string or Cord, so it is the user's responsibility to +// guarantee that the underlying storage is still valid when the DataPiece is +// accessed. class PROTOBUF_EXPORT DataPiece { public: // Identifies data type of the value. @@ -93,12 +94,13 @@ class PROTOBUF_EXPORT DataPiece { : type_(TYPE_FLOAT), float_(value), use_strict_base64_decoding_(false) {} explicit DataPiece(const bool value) : type_(TYPE_BOOL), bool_(value), use_strict_base64_decoding_(false) {} - DataPiece(StringPiece value, bool use_strict_base64_decoding) + DataPiece(absl::string_view value, bool use_strict_base64_decoding) : type_(TYPE_STRING), str_(value), use_strict_base64_decoding_(use_strict_base64_decoding) {} // Constructor for bytes. The second parameter is not used. - DataPiece(StringPiece value, bool /*dummy*/, bool use_strict_base64_decoding) + DataPiece(absl::string_view value, bool /*dummy*/, + bool use_strict_base64_decoding) : type_(TYPE_BYTES), str_(value), use_strict_base64_decoding_(use_strict_base64_decoding) {} @@ -120,41 +122,41 @@ class PROTOBUF_EXPORT DataPiece { bool use_strict_base64_decoding() { return use_strict_base64_decoding_; } - StringPiece str() const { + absl::string_view str() const { GOOGLE_LOG_IF(DFATAL, type_ != TYPE_STRING) << "Not a string type."; return str_; } // Parses, casts or converts the value stored in the DataPiece into an int32. - util::StatusOr ToInt32() const; + absl::StatusOr ToInt32() const; // Parses, casts or converts the value stored in the DataPiece into a uint32. - util::StatusOr ToUint32() const; + absl::StatusOr ToUint32() const; // Parses, casts or converts the value stored in the DataPiece into an int64. - util::StatusOr ToInt64() const; + absl::StatusOr ToInt64() const; // Parses, casts or converts the value stored in the DataPiece into a uint64. - util::StatusOr ToUint64() const; + absl::StatusOr ToUint64() const; // Parses, casts or converts the value stored in the DataPiece into a double. - util::StatusOr ToDouble() const; + absl::StatusOr ToDouble() const; // Parses, casts or converts the value stored in the DataPiece into a float. - util::StatusOr ToFloat() const; + absl::StatusOr ToFloat() const; // Parses, casts or converts the value stored in the DataPiece into a bool. - util::StatusOr ToBool() const; + absl::StatusOr ToBool() const; // Parses, casts or converts the value stored in the DataPiece into a string. - util::StatusOr ToString() const; + absl::StatusOr ToString() const; // Tries to convert the value contained in this datapiece to string. If the // conversion fails, it returns the default_string. - std::string ValueAsStringOrDefault(StringPiece default_string) const; + std::string ValueAsStringOrDefault(absl::string_view default_string) const; - util::StatusOr ToBytes() const; + absl::StatusOr ToBytes() const; private: friend class ProtoWriter; @@ -168,7 +170,7 @@ class PROTOBUF_EXPORT DataPiece { // Same as the ToEnum() method above but with additional flag to ignore // unknown enum values. - util::StatusOr ToEnum(const google::protobuf::Enum* enum_type, + absl::StatusOr ToEnum(const google::protobuf::Enum* enum_type, bool use_lower_camel_for_enums, bool case_insensitive_enum_parsing, bool ignore_unknown_enum_values, @@ -177,15 +179,15 @@ class PROTOBUF_EXPORT DataPiece { // For numeric conversion between // int32, int64, uint32, uint64, double, float and bool template - util::StatusOr GenericConvert() const; + absl::StatusOr GenericConvert() const; // For conversion from string to // int32, int64, uint32, uint64, double, float and bool template - util::StatusOr StringToNumber(bool (*func)(StringPiece, To*)) const; + absl::StatusOr StringToNumber(bool (*func)(absl::string_view, To*)) const; // Decodes a base64 string. Returns true on success. - bool DecodeBase64(StringPiece src, std::string* dest) const; + bool DecodeBase64(absl::string_view src, std::string* dest) const; // Helper function to initialize this DataPiece with 'other'. void InternalCopy(const DataPiece& other); @@ -202,7 +204,7 @@ class PROTOBUF_EXPORT DataPiece { double double_; float float_; bool bool_; - StringPiece str_; + absl::string_view str_; }; // Uses a stricter version of base64 decoding for byte fields. diff --git a/src/google/protobuf/util/internal/default_value_objectwriter.cc b/src/google/protobuf/util/internal/default_value_objectwriter.cc index 7f61cdafa7c7..cc7a36f03a22 100644 --- a/src/google/protobuf/util/internal/default_value_objectwriter.cc +++ b/src/google/protobuf/util/internal/default_value_objectwriter.cc @@ -47,11 +47,11 @@ namespace { // passed converter function on the DataPiece created from "value" argument. // If value is empty or if conversion fails, the default_value is returned. template -T ConvertTo(StringPiece value, - util::StatusOr (DataPiece::*converter_fn)() const, +T ConvertTo(absl::string_view value, + absl::StatusOr (DataPiece::*converter_fn)() const, T default_value) { if (value.empty()) return default_value; - util::StatusOr result = (DataPiece(value, true).*converter_fn)(); + absl::StatusOr result = (DataPiece(value, true).*converter_fn)(); return result.ok() ? result.value() : default_value; } } // namespace @@ -76,7 +76,7 @@ DefaultValueObjectWriter::~DefaultValueObjectWriter() { } DefaultValueObjectWriter* DefaultValueObjectWriter::RenderBool( - StringPiece name, bool value) { + absl::string_view name, bool value) { if (current_ == nullptr) { ow_->RenderBool(name, value); } else { @@ -86,7 +86,7 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::RenderBool( } DefaultValueObjectWriter* DefaultValueObjectWriter::RenderInt32( - StringPiece name, int32_t value) { + absl::string_view name, int32_t value) { if (current_ == nullptr) { ow_->RenderInt32(name, value); } else { @@ -96,7 +96,7 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::RenderInt32( } DefaultValueObjectWriter* DefaultValueObjectWriter::RenderUint32( - StringPiece name, uint32_t value) { + absl::string_view name, uint32_t value) { if (current_ == nullptr) { ow_->RenderUint32(name, value); } else { @@ -106,7 +106,7 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::RenderUint32( } DefaultValueObjectWriter* DefaultValueObjectWriter::RenderInt64( - StringPiece name, int64_t value) { + absl::string_view name, int64_t value) { if (current_ == nullptr) { ow_->RenderInt64(name, value); } else { @@ -116,7 +116,7 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::RenderInt64( } DefaultValueObjectWriter* DefaultValueObjectWriter::RenderUint64( - StringPiece name, uint64_t value) { + absl::string_view name, uint64_t value) { if (current_ == nullptr) { ow_->RenderUint64(name, value); } else { @@ -126,7 +126,7 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::RenderUint64( } DefaultValueObjectWriter* DefaultValueObjectWriter::RenderDouble( - StringPiece name, double value) { + absl::string_view name, double value) { if (current_ == nullptr) { ow_->RenderDouble(name, value); } else { @@ -136,7 +136,7 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::RenderDouble( } DefaultValueObjectWriter* DefaultValueObjectWriter::RenderFloat( - StringPiece name, float value) { + absl::string_view name, float value) { if (current_ == nullptr) { ow_->RenderBool(name, value); } else { @@ -146,12 +146,12 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::RenderFloat( } DefaultValueObjectWriter* DefaultValueObjectWriter::RenderString( - StringPiece name, StringPiece value) { + absl::string_view name, absl::string_view value) { if (current_ == nullptr) { ow_->RenderString(name, value); } else { - // Since StringPiece is essentially a pointer, takes a copy of "value" to - // avoid ownership issues. + // Since absl::string_view is essentially a pointer, takes a copy of + // "value" to avoid ownership issues. string_values_.emplace_back(new std::string(value)); RenderDataPiece(name, DataPiece(*string_values_.back(), true)); } @@ -159,12 +159,12 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::RenderString( } DefaultValueObjectWriter* DefaultValueObjectWriter::RenderBytes( - StringPiece name, StringPiece value) { + absl::string_view name, absl::string_view value) { if (current_ == nullptr) { ow_->RenderBytes(name, value); } else { - // Since StringPiece is essentially a pointer, takes a copy of "value" to - // avoid ownership issues. + // Since absl::string_view is essentially a pointer, takes a copy of + // "value" to avoid ownership issues. string_values_.emplace_back(new std::string(value)); RenderDataPiece(name, DataPiece(*string_values_.back(), false, true)); } @@ -172,7 +172,7 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::RenderBytes( } DefaultValueObjectWriter* DefaultValueObjectWriter::RenderNull( - StringPiece name) { + absl::string_view name) { if (current_ == nullptr) { ow_->RenderNull(name); } else { @@ -216,7 +216,7 @@ DefaultValueObjectWriter::Node::Node( field_scrub_callback_(std::move(field_scrub_callback)) {} DefaultValueObjectWriter::Node* DefaultValueObjectWriter::Node::FindChild( - StringPiece name) { + absl::string_view name) { if (name.empty() || kind_ != OBJECT) { return nullptr; } @@ -283,7 +283,7 @@ const google::protobuf::Type* DefaultValueObjectWriter::Node::GetMapValueType( // get the field_type in this case. break; } - util::StatusOr sub_type = + absl::StatusOr sub_type = typeinfo->ResolveTypeUrl(sub_field.type_url()); if (!sub_type.ok()) { GOOGLE_LOG(WARNING) << "Cannot resolve type '" << sub_field.type_url() << "'."; @@ -346,7 +346,7 @@ void DefaultValueObjectWriter::Node::PopulateChildren( if (field.kind() == google::protobuf::Field::TYPE_MESSAGE) { kind = OBJECT; - util::StatusOr found_result = + absl::StatusOr found_result = typeinfo->ResolveTypeUrl(field.type_url()); if (!found_result.ok()) { // "field" is of an unknown type. @@ -498,7 +498,7 @@ DataPiece DefaultValueObjectWriter::CreateDefaultDataPieceForField( } DefaultValueObjectWriter* DefaultValueObjectWriter::StartObject( - StringPiece name) { + absl::string_view name) { if (current_ == nullptr) { std::vector path; root_.reset(CreateNewNode(std::string(name), &type_, OBJECT, @@ -549,7 +549,7 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::EndObject() { } DefaultValueObjectWriter* DefaultValueObjectWriter::StartList( - StringPiece name) { + absl::string_view name) { if (current_ == nullptr) { std::vector path; root_.reset(CreateNewNode(std::string(name), &type_, LIST, @@ -593,18 +593,18 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::EndList() { return this; } -void DefaultValueObjectWriter::RenderDataPiece(StringPiece name, +void DefaultValueObjectWriter::RenderDataPiece(absl::string_view name, const DataPiece& data) { MaybePopulateChildrenOfAny(current_); if (current_->type() != nullptr && current_->type()->name() == kAnyType && name == "@type") { - util::StatusOr data_string = data.ToString(); + absl::StatusOr data_string = data.ToString(); if (data_string.ok()) { const std::string& string_value = data_string.value(); // If the type of current_ is "Any" and its "@type" field is being set // here, sets the type of current_ to be the type specified by the // "@type". - util::StatusOr found_type = + absl::StatusOr found_type = typeinfo_->ResolveTypeUrl(string_value); if (!found_type.ok()) { GOOGLE_LOG(WARNING) << "Failed to resolve type '" << string_value << "'."; diff --git a/src/google/protobuf/util/internal/default_value_objectwriter.h b/src/google/protobuf/util/internal/default_value_objectwriter.h index a9e1673fa1e4..3a0a062cbcf0 100644 --- a/src/google/protobuf/util/internal/default_value_objectwriter.h +++ b/src/google/protobuf/util/internal/default_value_objectwriter.h @@ -38,7 +38,7 @@ #include #include -#include +#include "absl/strings/string_view.h" #include #include #include @@ -81,44 +81,46 @@ class PROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter { const google::protobuf::Type& type, ObjectWriter* ow); + DefaultValueObjectWriter(const DefaultValueObjectWriter&) = delete; + DefaultValueObjectWriter& operator=(const DefaultValueObjectWriter&) = delete; ~DefaultValueObjectWriter() override; // ObjectWriter methods. - DefaultValueObjectWriter* StartObject(StringPiece name) override; + DefaultValueObjectWriter* StartObject(absl::string_view name) override; DefaultValueObjectWriter* EndObject() override; - DefaultValueObjectWriter* StartList(StringPiece name) override; + DefaultValueObjectWriter* StartList(absl::string_view name) override; DefaultValueObjectWriter* EndList() override; - DefaultValueObjectWriter* RenderBool(StringPiece name, + DefaultValueObjectWriter* RenderBool(absl::string_view name, bool value) override; - DefaultValueObjectWriter* RenderInt32(StringPiece name, + DefaultValueObjectWriter* RenderInt32(absl::string_view name, int32_t value) override; - DefaultValueObjectWriter* RenderUint32(StringPiece name, + DefaultValueObjectWriter* RenderUint32(absl::string_view name, uint32_t value) override; - DefaultValueObjectWriter* RenderInt64(StringPiece name, + DefaultValueObjectWriter* RenderInt64(absl::string_view name, int64_t value) override; - DefaultValueObjectWriter* RenderUint64(StringPiece name, + DefaultValueObjectWriter* RenderUint64(absl::string_view name, uint64_t value) override; - DefaultValueObjectWriter* RenderDouble(StringPiece name, + DefaultValueObjectWriter* RenderDouble(absl::string_view name, double value) override; - DefaultValueObjectWriter* RenderFloat(StringPiece name, + DefaultValueObjectWriter* RenderFloat(absl::string_view name, float value) override; - DefaultValueObjectWriter* RenderString(StringPiece name, - StringPiece value) override; - DefaultValueObjectWriter* RenderBytes(StringPiece name, - StringPiece value) override; + DefaultValueObjectWriter* RenderString(absl::string_view name, + absl::string_view value) override; + DefaultValueObjectWriter* RenderBytes(absl::string_view name, + absl::string_view value) override; - DefaultValueObjectWriter* RenderNull(StringPiece name) override; + DefaultValueObjectWriter* RenderNull(absl::string_view name) override; // Register the callback for scrubbing of fields. void RegisterFieldScrubCallBack(FieldScrubCallBack field_scrub_callback); @@ -153,6 +155,8 @@ class PROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter { const std::vector& path, bool suppress_empty_list, bool preserve_proto_field_names, bool use_ints_for_enums, FieldScrubCallBack field_scrub_callback); + Node(const Node&) = delete; + Node& operator=(const Node&) = delete; virtual ~Node() { for (int i = 0; i < children_.size(); ++i) { delete children_[i]; @@ -163,7 +167,7 @@ class PROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter { void AddChild(Node* child) { children_.push_back(child); } // Finds the child given its name. - Node* FindChild(StringPiece name); + Node* FindChild(absl::string_view name); // Populates children of this Node based on its type. If there are already // children created, they will be merged to the result. Caller should pass @@ -239,9 +243,6 @@ class PROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter { // Function for determining whether a field needs to be scrubbed or not. FieldScrubCallBack field_scrub_callback_; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Node); }; // Creates a new Node and returns it. Caller owns memory of returned object. @@ -279,7 +280,7 @@ class PROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter { void WriteRoot(); // Adds or replaces the data_ of a primitive child node. - void RenderDataPiece(StringPiece name, const DataPiece& data); + void RenderDataPiece(absl::string_view name, const DataPiece& data); // Returns the default enum value as a DataPiece, or the first enum value if // there is no default. For proto3, where we cannot specify an explicit @@ -318,8 +319,6 @@ class PROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter { FieldScrubCallBack field_scrub_callback_; ObjectWriter* ow_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DefaultValueObjectWriter); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/error_listener.h b/src/google/protobuf/util/internal/error_listener.h index 8c9c5016820b..2c3d9d56f7cf 100644 --- a/src/google/protobuf/util/internal/error_listener.h +++ b/src/google/protobuf/util/internal/error_listener.h @@ -39,7 +39,7 @@ #include #include #include -#include +#include "absl/strings/string_view.h" #include // Must be included last. @@ -53,49 +53,46 @@ namespace converter { // Interface for error listener. class PROTOBUF_EXPORT ErrorListener { public: + ErrorListener(const ErrorListener&) = delete; + ErrorListener& operator=(const ErrorListener&) = delete; virtual ~ErrorListener() {} // Reports an invalid name at the given location. virtual void InvalidName(const LocationTrackerInterface& loc, - StringPiece invalid_name, - StringPiece message) = 0; + absl::string_view invalid_name, + absl::string_view message) = 0; // Reports an invalid value for a field. virtual void InvalidValue(const LocationTrackerInterface& loc, - StringPiece type_name, - StringPiece value) = 0; + absl::string_view type_name, + absl::string_view value) = 0; // Reports a missing required field. virtual void MissingField(const LocationTrackerInterface& loc, - StringPiece missing_name) = 0; + absl::string_view missing_name) = 0; protected: ErrorListener() {} - - private: - // Do not add any data members to this class. - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorListener); }; // An error listener that ignores all errors. class PROTOBUF_EXPORT NoopErrorListener : public ErrorListener { public: NoopErrorListener() {} + NoopErrorListener(const NoopErrorListener&) = delete; + NoopErrorListener& operator=(const NoopErrorListener&) = delete; ~NoopErrorListener() override {} void InvalidName(const LocationTrackerInterface& /*loc*/, - StringPiece /* invalid_name */, - StringPiece /* message */) override {} + absl::string_view /* invalid_name */, + absl::string_view /* message */) override {} void InvalidValue(const LocationTrackerInterface& /*loc*/, - StringPiece /* type_name */, - StringPiece /* value */) override {} + absl::string_view /* type_name */, + absl::string_view /* value */) override {} void MissingField(const LocationTrackerInterface& /* loc */, - StringPiece /* missing_name */) override {} - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(NoopErrorListener); + absl::string_view /* missing_name */) override {} }; diff --git a/src/google/protobuf/util/internal/expecting_objectwriter.h b/src/google/protobuf/util/internal/expecting_objectwriter.h index 76fe2b60493b..b763f7080b9f 100644 --- a/src/google/protobuf/util/internal/expecting_objectwriter.h +++ b/src/google/protobuf/util/internal/expecting_objectwriter.h @@ -46,7 +46,7 @@ // ->EndObject(); // // // Actual testing -// mock.StartObject(StringPiece()) +// mock.StartObject(absl::string_view()) // ->RenderString("key", "value") // ->EndObject(); @@ -54,7 +54,7 @@ #include #include -#include +#include "absl/strings/string_view.h" #include namespace google { @@ -74,35 +74,37 @@ class MockObjectWriter : public ObjectWriter { public: MockObjectWriter() {} - MOCK_METHOD(ObjectWriter*, StartObject, (StringPiece), (override)); + MOCK_METHOD(ObjectWriter*, StartObject, (absl::string_view), (override)); MOCK_METHOD(ObjectWriter*, EndObject, (), (override)); - MOCK_METHOD(ObjectWriter*, StartList, (StringPiece), (override)); + MOCK_METHOD(ObjectWriter*, StartList, (absl::string_view), (override)); MOCK_METHOD(ObjectWriter*, EndList, (), (override)); - MOCK_METHOD(ObjectWriter*, RenderBool, (StringPiece, bool), (override)); - MOCK_METHOD(ObjectWriter*, RenderInt32, (StringPiece, int32_t), + MOCK_METHOD(ObjectWriter*, RenderBool, (absl::string_view, bool), (override)); + MOCK_METHOD(ObjectWriter*, RenderInt32, (absl::string_view, int32_t), (override)); - MOCK_METHOD(ObjectWriter*, RenderUint32, (StringPiece, uint32_t), + MOCK_METHOD(ObjectWriter*, RenderUint32, (absl::string_view, uint32_t), (override)); - MOCK_METHOD(ObjectWriter*, RenderInt64, (StringPiece, int64_t), + MOCK_METHOD(ObjectWriter*, RenderInt64, (absl::string_view, int64_t), (override)); - MOCK_METHOD(ObjectWriter*, RenderUint64, (StringPiece, uint64_t), + MOCK_METHOD(ObjectWriter*, RenderUint64, (absl::string_view, uint64_t), (override)); - MOCK_METHOD(ObjectWriter*, RenderDouble, (StringPiece, double), + MOCK_METHOD(ObjectWriter*, RenderDouble, (absl::string_view, double), (override)); - MOCK_METHOD(ObjectWriter*, RenderFloat, (StringPiece, float), + MOCK_METHOD(ObjectWriter*, RenderFloat, (absl::string_view, float), (override)); MOCK_METHOD(ObjectWriter*, RenderString, - (StringPiece, StringPiece), (override)); - MOCK_METHOD(ObjectWriter*, RenderBytes, (StringPiece, StringPiece), - (override)); - MOCK_METHOD(ObjectWriter*, RenderNull, (StringPiece), (override)); + (absl::string_view, absl::string_view), (override)); + MOCK_METHOD(ObjectWriter*, RenderBytes, + (absl::string_view, absl::string_view), (override)); + MOCK_METHOD(ObjectWriter*, RenderNull, (absl::string_view), (override)); }; class ExpectingObjectWriter : public ObjectWriter { public: explicit ExpectingObjectWriter(MockObjectWriter* mock) : mock_(mock) {} + ExpectingObjectWriter(const ExpectingObjectWriter&) = delete; + ExpectingObjectWriter& operator=(const ExpectingObjectWriter&) = delete; - ObjectWriter* StartObject(StringPiece name) override { + ObjectWriter* StartObject(absl::string_view name) override { (name.empty() ? EXPECT_CALL(*mock_, StartObject(IsEmpty())) : EXPECT_CALL(*mock_, StartObject(Eq(std::string(name))))) .WillOnce(Return(mock_)) @@ -117,7 +119,7 @@ class ExpectingObjectWriter : public ObjectWriter { return this; } - ObjectWriter* StartList(StringPiece name) override { + ObjectWriter* StartList(absl::string_view name) override { (name.empty() ? EXPECT_CALL(*mock_, StartList(IsEmpty())) : EXPECT_CALL(*mock_, StartList(Eq(std::string(name))))) .WillOnce(Return(mock_)) @@ -132,7 +134,7 @@ class ExpectingObjectWriter : public ObjectWriter { return this; } - ObjectWriter* RenderBool(StringPiece name, bool value) override { + ObjectWriter* RenderBool(absl::string_view name, bool value) override { (name.empty() ? EXPECT_CALL(*mock_, RenderBool(IsEmpty(), TypedEq(value))) : EXPECT_CALL(*mock_, @@ -142,7 +144,7 @@ class ExpectingObjectWriter : public ObjectWriter { return this; } - ObjectWriter* RenderInt32(StringPiece name, int32_t value) override { + ObjectWriter* RenderInt32(absl::string_view name, int32_t value) override { (name.empty() ? EXPECT_CALL(*mock_, RenderInt32(IsEmpty(), TypedEq(value))) : EXPECT_CALL(*mock_, RenderInt32(Eq(std::string(name)), @@ -152,7 +154,7 @@ class ExpectingObjectWriter : public ObjectWriter { return this; } - ObjectWriter* RenderUint32(StringPiece name, uint32_t value) override { + ObjectWriter* RenderUint32(absl::string_view name, uint32_t value) override { (name.empty() ? EXPECT_CALL(*mock_, RenderUint32(IsEmpty(), TypedEq(value))) : EXPECT_CALL(*mock_, RenderUint32(Eq(std::string(name)), @@ -162,7 +164,7 @@ class ExpectingObjectWriter : public ObjectWriter { return this; } - ObjectWriter* RenderInt64(StringPiece name, int64_t value) override { + ObjectWriter* RenderInt64(absl::string_view name, int64_t value) override { (name.empty() ? EXPECT_CALL(*mock_, RenderInt64(IsEmpty(), TypedEq(value))) : EXPECT_CALL(*mock_, RenderInt64(Eq(std::string(name)), @@ -172,7 +174,7 @@ class ExpectingObjectWriter : public ObjectWriter { return this; } - ObjectWriter* RenderUint64(StringPiece name, uint64_t value) override { + ObjectWriter* RenderUint64(absl::string_view name, uint64_t value) override { (name.empty() ? EXPECT_CALL(*mock_, RenderUint64(IsEmpty(), TypedEq(value))) : EXPECT_CALL(*mock_, RenderUint64(Eq(std::string(name)), @@ -182,7 +184,7 @@ class ExpectingObjectWriter : public ObjectWriter { return this; } - ObjectWriter* RenderDouble(StringPiece name, double value) override { + ObjectWriter* RenderDouble(absl::string_view name, double value) override { (name.empty() ? EXPECT_CALL(*mock_, RenderDouble(IsEmpty(), NanSensitiveDoubleEq(value))) @@ -193,7 +195,7 @@ class ExpectingObjectWriter : public ObjectWriter { return this; } - ObjectWriter* RenderFloat(StringPiece name, float value) override { + ObjectWriter* RenderFloat(absl::string_view name, float value) override { (name.empty() ? EXPECT_CALL(*mock_, RenderFloat(IsEmpty(), NanSensitiveFloatEq(value))) @@ -204,31 +206,32 @@ class ExpectingObjectWriter : public ObjectWriter { return this; } - ObjectWriter* RenderString(StringPiece name, - StringPiece value) override { + ObjectWriter* RenderString(absl::string_view name, + absl::string_view value) override { (name.empty() ? EXPECT_CALL(*mock_, RenderString(IsEmpty(), - TypedEq( + TypedEq( std::string(value)))) : EXPECT_CALL(*mock_, RenderString(Eq(std::string(name)), - TypedEq( + TypedEq( std::string(value))))) .WillOnce(Return(mock_)) .RetiresOnSaturation(); return this; } - virtual ObjectWriter* RenderBytes(StringPiece name, StringPiece value) { - (name.empty() - ? EXPECT_CALL(*mock_, RenderBytes(IsEmpty(), TypedEq( - value.ToString()))) - : EXPECT_CALL(*mock_, - RenderBytes(Eq(std::string(name)), - TypedEq(value.ToString())))) + virtual ObjectWriter* RenderBytes(absl::string_view name, + absl::string_view value) { + (name.empty() ? EXPECT_CALL(*mock_, RenderBytes(IsEmpty(), + TypedEq( + std::string(value)))) + : EXPECT_CALL(*mock_, RenderBytes(Eq(std::string(name)), + TypedEq( + std::string(value))))) .WillOnce(Return(mock_)) .RetiresOnSaturation(); return this; } - ObjectWriter* RenderNull(StringPiece name) override { + ObjectWriter* RenderNull(absl::string_view name) override { (name.empty() ? EXPECT_CALL(*mock_, RenderNull(IsEmpty())) : EXPECT_CALL(*mock_, RenderNull(Eq(std::string(name)))) .WillOnce(Return(mock_)) @@ -238,8 +241,6 @@ class ExpectingObjectWriter : public ObjectWriter { private: MockObjectWriter* mock_; - - GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ExpectingObjectWriter); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/field_mask_utility.cc b/src/google/protobuf/util/internal/field_mask_utility.cc index 521bf48b7e4a..8800a18935df 100644 --- a/src/google/protobuf/util/internal/field_mask_utility.cc +++ b/src/google/protobuf/util/internal/field_mask_utility.cc @@ -30,8 +30,9 @@ #include -#include +#include "absl/status/status.h" #include +#include "absl/strings/str_cat.h" #include #include @@ -46,8 +47,8 @@ namespace converter { namespace { // Appends a FieldMask path segment to a prefix. -std::string AppendPathSegmentToPrefix(StringPiece prefix, - StringPiece segment) { +std::string AppendPathSegmentToPrefix(absl::string_view prefix, + absl::string_view segment) { if (prefix.empty()) { return std::string(segment); } @@ -56,14 +57,14 @@ std::string AppendPathSegmentToPrefix(StringPiece prefix, } // If the segment is a map key, appends it to the prefix without the ".". if (HasPrefixString(segment, "[\"")) { - return StrCat(prefix, segment); + return absl::StrCat(prefix, segment); } - return StrCat(prefix, ".", segment); + return absl::StrCat(prefix, ".", segment); } } // namespace -std::string ConvertFieldMaskPath(const StringPiece path, +std::string ConvertFieldMaskPath(const absl::string_view path, ConverterCallback converter) { std::string result; result.reserve(path.size() << 1); @@ -107,7 +108,7 @@ std::string ConvertFieldMaskPath(const StringPiece path, return result; } -util::Status DecodeCompactFieldMaskPaths(StringPiece paths, +absl::Status DecodeCompactFieldMaskPaths(absl::string_view paths, PathSinkCallback path_sink) { std::stack prefix; int length = paths.length(); @@ -134,7 +135,7 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, } // Un-escaped '"' must be followed with a ']'. if (i >= length - 1 || paths[i + 1] != ']') { - return util::InvalidArgumentError(StrCat( + return absl::InvalidArgumentError(absl::StrCat( "Invalid FieldMask '", paths, "'. Map keys should be represented as [\"some_key\"].")); } @@ -145,7 +146,7 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, // Checks whether the key ends at the end of a path segment. if (i < length - 1 && paths[i + 1] != '.' && paths[i + 1] != ',' && paths[i + 1] != ')' && paths[i + 1] != '(') { - return util::InvalidArgumentError(StrCat( + return absl::InvalidArgumentError(absl::StrCat( "Invalid FieldMask '", paths, "'. Map keys should be at the end of a path segment.")); } @@ -156,7 +157,7 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, // We are not in a map key, look for the start of one. if (paths[i] == '[') { if (i >= length - 1 || paths[i + 1] != '\"') { - return util::InvalidArgumentError(StrCat( + return absl::InvalidArgumentError(absl::StrCat( "Invalid FieldMask '", paths, "'. Map keys should be represented as [\"some_key\"].")); } @@ -173,7 +174,7 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, } // Gets the current segment - sub-string between previous position (after // '(', ')', ',', or the beginning of the input) and the current position. - StringPiece segment = + absl::string_view segment = paths.substr(previous_position, i - previous_position); std::string current_prefix = prefix.empty() ? "" : prefix.top(); @@ -191,8 +192,8 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, // Removes the last prefix after seeing a ')'. if (i < length && paths[i] == ')') { if (prefix.empty()) { - return util::InvalidArgumentError( - StrCat("Invalid FieldMask '", paths, + return absl::InvalidArgumentError( + absl::StrCat("Invalid FieldMask '", paths, "'. Cannot find matching '(' for all ')'.")); } prefix.pop(); @@ -200,16 +201,16 @@ util::Status DecodeCompactFieldMaskPaths(StringPiece paths, previous_position = i + 1; } if (in_map_key) { - return util::InvalidArgumentError( - StrCat("Invalid FieldMask '", paths, + return absl::InvalidArgumentError( + absl::StrCat("Invalid FieldMask '", paths, "'. Cannot find matching ']' for all '['.")); } if (!prefix.empty()) { - return util::InvalidArgumentError( - StrCat("Invalid FieldMask '", paths, + return absl::InvalidArgumentError( + absl::StrCat("Invalid FieldMask '", paths, "'. Cannot find matching ')' for all '('.")); } - return util::Status(); + return absl::Status(); } } // namespace converter diff --git a/src/google/protobuf/util/internal/field_mask_utility.h b/src/google/protobuf/util/internal/field_mask_utility.h index 188233378758..d18b8d398181 100644 --- a/src/google/protobuf/util/internal/field_mask_utility.h +++ b/src/google/protobuf/util/internal/field_mask_utility.h @@ -38,23 +38,23 @@ #include #include -#include -#include -#include +#include "absl/status/status.h" +#include "absl/strings/string_view.h" + namespace google { namespace protobuf { namespace util { namespace converter { -typedef std::function ConverterCallback; -typedef std::function PathSinkCallback; +typedef std::function ConverterCallback; +typedef std::function PathSinkCallback; // Applies a 'converter' to each segment of a FieldMask path and returns the // result. Quoted strings in the 'path' are copied to the output as-is without // converting their content. Escaping is supported within quoted strings. // For example, "ab\"_c" will be returned as "ab\"_c" without any changes. -std::string ConvertFieldMaskPath(const StringPiece path, +std::string ConvertFieldMaskPath(const absl::string_view path, ConverterCallback converter); // Decodes a compact list of FieldMasks. For example, "a.b,a.c.d,a.c.e" will be @@ -63,7 +63,7 @@ std::string ConvertFieldMaskPath(const StringPiece path, // resulting path. // Note that we also support Apiary style FieldMask form. The above example in // the Apiary style will look like "a.b,a.c(d,e)". -util::Status DecodeCompactFieldMaskPaths(StringPiece paths, +absl::Status DecodeCompactFieldMaskPaths(absl::string_view paths, PathSinkCallback path_sink); } // namespace converter diff --git a/src/google/protobuf/util/internal/json_escaping.cc b/src/google/protobuf/util/internal/json_escaping.cc index e4fa8cf78864..ac6a0447b53e 100644 --- a/src/google/protobuf/util/internal/json_escaping.cc +++ b/src/google/protobuf/util/internal/json_escaping.cc @@ -127,7 +127,7 @@ inline uint16_t ToHighSurrogate(uint32_t cp) { // Returns false if we encounter an invalid UTF-8 string. Returns true // otherwise, including the case when we reach the end of the input (str) // before a complete unicode code point is read. -bool ReadCodePoint(StringPiece str, int index, uint32_t* cp, +bool ReadCodePoint(absl::string_view str, int index, uint32_t* cp, int* num_left, int* num_read) { if (*num_left == 0) { // Last read was complete. Start reading a new unicode code point. @@ -190,9 +190,9 @@ bool ReadCodePoint(StringPiece str, int index, uint32_t* cp, } // Stores the 16-bit unicode code point as its hexadecimal digits in buffer -// and returns a StringPiece that points to this buffer. The input buffer needs -// to be at least 6 bytes long. -StringPiece ToHex(uint16_t cp, char* buffer) { +// and returns an absl::string_view that points to this buffer. The input +// buffer needs to be at least 6 bytes long. +absl::string_view ToHex(uint16_t cp, char* buffer) { buffer[5] = kHex[cp & 0x0f]; cp >>= 4; buffer[4] = kHex[cp & 0x0f]; @@ -200,13 +200,13 @@ StringPiece ToHex(uint16_t cp, char* buffer) { buffer[3] = kHex[cp & 0x0f]; cp >>= 4; buffer[2] = kHex[cp & 0x0f]; - return StringPiece(buffer, 6); + return absl::string_view(buffer, 6); } // Stores the 32-bit unicode code point as its hexadecimal digits in buffer -// and returns a StringPiece that points to this buffer. The input buffer needs -// to be at least 12 bytes long. -StringPiece ToSurrogateHex(uint32_t cp, char* buffer) { +// and returns an absl::string_view that points to this buffer. The input +// buffer needs to be at least 12 bytes long. +absl::string_view ToSurrogateHex(uint32_t cp, char* buffer) { uint16_t low = ToLowSurrogate(cp); uint16_t high = ToHighSurrogate(cp); @@ -226,17 +226,17 @@ StringPiece ToSurrogateHex(uint32_t cp, char* buffer) { high >>= 4; buffer[2] = kHex[high & 0x0f]; - return StringPiece(buffer, 12); + return absl::string_view(buffer, 12); } -// If the given unicode code point needs escaping, then returns the -// escaped form. The returned StringPiece either points to statically -// pre-allocated char[] or to the given buffer. The input buffer needs -// to be at least 12 bytes long. +// If the given unicode code point needs escaping, then returns the escaped +// form. The returned absl::string_view either points to statically +// pre-allocated char[] or to the given buffer. The input buffer needs to be at +// least 12 bytes long. // -// If the given unicode code point does not need escaping, an empty -// StringPiece is returned. -StringPiece EscapeCodePoint(uint32_t cp, char* buffer) { +// If the given unicode code point does not need escaping, an empty string is +// returned. +absl::string_view EscapeCodePoint(uint32_t cp, char* buffer) { if (cp < 0xa0) return kCommonEscapes[cp]; switch (cp) { // These are not required by json spec @@ -268,33 +268,33 @@ StringPiece EscapeCodePoint(uint32_t cp, char* buffer) { return ToSurrogateHex(cp, buffer); } } - return StringPiece(); + return absl::string_view(); } // Tries to escape the given code point first. If the given code point // does not need to be escaped, but force_output is true, then render // the given multi-byte code point in UTF8 in the buffer and returns it. -StringPiece EscapeCodePoint(uint32_t cp, char* buffer, +absl::string_view EscapeCodePoint(uint32_t cp, char* buffer, bool force_output) { - StringPiece sp = EscapeCodePoint(cp, buffer); + absl::string_view sp = EscapeCodePoint(cp, buffer); if (force_output && sp.empty()) { buffer[5] = (cp & 0x3f) | 0x80; cp >>= 6; if (cp <= 0x1f) { buffer[4] = cp | 0xc0; - sp = StringPiece(buffer + 4, 2); + sp = absl::string_view(buffer + 4, 2); return sp; } buffer[4] = (cp & 0x3f) | 0x80; cp >>= 6; if (cp <= 0x0f) { buffer[3] = cp | 0xe0; - sp = StringPiece(buffer + 3, 3); + sp = absl::string_view(buffer + 3, 3); return sp; } buffer[3] = (cp & 0x3f) | 0x80; buffer[2] = ((cp >> 6) & 0x07) | 0xf0; - sp = StringPiece(buffer + 2, 4); + sp = absl::string_view(buffer + 2, 4); } return sp; } @@ -307,8 +307,8 @@ void JsonEscaping::Escape(strings::ByteSource* input, uint32_t cp = 0; // Current unicode code point. int num_left = 0; // Num of chars to read to complete the code point. while (input->Available() > 0) { - StringPiece str = input->Peek(); - StringPiece escaped; + absl::string_view str = input->Peek(); + absl::string_view escaped; int i = 0; int num_read; bool ok; @@ -317,7 +317,7 @@ void JsonEscaping::Escape(strings::ByteSource* input, // i) a code point that needs to be escaped; or // ii) a split code point is completely read; or // iii) a character that is not a valid utf8; or - // iv) end of the StringPiece str is reached. + // iv) end of the string is reached. do { ok = ReadCodePoint(str, i, &cp, &num_left, &num_read); if (num_left > 0 || !ok) break; // case iii or iv @@ -344,7 +344,7 @@ void JsonEscaping::Escape(strings::ByteSource* input, } } -void JsonEscaping::Escape(StringPiece input, strings::ByteSink* output) { +void JsonEscaping::Escape(absl::string_view input, strings::ByteSink* output) { const size_t len = input.length(); const char* p = input.data(); diff --git a/src/google/protobuf/util/internal/json_escaping.h b/src/google/protobuf/util/internal/json_escaping.h index 7d54f22a38f7..cbf5644c604b 100644 --- a/src/google/protobuf/util/internal/json_escaping.h +++ b/src/google/protobuf/util/internal/json_escaping.h @@ -84,7 +84,7 @@ class JsonEscaping { // Escape the given ByteSource to the given ByteSink. // This is optimized for the case where the string is all printable 7-bit // ASCII and does not contain a few other characters (such as quotes). - static void Escape(StringPiece input, strings::ByteSink* output); + static void Escape(absl::string_view input, strings::ByteSink* output); private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(JsonEscaping); diff --git a/src/google/protobuf/util/internal/json_objectwriter.cc b/src/google/protobuf/util/internal/json_objectwriter.cc index 1a86f0053302..376cc4deb921 100644 --- a/src/google/protobuf/util/internal/json_objectwriter.cc +++ b/src/google/protobuf/util/internal/json_objectwriter.cc @@ -34,26 +34,28 @@ #include #include -#include #include #include -#include #include +#include "absl/base/casts.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include +#include + namespace google { namespace protobuf { namespace util { namespace converter { - JsonObjectWriter::~JsonObjectWriter() { if (element_ && !element_->is_root()) { GOOGLE_LOG(WARNING) << "JsonObjectWriter was not fully closed."; } } -JsonObjectWriter* JsonObjectWriter::StartObject(StringPiece name) { +JsonObjectWriter* JsonObjectWriter::StartObject(absl::string_view name) { WritePrefix(name); WriteChar('{'); PushObject(); @@ -67,7 +69,7 @@ JsonObjectWriter* JsonObjectWriter::EndObject() { return this; } -JsonObjectWriter* JsonObjectWriter::StartList(StringPiece name) { +JsonObjectWriter* JsonObjectWriter::StartList(absl::string_view name) { WritePrefix(name); WriteChar('['); PushArray(); @@ -81,40 +83,40 @@ JsonObjectWriter* JsonObjectWriter::EndList() { return this; } -JsonObjectWriter* JsonObjectWriter::RenderBool(StringPiece name, +JsonObjectWriter* JsonObjectWriter::RenderBool(absl::string_view name, bool value) { return RenderSimple(name, value ? "true" : "false"); } -JsonObjectWriter* JsonObjectWriter::RenderInt32(StringPiece name, +JsonObjectWriter* JsonObjectWriter::RenderInt32(absl::string_view name, int32_t value) { - return RenderSimple(name, StrCat(value)); + return RenderSimple(name, absl::StrCat(value)); } -JsonObjectWriter* JsonObjectWriter::RenderUint32(StringPiece name, +JsonObjectWriter* JsonObjectWriter::RenderUint32(absl::string_view name, uint32_t value) { - return RenderSimple(name, StrCat(value)); + return RenderSimple(name, absl::StrCat(value)); } -JsonObjectWriter* JsonObjectWriter::RenderInt64(StringPiece name, +JsonObjectWriter* JsonObjectWriter::RenderInt64(absl::string_view name, int64_t value) { WritePrefix(name); WriteChar('"'); - WriteRawString(StrCat(value)); + WriteRawString(absl::StrCat(value)); WriteChar('"'); return this; } -JsonObjectWriter* JsonObjectWriter::RenderUint64(StringPiece name, +JsonObjectWriter* JsonObjectWriter::RenderUint64(absl::string_view name, uint64_t value) { WritePrefix(name); WriteChar('"'); - WriteRawString(StrCat(value)); + WriteRawString(absl::StrCat(value)); WriteChar('"'); return this; } -JsonObjectWriter* JsonObjectWriter::RenderDouble(StringPiece name, +JsonObjectWriter* JsonObjectWriter::RenderDouble(absl::string_view name, double value) { if (std::isfinite(value)) { return RenderSimple(name, SimpleDtoa(value)); @@ -124,7 +126,7 @@ JsonObjectWriter* JsonObjectWriter::RenderDouble(StringPiece name, return RenderString(name, DoubleAsString(value)); } -JsonObjectWriter* JsonObjectWriter::RenderFloat(StringPiece name, +JsonObjectWriter* JsonObjectWriter::RenderFloat(absl::string_view name, float value) { if (std::isfinite(value)) { return RenderSimple(name, SimpleFtoa(value)); @@ -134,8 +136,8 @@ JsonObjectWriter* JsonObjectWriter::RenderFloat(StringPiece name, return RenderString(name, FloatAsString(value)); } -JsonObjectWriter* JsonObjectWriter::RenderString(StringPiece name, - StringPiece value) { +JsonObjectWriter* JsonObjectWriter::RenderString(absl::string_view name, + absl::string_view value) { WritePrefix(name); WriteChar('"'); JsonEscaping::Escape(value, &sink_); @@ -143,15 +145,15 @@ JsonObjectWriter* JsonObjectWriter::RenderString(StringPiece name, return this; } -JsonObjectWriter* JsonObjectWriter::RenderBytes(StringPiece name, - StringPiece value) { +JsonObjectWriter* JsonObjectWriter::RenderBytes(absl::string_view name, + absl::string_view value) { WritePrefix(name); std::string base64; if (use_websafe_base64_for_bytes_) - WebSafeBase64EscapeWithPadding(std::string(value), &base64); + strings::WebSafeBase64EscapeWithPadding(value, &base64); else - Base64Escape(value, &base64); + absl::Base64Escape(value, &base64); WriteChar('"'); // TODO(wpoon): Consider a ByteSink solution that writes the base64 bytes @@ -162,15 +164,15 @@ JsonObjectWriter* JsonObjectWriter::RenderBytes(StringPiece name, return this; } -JsonObjectWriter* JsonObjectWriter::RenderNull(StringPiece name) { +JsonObjectWriter* JsonObjectWriter::RenderNull(absl::string_view name) { return RenderSimple(name, "null"); } -JsonObjectWriter* JsonObjectWriter::RenderNullAsEmpty(StringPiece name) { +JsonObjectWriter* JsonObjectWriter::RenderNullAsEmpty(absl::string_view name) { return RenderSimple(name, ""); } -void JsonObjectWriter::WritePrefix(StringPiece name) { +void JsonObjectWriter::WritePrefix(absl::string_view name) { bool not_first = !element()->is_first(); if (not_first) WriteChar(','); if (not_first || !element()->is_root()) NewLine(); diff --git a/src/google/protobuf/util/internal/json_objectwriter.h b/src/google/protobuf/util/internal/json_objectwriter.h index cb7dff6e9fe7..23d36e467374 100644 --- a/src/google/protobuf/util/internal/json_objectwriter.h +++ b/src/google/protobuf/util/internal/json_objectwriter.h @@ -90,7 +90,7 @@ namespace converter { // JsonObjectWriter is thread-unsafe. class PROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { public: - JsonObjectWriter(StringPiece indent_string, io::CodedOutputStream* out) + JsonObjectWriter(absl::string_view indent_string, io::CodedOutputStream* out) : element_(new Element(/*parent=*/nullptr, /*is_json_object=*/false)), stream_(out), sink_(out), @@ -111,27 +111,30 @@ class PROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { } } } + JsonObjectWriter(const JsonObjectWriter&) = delete; + JsonObjectWriter& operator=(const JsonObjectWriter&) = delete; ~JsonObjectWriter() override; // ObjectWriter methods. - JsonObjectWriter* StartObject(StringPiece name) override; + JsonObjectWriter* StartObject(absl::string_view name) override; JsonObjectWriter* EndObject() override; - JsonObjectWriter* StartList(StringPiece name) override; + JsonObjectWriter* StartList(absl::string_view name) override; JsonObjectWriter* EndList() override; - JsonObjectWriter* RenderBool(StringPiece name, bool value) override; - JsonObjectWriter* RenderInt32(StringPiece name, int32_t value) override; - JsonObjectWriter* RenderUint32(StringPiece name, + JsonObjectWriter* RenderBool(absl::string_view name, bool value) override; + JsonObjectWriter* RenderInt32(absl::string_view name, int32_t value) override; + JsonObjectWriter* RenderUint32(absl::string_view name, uint32_t value) override; - JsonObjectWriter* RenderInt64(StringPiece name, int64_t value) override; - JsonObjectWriter* RenderUint64(StringPiece name, + JsonObjectWriter* RenderInt64(absl::string_view name, int64_t value) override; + JsonObjectWriter* RenderUint64(absl::string_view name, uint64_t value) override; - JsonObjectWriter* RenderDouble(StringPiece name, double value) override; - JsonObjectWriter* RenderFloat(StringPiece name, float value) override; - JsonObjectWriter* RenderString(StringPiece name, - StringPiece value) override; - JsonObjectWriter* RenderBytes(StringPiece name, StringPiece value) override; - JsonObjectWriter* RenderNull(StringPiece name) override; - virtual JsonObjectWriter* RenderNullAsEmpty(StringPiece name); + JsonObjectWriter* RenderDouble(absl::string_view name, double value) override; + JsonObjectWriter* RenderFloat(absl::string_view name, float value) override; + JsonObjectWriter* RenderString(absl::string_view name, + absl::string_view value) override; + JsonObjectWriter* RenderBytes(absl::string_view name, + absl::string_view value) override; + JsonObjectWriter* RenderNull(absl::string_view name) override; + virtual JsonObjectWriter* RenderNullAsEmpty(absl::string_view name); void set_use_websafe_base64_for_bytes(bool value) { use_websafe_base64_for_bytes_ = value; @@ -144,6 +147,8 @@ class PROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { : BaseElement(parent), is_first_(true), is_json_object_(is_json_object) {} + Element(const Element&) = delete; + Element& operator=(const Element&) = delete; // Called before each field of the Element is to be processed. // Returns true if this is the first call (processing the first field). @@ -162,8 +167,6 @@ class PROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { private: bool is_first_; bool is_json_object_; - - GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(Element); }; Element* element() override { return element_.get(); } @@ -172,6 +175,8 @@ class PROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { class PROTOBUF_EXPORT ByteSinkWrapper : public strings::ByteSink { public: explicit ByteSinkWrapper(io::CodedOutputStream* stream) : stream_(stream) {} + ByteSinkWrapper(const ByteSinkWrapper&) = delete; + ByteSinkWrapper& operator=(const ByteSinkWrapper&) = delete; ~ByteSinkWrapper() override {} // ByteSink methods. @@ -181,15 +186,13 @@ class PROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { private: io::CodedOutputStream* stream_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ByteSinkWrapper); }; // Renders a simple value as a string. By default all non-string Render // methods convert their argument to a string and call this method. This // method can then be used to render the simple value without escaping it. - JsonObjectWriter* RenderSimple(StringPiece name, - StringPiece value) { + JsonObjectWriter* RenderSimple(absl::string_view name, + absl::string_view value) { WritePrefix(name); WriteRawString(value); return this; @@ -242,13 +245,13 @@ class PROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { // Writes a prefix. This will write out any pretty printing and // commas that are required, followed by the name and a ':' if // the name is not null. - void WritePrefix(StringPiece name); + void WritePrefix(absl::string_view name); // Writes an individual character to the output. void WriteChar(const char c) { stream_->WriteRaw(&c, sizeof(c)); } // Writes a string to the output. - void WriteRawString(StringPiece s) { + void WriteRawString(absl::string_view s) { stream_->WriteRaw(s.data(), s.length()); } @@ -264,8 +267,6 @@ class PROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { // Whether to use regular or websafe base64 encoding for byte fields. Defaults // to regular base64 encoding. bool use_websafe_base64_for_bytes_; - - GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(JsonObjectWriter); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/json_stream_parser.cc b/src/google/protobuf/util/internal/json_stream_parser.cc index 5c34dbccafb9..e7990bbb673e 100644 --- a/src/google/protobuf/util/internal/json_stream_parser.cc +++ b/src/google/protobuf/util/internal/json_stream_parser.cc @@ -40,7 +40,11 @@ #include #include #include -#include +#include "absl/status/status.h" +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" #include #include @@ -57,9 +61,9 @@ static const int kUnicodeEscapedLength = 6; static const int kDefaultMaxRecursionDepth = 100; // These cannot be constexpr for portability with VS2015. -static const StringPiece kKeywordTrue = "true"; -static const StringPiece kKeywordFalse = "false"; -static const StringPiece kKeywordNull = "null"; +static const absl::string_view kKeywordTrue = "true"; +static const absl::string_view kKeywordFalse = "false"; +static const absl::string_view kKeywordNull = "null"; inline bool IsLetter(char c) { return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || (c == '_') || @@ -72,31 +76,31 @@ inline bool IsAlphanumeric(char c) { // Indicates a character may not be part of an unquoted key. inline bool IsKeySeparator(char c) { - return (ascii_isspace(c) || c == '"' || c == '\'' || c == '{' || + return (absl::ascii_isspace(c) || c == '"' || c == '\'' || c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ','); } -inline void ReplaceInvalidCodePoints(StringPiece str, +inline void ReplaceInvalidCodePoints(absl::string_view str, const std::string& replacement, std::string* dst) { while (!str.empty()) { int n_valid_bytes = internal::UTF8SpnStructurallyValid(str); - StringPiece valid_part = str.substr(0, n_valid_bytes); - StrAppend(dst, valid_part); + absl::string_view valid_part = str.substr(0, n_valid_bytes); + absl::StrAppend(dst, valid_part); if (n_valid_bytes == str.size()) { break; } // Append replacement value. - StrAppend(dst, replacement); + absl::StrAppend(dst, replacement); // Move past valid bytes + one invalid byte. str.remove_prefix(n_valid_bytes + 1); } } -static bool ConsumeKey(StringPiece* input, StringPiece* key) { +static bool ConsumeKey(absl::string_view* input, absl::string_view* key) { if (input->empty() || !IsLetter((*input)[0])) return false; int len = 1; for (; len < input->size(); ++len) { @@ -104,14 +108,14 @@ static bool ConsumeKey(StringPiece* input, StringPiece* key) { break; } } - *key = StringPiece(input->data(), len); - *input = StringPiece(input->data() + len, input->size() - len); + *key = absl::string_view(input->data(), len); + *input = absl::string_view(input->data() + len, input->size() - len); return true; } // Same as 'ConsumeKey', but allows a widened set of key characters. -static bool ConsumeKeyPermissive(StringPiece* input, - StringPiece* key) { +static bool ConsumeKeyPermissive(absl::string_view* input, + absl::string_view* key) { if (input->empty() || !IsLetter((*input)[0])) return false; int len = 1; for (; len < input->size(); ++len) { @@ -119,12 +123,12 @@ static bool ConsumeKeyPermissive(StringPiece* input, break; } } - *key = StringPiece(input->data(), len); - *input = StringPiece(input->data() + len, input->size() - len); + *key = absl::string_view(input->data(), len); + *input = absl::string_view(input->data() + len, input->size() - len); return true; } -static bool MatchKey(StringPiece input) { +static bool MatchKey(absl::string_view input) { return !input.empty() && IsLetter(input[0]); } @@ -157,40 +161,40 @@ JsonStreamParser::JsonStreamParser(ObjectWriter* ow) JsonStreamParser::~JsonStreamParser() {} -util::Status JsonStreamParser::Parse(StringPiece json) { - StringPiece chunk = json; +absl::Status JsonStreamParser::Parse(absl::string_view json) { + absl::string_view chunk = json; // If we have leftovers from a previous chunk, append the new chunk to it - // and create a new StringPiece pointing at the string's data. This could - // be large but we rely on the chunks to be small, assuming they are + // and create a new absl::string_view pointing at the string's data. This + // could be large but we rely on the chunks to be small, assuming they are // fragments of a Cord. if (!leftover_.empty()) { // Don't point chunk to leftover_ because leftover_ will be updated in // ParseChunk(chunk). chunk_storage_.swap(leftover_); - StrAppend(&chunk_storage_, json); - chunk = StringPiece(chunk_storage_); + absl::StrAppend(&chunk_storage_, json); + chunk = absl::string_view(chunk_storage_); } // Find the structurally valid UTF8 prefix and parse only that. int n = internal::UTF8SpnStructurallyValid(chunk); if (n > 0) { - util::Status status = ParseChunk(chunk.substr(0, n)); + absl::Status status = ParseChunk(chunk.substr(0, n)); // Any leftover characters are stashed in leftover_ for later parsing when // there is more data available. - StrAppend(&leftover_, chunk.substr(n)); + absl::StrAppend(&leftover_, chunk.substr(n)); return status; } else { leftover_.assign(chunk.data(), chunk.size()); - return util::Status(); + return absl::Status(); } } -util::Status JsonStreamParser::FinishParse() { +absl::Status JsonStreamParser::FinishParse() { // If we do not expect anything and there is nothing left to parse we're all // done. if (stack_.empty() && leftover_.empty()) { - return util::Status(); + return absl::Status(); } // Lifetime needs to last until RunParser returns, so keep this variable @@ -215,7 +219,7 @@ util::Status JsonStreamParser::FinishParse() { // Parse the remainder in finishing mode, which reports errors for things like // unterminated strings or unknown tokens that would normally be retried. finishing_ = true; - util::Status result = RunParser(); + absl::Status result = RunParser(); if (result.ok()) { SkipWhitespace(); if (!p_.empty()) { @@ -227,14 +231,14 @@ util::Status JsonStreamParser::FinishParse() { return result; } -util::Status JsonStreamParser::ParseChunk(StringPiece chunk) { +absl::Status JsonStreamParser::ParseChunk(absl::string_view chunk) { // Do not do any work if the chunk is empty. - if (chunk.empty()) return util::Status(); + if (chunk.empty()) return absl::Status(); p_ = json_ = chunk; finishing_ = false; - util::Status result = RunParser(); + absl::Status result = RunParser(); if (!result.ok()) return result; SkipWhitespace(); @@ -253,7 +257,7 @@ util::Status JsonStreamParser::ParseChunk(StringPiece chunk) { // unparsed data left, we save it for later parse. leftover_ = std::string(p_); } - return util::Status(); + return absl::Status(); } bool JsonStreamParser::IsInputAllWhiteSpaces(TokenType type) { @@ -267,16 +271,16 @@ bool JsonStreamParser::IsInputAllWhiteSpaces(TokenType type) { return false; } -util::Status JsonStreamParser::RunParser() { +absl::Status JsonStreamParser::RunParser() { while (!stack_.empty()) { ParseType type = stack_.top(); TokenType t = (string_open_ == 0) ? GetNextTokenType() : BEGIN_STRING; stack_.pop(); - util::Status result; + absl::Status result; switch (type) { case VALUE: if (allow_no_root_element_ && IsInputAllWhiteSpaces(t)) { - return util::Status(); + return absl::Status(); } result = ParseValue(t); break; @@ -303,28 +307,28 @@ util::Status JsonStreamParser::RunParser() { default: result = - util::InternalError(StrCat("Unknown parse type: ", type)); + absl::InternalError(absl::StrCat("Unknown parse type: ", type)); break; } if (!result.ok()) { // If we were cancelled, save our state and try again later. - if (!finishing_ && util::IsCancelled(result)) { + if (!finishing_ && absl::IsCancelled(result)) { stack_.push(type); // If we have a key we still need to render, make sure to save off the // contents in our own storage. if (!key_.empty() && key_storage_.empty()) { - StrAppend(&key_storage_, key_); - key_ = StringPiece(key_storage_); + absl::StrAppend(&key_storage_, key_); + key_ = absl::string_view(key_storage_); } - result = util::Status(); + result = absl::Status(); } return result; } } - return util::Status(); + return absl::Status(); } -util::Status JsonStreamParser::ParseValue(TokenType type) { +absl::Status JsonStreamParser::ParseValue(TokenType type) { switch (type) { case BEGIN_OBJECT: return HandleBeginObject(); @@ -348,7 +352,7 @@ util::Status JsonStreamParser::ParseValue(TokenType type) { // don't know if the next char would be e, completing it, or something // else, making it invalid. if (!finishing_ && p_.length() < kKeywordFalse.length()) { - return util::CancelledError(""); + return absl::CancelledError(""); } if (allow_empty_null_ && IsEmptyNullAllowed(type)) { @@ -360,18 +364,18 @@ util::Status JsonStreamParser::ParseValue(TokenType type) { } } -util::Status JsonStreamParser::ParseString() { - util::Status result = ParseStringHelper(); +absl::Status JsonStreamParser::ParseString() { + absl::Status result = ParseStringHelper(); if (result.ok()) { ow_->RenderString(key_, parsed_); - key_ = StringPiece(); - parsed_ = StringPiece(); + key_ = absl::string_view(); + parsed_ = absl::string_view(); parsed_storage_.clear(); } return result; } -util::Status JsonStreamParser::ParseStringHelper() { +absl::Status JsonStreamParser::ParseStringHelper() { // If we haven't seen the start quote, grab it and remember it for later. if (string_open_ == 0) { string_open_ = *p_.data(); @@ -391,14 +395,14 @@ util::Status JsonStreamParser::ParseStringHelper() { // depending on if we expect more data later. if (p_.length() == 1) { if (!finishing_) { - return util::CancelledError(""); + return absl::CancelledError(""); } return ReportFailure("Closing quote expected in string.", ParseErrorType::EXPECTED_CLOSING_QUOTE); } // Parse a unicode escape if we found \u in the string. if (data[1] == 'u') { - util::Status result = ParseUnicodeEscape(); + absl::Status result = ParseUnicodeEscape(); if (!result.ok()) { return result; } @@ -438,18 +442,18 @@ util::Status JsonStreamParser::ParseStringHelper() { if (*data == string_open_) { // If we didn't copy anything, reuse the input buffer. if (parsed_storage_.empty()) { - parsed_ = StringPiece(last, data - last); + parsed_ = absl::string_view(last, data - last); } else { if (last < data) { parsed_storage_.append(last, data - last); } - parsed_ = StringPiece(parsed_storage_); + parsed_ = absl::string_view(parsed_storage_); } // Clear the quote char so next time we try to parse a string we'll // start fresh. string_open_ = 0; Advance(); - return util::Status(); + return absl::Status(); } // Normal character, just advance past it. Advance(); @@ -460,7 +464,7 @@ util::Status JsonStreamParser::ParseStringHelper() { } // If we didn't find the closing quote but we expect more data, cancel for now if (!finishing_) { - return util::CancelledError(""); + return absl::CancelledError(""); } // End of string reached without a closing quote, report an error. string_open_ = 0; @@ -475,10 +479,10 @@ util::Status JsonStreamParser::ParseStringHelper() { // There are some security exploits with UTF-8 that we should be careful of: // - http://www.unicode.org/reports/tr36/#UTF-8_Exploit // - http://sites/intl-eng/design-guide/core-application -util::Status JsonStreamParser::ParseUnicodeEscape() { +absl::Status JsonStreamParser::ParseUnicodeEscape() { if (p_.length() < kUnicodeEscapedLength) { if (!finishing_) { - return util::CancelledError(""); + return absl::CancelledError(""); } return ReportFailure("Illegal hex string.", ParseErrorType::ILLEGAL_HEX_STRING); @@ -497,7 +501,7 @@ util::Status JsonStreamParser::ParseUnicodeEscape() { code <= JsonEscaping::kMaxHighSurrogate) { if (p_.length() < 2 * kUnicodeEscapedLength) { if (!finishing_) { - return util::CancelledError(""); + return absl::CancelledError(""); } if (!coerce_to_utf8_) { return ReportFailure("Missing low surrogate.", @@ -539,27 +543,27 @@ util::Status JsonStreamParser::ParseUnicodeEscape() { // Advance past the [final] code unit escape. p_.remove_prefix(kUnicodeEscapedLength); parsed_storage_.append(buf, len); - return util::Status(); + return absl::Status(); } -util::Status JsonStreamParser::ParseNumber() { +absl::Status JsonStreamParser::ParseNumber() { NumberResult number; - util::Status result = ParseNumberHelper(&number); + absl::Status result = ParseNumberHelper(&number); if (result.ok()) { switch (number.type) { case NumberResult::DOUBLE: ow_->RenderDouble(key_, number.double_val); - key_ = StringPiece(); + key_ = absl::string_view(); break; case NumberResult::INT: ow_->RenderInt64(key_, number.int_val); - key_ = StringPiece(); + key_ = absl::string_view(); break; case NumberResult::UINT: ow_->RenderUint64(key_, number.uint_val); - key_ = StringPiece(); + key_ = absl::string_view(); break; default: @@ -570,7 +574,7 @@ util::Status JsonStreamParser::ParseNumber() { return result; } -util::Status JsonStreamParser::ParseDoubleHelper(const std::string& number, +absl::Status JsonStreamParser::ParseDoubleHelper(const std::string& number, NumberResult* result) { if (!safe_strtod(number, &result->double_val)) { return ReportFailure("Unable to parse number.", @@ -581,10 +585,10 @@ util::Status JsonStreamParser::ParseDoubleHelper(const std::string& number, ParseErrorType::NUMBER_EXCEEDS_RANGE_DOUBLE); } result->type = NumberResult::DOUBLE; - return util::Status(); + return absl::Status(); } -util::Status JsonStreamParser::ParseNumberHelper(NumberResult* result) { +absl::Status JsonStreamParser::ParseNumberHelper(NumberResult* result) { const char* data = p_.data(); int length = p_.length(); @@ -611,7 +615,7 @@ util::Status JsonStreamParser::ParseNumberHelper(NumberResult* result) { // If the entire input is a valid number, and we may have more content in the // future, we abort for now and resume when we know more. if (index == length && !finishing_) { - return util::CancelledError(""); + return absl::CancelledError(""); } // Create a string containing just the number, so we can use safe_strtoX @@ -619,7 +623,7 @@ util::Status JsonStreamParser::ParseNumberHelper(NumberResult* result) { // Floating point number, parse as a double. if (floating) { - util::Status status = ParseDoubleHelper(number, result); + absl::Status status = ParseDoubleHelper(number, result); if (status.ok()) { p_.remove_prefix(index); } @@ -637,10 +641,10 @@ util::Status JsonStreamParser::ParseNumberHelper(NumberResult* result) { if (safe_strtou64(number, &result->uint_val)) { result->type = NumberResult::UINT; p_.remove_prefix(index); - return util::Status(); + return absl::Status(); } else { // If the value is too large, parse it as double. - util::Status status = ParseDoubleHelper(number, result); + absl::Status status = ParseDoubleHelper(number, result); if (status.ok()) { p_.remove_prefix(index); } @@ -658,10 +662,10 @@ util::Status JsonStreamParser::ParseNumberHelper(NumberResult* result) { if (safe_strto64(number, &result->int_val)) { result->type = NumberResult::INT; p_.remove_prefix(index); - return util::Status(); + return absl::Status(); } else { // If the value is too large, parse it as double. - util::Status status = ParseDoubleHelper(number, result); + absl::Status status = ParseDoubleHelper(number, result); if (status.ok()) { p_.remove_prefix(index); } @@ -669,7 +673,7 @@ util::Status JsonStreamParser::ParseNumberHelper(NumberResult* result) { } } -util::Status JsonStreamParser::HandleBeginObject() { +absl::Status JsonStreamParser::HandleBeginObject() { GOOGLE_DCHECK_EQ('{', *p_.data()); Advance(); ow_->StartObject(key_); @@ -677,12 +681,12 @@ util::Status JsonStreamParser::HandleBeginObject() { if (!status.ok()) { return status; } - key_ = StringPiece(); + key_ = absl::string_view(); stack_.push(ENTRY); - return util::Status(); + return absl::Status(); } -util::Status JsonStreamParser::ParseObjectMid(TokenType type) { +absl::Status JsonStreamParser::ParseObjectMid(TokenType type) { if (type == UNKNOWN) { return ReportUnknown("Expected , or } after key:value pair.", ParseErrorType::EXPECTED_COMMA_OR_BRACES); @@ -693,20 +697,20 @@ util::Status JsonStreamParser::ParseObjectMid(TokenType type) { Advance(); ow_->EndObject(); --recursion_depth_; - return util::Status(); + return absl::Status(); } // Found a comma, advance past it and get ready for an entry. if (type == VALUE_SEPARATOR) { Advance(); stack_.push(ENTRY); - return util::Status(); + return absl::Status(); } // Illegal token after key:value pair. return ReportFailure("Expected , or } after key:value pair.", ParseErrorType::EXPECTED_COMMA_OR_BRACES); } -util::Status JsonStreamParser::ParseEntry(TokenType type) { +absl::Status JsonStreamParser::ParseEntry(TokenType type) { if (type == UNKNOWN) { return ReportUnknown("Expected an object key or }.", ParseErrorType::EXPECTED_OBJECT_KEY_OR_BRACES); @@ -717,10 +721,10 @@ util::Status JsonStreamParser::ParseEntry(TokenType type) { ow_->EndObject(); Advance(); --recursion_depth_; - return util::Status(); + return absl::Status(); } - util::Status result; + absl::Status result; if (type == BEGIN_STRING) { // Key is a string (standard JSON), parse it and store the string. result = ParseStringHelper(); @@ -728,14 +732,15 @@ util::Status JsonStreamParser::ParseEntry(TokenType type) { key_storage_.clear(); if (!parsed_storage_.empty()) { parsed_storage_.swap(key_storage_); - key_ = StringPiece(key_storage_); + key_ = absl::string_view(key_storage_); } else { key_ = parsed_; } - parsed_ = StringPiece(); + parsed_ = absl::string_view(); } } else if (type == BEGIN_KEY) { - // Key is a bare key (back compat), create a StringPiece pointing to it. + // Key is a bare key (back compat), create a absl::string_view pointing to + // it. result = ParseKey(); } else if (type == BEGIN_NULL || type == BEGIN_TRUE || type == BEGIN_FALSE) { // Key may be a bare key that begins with a reserved word. @@ -758,7 +763,7 @@ util::Status JsonStreamParser::ParseEntry(TokenType type) { return result; } -util::Status JsonStreamParser::ParseEntryMid(TokenType type) { +absl::Status JsonStreamParser::ParseEntryMid(TokenType type) { if (type == UNKNOWN) { return ReportUnknown("Expected : between key:value pair.", ParseErrorType::EXPECTED_COLON); @@ -766,22 +771,22 @@ util::Status JsonStreamParser::ParseEntryMid(TokenType type) { if (type == ENTRY_SEPARATOR) { Advance(); stack_.push(VALUE); - return util::Status(); + return absl::Status(); } return ReportFailure("Expected : between key:value pair.", ParseErrorType::EXPECTED_COLON); } -util::Status JsonStreamParser::HandleBeginArray() { +absl::Status JsonStreamParser::HandleBeginArray() { GOOGLE_DCHECK_EQ('[', *p_.data()); Advance(); ow_->StartList(key_); - key_ = StringPiece(); + key_ = absl::string_view(); stack_.push(ARRAY_VALUE); - return util::Status(); + return absl::Status(); } -util::Status JsonStreamParser::ParseArrayValue(TokenType type) { +absl::Status JsonStreamParser::ParseArrayValue(TokenType type) { if (type == UNKNOWN) { return ReportUnknown("Expected a value or ] within an array.", ParseErrorType::EXPECTED_VALUE_OR_BRACKET); @@ -790,15 +795,15 @@ util::Status JsonStreamParser::ParseArrayValue(TokenType type) { if (type == END_ARRAY) { ow_->EndList(); Advance(); - return util::Status(); + return absl::Status(); } // The ParseValue call may push something onto the stack so we need to make // sure an ARRAY_MID is after it, so we push it on now. Also, the parsing of // empty-null array value is relying on this ARRAY_MID token. stack_.push(ARRAY_MID); - util::Status result = ParseValue(type); - if (util::IsCancelled(result)) { + absl::Status result = ParseValue(type); + if (absl::IsCancelled(result)) { // If we were cancelled, pop back off the ARRAY_MID so we don't try to // push it on again when we try over. stack_.pop(); @@ -806,7 +811,7 @@ util::Status JsonStreamParser::ParseArrayValue(TokenType type) { return result; } -util::Status JsonStreamParser::ParseArrayMid(TokenType type) { +absl::Status JsonStreamParser::ParseArrayMid(TokenType type) { if (type == UNKNOWN) { return ReportUnknown("Expected , or ] after array value.", ParseErrorType::EXPECTED_COMMA_OR_BRACKET); @@ -815,45 +820,45 @@ util::Status JsonStreamParser::ParseArrayMid(TokenType type) { if (type == END_ARRAY) { ow_->EndList(); Advance(); - return util::Status(); + return absl::Status(); } // Found a comma, advance past it and expect an array value next. if (type == VALUE_SEPARATOR) { Advance(); stack_.push(ARRAY_VALUE); - return util::Status(); + return absl::Status(); } // Illegal token after array value. return ReportFailure("Expected , or ] after array value.", ParseErrorType::EXPECTED_COMMA_OR_BRACKET); } -util::Status JsonStreamParser::ParseTrue() { +absl::Status JsonStreamParser::ParseTrue() { ow_->RenderBool(key_, true); - key_ = StringPiece(); + key_ = absl::string_view(); p_.remove_prefix(kKeywordTrue.length()); - return util::Status(); + return absl::Status(); } -util::Status JsonStreamParser::ParseFalse() { +absl::Status JsonStreamParser::ParseFalse() { ow_->RenderBool(key_, false); - key_ = StringPiece(); + key_ = absl::string_view(); p_.remove_prefix(kKeywordFalse.length()); - return util::Status(); + return absl::Status(); } -util::Status JsonStreamParser::ParseNull() { +absl::Status JsonStreamParser::ParseNull() { ow_->RenderNull(key_); - key_ = StringPiece(); + key_ = absl::string_view(); p_.remove_prefix(kKeywordNull.length()); - return util::Status(); + return absl::Status(); } -util::Status JsonStreamParser::ParseEmptyNull() { +absl::Status JsonStreamParser::ParseEmptyNull() { ow_->RenderNull(key_); - key_ = StringPiece(); - return util::Status(); + key_ = absl::string_view(); + return absl::Status(); } bool JsonStreamParser::IsEmptyNullAllowed(TokenType type) { @@ -862,7 +867,7 @@ bool JsonStreamParser::IsEmptyNullAllowed(TokenType type) { stack_.top() == OBJ_MID; } -util::Status JsonStreamParser::ReportFailure(StringPiece message, +absl::Status JsonStreamParser::ReportFailure(absl::string_view message, ParseErrorType parse_code) { (void)parse_code; // Parameter is used in Google-internal code. static const int kContextLength = 20; @@ -871,54 +876,54 @@ util::Status JsonStreamParser::ReportFailure(StringPiece message, const char* begin = std::max(p_start - kContextLength, json_start); const char* end = std::min(p_start + kContextLength, json_start + json_.size()); - StringPiece segment(begin, end - begin); + absl::string_view segment(begin, end - begin); std::string location(p_start - begin, ' '); location.push_back('^'); - auto status = util::InvalidArgumentError( - StrCat(message, "\n", segment, "\n", location)); + auto status = absl::InvalidArgumentError( + absl::StrCat(message, "\n", segment, "\n", location)); return status; } -util::Status JsonStreamParser::ReportUnknown(StringPiece message, +absl::Status JsonStreamParser::ReportUnknown(absl::string_view message, ParseErrorType parse_code) { // If we aren't finishing the parse, cancel parsing and try later. if (!finishing_) { - return util::CancelledError(""); + return absl::CancelledError(""); } if (p_.empty()) { - return ReportFailure(StrCat("Unexpected end of string. ", message), + return ReportFailure(absl::StrCat("Unexpected end of string. ", message), parse_code); } return ReportFailure(message, parse_code); } -util::Status JsonStreamParser::IncrementRecursionDepth( - StringPiece key) const { +absl::Status JsonStreamParser::IncrementRecursionDepth( + absl::string_view key) const { if (++recursion_depth_ > max_recursion_depth_) { - return util::InvalidArgumentError(StrCat( + return absl::InvalidArgumentError(absl::StrCat( "Message too deep. Max recursion depth reached for key '", key, "'")); } - return util::Status(); + return absl::Status(); } void JsonStreamParser::SkipWhitespace() { - while (!p_.empty() && ascii_isspace(*p_.data())) { + while (!p_.empty() && absl::ascii_isspace(*p_.data())) { Advance(); } - if (!p_.empty() && !ascii_isspace(*p_.data())) { + if (!p_.empty() && !absl::ascii_isspace(*p_.data())) { seen_non_whitespace_ = true; } } void JsonStreamParser::Advance() { // Advance by moving one UTF8 character while making sure we don't go beyond - // the length of StringPiece. + // the length of the string. p_.remove_prefix(std::min( p_.length(), UTF8FirstLetterNumBytes(p_.data(), p_.length()))); } -util::Status JsonStreamParser::ParseKey() { - StringPiece original = p_; +absl::Status JsonStreamParser::ParseKey() { + absl::string_view original = p_; if (allow_permissive_key_naming_) { if (!ConsumeKeyPermissive(&p_, &key_)) { @@ -936,11 +941,11 @@ util::Status JsonStreamParser::ParseKey() { // we can't know if the key was complete or not. if (!finishing_ && p_.empty()) { p_ = original; - return util::CancelledError(""); + return absl::CancelledError(""); } // Since we aren't using the key storage, clear it out. key_storage_.clear(); - return util::Status(); + return absl::Status(); } JsonStreamParser::TokenType JsonStreamParser::GetNextTokenType() { @@ -955,7 +960,7 @@ JsonStreamParser::TokenType JsonStreamParser::GetNextTokenType() { // TODO(sven): Split this method based on context since different contexts // support different tokens. Would slightly speed up processing? const char* data = p_.data(); - StringPiece data_view = StringPiece(data, size); + absl::string_view data_view = absl::string_view(data, size); if (*data == '\"' || *data == '\'') return BEGIN_STRING; if (*data == '-' || ('0' <= *data && *data <= '9')) { return BEGIN_NUMBER; diff --git a/src/google/protobuf/util/internal/json_stream_parser.h b/src/google/protobuf/util/internal/json_stream_parser.h index 09f17ad0851c..0e3d9ff3bb6c 100644 --- a/src/google/protobuf/util/internal/json_stream_parser.h +++ b/src/google/protobuf/util/internal/json_stream_parser.h @@ -36,9 +36,9 @@ #include #include -#include -#include -#include +#include "absl/status/status.h" +#include "absl/strings/string_view.h" + // Must be included last. #include @@ -75,20 +75,23 @@ class PROTOBUF_EXPORT JsonStreamParser { public: // Creates a JsonStreamParser that will write to the given ObjectWriter. explicit JsonStreamParser(ObjectWriter* ow); + JsonStreamParser() = delete; + JsonStreamParser(const JsonStreamParser&) = delete; + JsonStreamParser& operator=(const JsonStreamParser&) = delete; virtual ~JsonStreamParser(); - // Parses a UTF-8 encoded JSON string from a StringPiece. If the returned - // status is non-ok, the status might contain a payload ParseErrorType with - // type_url kParseErrorTypeUrl and a payload containing string snippet of the - // error with type_url kParseErrorSnippetUrl. - util::Status Parse(StringPiece json); + // Parses a UTF-8 encoded JSON string from an absl::string_view. If the + // returned status is non-ok, the status might contain a payload + // ParseErrorType with type_url kParseErrorTypeUrl and a payload containing + // string snippet of the error with type_url kParseErrorSnippetUrl. + absl::Status Parse(absl::string_view json); // Finish parsing the JSON string. If the returned status is non-ok, the // status might contain a payload ParseErrorType with type_url // kParseErrorTypeUrl and a payload containing string snippet of the error // with type_url kParseErrorSnippetUrl. - util::Status FinishParse(); + absl::Status FinishParse(); // Sets the max recursion depth of JSON message to be deserialized. JSON @@ -164,69 +167,69 @@ class PROTOBUF_EXPORT JsonStreamParser { }; // Parses a single chunk of JSON, returning an error if the JSON was invalid. - util::Status ParseChunk(StringPiece chunk); + absl::Status ParseChunk(absl::string_view chunk); // Runs the parser based on stack_ and p_, until the stack is empty or p_ runs // out of data. If we unexpectedly run out of p_ we push the latest back onto // the stack and return. - util::Status RunParser(); + absl::Status RunParser(); // Parses a value from p_ and writes it to ow_. // A value may be an object, array, true, false, null, string or number. - util::Status ParseValue(TokenType type); + absl::Status ParseValue(TokenType type); // Parses a string and writes it out to the ow_. - util::Status ParseString(); + absl::Status ParseString(); // Parses a string, storing the result in parsed_. - util::Status ParseStringHelper(); + absl::Status ParseStringHelper(); // This function parses unicode escape sequences in strings. It returns an // error when there's a parsing error, either the size is not the expected // size or a character is not a hex digit. When it returns str will contain // what has been successfully parsed so far. - util::Status ParseUnicodeEscape(); + absl::Status ParseUnicodeEscape(); // Expects p_ to point to a JSON number, writes the number to the writer using // the appropriate Render method based on the type of number. - util::Status ParseNumber(); + absl::Status ParseNumber(); // Parse a number into a NumberResult, reporting an error if no number could // be parsed. This method will try to parse into a uint64, int64, or double // based on whether the number was positive or negative or had a decimal // component. - util::Status ParseNumberHelper(NumberResult* result); + absl::Status ParseNumberHelper(NumberResult* result); // Parse a number as double into a NumberResult. - util::Status ParseDoubleHelper(const std::string& number, + absl::Status ParseDoubleHelper(const std::string& number, NumberResult* result); // Handles a { during parsing of a value. - util::Status HandleBeginObject(); + absl::Status HandleBeginObject(); // Parses from the ENTRY state. - util::Status ParseEntry(TokenType type); + absl::Status ParseEntry(TokenType type); // Parses from the ENTRY_MID state. - util::Status ParseEntryMid(TokenType type); + absl::Status ParseEntryMid(TokenType type); // Parses from the OBJ_MID state. - util::Status ParseObjectMid(TokenType type); + absl::Status ParseObjectMid(TokenType type); // Handles a [ during parsing of a value. - util::Status HandleBeginArray(); + absl::Status HandleBeginArray(); // Parses from the ARRAY_VALUE state. - util::Status ParseArrayValue(TokenType type); + absl::Status ParseArrayValue(TokenType type); // Parses from the ARRAY_MID state. - util::Status ParseArrayMid(TokenType type); + absl::Status ParseArrayMid(TokenType type); // Expects p_ to point to an unquoted literal - util::Status ParseTrue(); - util::Status ParseFalse(); - util::Status ParseNull(); - util::Status ParseEmptyNull(); + absl::Status ParseTrue(); + absl::Status ParseFalse(); + absl::Status ParseNull(); + absl::Status ParseEmptyNull(); // Whether an empty-null is allowed in the current state. bool IsEmptyNullAllowed(TokenType type); @@ -235,19 +238,19 @@ class PROTOBUF_EXPORT JsonStreamParser { bool IsInputAllWhiteSpaces(TokenType type); // Report a failure as a util::Status. - util::Status ReportFailure(StringPiece message, + absl::Status ReportFailure(absl::string_view message, ParseErrorType parse_code); // Report a failure due to an UNKNOWN token type. We check if we hit the // end of the stream and if we're finishing or not to detect what type of // status to return in this case. - util::Status ReportUnknown(StringPiece message, + absl::Status ReportUnknown(absl::string_view message, ParseErrorType parse_code); // Helper function to check recursion depth and increment it. It will return // OkStatus() if the current depth is allowed. Otherwise an error is returned. // key is used for error reporting. - util::Status IncrementRecursionDepth(StringPiece key) const; + absl::Status IncrementRecursionDepth(absl::string_view key) const; // Advance p_ past all whitespace or until the end of the string. void SkipWhitespace(); @@ -256,7 +259,7 @@ class PROTOBUF_EXPORT JsonStreamParser { void Advance(); // Expects p_ to point to the beginning of a key. - util::Status ParseKey(); + absl::Status ParseKey(); // Return the type of the next token at p_. TokenType GetNextTokenType(); @@ -274,13 +277,13 @@ class PROTOBUF_EXPORT JsonStreamParser { // The current chunk of JSON being parsed. Primarily used for providing // context during error reporting. - StringPiece json_; + absl::string_view json_; // A pointer within the current JSON being parsed, used to track location. - StringPiece p_; + absl::string_view p_; // Stores the last key read, as we separate parsing of keys and values. - StringPiece key_; + absl::string_view key_; // Storage for key_ if we need to keep ownership, for example between chunks // or if the key was unescaped from a JSON string. @@ -301,7 +304,7 @@ class PROTOBUF_EXPORT JsonStreamParser { bool allow_no_root_element_; // String we parsed during a call to ParseStringHelper(). - StringPiece parsed_; + absl::string_view parsed_; // Storage for the string we parsed. This may be empty if the string was able // to be parsed directly from the input. @@ -336,8 +339,6 @@ class PROTOBUF_EXPORT JsonStreamParser { // Maximum allowed recursion depth. int max_recursion_depth_; - - GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(JsonStreamParser); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/json_stream_parser_test.cc b/src/google/protobuf/util/internal/json_stream_parser_test.cc index 40157a27d429..06636b84583a 100644 --- a/src/google/protobuf/util/internal/json_stream_parser_test.cc +++ b/src/google/protobuf/util/internal/json_stream_parser_test.cc @@ -36,9 +36,10 @@ #include #include #include -#include +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "absl/time/time.h" #include -#include namespace google { @@ -87,7 +88,7 @@ class JsonStreamParserTest : public ::testing::Test { JsonStreamParserTest() : mock_(), ow_(&mock_) {} ~JsonStreamParserTest() override {} - util::Status RunTest(StringPiece json, int split, + absl::Status RunTest(absl::string_view json, int split, std::function setup) { JsonStreamParser parser(&mock_); setup(&parser); @@ -96,8 +97,8 @@ class JsonStreamParserTest : public ::testing::Test { if (split == json.length()) { GOOGLE_LOG(INFO) << "Testing split every char: " << json; for (int i = 0; i < json.length(); ++i) { - StringPiece single = json.substr(i, 1); - util::Status result = parser.Parse(single); + absl::string_view single = json.substr(i, 1); + absl::Status result = parser.Parse(single); if (!result.ok()) { return result; } @@ -106,10 +107,10 @@ class JsonStreamParserTest : public ::testing::Test { } // Normal case, split at the split point and parse two substrings. - StringPiece first = json.substr(0, split); - StringPiece rest = json.substr(split); + absl::string_view first = json.substr(0, split); + absl::string_view rest = json.substr(split); GOOGLE_LOG(INFO) << "Testing split: " << first << "><" << rest; - util::Status result = parser.Parse(first); + absl::Status result = parser.Parse(first); if (result.ok()) { result = parser.Parse(rest); if (result.ok()) { @@ -123,10 +124,10 @@ class JsonStreamParserTest : public ::testing::Test { } void DoTest( - StringPiece json, int split, + absl::string_view json, int split, std::function setup = [](JsonStreamParser* p) { }) { - util::Status result = RunTest(json, split, setup); + absl::Status result = RunTest(json, split, setup); if (!result.ok()) { GOOGLE_LOG(WARNING) << result; } @@ -134,23 +135,23 @@ class JsonStreamParserTest : public ::testing::Test { } void DoErrorTest( - StringPiece json, int split, StringPiece error_prefix, + absl::string_view json, int split, absl::string_view error_prefix, std::function setup = [](JsonStreamParser* p) { }) { - util::Status result = RunTest(json, split, setup); - EXPECT_TRUE(util::IsInvalidArgument(result)); - StringPiece error_message(result.message()); + absl::Status result = RunTest(json, split, setup); + EXPECT_TRUE(absl::IsInvalidArgument(result)); + absl::string_view error_message(result.message()); EXPECT_EQ(error_prefix, error_message.substr(0, error_prefix.size())); } void DoErrorTest( - StringPiece json, int split, StringPiece error_prefix, + absl::string_view json, int split, absl::string_view error_prefix, ParseErrorType expected_parse_error_type, std::function setup = [](JsonStreamParser* p) { }) { - util::Status result = RunTest(json, split, setup); - EXPECT_TRUE(util::IsInvalidArgument(result)); - StringPiece error_message(result.message()); + absl::Status result = RunTest(json, split, setup); + EXPECT_TRUE(absl::IsInvalidArgument(result)); + absl::string_view error_message(result.message()); EXPECT_EQ(error_prefix, error_message.substr(0, error_prefix.size())); } @@ -170,7 +171,7 @@ class JsonStreamParserTest : public ::testing::Test { // - true, false, null TEST_F(JsonStreamParserTest, SimpleTrue) { - StringPiece str = "true"; + absl::string_view str = "true"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderBool("", true); DoTest(str, i); @@ -178,7 +179,7 @@ TEST_F(JsonStreamParserTest, SimpleTrue) { } TEST_F(JsonStreamParserTest, SimpleFalse) { - StringPiece str = "false"; + absl::string_view str = "false"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderBool("", false); DoTest(str, i); @@ -186,7 +187,7 @@ TEST_F(JsonStreamParserTest, SimpleFalse) { } TEST_F(JsonStreamParserTest, SimpleNull) { - StringPiece str = "null"; + absl::string_view str = "null"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderNull(""); DoTest(str, i); @@ -195,7 +196,7 @@ TEST_F(JsonStreamParserTest, SimpleNull) { // - empty object and array. TEST_F(JsonStreamParserTest, EmptyObject) { - StringPiece str = "{}"; + absl::string_view str = "{}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject("")->EndObject(); DoTest(str, i); @@ -203,7 +204,7 @@ TEST_F(JsonStreamParserTest, EmptyObject) { } TEST_F(JsonStreamParserTest, EmptyList) { - StringPiece str = "[]"; + absl::string_view str = "[]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList("")->EndList(); DoTest(str, i); @@ -212,7 +213,7 @@ TEST_F(JsonStreamParserTest, EmptyList) { // - negative and positive double and int, unsigned int TEST_F(JsonStreamParserTest, SimpleDouble) { - StringPiece str = "42.5"; + absl::string_view str = "42.5"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderDouble("", 42.5); DoTest(str, i); @@ -220,7 +221,7 @@ TEST_F(JsonStreamParserTest, SimpleDouble) { } TEST_F(JsonStreamParserTest, ScientificDouble) { - StringPiece str = "1.2345e-10"; + absl::string_view str = "1.2345e-10"; for (int i = 0; i < str.length(); ++i) { ow_.RenderDouble("", 1.2345e-10); DoTest(str, i); @@ -228,7 +229,7 @@ TEST_F(JsonStreamParserTest, ScientificDouble) { } TEST_F(JsonStreamParserTest, SimpleNegativeDouble) { - StringPiece str = "-1045.235"; + absl::string_view str = "-1045.235"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderDouble("", -1045.235); DoTest(str, i); @@ -236,7 +237,7 @@ TEST_F(JsonStreamParserTest, SimpleNegativeDouble) { } TEST_F(JsonStreamParserTest, SimpleInt) { - StringPiece str = "123456"; + absl::string_view str = "123456"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderUint64("", 123456); DoTest(str, i); @@ -244,7 +245,7 @@ TEST_F(JsonStreamParserTest, SimpleInt) { } TEST_F(JsonStreamParserTest, SimpleNegativeInt) { - StringPiece str = "-79497823553162765"; + absl::string_view str = "-79497823553162765"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderInt64("", int64_t{-79497823553162765}); DoTest(str, i); @@ -252,7 +253,7 @@ TEST_F(JsonStreamParserTest, SimpleNegativeInt) { } TEST_F(JsonStreamParserTest, SimpleUnsignedInt) { - StringPiece str = "11779497823553162765"; + absl::string_view str = "11779497823553162765"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderUint64("", uint64_t{11779497823553162765u}); DoTest(str, i); @@ -260,7 +261,7 @@ TEST_F(JsonStreamParserTest, SimpleUnsignedInt) { } TEST_F(JsonStreamParserTest, OctalNumberIsInvalid) { - StringPiece str = "01234"; + absl::string_view str = "01234"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Octal/hex numbers are not valid JSON values.", ParseErrorType::OCTAL_OR_HEX_ARE_NOT_VALID_JSON_VALUES); @@ -273,7 +274,7 @@ TEST_F(JsonStreamParserTest, OctalNumberIsInvalid) { } TEST_F(JsonStreamParserTest, HexNumberIsInvalid) { - StringPiece str = "0x1234"; + absl::string_view str = "0x1234"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Octal/hex numbers are not valid JSON values.", ParseErrorType::OCTAL_OR_HEX_ARE_NOT_VALID_JSON_VALUES); @@ -292,7 +293,7 @@ TEST_F(JsonStreamParserTest, HexNumberIsInvalid) { // - single and double quoted strings TEST_F(JsonStreamParserTest, EmptyDoubleQuotedString) { - StringPiece str = "\"\""; + absl::string_view str = "\"\""; for (int i = 0; i <= str.length(); ++i) { ow_.RenderString("", ""); DoTest(str, i); @@ -300,7 +301,7 @@ TEST_F(JsonStreamParserTest, EmptyDoubleQuotedString) { } TEST_F(JsonStreamParserTest, EmptySingleQuotedString) { - StringPiece str = "''"; + absl::string_view str = "''"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderString("", ""); DoTest(str, i); @@ -308,7 +309,7 @@ TEST_F(JsonStreamParserTest, EmptySingleQuotedString) { } TEST_F(JsonStreamParserTest, SimpleDoubleQuotedString) { - StringPiece str = "\"Some String\""; + absl::string_view str = "\"Some String\""; for (int i = 0; i <= str.length(); ++i) { ow_.RenderString("", "Some String"); DoTest(str, i); @@ -316,7 +317,7 @@ TEST_F(JsonStreamParserTest, SimpleDoubleQuotedString) { } TEST_F(JsonStreamParserTest, SimpleSingleQuotedString) { - StringPiece str = "'Another String'"; + absl::string_view str = "'Another String'"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderString("", "Another String"); DoTest(str, i); @@ -325,7 +326,7 @@ TEST_F(JsonStreamParserTest, SimpleSingleQuotedString) { // - string key, unquoted key, numeric key TEST_F(JsonStreamParserTest, ObjectKeyTypes) { - StringPiece str = + absl::string_view str = "{'s': true, \"d\": false, key: null, snake_key: [], camelKey: {}}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject("") @@ -342,7 +343,7 @@ TEST_F(JsonStreamParserTest, ObjectKeyTypes) { } TEST_F(JsonStreamParserTest, UnquotedObjectKeyWithReservedPrefxes) { - StringPiece str = "{ nullkey: \"a\", truekey: \"b\", falsekey: \"c\"}"; + absl::string_view str = "{ nullkey: \"a\", truekey: \"b\", falsekey: \"c\"}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject("") ->RenderString("nullkey", "a") @@ -354,7 +355,7 @@ TEST_F(JsonStreamParserTest, UnquotedObjectKeyWithReservedPrefxes) { } TEST_F(JsonStreamParserTest, UnquotedObjectKeyWithReservedKeyword) { - StringPiece str = "{ null: \"a\", true: \"b\", false: \"c\"}"; + absl::string_view str = "{ null: \"a\", true: \"b\", false: \"c\"}"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Expected an object key or }.", ParseErrorType::EXPECTED_OBJECT_KEY_OR_BRACES); @@ -362,7 +363,7 @@ TEST_F(JsonStreamParserTest, UnquotedObjectKeyWithReservedKeyword) { } TEST_F(JsonStreamParserTest, UnquotedObjectKeyWithEmbeddedNonAlphanumeric) { - StringPiece str = "{ foo-bar-baz: \"a\"}"; + absl::string_view str = "{ foo-bar-baz: \"a\"}"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Expected : between key:value pair.", ParseErrorType::EXPECTED_COLON); @@ -372,7 +373,7 @@ TEST_F(JsonStreamParserTest, UnquotedObjectKeyWithEmbeddedNonAlphanumeric) { // - array containing primitive values (true, false, null, num, string) TEST_F(JsonStreamParserTest, ArrayPrimitiveValues) { - StringPiece str = "[true, false, null, 'one', \"two\"]"; + absl::string_view str = "[true, false, null, 'one', \"two\"]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList("") ->RenderBool("", true) @@ -387,7 +388,7 @@ TEST_F(JsonStreamParserTest, ArrayPrimitiveValues) { // - array containing array, object TEST_F(JsonStreamParserTest, ArrayComplexValues) { - StringPiece str = + absl::string_view str = "[[22, -127, 45.3, -1056.4, 11779497823553162765], {'key': true}]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList("") @@ -409,7 +410,7 @@ TEST_F(JsonStreamParserTest, ArrayComplexValues) { // - object containing array, object, value (true, false, null, num, string) TEST_F(JsonStreamParserTest, ObjectValues) { - StringPiece str = + absl::string_view str = "{t: true, f: false, n: null, s: 'a string', d: \"another string\", pi: " "22, ni: -127, pd: 45.3, nd: -1056.4, pl: 11779497823553162765, l: [[]], " "o: {'key': true}}"; @@ -439,7 +440,7 @@ TEST_F(JsonStreamParserTest, ObjectValues) { TEST_F(JsonStreamParserTest, RejectNonUtf8WhenNotCoerced) { - StringPiece json = "{\"address\":\xFF\"חרושת 23, רעננה, ישראל\"}"; + absl::string_view json = "{\"address\":\xFF\"חרושת 23, רעננה, ישראל\"}"; for (int i = 0; i <= json.length(); ++i) { DoErrorTest(json, i, "Encountered non UTF-8 code points.", ParseErrorType::NON_UTF_8); @@ -455,7 +456,7 @@ TEST_F(JsonStreamParserTest, RejectNonUtf8WhenNotCoerced) { // - unicode handling in strings TEST_F(JsonStreamParserTest, UnicodeEscaping) { - StringPiece str = "[\"\\u0639\\u0631\\u0628\\u0649\"]"; + absl::string_view str = "[\"\\u0639\\u0631\\u0628\\u0649\"]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList("") ->RenderString("", "\xD8\xB9\xD8\xB1\xD8\xA8\xD9\x89") @@ -466,7 +467,7 @@ TEST_F(JsonStreamParserTest, UnicodeEscaping) { // - unicode UTF-16 surrogate pair handling in strings TEST_F(JsonStreamParserTest, UnicodeSurrogatePairEscaping) { - StringPiece str = + absl::string_view str = "[\"\\u0bee\\ud800\\uddf1\\uD80C\\uDDA4\\uD83d\\udC1D\\uD83C\\uDF6F\"]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList("") @@ -481,7 +482,7 @@ TEST_F(JsonStreamParserTest, UnicodeSurrogatePairEscaping) { TEST_F(JsonStreamParserTest, UnicodeEscapingInvalidCodePointWhenNotCoerced) { // A low surrogate alone. - StringPiece str = "[\"\\ude36\"]"; + absl::string_view str = "[\"\\ude36\"]"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Invalid unicode code point.", ParseErrorType::INVALID_UNICODE); @@ -490,7 +491,7 @@ TEST_F(JsonStreamParserTest, UnicodeEscapingInvalidCodePointWhenNotCoerced) { TEST_F(JsonStreamParserTest, UnicodeEscapingMissingLowSurrogateWhenNotCoerced) { // A high surrogate alone. - StringPiece str = "[\"\\ud83d\"]"; + absl::string_view str = "[\"\\ud83d\"]"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Missing low surrogate.", ParseErrorType::MISSING_LOW_SURROGATE); @@ -517,7 +518,7 @@ TEST_F(JsonStreamParserTest, UnicodeEscapingMissingLowSurrogateWhenNotCoerced) { // - ascii escaping (\b, \f, \n, \r, \t, \v) TEST_F(JsonStreamParserTest, AsciiEscaping) { - StringPiece str = + absl::string_view str = "[\"\\b\", \"\\ning\", \"test\\f\", \"\\r\\t\", \"test\\\\\\ving\"]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList("") @@ -533,7 +534,7 @@ TEST_F(JsonStreamParserTest, AsciiEscaping) { // - trailing commas, we support a single trailing comma but no internal commas. TEST_F(JsonStreamParserTest, TrailingCommas) { - StringPiece str = "[['a',true,], {b: null,},]"; + absl::string_view str = "[['a',true,], {b: null,},]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList("") ->StartList("") @@ -552,7 +553,7 @@ TEST_F(JsonStreamParserTest, TrailingCommas) { // illegal literals TEST_F(JsonStreamParserTest, ExtraTextAfterTrue) { - StringPiece str = "truee"; + absl::string_view str = "truee"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderBool("", true); DoErrorTest(str, i, "Parsing terminated before end of input.", @@ -561,7 +562,7 @@ TEST_F(JsonStreamParserTest, ExtraTextAfterTrue) { } TEST_F(JsonStreamParserTest, InvalidNumberDashOnly) { - StringPiece str = "-"; + absl::string_view str = "-"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Unable to parse number.", ParseErrorType::UNABLE_TO_PARSE_NUMBER); @@ -569,7 +570,7 @@ TEST_F(JsonStreamParserTest, InvalidNumberDashOnly) { } TEST_F(JsonStreamParserTest, InvalidNumberDashName) { - StringPiece str = "-foo"; + absl::string_view str = "-foo"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Unable to parse number.", ParseErrorType::UNABLE_TO_PARSE_NUMBER); @@ -577,7 +578,7 @@ TEST_F(JsonStreamParserTest, InvalidNumberDashName) { } TEST_F(JsonStreamParserTest, InvalidLiteralInArray) { - StringPiece str = "[nule]"; + absl::string_view str = "[nule]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList(""); DoErrorTest(str, i, "Unexpected token.", ParseErrorType::UNEXPECTED_TOKEN); @@ -585,7 +586,7 @@ TEST_F(JsonStreamParserTest, InvalidLiteralInArray) { } TEST_F(JsonStreamParserTest, InvalidLiteralInObject) { - StringPiece str = "{123false}"; + absl::string_view str = "{123false}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Expected an object key or }.", @@ -595,7 +596,7 @@ TEST_F(JsonStreamParserTest, InvalidLiteralInObject) { // mismatched quotes failure on strings TEST_F(JsonStreamParserTest, MismatchedSingleQuotedLiteral) { - StringPiece str = "'Some str\""; + absl::string_view str = "'Some str\""; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Closing quote expected in string.", ParseErrorType::EXPECTED_CLOSING_QUOTE); @@ -603,7 +604,7 @@ TEST_F(JsonStreamParserTest, MismatchedSingleQuotedLiteral) { } TEST_F(JsonStreamParserTest, MismatchedDoubleQuotedLiteral) { - StringPiece str = "\"Another string that ends poorly!'"; + absl::string_view str = "\"Another string that ends poorly!'"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Closing quote expected in string.", ParseErrorType::EXPECTED_CLOSING_QUOTE); @@ -612,7 +613,7 @@ TEST_F(JsonStreamParserTest, MismatchedDoubleQuotedLiteral) { // unterminated strings TEST_F(JsonStreamParserTest, UnterminatedLiteralString) { - StringPiece str = "\"Forgot the rest of i"; + absl::string_view str = "\"Forgot the rest of i"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Closing quote expected in string.", ParseErrorType::EXPECTED_CLOSING_QUOTE); @@ -620,7 +621,7 @@ TEST_F(JsonStreamParserTest, UnterminatedLiteralString) { } TEST_F(JsonStreamParserTest, UnterminatedStringEscape) { - StringPiece str = "\"Forgot the rest of \\"; + absl::string_view str = "\"Forgot the rest of \\"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Closing quote expected in string.", ParseErrorType::EXPECTED_CLOSING_QUOTE); @@ -628,7 +629,7 @@ TEST_F(JsonStreamParserTest, UnterminatedStringEscape) { } TEST_F(JsonStreamParserTest, UnterminatedStringInArray) { - StringPiece str = "[\"Forgot to close the string]"; + absl::string_view str = "[\"Forgot to close the string]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList(""); DoErrorTest(str, i, "Closing quote expected in string.", @@ -637,7 +638,7 @@ TEST_F(JsonStreamParserTest, UnterminatedStringInArray) { } TEST_F(JsonStreamParserTest, UnterminatedStringInObject) { - StringPiece str = "{f: \"Forgot to close the string}"; + absl::string_view str = "{f: \"Forgot to close the string}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Closing quote expected in string.", @@ -646,7 +647,7 @@ TEST_F(JsonStreamParserTest, UnterminatedStringInObject) { } TEST_F(JsonStreamParserTest, UnterminatedObject) { - StringPiece str = "{"; + absl::string_view str = "{"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Unexpected end of string.", @@ -657,7 +658,7 @@ TEST_F(JsonStreamParserTest, UnterminatedObject) { // mismatched object and array closing TEST_F(JsonStreamParserTest, MismatchedCloseObject) { - StringPiece str = "{'key': true]"; + absl::string_view str = "{'key': true]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject("")->RenderBool("key", true); DoErrorTest(str, i, "Expected , or } after key:value pair.", @@ -666,7 +667,7 @@ TEST_F(JsonStreamParserTest, MismatchedCloseObject) { } TEST_F(JsonStreamParserTest, MismatchedCloseArray) { - StringPiece str = "[true, null}"; + absl::string_view str = "[true, null}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList("")->RenderBool("", true)->RenderNull(""); DoErrorTest(str, i, "Expected , or ] after array value.", @@ -676,7 +677,7 @@ TEST_F(JsonStreamParserTest, MismatchedCloseArray) { // Invalid object keys. TEST_F(JsonStreamParserTest, InvalidNumericObjectKey) { - StringPiece str = "{42: true}"; + absl::string_view str = "{42: true}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Expected an object key or }.", @@ -685,7 +686,7 @@ TEST_F(JsonStreamParserTest, InvalidNumericObjectKey) { } TEST_F(JsonStreamParserTest, InvalidLiteralObjectInObject) { - StringPiece str = "{{bob: true}}"; + absl::string_view str = "{{bob: true}}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Expected an object key or }.", @@ -694,7 +695,7 @@ TEST_F(JsonStreamParserTest, InvalidLiteralObjectInObject) { } TEST_F(JsonStreamParserTest, InvalidLiteralArrayInObject) { - StringPiece str = "{[null]}"; + absl::string_view str = "{[null]}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Expected an object key or }.", @@ -703,7 +704,7 @@ TEST_F(JsonStreamParserTest, InvalidLiteralArrayInObject) { } TEST_F(JsonStreamParserTest, InvalidLiteralValueInObject) { - StringPiece str = "{false}"; + absl::string_view str = "{false}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Expected an object key or }.", @@ -712,7 +713,7 @@ TEST_F(JsonStreamParserTest, InvalidLiteralValueInObject) { } TEST_F(JsonStreamParserTest, MissingColonAfterStringInObject) { - StringPiece str = "{\"key\"}"; + absl::string_view str = "{\"key\"}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Expected : between key:value pair.", @@ -721,7 +722,7 @@ TEST_F(JsonStreamParserTest, MissingColonAfterStringInObject) { } TEST_F(JsonStreamParserTest, MissingColonAfterKeyInObject) { - StringPiece str = "{key}"; + absl::string_view str = "{key}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Expected : between key:value pair.", @@ -730,7 +731,7 @@ TEST_F(JsonStreamParserTest, MissingColonAfterKeyInObject) { } TEST_F(JsonStreamParserTest, EndOfTextAfterKeyInObject) { - StringPiece str = "{key"; + absl::string_view str = "{key"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Unexpected end of string.", @@ -739,7 +740,7 @@ TEST_F(JsonStreamParserTest, EndOfTextAfterKeyInObject) { } TEST_F(JsonStreamParserTest, MissingValueAfterColonInObject) { - StringPiece str = "{key:}"; + absl::string_view str = "{key:}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Unexpected token.", ParseErrorType::UNEXPECTED_TOKEN); @@ -747,7 +748,7 @@ TEST_F(JsonStreamParserTest, MissingValueAfterColonInObject) { } TEST_F(JsonStreamParserTest, MissingCommaBetweenObjectEntries) { - StringPiece str = "{key:20 'hello': true}"; + absl::string_view str = "{key:20 'hello': true}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject("")->RenderUint64("key", 20); DoErrorTest(str, i, "Expected , or } after key:value pair.", @@ -756,7 +757,7 @@ TEST_F(JsonStreamParserTest, MissingCommaBetweenObjectEntries) { } TEST_F(JsonStreamParserTest, InvalidLiteralAsObjectKey) { - StringPiece str = "{false: 20}"; + absl::string_view str = "{false: 20}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Expected an object key or }.", @@ -765,7 +766,7 @@ TEST_F(JsonStreamParserTest, InvalidLiteralAsObjectKey) { } TEST_F(JsonStreamParserTest, ExtraCharactersAfterObject) { - StringPiece str = "{}}"; + absl::string_view str = "{}}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject("")->EndObject(); DoErrorTest(str, i, "Parsing terminated before end of input.", @@ -774,7 +775,7 @@ TEST_F(JsonStreamParserTest, ExtraCharactersAfterObject) { } TEST_F(JsonStreamParserTest, PositiveNumberTooBigIsDouble) { - StringPiece str = "18446744073709551616"; // 2^64 + absl::string_view str = "18446744073709551616"; // 2^64 for (int i = 0; i <= str.length(); ++i) { ow_.RenderDouble("", 18446744073709552000.0); DoTest(str, i); @@ -782,7 +783,7 @@ TEST_F(JsonStreamParserTest, PositiveNumberTooBigIsDouble) { } TEST_F(JsonStreamParserTest, NegativeNumberTooBigIsDouble) { - StringPiece str = "-18446744073709551616"; + absl::string_view str = "-18446744073709551616"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderDouble("", -18446744073709551616.0); DoTest(str, i); @@ -790,7 +791,7 @@ TEST_F(JsonStreamParserTest, NegativeNumberTooBigIsDouble) { } TEST_F(JsonStreamParserTest, DoubleTooBig) { - StringPiece str = "[1.89769e+308]"; + absl::string_view str = "[1.89769e+308]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList(""); DoErrorTest(str, i, "Number exceeds the range of double.", @@ -807,7 +808,7 @@ TEST_F(JsonStreamParserTest, DoubleTooBig) { // invalid bare backslash. TEST_F(JsonStreamParserTest, UnfinishedEscape) { - StringPiece str = "\"\\"; + absl::string_view str = "\"\\"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Closing quote expected in string.", ParseErrorType::EXPECTED_CLOSING_QUOTE); @@ -816,7 +817,7 @@ TEST_F(JsonStreamParserTest, UnfinishedEscape) { // invalid bare backslash u. TEST_F(JsonStreamParserTest, UnfinishedUnicodeEscape) { - StringPiece str = "\"\\u"; + absl::string_view str = "\"\\u"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Illegal hex string.", ParseErrorType::ILLEGAL_HEX_STRING); @@ -825,7 +826,7 @@ TEST_F(JsonStreamParserTest, UnfinishedUnicodeEscape) { // invalid unicode sequence. TEST_F(JsonStreamParserTest, UnicodeEscapeCutOff) { - StringPiece str = "\"\\u12"; + absl::string_view str = "\"\\u12"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Illegal hex string.", ParseErrorType::ILLEGAL_HEX_STRING); @@ -834,7 +835,7 @@ TEST_F(JsonStreamParserTest, UnicodeEscapeCutOff) { // invalid unicode sequence (valid in modern EcmaScript but not in JSON). TEST_F(JsonStreamParserTest, BracketedUnicodeEscape) { - StringPiece str = "\"\\u{1f36f}\""; + absl::string_view str = "\"\\u{1f36f}\""; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Invalid escape sequence.", ParseErrorType::INVALID_ESCAPE_SEQUENCE); @@ -843,7 +844,7 @@ TEST_F(JsonStreamParserTest, BracketedUnicodeEscape) { TEST_F(JsonStreamParserTest, UnicodeEscapeInvalidCharacters) { - StringPiece str = "\"\\u12$4hello"; + absl::string_view str = "\"\\u12$4hello"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Invalid escape sequence.", ParseErrorType::INVALID_ESCAPE_SEQUENCE); @@ -852,7 +853,7 @@ TEST_F(JsonStreamParserTest, UnicodeEscapeInvalidCharacters) { // invalid unicode sequence in low half surrogate: g is not a hex digit. TEST_F(JsonStreamParserTest, UnicodeEscapeLowHalfSurrogateInvalidCharacters) { - StringPiece str = "\"\\ud800\\udcfg\""; + absl::string_view str = "\"\\ud800\\udcfg\""; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Invalid escape sequence.", ParseErrorType::INVALID_ESCAPE_SEQUENCE); @@ -861,7 +862,7 @@ TEST_F(JsonStreamParserTest, UnicodeEscapeLowHalfSurrogateInvalidCharacters) { // Extra commas with an object or array. TEST_F(JsonStreamParserTest, ExtraCommaInObject) { - StringPiece str = "{'k1': true,,'k2': false}"; + absl::string_view str = "{'k1': true,,'k2': false}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject("")->RenderBool("k1", true); DoErrorTest(str, i, "Expected an object key or }.", @@ -870,7 +871,7 @@ TEST_F(JsonStreamParserTest, ExtraCommaInObject) { } TEST_F(JsonStreamParserTest, ExtraCommaInArray) { - StringPiece str = "[true,,false}"; + absl::string_view str = "[true,,false}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList("")->RenderBool("", true); DoErrorTest(str, i, "Unexpected token.", ParseErrorType::UNEXPECTED_TOKEN); @@ -879,7 +880,7 @@ TEST_F(JsonStreamParserTest, ExtraCommaInArray) { // Extra text beyond end of value. TEST_F(JsonStreamParserTest, ExtraTextAfterLiteral) { - StringPiece str = "'hello', 'world'"; + absl::string_view str = "'hello', 'world'"; for (int i = 0; i <= str.length(); ++i) { ow_.RenderString("", "hello"); DoErrorTest(str, i, "Parsing terminated before end of input.", @@ -888,7 +889,7 @@ TEST_F(JsonStreamParserTest, ExtraTextAfterLiteral) { } TEST_F(JsonStreamParserTest, ExtraTextAfterObject) { - StringPiece str = "{'key': true} 'oops'"; + absl::string_view str = "{'key': true} 'oops'"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject("")->RenderBool("key", true)->EndObject(); DoErrorTest(str, i, "Parsing terminated before end of input.", @@ -897,7 +898,7 @@ TEST_F(JsonStreamParserTest, ExtraTextAfterObject) { } TEST_F(JsonStreamParserTest, ExtraTextAfterArray) { - StringPiece str = "[null] 'oops'"; + absl::string_view str = "[null] 'oops'"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList("")->RenderNull("")->EndList(); DoErrorTest(str, i, "Parsing terminated before end of input.", @@ -907,14 +908,14 @@ TEST_F(JsonStreamParserTest, ExtraTextAfterArray) { // Random unknown text in the value. TEST_F(JsonStreamParserTest, UnknownCharactersAsValue) { - StringPiece str = "*"; + absl::string_view str = "*"; for (int i = 0; i <= str.length(); ++i) { DoErrorTest(str, i, "Expected a value.", ParseErrorType::EXPECTED_VALUE); } } TEST_F(JsonStreamParserTest, UnknownCharactersInArray) { - StringPiece str = "[*]"; + absl::string_view str = "[*]"; for (int i = 0; i <= str.length(); ++i) { ow_.StartList(""); DoErrorTest(str, i, "Expected a value or ] within an array.", @@ -923,7 +924,7 @@ TEST_F(JsonStreamParserTest, UnknownCharactersInArray) { } TEST_F(JsonStreamParserTest, UnknownCharactersInObject) { - StringPiece str = "{'key': *}"; + absl::string_view str = "{'key': *}"; for (int i = 0; i <= str.length(); ++i) { ow_.StartObject(""); DoErrorTest(str, i, "Expected a value.", ParseErrorType::EXPECTED_VALUE); @@ -934,11 +935,11 @@ TEST_F(JsonStreamParserTest, DeepNestJsonNotExceedLimit) { int count = 99; std::string str; for (int i = 0; i < count; ++i) { - StrAppend(&str, "{'a':"); + absl::StrAppend(&str, "{'a':"); } - StrAppend(&str, "{'nest64':'v1', 'nest64': false, 'nest64': ['v2']}"); + absl::StrAppend(&str, "{'nest64':'v1', 'nest64': false, 'nest64': ['v2']}"); for (int i = 0; i < count; ++i) { - StrAppend(&str, "}"); + absl::StrAppend(&str, "}"); } ow_.StartObject(""); for (int i = 0; i < count; ++i) { @@ -960,14 +961,14 @@ TEST_F(JsonStreamParserTest, DeepNestJsonExceedLimit) { int count = 98; std::string str; for (int i = 0; i < count; ++i) { - StrAppend(&str, "{'a':"); + absl::StrAppend(&str, "{'a':"); } // Supports trailing commas. - StrAppend(&str, + absl::StrAppend(&str, "{'nest11' : [{'nest12' : null,},]," "'nest21' : {'nest22' : {'nest23' : false}}}"); for (int i = 0; i < count; ++i) { - StrAppend(&str, "}"); + absl::StrAppend(&str, "}"); } DoErrorTest(str, 0, "Message too deep. Max recursion depth reached for key 'nest22'"); diff --git a/src/google/protobuf/util/internal/location_tracker.h b/src/google/protobuf/util/internal/location_tracker.h index 68fefcc85cb3..a0c4bad39784 100644 --- a/src/google/protobuf/util/internal/location_tracker.h +++ b/src/google/protobuf/util/internal/location_tracker.h @@ -47,6 +47,8 @@ namespace converter { // the location information for the purpose of error reporting. class PROTOBUF_EXPORT LocationTrackerInterface { public: + LocationTrackerInterface(const LocationTrackerInterface&) = delete; + LocationTrackerInterface& operator=(const LocationTrackerInterface&) = delete; virtual ~LocationTrackerInterface() {} // Returns the object location as human readable string. @@ -57,7 +59,6 @@ class PROTOBUF_EXPORT LocationTrackerInterface { private: // Please do not add any data members to this class. - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LocationTrackerInterface); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/mock_error_listener.h b/src/google/protobuf/util/internal/mock_error_listener.h index 3fbdd883ebcb..b90b7a0f5cbc 100644 --- a/src/google/protobuf/util/internal/mock_error_listener.h +++ b/src/google/protobuf/util/internal/mock_error_listener.h @@ -32,7 +32,7 @@ #define GOOGLE_PROTOBUF_UTIL_INTERNAL_MOCK_ERROR_LISTENER_H__ #include -#include +#include "absl/strings/string_view.h" #include #include @@ -48,15 +48,15 @@ class MockErrorListener : public ErrorListener { MOCK_METHOD(void, InvalidName, (const LocationTrackerInterface& loc, - StringPiece unknown_name, StringPiece message), + absl::string_view unknown_name, absl::string_view message), (override)); MOCK_METHOD(void, InvalidValue, - (const LocationTrackerInterface& loc, StringPiece type_name, - StringPiece value), + (const LocationTrackerInterface& loc, absl::string_view type_name, + absl::string_view value), (override)); MOCK_METHOD(void, MissingField, (const LocationTrackerInterface& loc, - StringPiece missing_name), + absl::string_view missing_name), (override)); }; diff --git a/src/google/protobuf/util/internal/object_location_tracker.h b/src/google/protobuf/util/internal/object_location_tracker.h index 47821e6cb2c9..05abc566260f 100644 --- a/src/google/protobuf/util/internal/object_location_tracker.h +++ b/src/google/protobuf/util/internal/object_location_tracker.h @@ -46,14 +46,13 @@ class ObjectLocationTracker : public LocationTrackerInterface { public: // Creates an empty location tracker. ObjectLocationTracker() {} + ObjectLocationTracker(const ObjectLocationTracker&) = delete; + ObjectLocationTracker& operator=(const ObjectLocationTracker&) = delete; ~ObjectLocationTracker() override {} // Returns empty because nothing is tracked. std::string ToString() const override { return ""; } - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjectLocationTracker); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/object_source.h b/src/google/protobuf/util/internal/object_source.h index fc7672e5fa5f..229e8d9748bb 100644 --- a/src/google/protobuf/util/internal/object_source.h +++ b/src/google/protobuf/util/internal/object_source.h @@ -32,9 +32,9 @@ #define GOOGLE_PROTOBUF_UTIL_INTERNAL_OBJECT_SOURCE_H__ #include -#include -#include -#include +#include "absl/status/status.h" +#include "absl/strings/string_view.h" + // Must be included last. #include @@ -54,25 +54,23 @@ class ObjectWriter; // Derived classes could be thread-unsafe. class PROTOBUF_EXPORT ObjectSource { public: + ObjectSource(const ObjectSource&) = delete; + ObjectSource& operator=(const ObjectSource&) = delete; virtual ~ObjectSource() {} // Writes to the ObjectWriter - virtual util::Status WriteTo(ObjectWriter* ow) const { + virtual absl::Status WriteTo(ObjectWriter* ow) const { return NamedWriteTo("", ow); } // Writes to the ObjectWriter with a custom name for the message. // This is useful when you chain ObjectSource together by embedding one // within another. - virtual util::Status NamedWriteTo(StringPiece name, + virtual absl::Status NamedWriteTo(absl::string_view name, ObjectWriter* ow) const = 0; protected: ObjectSource() {} - - private: - // Do not add any data members to this class. - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjectSource); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/object_writer.cc b/src/google/protobuf/util/internal/object_writer.cc index 4dabd378e471..680b8944aa6b 100644 --- a/src/google/protobuf/util/internal/object_writer.cc +++ b/src/google/protobuf/util/internal/object_writer.cc @@ -39,7 +39,7 @@ namespace converter { // static void ObjectWriter::RenderDataPieceTo(const DataPiece& data, - StringPiece name, ObjectWriter* ow) { + absl::string_view name, ObjectWriter* ow) { switch (data.type()) { case DataPiece::TYPE_INT32: { ow->RenderInt32(name, data.ToInt32().value()); diff --git a/src/google/protobuf/util/internal/object_writer.h b/src/google/protobuf/util/internal/object_writer.h index bc4095b6d423..090f55392eab 100644 --- a/src/google/protobuf/util/internal/object_writer.h +++ b/src/google/protobuf/util/internal/object_writer.h @@ -34,7 +34,7 @@ #include #include -#include +#include "absl/strings/string_view.h" // Must be included last. #include @@ -63,56 +63,59 @@ class DataPiece; // and get rid the need to call EndXXX(). class PROTOBUF_EXPORT ObjectWriter { public: + ObjectWriter(const ObjectWriter&) = delete; + ObjectWriter& operator=(const ObjectWriter&) = delete; virtual ~ObjectWriter() {} // Starts an object. If the name is empty, the object will not be named. - virtual ObjectWriter* StartObject(StringPiece name) = 0; + virtual ObjectWriter* StartObject(absl::string_view name) = 0; // Ends an object. virtual ObjectWriter* EndObject() = 0; // Starts a list. If the name is empty, the list will not be named. - virtual ObjectWriter* StartList(StringPiece name) = 0; + virtual ObjectWriter* StartList(absl::string_view name) = 0; // Ends a list. virtual ObjectWriter* EndList() = 0; // Renders a boolean value. - virtual ObjectWriter* RenderBool(StringPiece name, bool value) = 0; + virtual ObjectWriter* RenderBool(absl::string_view name, bool value) = 0; // Renders an 32-bit integer value. - virtual ObjectWriter* RenderInt32(StringPiece name, int32_t value) = 0; + virtual ObjectWriter* RenderInt32(absl::string_view name, int32_t value) = 0; // Renders an 32-bit unsigned integer value. - virtual ObjectWriter* RenderUint32(StringPiece name, + virtual ObjectWriter* RenderUint32(absl::string_view name, uint32_t value) = 0; // Renders a 64-bit integer value. - virtual ObjectWriter* RenderInt64(StringPiece name, int64_t value) = 0; + virtual ObjectWriter* RenderInt64(absl::string_view name, int64_t value) = 0; // Renders an 64-bit unsigned integer value. - virtual ObjectWriter* RenderUint64(StringPiece name, + virtual ObjectWriter* RenderUint64(absl::string_view name, uint64_t value) = 0; // Renders a double value. - virtual ObjectWriter* RenderDouble(StringPiece name, double value) = 0; + virtual ObjectWriter* RenderDouble(absl::string_view name, double value) = 0; // Renders a float value. - virtual ObjectWriter* RenderFloat(StringPiece name, float value) = 0; + virtual ObjectWriter* RenderFloat(absl::string_view name, float value) = 0; - // Renders a StringPiece value. This is for rendering strings. - virtual ObjectWriter* RenderString(StringPiece name, - StringPiece value) = 0; + // Renders a string value. + virtual ObjectWriter* RenderString(absl::string_view name, + absl::string_view value) = 0; // Renders a bytes value. - virtual ObjectWriter* RenderBytes(StringPiece name, StringPiece value) = 0; + virtual ObjectWriter* RenderBytes(absl::string_view name, + absl::string_view value) = 0; // Renders a Null value. - virtual ObjectWriter* RenderNull(StringPiece name) = 0; + virtual ObjectWriter* RenderNull(absl::string_view name) = 0; // Renders a DataPiece object to a ObjectWriter. - static void RenderDataPieceTo(const DataPiece& data, StringPiece name, + static void RenderDataPieceTo(const DataPiece& data, absl::string_view name, ObjectWriter* ow); @@ -136,9 +139,6 @@ class PROTOBUF_EXPORT ObjectWriter { // If set to true, we use the stricter version of base64 decoding for byte // fields by making sure decoded version encodes back to the original string. bool use_strict_base64_decoding_; - - // Do not add any data members to this class. - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjectWriter); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/proto_writer.cc b/src/google/protobuf/util/internal/proto_writer.cc index 777d060bbd7c..f5799aec8e45 100644 --- a/src/google/protobuf/util/internal/proto_writer.cc +++ b/src/google/protobuf/util/internal/proto_writer.cc @@ -35,11 +35,15 @@ #include #include -#include #include #include -#include -#include +#include "absl/base/call_once.h" +#include "absl/status/statusor.h" +#include "absl/strings/ascii.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" +#include "absl/time/clock.h" +#include "absl/time/time.h" #include #include #include @@ -119,9 +123,9 @@ ProtoWriter::~ProtoWriter() { namespace { // Writes an INT32 field, including tag to the stream. -inline util::Status WriteInt32(int field_number, const DataPiece& data, +inline absl::Status WriteInt32(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr i32 = data.ToInt32(); + absl::StatusOr i32 = data.ToInt32(); if (i32.ok()) { WireFormatLite::WriteInt32(field_number, i32.value(), stream); } @@ -129,9 +133,9 @@ inline util::Status WriteInt32(int field_number, const DataPiece& data, } // writes an SFIXED32 field, including tag, to the stream. -inline util::Status WriteSFixed32(int field_number, const DataPiece& data, +inline absl::Status WriteSFixed32(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr i32 = data.ToInt32(); + absl::StatusOr i32 = data.ToInt32(); if (i32.ok()) { WireFormatLite::WriteSFixed32(field_number, i32.value(), stream); } @@ -139,9 +143,9 @@ inline util::Status WriteSFixed32(int field_number, const DataPiece& data, } // Writes an SINT32 field, including tag, to the stream. -inline util::Status WriteSInt32(int field_number, const DataPiece& data, +inline absl::Status WriteSInt32(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr i32 = data.ToInt32(); + absl::StatusOr i32 = data.ToInt32(); if (i32.ok()) { WireFormatLite::WriteSInt32(field_number, i32.value(), stream); } @@ -149,9 +153,9 @@ inline util::Status WriteSInt32(int field_number, const DataPiece& data, } // Writes a FIXED32 field, including tag, to the stream. -inline util::Status WriteFixed32(int field_number, const DataPiece& data, +inline absl::Status WriteFixed32(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr u32 = data.ToUint32(); + absl::StatusOr u32 = data.ToUint32(); if (u32.ok()) { WireFormatLite::WriteFixed32(field_number, u32.value(), stream); } @@ -159,9 +163,9 @@ inline util::Status WriteFixed32(int field_number, const DataPiece& data, } // Writes a UINT32 field, including tag, to the stream. -inline util::Status WriteUInt32(int field_number, const DataPiece& data, +inline absl::Status WriteUInt32(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr u32 = data.ToUint32(); + absl::StatusOr u32 = data.ToUint32(); if (u32.ok()) { WireFormatLite::WriteUInt32(field_number, u32.value(), stream); } @@ -169,9 +173,9 @@ inline util::Status WriteUInt32(int field_number, const DataPiece& data, } // Writes an INT64 field, including tag, to the stream. -inline util::Status WriteInt64(int field_number, const DataPiece& data, +inline absl::Status WriteInt64(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr i64 = data.ToInt64(); + absl::StatusOr i64 = data.ToInt64(); if (i64.ok()) { WireFormatLite::WriteInt64(field_number, i64.value(), stream); } @@ -179,9 +183,9 @@ inline util::Status WriteInt64(int field_number, const DataPiece& data, } // Writes an SFIXED64 field, including tag, to the stream. -inline util::Status WriteSFixed64(int field_number, const DataPiece& data, +inline absl::Status WriteSFixed64(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr i64 = data.ToInt64(); + absl::StatusOr i64 = data.ToInt64(); if (i64.ok()) { WireFormatLite::WriteSFixed64(field_number, i64.value(), stream); } @@ -189,9 +193,9 @@ inline util::Status WriteSFixed64(int field_number, const DataPiece& data, } // Writes an SINT64 field, including tag, to the stream. -inline util::Status WriteSInt64(int field_number, const DataPiece& data, +inline absl::Status WriteSInt64(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr i64 = data.ToInt64(); + absl::StatusOr i64 = data.ToInt64(); if (i64.ok()) { WireFormatLite::WriteSInt64(field_number, i64.value(), stream); } @@ -199,9 +203,9 @@ inline util::Status WriteSInt64(int field_number, const DataPiece& data, } // Writes a FIXED64 field, including tag, to the stream. -inline util::Status WriteFixed64(int field_number, const DataPiece& data, +inline absl::Status WriteFixed64(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr u64 = data.ToUint64(); + absl::StatusOr u64 = data.ToUint64(); if (u64.ok()) { WireFormatLite::WriteFixed64(field_number, u64.value(), stream); } @@ -209,9 +213,9 @@ inline util::Status WriteFixed64(int field_number, const DataPiece& data, } // Writes a UINT64 field, including tag, to the stream. -inline util::Status WriteUInt64(int field_number, const DataPiece& data, +inline absl::Status WriteUInt64(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr u64 = data.ToUint64(); + absl::StatusOr u64 = data.ToUint64(); if (u64.ok()) { WireFormatLite::WriteUInt64(field_number, u64.value(), stream); } @@ -219,9 +223,9 @@ inline util::Status WriteUInt64(int field_number, const DataPiece& data, } // Writes a DOUBLE field, including tag, to the stream. -inline util::Status WriteDouble(int field_number, const DataPiece& data, +inline absl::Status WriteDouble(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr d = data.ToDouble(); + absl::StatusOr d = data.ToDouble(); if (d.ok()) { WireFormatLite::WriteDouble(field_number, d.value(), stream); } @@ -229,9 +233,9 @@ inline util::Status WriteDouble(int field_number, const DataPiece& data, } // Writes a FLOAT field, including tag, to the stream. -inline util::Status WriteFloat(int field_number, const DataPiece& data, +inline absl::Status WriteFloat(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr f = data.ToFloat(); + absl::StatusOr f = data.ToFloat(); if (f.ok()) { WireFormatLite::WriteFloat(field_number, f.value(), stream); } @@ -239,9 +243,9 @@ inline util::Status WriteFloat(int field_number, const DataPiece& data, } // Writes a BOOL field, including tag, to the stream. -inline util::Status WriteBool(int field_number, const DataPiece& data, +inline absl::Status WriteBool(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr b = data.ToBool(); + absl::StatusOr b = data.ToBool(); if (b.ok()) { WireFormatLite::WriteBool(field_number, b.value(), stream); } @@ -249,9 +253,9 @@ inline util::Status WriteBool(int field_number, const DataPiece& data, } // Writes a BYTES field, including tag, to the stream. -inline util::Status WriteBytes(int field_number, const DataPiece& data, +inline absl::Status WriteBytes(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr c = data.ToBytes(); + absl::StatusOr c = data.ToBytes(); if (c.ok()) { WireFormatLite::WriteBytes(field_number, c.value(), stream); } @@ -259,9 +263,9 @@ inline util::Status WriteBytes(int field_number, const DataPiece& data, } // Writes a STRING field, including tag, to the stream. -inline util::Status WriteString(int field_number, const DataPiece& data, +inline absl::Status WriteString(int field_number, const DataPiece& data, CodedOutputStream* stream) { - util::StatusOr s = data.ToString(); + absl::StatusOr s = data.ToString(); if (s.ok()) { WireFormatLite::WriteString(field_number, s.value(), stream); } @@ -408,22 +412,22 @@ std::string ProtoWriter::ProtoElement::ToString() const { std::string name = now->parent_field_->name(); int i = 0; while (i < name.size() && - (ascii_isalnum(name[i]) || name[i] == '_')) + (absl::ascii_isalnum(name[i]) || name[i] == '_')) ++i; if (i > 0 && i == name.size()) { // safe field name if (loc.empty()) { loc = name; } else { - StrAppend(&loc, ".", name); + absl::StrAppend(&loc, ".", name); } } else { - StrAppend(&loc, "[\"", CEscape(name), "\"]"); + absl::StrAppend(&loc, "[\"", absl::CEscape(name), "\"]"); } } int array_index_now = now->array_index_; if (ow_->IsRepeated(*(now->parent_field_)) && array_index_now > 0) { - StrAppend(&loc, "[", array_index_now - 1, "]"); + absl::StrAppend(&loc, "[", array_index_now - 1, "]"); } } @@ -438,22 +442,22 @@ void ProtoWriter::ProtoElement::TakeOneofIndex(int32_t index) { oneof_indices_[index] = true; } -void ProtoWriter::InvalidName(StringPiece unknown_name, - StringPiece message) { +void ProtoWriter::InvalidName(absl::string_view unknown_name, + absl::string_view message) { listener_->InvalidName(location(), unknown_name, message); } -void ProtoWriter::InvalidValue(StringPiece type_name, - StringPiece value) { +void ProtoWriter::InvalidValue(absl::string_view type_name, + absl::string_view value) { listener_->InvalidValue(location(), type_name, value); } -void ProtoWriter::MissingField(StringPiece missing_name) { +void ProtoWriter::MissingField(absl::string_view missing_name) { listener_->MissingField(location(), missing_name); } ProtoWriter* ProtoWriter::StartObject( - StringPiece name) { + absl::string_view name) { // Starting the root message. Create the root ProtoElement and return. if (element_ == nullptr) { if (!name.empty()) { @@ -477,7 +481,7 @@ ProtoWriter* ProtoWriter::StartObject( const google::protobuf::Type* type = LookupType(field); if (type == nullptr) { ++invalid_depth_; - InvalidName(name, StrCat("Missing descriptor for field: ", + InvalidName(name, absl::StrCat("Missing descriptor for field: ", field->type_url())); return this; } @@ -506,7 +510,7 @@ ProtoWriter* ProtoWriter::EndObject() { } ProtoWriter* ProtoWriter::StartList( - StringPiece name) { + absl::string_view name) { const google::protobuf::Field* field = BeginNamed(name, true); @@ -520,7 +524,7 @@ ProtoWriter* ProtoWriter::StartList( const google::protobuf::Type* type = LookupType(field); if (type == nullptr) { ++invalid_depth_; - InvalidName(name, StrCat("Missing descriptor for field: ", + InvalidName(name, absl::StrCat("Missing descriptor for field: ", field->type_url())); return this; } @@ -539,8 +543,8 @@ ProtoWriter* ProtoWriter::EndList() { } ProtoWriter* ProtoWriter::RenderDataPiece( - StringPiece name, const DataPiece& data) { - util::Status status; + absl::string_view name, const DataPiece& data) { + absl::Status status; if (invalid_depth_ > 0) return this; const google::protobuf::Field* field = Lookup(name); @@ -551,7 +555,7 @@ ProtoWriter* ProtoWriter::RenderDataPiece( const google::protobuf::Type* type = LookupType(field); if (type == nullptr) { - InvalidName(name, StrCat("Missing descriptor for field: ", + InvalidName(name, absl::StrCat("Missing descriptor for field: ", field->type_url())); return this; } @@ -560,14 +564,14 @@ ProtoWriter* ProtoWriter::RenderDataPiece( } bool ProtoWriter::ValidOneof(const google::protobuf::Field& field, - StringPiece unnormalized_name) { + absl::string_view unnormalized_name) { if (element_ == nullptr) return true; if (field.oneof_index() > 0) { if (element_->IsOneofIndexTaken(field.oneof_index())) { InvalidValue( "oneof", - StrCat( + absl::StrCat( "oneof field '", element_->type().oneofs(field.oneof_index() - 1), "' is already set. Cannot set '", unnormalized_name, "'")); return false; @@ -594,14 +598,14 @@ ProtoWriter* ProtoWriter::StartListField(const google::protobuf::Field& field, return this; } -util::Status ProtoWriter::WriteEnum(int field_number, const DataPiece& data, +absl::Status ProtoWriter::WriteEnum(int field_number, const DataPiece& data, const google::protobuf::Enum* enum_type, CodedOutputStream* stream, bool use_lower_camel_for_enums, bool case_insensitive_enum_parsing, bool ignore_unknown_values) { bool is_unknown_enum_value = false; - util::StatusOr e = data.ToEnum( + absl::StatusOr e = data.ToEnum( enum_type, use_lower_camel_for_enums, case_insensitive_enum_parsing, ignore_unknown_values, &is_unknown_enum_value); if (e.ok() && !is_unknown_enum_value) { @@ -613,7 +617,7 @@ util::Status ProtoWriter::WriteEnum(int field_number, const DataPiece& data, ProtoWriter* ProtoWriter::RenderPrimitiveField( const google::protobuf::Field& field, const google::protobuf::Type& type, const DataPiece& data) { - util::Status status; + absl::Status status; // Pushing a ProtoElement and then pop it off at the end for 2 purposes: // error location reporting and required field accounting. @@ -693,7 +697,7 @@ ProtoWriter* ProtoWriter::RenderPrimitiveField( break; } default: // TYPE_GROUP, TYPE_MESSAGE, TYPE_UNKNOWN. - status = util::InvalidArgumentError(data.ValueAsStringOrDefault("")); + status = absl::InvalidArgumentError(data.ValueAsStringOrDefault("")); } if (!status.ok()) { @@ -714,7 +718,7 @@ ProtoWriter* ProtoWriter::RenderPrimitiveField( return this; } -const google::protobuf::Field* ProtoWriter::BeginNamed(StringPiece name, +const google::protobuf::Field* ProtoWriter::BeginNamed(absl::string_view name, bool is_list) { if (invalid_depth_ > 0) { ++invalid_depth_; @@ -735,7 +739,7 @@ const google::protobuf::Field* ProtoWriter::BeginNamed(StringPiece name, } const google::protobuf::Field* ProtoWriter::Lookup( - StringPiece unnormalized_name) { + absl::string_view unnormalized_name) { ProtoElement* e = element(); if (e == nullptr) { InvalidName(unnormalized_name, "Root element must be a message."); diff --git a/src/google/protobuf/util/internal/proto_writer.h b/src/google/protobuf/util/internal/proto_writer.h index a7cf6acf78ce..6f49c6db3f0b 100644 --- a/src/google/protobuf/util/internal/proto_writer.h +++ b/src/google/protobuf/util/internal/proto_writer.h @@ -43,14 +43,13 @@ #include #include #include -#include +#include "absl/status/status.h" #include #include #include #include #include -#include -#include + // Must be included last. #include @@ -74,53 +73,57 @@ class PROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { // Constructor. Does not take ownership of any parameter passed in. ProtoWriter(TypeResolver* type_resolver, const google::protobuf::Type& type, strings::ByteSink* output, ErrorListener* listener); + ProtoWriter() = delete; + ProtoWriter(const ProtoWriter&) = delete; + ProtoWriter& operator=(const ProtoWriter&) = delete; ~ProtoWriter() override; // ObjectWriter methods. - ProtoWriter* StartObject(StringPiece name) override; + ProtoWriter* StartObject(absl::string_view name) override; ProtoWriter* EndObject() override; - ProtoWriter* StartList(StringPiece name) override; + ProtoWriter* StartList(absl::string_view name) override; ProtoWriter* EndList() override; - ProtoWriter* RenderBool(StringPiece name, bool value) override { + ProtoWriter* RenderBool(absl::string_view name, bool value) override { return RenderDataPiece(name, DataPiece(value)); } - ProtoWriter* RenderInt32(StringPiece name, int32_t value) override { + ProtoWriter* RenderInt32(absl::string_view name, int32_t value) override { return RenderDataPiece(name, DataPiece(value)); } - ProtoWriter* RenderUint32(StringPiece name, uint32_t value) override { + ProtoWriter* RenderUint32(absl::string_view name, uint32_t value) override { return RenderDataPiece(name, DataPiece(value)); } - ProtoWriter* RenderInt64(StringPiece name, int64_t value) override { + ProtoWriter* RenderInt64(absl::string_view name, int64_t value) override { return RenderDataPiece(name, DataPiece(value)); } - ProtoWriter* RenderUint64(StringPiece name, uint64_t value) override { + ProtoWriter* RenderUint64(absl::string_view name, uint64_t value) override { return RenderDataPiece(name, DataPiece(value)); } - ProtoWriter* RenderDouble(StringPiece name, double value) override { + ProtoWriter* RenderDouble(absl::string_view name, double value) override { return RenderDataPiece(name, DataPiece(value)); } - ProtoWriter* RenderFloat(StringPiece name, float value) override { + ProtoWriter* RenderFloat(absl::string_view name, float value) override { return RenderDataPiece(name, DataPiece(value)); } - ProtoWriter* RenderString(StringPiece name, - StringPiece value) override { + ProtoWriter* RenderString(absl::string_view name, + absl::string_view value) override { return RenderDataPiece(name, DataPiece(value, use_strict_base64_decoding())); } - ProtoWriter* RenderBytes(StringPiece name, StringPiece value) override { + ProtoWriter* RenderBytes(absl::string_view name, + absl::string_view value) override { return RenderDataPiece( name, DataPiece(value, false, use_strict_base64_decoding())); } - ProtoWriter* RenderNull(StringPiece name) override { + ProtoWriter* RenderNull(absl::string_view name) override { return RenderDataPiece(name, DataPiece::NullData()); } // Renders a DataPiece 'value' into a field whose wire type is determined // from the given field 'name'. - virtual ProtoWriter* RenderDataPiece(StringPiece name, + virtual ProtoWriter* RenderDataPiece(absl::string_view name, const DataPiece& data); @@ -178,6 +181,9 @@ class PROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { // Constructor for a field of an element. ProtoElement(ProtoElement* parent, const google::protobuf::Field* field, const google::protobuf::Type& type, bool is_list); + ProtoElement() = delete; + ProtoElement(const ProtoElement&) = delete; + ProtoElement& operator=(const ProtoElement&) = delete; ~ProtoElement() override {} @@ -245,8 +251,6 @@ class PROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { // Set of oneof indices already seen for the type_. Used to validate // incoming messages so no more than one oneof is set. std::vector oneof_indices_; - - GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoElement); }; // Container for inserting 'size' information at the 'pos' position. @@ -261,20 +265,20 @@ class PROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { ProtoElement* element() override { return element_.get(); } // Helper methods for calling ErrorListener. See error_listener.h. - void InvalidName(StringPiece unknown_name, StringPiece message); - void InvalidValue(StringPiece type_name, StringPiece value); - void MissingField(StringPiece missing_name); + void InvalidName(absl::string_view unknown_name, absl::string_view message); + void InvalidValue(absl::string_view type_name, absl::string_view value); + void MissingField(absl::string_view missing_name); // Common code for BeginObject() and BeginList() that does invalid_depth_ // bookkeeping associated with name lookup. - const google::protobuf::Field* BeginNamed(StringPiece name, + const google::protobuf::Field* BeginNamed(absl::string_view name, bool is_list); // Lookup the field in the current element. Looks in the base descriptor // and in any extension. This will report an error if the field cannot be // found when ignore_unknown_names_ is false or if multiple matching // extensions are found. - const google::protobuf::Field* Lookup(StringPiece name); + const google::protobuf::Field* Lookup(absl::string_view name); // Lookup the field type in the type descriptor. Returns nullptr if the type // is not known. @@ -296,7 +300,7 @@ class PROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { // false. It also calls the appropriate error callback. // unnormalized_name is used for error string. bool ValidOneof(const google::protobuf::Field& field, - StringPiece unnormalized_name); + absl::string_view unnormalized_name); // Returns true if the field is repeated. bool IsRepeated(const google::protobuf::Field& field); @@ -316,7 +320,7 @@ class PROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { private: // Writes an ENUM field, including tag, to the stream. - static util::Status WriteEnum(int field_number, const DataPiece& data, + static absl::Status WriteEnum(int field_number, const DataPiece& data, const google::protobuf::Enum* enum_type, io::CodedOutputStream* stream, bool use_lower_camel_for_enums, @@ -375,8 +379,6 @@ class PROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { ErrorListener* listener_; int invalid_depth_; std::unique_ptr tracker_; - - GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoWriter); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/protostream_objectsource.cc b/src/google/protobuf/util/internal/protostream_objectsource.cc index 36b6ed92a493..cc905dc46c54 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource.cc @@ -39,15 +39,17 @@ #include #include #include -#include #include #include #include #include -#include -#include +#include "absl/base/call_once.h" +#include "absl/base/casts.h" +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" #include -#include +#include "absl/strings/string_view.h" +#include "absl/time/time.h" #include #include #include @@ -85,7 +87,7 @@ const google::protobuf::EnumValue* FindEnumValueByNumber( // Utility function to format nanos. std::string FormatNanos(uint32_t nanos, bool with_trailing_zeros); -util::StatusOr MapKeyDefaultValueAsString( +absl::StatusOr MapKeyDefaultValueAsString( const google::protobuf::Field& field) { switch (field.kind()) { case google::protobuf::Field::TYPE_BOOL: @@ -104,7 +106,7 @@ util::StatusOr MapKeyDefaultValueAsString( case google::protobuf::Field::TYPE_STRING: return std::string(); default: - return util::InternalError("Invalid map key type."); + return absl::InternalError("Invalid map key type."); } } } // namespace @@ -142,7 +144,7 @@ ProtoStreamObjectSource::~ProtoStreamObjectSource() { } } -util::Status ProtoStreamObjectSource::NamedWriteTo(StringPiece name, +absl::Status ProtoStreamObjectSource::NamedWriteTo(absl::string_view name, ObjectWriter* ow) const { return WriteMessage(type_, name, 0, true, ow); } @@ -167,8 +169,8 @@ const google::protobuf::Field* ProtoStreamObjectSource::FindAndVerifyField( return field; } -util::Status ProtoStreamObjectSource::WriteMessage( - const google::protobuf::Type& type, StringPiece name, +absl::Status ProtoStreamObjectSource::WriteMessage( + const google::protobuf::Type& type, absl::string_view name, const uint32_t end_tag, bool include_start_and_end, ObjectWriter* ow) const { @@ -228,11 +230,11 @@ util::Status ProtoStreamObjectSource::WriteMessage( if (include_start_and_end) { ow->EndObject(); } - return util::Status(); + return absl::Status(); } -util::StatusOr ProtoStreamObjectSource::RenderList( - const google::protobuf::Field* field, StringPiece name, +absl::StatusOr ProtoStreamObjectSource::RenderList( + const google::protobuf::Field* field, absl::string_view name, uint32_t list_tag, ObjectWriter* ow) const { uint32_t tag_to_return = 0; ow->StartList(name); @@ -253,8 +255,8 @@ util::StatusOr ProtoStreamObjectSource::RenderList( return tag_to_return; } -util::StatusOr ProtoStreamObjectSource::RenderMap( - const google::protobuf::Field* field, StringPiece /* name */, +absl::StatusOr ProtoStreamObjectSource::RenderMap( + const google::protobuf::Field* field, absl::string_view /* name */, uint32_t list_tag, ObjectWriter* ow) const { const google::protobuf::Type* field_type = typeinfo_->GetTypeByTypeUrl(field->type_url()); @@ -284,7 +286,7 @@ util::StatusOr ProtoStreamObjectSource::RenderMap( if (key_field == nullptr) { // The Type info for this map entry is incorrect. It should always // have a field named "key" and with field number 1. - return util::InternalError("Invalid map entry."); + return absl::InternalError("Invalid map entry."); } ASSIGN_OR_RETURN(map_key, MapKeyDefaultValueAsString(*key_field)); } @@ -292,7 +294,7 @@ util::StatusOr ProtoStreamObjectSource::RenderMap( } else { // The Type info for this map entry is incorrect. It should contain // exactly two fields with field number 1 and 2. - return util::InternalError("Invalid map entry."); + return absl::InternalError("Invalid map entry."); } } stream_->PopLimit(old_limit); @@ -300,61 +302,69 @@ util::StatusOr ProtoStreamObjectSource::RenderMap( return tag_to_return; } -util::Status ProtoStreamObjectSource::RenderPacked( +absl::Status ProtoStreamObjectSource::RenderPacked( const google::protobuf::Field* field, ObjectWriter* ow) const { uint32_t length; stream_->ReadVarint32(&length); int old_limit = stream_->PushLimit(length); while (stream_->BytesUntilLimit() > 0) { - RETURN_IF_ERROR(RenderField(field, StringPiece(), ow)); + RETURN_IF_ERROR(RenderField(field, absl::string_view(), ow)); } stream_->PopLimit(old_limit); - return util::Status(); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderTimestamp( +absl::Status ProtoStreamObjectSource::RenderTimestamp( const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { std::pair p = os->ReadSecondsAndNanos(type); int64_t seconds = p.first; int32_t nanos = p.second; if (seconds > kTimestampMaxSeconds || seconds < kTimestampMinSeconds) { - return util::InternalError(StrCat( + return absl::InternalError(absl::StrCat( "Timestamp seconds exceeds limit for field: ", field_name)); } if (nanos < 0 || nanos >= kNanosPerSecond) { - return util::InternalError( - StrCat("Timestamp nanos exceeds limit for field: ", field_name)); + return absl::InternalError( + absl::StrCat("Timestamp nanos exceeds limit for field: ", field_name)); } - ow->RenderString(field_name, - ::google::protobuf::internal::FormatTime(seconds, nanos)); - - return util::Status(); + absl::Time tm = absl::FromUnixSeconds(seconds); + std::string formatted_seconds = + absl::FormatTime(kRfc3339TimeFormat, tm, absl::UTCTimeZone()); + std::string formatted_time = StringPrintf( + "%s%sZ", formatted_seconds.c_str(), + FormatNanos( + nanos, + false + ) + .c_str()); + ow->RenderString(field_name, formatted_time); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderDuration( +absl::Status ProtoStreamObjectSource::RenderDuration( const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { std::pair p = os->ReadSecondsAndNanos(type); int64_t seconds = p.first; int32_t nanos = p.second; if (seconds > kDurationMaxSeconds || seconds < kDurationMinSeconds) { - return util::InternalError( - StrCat("Duration seconds exceeds limit for field: ", field_name)); + return absl::InternalError( + absl::StrCat("Duration seconds exceeds limit for field: ", field_name)); } if (nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) { - return util::InternalError( - StrCat("Duration nanos exceeds limit for field: ", field_name)); + return absl::InternalError( + absl::StrCat("Duration nanos exceeds limit for field: ", field_name)); } std::string sign = ""; if (seconds < 0) { if (nanos > 0) { - return util::InternalError( - StrCat("Duration nanos is non-negative, but seconds is " + return absl::InternalError( + absl::StrCat("Duration nanos is non-negative, but seconds is " "negative for field: ", field_name)); } @@ -373,90 +383,90 @@ util::Status ProtoStreamObjectSource::RenderDuration( ) .c_str()); ow->RenderString(field_name, formatted_duration); - return util::Status(); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderDouble( +absl::Status ProtoStreamObjectSource::RenderDouble( const ProtoStreamObjectSource* os, const google::protobuf::Type& /*type*/, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { uint32_t tag = os->stream_->ReadTag(); uint64_t buffer64 = 0; // default value of Double wrapper value if (tag != 0) { os->stream_->ReadLittleEndian64(&buffer64); os->stream_->ReadTag(); } - ow->RenderDouble(field_name, bit_cast(buffer64)); - return util::Status(); + ow->RenderDouble(field_name, absl::bit_cast(buffer64)); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderFloat( +absl::Status ProtoStreamObjectSource::RenderFloat( const ProtoStreamObjectSource* os, const google::protobuf::Type& /*type*/, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { uint32_t tag = os->stream_->ReadTag(); uint32_t buffer32 = 0; // default value of Float wrapper value if (tag != 0) { os->stream_->ReadLittleEndian32(&buffer32); os->stream_->ReadTag(); } - ow->RenderFloat(field_name, bit_cast(buffer32)); - return util::Status(); + ow->RenderFloat(field_name, absl::bit_cast(buffer32)); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderInt64( +absl::Status ProtoStreamObjectSource::RenderInt64( const ProtoStreamObjectSource* os, const google::protobuf::Type& /*type*/, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { uint32_t tag = os->stream_->ReadTag(); uint64_t buffer64 = 0; // default value of Int64 wrapper value if (tag != 0) { os->stream_->ReadVarint64(&buffer64); os->stream_->ReadTag(); } - ow->RenderInt64(field_name, bit_cast(buffer64)); - return util::Status(); + ow->RenderInt64(field_name, absl::bit_cast(buffer64)); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderUInt64( +absl::Status ProtoStreamObjectSource::RenderUInt64( const ProtoStreamObjectSource* os, const google::protobuf::Type& /*type*/, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { uint32_t tag = os->stream_->ReadTag(); uint64_t buffer64 = 0; // default value of UInt64 wrapper value if (tag != 0) { os->stream_->ReadVarint64(&buffer64); os->stream_->ReadTag(); } - ow->RenderUint64(field_name, bit_cast(buffer64)); - return util::Status(); + ow->RenderUint64(field_name, absl::bit_cast(buffer64)); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderInt32( +absl::Status ProtoStreamObjectSource::RenderInt32( const ProtoStreamObjectSource* os, const google::protobuf::Type& /*type*/, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { uint32_t tag = os->stream_->ReadTag(); uint32_t buffer32 = 0; // default value of Int32 wrapper value if (tag != 0) { os->stream_->ReadVarint32(&buffer32); os->stream_->ReadTag(); } - ow->RenderInt32(field_name, bit_cast(buffer32)); - return util::Status(); + ow->RenderInt32(field_name, absl::bit_cast(buffer32)); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderUInt32( +absl::Status ProtoStreamObjectSource::RenderUInt32( const ProtoStreamObjectSource* os, const google::protobuf::Type& /*type*/, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { uint32_t tag = os->stream_->ReadTag(); uint32_t buffer32 = 0; // default value of UInt32 wrapper value if (tag != 0) { os->stream_->ReadVarint32(&buffer32); os->stream_->ReadTag(); } - ow->RenderUint32(field_name, bit_cast(buffer32)); - return util::Status(); + ow->RenderUint32(field_name, absl::bit_cast(buffer32)); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderBool( +absl::Status ProtoStreamObjectSource::RenderBool( const ProtoStreamObjectSource* os, const google::protobuf::Type& /*type*/, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { uint32_t tag = os->stream_->ReadTag(); uint64_t buffer64 = 0; // results in 'false' value as default, which is the // default value of Bool wrapper @@ -465,12 +475,12 @@ util::Status ProtoStreamObjectSource::RenderBool( os->stream_->ReadTag(); } ow->RenderBool(field_name, buffer64 != 0); - return util::Status(); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderString( +absl::Status ProtoStreamObjectSource::RenderString( const ProtoStreamObjectSource* os, const google::protobuf::Type& /*type*/, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { uint32_t tag = os->stream_->ReadTag(); uint32_t buffer32; std::string str; // default value of empty for String wrapper @@ -480,12 +490,12 @@ util::Status ProtoStreamObjectSource::RenderString( os->stream_->ReadTag(); } ow->RenderString(field_name, str); - return util::Status(); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderBytes( +absl::Status ProtoStreamObjectSource::RenderBytes( const ProtoStreamObjectSource* os, const google::protobuf::Type& /*type*/, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { uint32_t tag = os->stream_->ReadTag(); uint32_t buffer32; std::string str; @@ -495,12 +505,12 @@ util::Status ProtoStreamObjectSource::RenderBytes( os->stream_->ReadTag(); } ow->RenderBytes(field_name, str); - return util::Status(); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderStruct( +absl::Status ProtoStreamObjectSource::RenderStruct( const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { const google::protobuf::Field* field = nullptr; uint32_t tag = os->stream_->ReadTag(); ow->StartObject(field_name); @@ -518,12 +528,12 @@ util::Status ProtoStreamObjectSource::RenderStruct( } } ow->EndObject(); - return util::Status(); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderStructValue( +absl::Status ProtoStreamObjectSource::RenderStructValue( const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { const google::protobuf::Field* field = nullptr; for (uint32_t tag = os->stream_->ReadTag(); tag != 0; tag = os->stream_->ReadTag()) { @@ -534,20 +544,20 @@ util::Status ProtoStreamObjectSource::RenderStructValue( } RETURN_IF_ERROR(os->RenderField(field, field_name, ow)); } - return util::Status(); + return absl::Status(); } // TODO(skarvaje): Avoid code duplication of for loops and SkipField logic. -util::Status ProtoStreamObjectSource::RenderStructListValue( +absl::Status ProtoStreamObjectSource::RenderStructListValue( const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { uint32_t tag = os->stream_->ReadTag(); // Render empty list when we find empty ListValue message. if (tag == 0) { ow->StartList(field_name); ow->EndList(); - return util::Status(); + return absl::Status(); } while (tag != 0) { @@ -559,12 +569,12 @@ util::Status ProtoStreamObjectSource::RenderStructListValue( } ASSIGN_OR_RETURN(tag, os->RenderList(field, field_name, tag, ow)); } - return util::Status(); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderAny( +absl::Status ProtoStreamObjectSource::RenderAny( const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { // An Any is of the form { string type_url = 1; bytes value = 2; } uint32_t tag; std::string type_url; @@ -600,22 +610,22 @@ util::Status ProtoStreamObjectSource::RenderAny( ow->RenderString("@type", type_url); } ow->EndObject(); - return util::Status(); + return absl::Status(); } // If there is a value but no type, we cannot render it, so report an error. if (type_url.empty()) { // TODO(sven): Add an external message once those are ready. - return util::InternalError("Invalid Any, the type_url is missing."); + return absl::InternalError("Invalid Any, the type_url is missing."); } - util::StatusOr resolved_type = + absl::StatusOr resolved_type = os->typeinfo_->ResolveTypeUrl(type_url); if (!resolved_type.ok()) { // Convert into an internal error, since this means the backend gave us // an invalid response (missing or invalid type information). - return util::InternalError(resolved_type.status().message()); + return absl::InternalError(resolved_type.status().message()); } // nested_type cannot be null at this time. const google::protobuf::Type* nested_type = resolved_type.value(); @@ -630,15 +640,15 @@ util::Status ProtoStreamObjectSource::RenderAny( // We manually call start and end object here so we can inject the @type. ow->StartObject(field_name); ow->RenderString("@type", type_url); - util::Status result = + absl::Status result = nested_os.WriteMessage(nested_os.type_, "value", 0, false, ow); ow->EndObject(); return result; } -util::Status ProtoStreamObjectSource::RenderFieldMask( +absl::Status ProtoStreamObjectSource::RenderFieldMask( const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece field_name, ObjectWriter* ow) { + absl::string_view field_name, ObjectWriter* ow) { std::string combined; uint32_t buffer32; uint32_t paths_field_tag = 0; @@ -652,7 +662,7 @@ util::Status ProtoStreamObjectSource::RenderFieldMask( } } if (paths_field_tag != tag) { - return util::InternalError("Invalid FieldMask, unexpected field."); + return absl::InternalError("Invalid FieldMask, unexpected field."); } std::string str; os->stream_->ReadVarint32(&buffer32); // string size. @@ -663,13 +673,13 @@ util::Status ProtoStreamObjectSource::RenderFieldMask( combined.append(ConvertFieldMaskPath(str, &ToCamelCase)); } ow->RenderString(field_name, combined); - return util::Status(); + return absl::Status(); } std::unordered_map* ProtoStreamObjectSource::renderers_ = nullptr; -PROTOBUF_NAMESPACE_ID::internal::once_flag source_renderers_init_; +absl::once_flag source_renderers_init_; void ProtoStreamObjectSource::InitRendererMap() { @@ -717,13 +727,12 @@ void ProtoStreamObjectSource::DeleteRendererMap() { // static ProtoStreamObjectSource::TypeRenderer* ProtoStreamObjectSource::FindTypeRenderer(const std::string& type_url) { - PROTOBUF_NAMESPACE_ID::internal::call_once(source_renderers_init_, - InitRendererMap); + absl::call_once(source_renderers_init_, InitRendererMap); return FindOrNull(*renderers_, type_url); } -util::Status ProtoStreamObjectSource::RenderField( - const google::protobuf::Field* field, StringPiece field_name, +absl::Status ProtoStreamObjectSource::RenderField( + const google::protobuf::Field* field, absl::string_view field_name, ObjectWriter* ow) const { // Short-circuit message types as it tends to call WriteMessage recursively // and ends up using a lot of stack space. Keep the stack usage of this @@ -736,8 +745,8 @@ util::Status ProtoStreamObjectSource::RenderField( const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(field->type_url()); if (type == nullptr) { - return util::InternalError( - StrCat("Invalid configuration. Could not find the type: ", + return absl::InternalError( + absl::StrCat("Invalid configuration. Could not find the type: ", field->type_url())); } @@ -753,7 +762,7 @@ util::Status ProtoStreamObjectSource::RenderField( --recursion_depth_; if (!stream_->ConsumedEntireMessage()) { - return util::InvalidArgumentError( + return absl::InvalidArgumentError( "Nested protocol message not parsed in its entirety."); } stream_->PopLimit(old_limit); @@ -761,11 +770,11 @@ util::Status ProtoStreamObjectSource::RenderField( // Render all other non-message types. return RenderNonMessageField(field, field_name, ow); } - return util::Status(); + return absl::Status(); } -util::Status ProtoStreamObjectSource::RenderNonMessageField( - const google::protobuf::Field* field, StringPiece field_name, +absl::Status ProtoStreamObjectSource::RenderNonMessageField( + const google::protobuf::Field* field, absl::string_view field_name, ObjectWriter* ow) const { // Temporary buffers of different types. uint32_t buffer32 = 0; @@ -779,22 +788,22 @@ util::Status ProtoStreamObjectSource::RenderNonMessageField( } case google::protobuf::Field::TYPE_INT32: { stream_->ReadVarint32(&buffer32); - ow->RenderInt32(field_name, bit_cast(buffer32)); + ow->RenderInt32(field_name, absl::bit_cast(buffer32)); break; } case google::protobuf::Field::TYPE_INT64: { stream_->ReadVarint64(&buffer64); - ow->RenderInt64(field_name, bit_cast(buffer64)); + ow->RenderInt64(field_name, absl::bit_cast(buffer64)); break; } case google::protobuf::Field::TYPE_UINT32: { stream_->ReadVarint32(&buffer32); - ow->RenderUint32(field_name, bit_cast(buffer32)); + ow->RenderUint32(field_name, absl::bit_cast(buffer32)); break; } case google::protobuf::Field::TYPE_UINT64: { stream_->ReadVarint64(&buffer64); - ow->RenderUint64(field_name, bit_cast(buffer64)); + ow->RenderUint64(field_name, absl::bit_cast(buffer64)); break; } case google::protobuf::Field::TYPE_SINT32: { @@ -809,32 +818,32 @@ util::Status ProtoStreamObjectSource::RenderNonMessageField( } case google::protobuf::Field::TYPE_SFIXED32: { stream_->ReadLittleEndian32(&buffer32); - ow->RenderInt32(field_name, bit_cast(buffer32)); + ow->RenderInt32(field_name, absl::bit_cast(buffer32)); break; } case google::protobuf::Field::TYPE_SFIXED64: { stream_->ReadLittleEndian64(&buffer64); - ow->RenderInt64(field_name, bit_cast(buffer64)); + ow->RenderInt64(field_name, absl::bit_cast(buffer64)); break; } case google::protobuf::Field::TYPE_FIXED32: { stream_->ReadLittleEndian32(&buffer32); - ow->RenderUint32(field_name, bit_cast(buffer32)); + ow->RenderUint32(field_name, absl::bit_cast(buffer32)); break; } case google::protobuf::Field::TYPE_FIXED64: { stream_->ReadLittleEndian64(&buffer64); - ow->RenderUint64(field_name, bit_cast(buffer64)); + ow->RenderUint64(field_name, absl::bit_cast(buffer64)); break; } case google::protobuf::Field::TYPE_FLOAT: { stream_->ReadLittleEndian32(&buffer32); - ow->RenderFloat(field_name, bit_cast(buffer32)); + ow->RenderFloat(field_name, absl::bit_cast(buffer32)); break; } case google::protobuf::Field::TYPE_DOUBLE: { stream_->ReadLittleEndian64(&buffer64); - ow->RenderDouble(field_name, bit_cast(buffer64)); + ow->RenderDouble(field_name, absl::bit_cast(buffer64)); break; } case google::protobuf::Field::TYPE_ENUM: { @@ -888,7 +897,7 @@ util::Status ProtoStreamObjectSource::RenderNonMessageField( default: break; } - return util::Status(); + return absl::Status(); } // TODO(skarvaje): Fix this to avoid code duplication. @@ -905,73 +914,73 @@ std::string ProtoStreamObjectSource::ReadFieldValueAsString( case google::protobuf::Field::TYPE_INT32: { uint32_t buffer32; stream_->ReadVarint32(&buffer32); - result = StrCat(bit_cast(buffer32)); + result = absl::StrCat(absl::bit_cast(buffer32)); break; } case google::protobuf::Field::TYPE_INT64: { uint64_t buffer64; stream_->ReadVarint64(&buffer64); - result = StrCat(bit_cast(buffer64)); + result = absl::StrCat(absl::bit_cast(buffer64)); break; } case google::protobuf::Field::TYPE_UINT32: { uint32_t buffer32; stream_->ReadVarint32(&buffer32); - result = StrCat(bit_cast(buffer32)); + result = absl::StrCat(absl::bit_cast(buffer32)); break; } case google::protobuf::Field::TYPE_UINT64: { uint64_t buffer64; stream_->ReadVarint64(&buffer64); - result = StrCat(bit_cast(buffer64)); + result = absl::StrCat(absl::bit_cast(buffer64)); break; } case google::protobuf::Field::TYPE_SINT32: { uint32_t buffer32; stream_->ReadVarint32(&buffer32); - result = StrCat(WireFormatLite::ZigZagDecode32(buffer32)); + result = absl::StrCat(WireFormatLite::ZigZagDecode32(buffer32)); break; } case google::protobuf::Field::TYPE_SINT64: { uint64_t buffer64; stream_->ReadVarint64(&buffer64); - result = StrCat(WireFormatLite::ZigZagDecode64(buffer64)); + result = absl::StrCat(WireFormatLite::ZigZagDecode64(buffer64)); break; } case google::protobuf::Field::TYPE_SFIXED32: { uint32_t buffer32; stream_->ReadLittleEndian32(&buffer32); - result = StrCat(bit_cast(buffer32)); + result = absl::StrCat(absl::bit_cast(buffer32)); break; } case google::protobuf::Field::TYPE_SFIXED64: { uint64_t buffer64; stream_->ReadLittleEndian64(&buffer64); - result = StrCat(bit_cast(buffer64)); + result = absl::StrCat(absl::bit_cast(buffer64)); break; } case google::protobuf::Field::TYPE_FIXED32: { uint32_t buffer32; stream_->ReadLittleEndian32(&buffer32); - result = StrCat(bit_cast(buffer32)); + result = absl::StrCat(absl::bit_cast(buffer32)); break; } case google::protobuf::Field::TYPE_FIXED64: { uint64_t buffer64; stream_->ReadLittleEndian64(&buffer64); - result = StrCat(bit_cast(buffer64)); + result = absl::StrCat(absl::bit_cast(buffer64)); break; } case google::protobuf::Field::TYPE_FLOAT: { uint32_t buffer32; stream_->ReadLittleEndian32(&buffer32); - result = SimpleFtoa(bit_cast(buffer32)); + result = SimpleFtoa(absl::bit_cast(buffer32)); break; } case google::protobuf::Field::TYPE_DOUBLE: { uint64_t buffer64; stream_->ReadLittleEndian64(&buffer64); - result = SimpleDtoa(bit_cast(buffer64)); + result = SimpleDtoa(absl::bit_cast(buffer64)); break; } case google::protobuf::Field::TYPE_ENUM: { @@ -1039,24 +1048,24 @@ std::pair ProtoStreamObjectSource::ReadSecondsAndNanos( if (field->number() == 1) { // read seconds stream_->ReadVarint64(&seconds); - signed_seconds = bit_cast(seconds); + signed_seconds = absl::bit_cast(seconds); } else if (field->number() == 2) { // read nanos stream_->ReadVarint32(&nanos); - signed_nanos = bit_cast(nanos); + signed_nanos = absl::bit_cast(nanos); } } return std::pair(signed_seconds, signed_nanos); } -util::Status ProtoStreamObjectSource::IncrementRecursionDepth( - StringPiece type_name, StringPiece field_name) const { +absl::Status ProtoStreamObjectSource::IncrementRecursionDepth( + absl::string_view type_name, absl::string_view field_name) const { if (++recursion_depth_ > max_recursion_depth_) { - return util::InvalidArgumentError( - StrCat("Message too deep. Max recursion depth reached for type '", + return absl::InvalidArgumentError( + absl::StrCat("Message too deep. Max recursion depth reached for type '", type_name, "', field '", field_name, "'")); } - return util::Status(); + return absl::Status(); } namespace { diff --git a/src/google/protobuf/util/internal/protostream_objectsource.h b/src/google/protobuf/util/internal/protostream_objectsource.h index 17bb702bb5c1..377c6bdeaadb 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.h +++ b/src/google/protobuf/util/internal/protostream_objectsource.h @@ -36,18 +36,16 @@ #include #include -#include #include #include -#include -#include +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" #include #include #include #include -#include -#include // Must be included last. @@ -120,10 +118,12 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { TypeResolver* type_resolver, const google::protobuf::Type& type, const RenderOptions& render_options); - + ProtoStreamObjectSource() = delete; + ProtoStreamObjectSource(const ProtoStreamObjectSource&) = delete; + ProtoStreamObjectSource& operator=(const ProtoStreamObjectSource&) = delete; ~ProtoStreamObjectSource() override; - util::Status NamedWriteTo(StringPiece name, + absl::Status NamedWriteTo(absl::string_view name, ObjectWriter* ow) const override; // Sets the max recursion depth of proto message to be deserialized. Proto @@ -139,8 +139,8 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { // nested messages (end with 0) and nested groups (end with group end tag). // The include_start_and_end parameter allows this method to be called when // already inside of an object, and skip calling StartObject and EndObject. - virtual util::Status WriteMessage(const google::protobuf::Type& type, - StringPiece name, + virtual absl::Status WriteMessage(const google::protobuf::Type& type, + absl::string_view name, const uint32_t end_tag, bool include_start_and_end, ObjectWriter* ow) const; @@ -148,8 +148,8 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { // Renders a repeating field (packed or unpacked). Returns the next tag after // reading all sequential repeating elements. The caller should use this tag // before reading more tags from the stream. - virtual util::StatusOr RenderList( - const google::protobuf::Field* field, StringPiece name, + virtual absl::StatusOr RenderList( + const google::protobuf::Field* field, absl::string_view name, uint32_t list_tag, ObjectWriter* ow) const; // Looks up a field and verify its consistency with wire type in tag. @@ -157,8 +157,8 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { const google::protobuf::Type& type, uint32_t tag) const; // Renders a field value to the ObjectWriter. - virtual util::Status RenderField(const google::protobuf::Field* field, - StringPiece field_name, + virtual absl::Status RenderField(const google::protobuf::Field* field, + absl::string_view field_name, ObjectWriter* ow) const; // Reads field value according to Field spec in 'field' and returns the read @@ -177,9 +177,9 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { const google::protobuf::Type& type, const RenderOptions& render_options); // Function that renders a well known type with a modified behavior. - typedef util::Status (*TypeRenderer)(const ProtoStreamObjectSource*, + typedef absl::Status (*TypeRenderer)(const ProtoStreamObjectSource*, const google::protobuf::Type&, - StringPiece, ObjectWriter*); + absl::string_view, ObjectWriter*); // TODO(skarvaje): Mark these methods as non-const as they modify internal // state (stream_). @@ -187,82 +187,82 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { // Renders a NWP map. // Returns the next tag after reading all map entries. The caller should use // this tag before reading more tags from the stream. - util::StatusOr RenderMap(const google::protobuf::Field* field, - StringPiece name, uint32_t list_tag, + absl::StatusOr RenderMap(const google::protobuf::Field* field, + absl::string_view name, uint32_t list_tag, ObjectWriter* ow) const; // Renders a packed repeating field. A packed field is stored as: // {tag length item1 item2 item3} instead of the less efficient // {tag item1 tag item2 tag item3}. - util::Status RenderPacked(const google::protobuf::Field* field, + absl::Status RenderPacked(const google::protobuf::Field* field, ObjectWriter* ow) const; // Renders a google.protobuf.Timestamp value to ObjectWriter - static util::Status RenderTimestamp(const ProtoStreamObjectSource* os, + static absl::Status RenderTimestamp(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); + absl::string_view name, ObjectWriter* ow); // Renders a google.protobuf.Duration value to ObjectWriter - static util::Status RenderDuration(const ProtoStreamObjectSource* os, + static absl::Status RenderDuration(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); + absl::string_view name, ObjectWriter* ow); // Following RenderTYPE functions render well known types in // google/protobuf/wrappers.proto corresponding to TYPE. - static util::Status RenderDouble(const ProtoStreamObjectSource* os, + static absl::Status RenderDouble(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); - static util::Status RenderFloat(const ProtoStreamObjectSource* os, + absl::string_view name, ObjectWriter* ow); + static absl::Status RenderFloat(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); - static util::Status RenderInt64(const ProtoStreamObjectSource* os, + absl::string_view name, ObjectWriter* ow); + static absl::Status RenderInt64(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); - static util::Status RenderUInt64(const ProtoStreamObjectSource* os, + absl::string_view name, ObjectWriter* ow); + static absl::Status RenderUInt64(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); - static util::Status RenderInt32(const ProtoStreamObjectSource* os, + absl::string_view name, ObjectWriter* ow); + static absl::Status RenderInt32(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); - static util::Status RenderUInt32(const ProtoStreamObjectSource* os, + absl::string_view name, ObjectWriter* ow); + static absl::Status RenderUInt32(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); - static util::Status RenderBool(const ProtoStreamObjectSource* os, + absl::string_view name, ObjectWriter* ow); + static absl::Status RenderBool(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); - static util::Status RenderString(const ProtoStreamObjectSource* os, + absl::string_view name, ObjectWriter* ow); + static absl::Status RenderString(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); - static util::Status RenderBytes(const ProtoStreamObjectSource* os, + absl::string_view name, ObjectWriter* ow); + static absl::Status RenderBytes(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); + absl::string_view name, ObjectWriter* ow); // Renders a google.protobuf.Struct to ObjectWriter. - static util::Status RenderStruct(const ProtoStreamObjectSource* os, + static absl::Status RenderStruct(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); + absl::string_view name, ObjectWriter* ow); // Helper to render google.protobuf.Struct's Value fields to ObjectWriter. - static util::Status RenderStructValue(const ProtoStreamObjectSource* os, + static absl::Status RenderStructValue(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, + absl::string_view name, ObjectWriter* ow); // Helper to render google.protobuf.Struct's ListValue fields to ObjectWriter. - static util::Status RenderStructListValue(const ProtoStreamObjectSource* os, + static absl::Status RenderStructListValue(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, + absl::string_view name, ObjectWriter* ow); // Render the "Any" type. - static util::Status RenderAny(const ProtoStreamObjectSource* os, + static absl::Status RenderAny(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); + absl::string_view name, ObjectWriter* ow); // Render the "FieldMask" type. - static util::Status RenderFieldMask(const ProtoStreamObjectSource* os, + static absl::Status RenderFieldMask(const ProtoStreamObjectSource* os, const google::protobuf::Type& type, - StringPiece name, ObjectWriter* ow); + absl::string_view name, ObjectWriter* ow); static std::unordered_map* renderers_; static void InitRendererMap(); @@ -271,8 +271,8 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { // Same as above but renders all non-message field types. Callers don't call // this function directly. They just use RenderField. - util::Status RenderNonMessageField(const google::protobuf::Field* field, - StringPiece field_name, + absl::Status RenderNonMessageField(const google::protobuf::Field* field, + absl::string_view field_name, ObjectWriter* ow) const; @@ -287,8 +287,8 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { // Helper function to check recursion depth and increment it. It will return // OkStatus() if the current depth is allowed. Otherwise an error is returned. // type_name and field_name are used for error reporting. - util::Status IncrementRecursionDepth(StringPiece type_name, - StringPiece field_name) const; + absl::Status IncrementRecursionDepth(absl::string_view type_name, + absl::string_view field_name) const; // Input stream to read from. Ownership rests with the caller. mutable io::CodedInputStream* stream_; @@ -312,8 +312,6 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { // Maximum allowed recursion depth. int max_recursion_depth_; - - GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoStreamObjectSource); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/protostream_objectsource_test.cc b/src/google/protobuf/util/internal/protostream_objectsource_test.cc index 95e5f1633eea..e36785ff256c 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource_test.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource_test.cc @@ -40,8 +40,9 @@ #include #include #include -#include -#include +#include "absl/base/casts.h" +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" #include #include #include @@ -106,11 +107,11 @@ class ProtostreamObjectSourceTest ~ProtostreamObjectSourceTest() override {} void DoTest(const Message& msg, const Descriptor* descriptor) { - util::Status status = ExecuteTest(msg, descriptor); - EXPECT_EQ(util::Status(), status); + absl::Status status = ExecuteTest(msg, descriptor); + EXPECT_EQ(absl::Status(), status); } - util::Status ExecuteTest(const Message& msg, const Descriptor* descriptor) { + absl::Status ExecuteTest(const Message& msg, const Descriptor* descriptor) { std::ostringstream oss; msg.SerializePartialToOstream(&oss); std::string proto = oss.str(); @@ -130,13 +131,13 @@ class ProtostreamObjectSourceTest void PrepareExpectingObjectWriterForRepeatedPrimitive() { ow_.StartObject("") ->StartList("repFix32") - ->RenderUint32("", bit_cast(3201)) - ->RenderUint32("", bit_cast(0)) - ->RenderUint32("", bit_cast(3202)) + ->RenderUint32("", absl::bit_cast(3201)) + ->RenderUint32("", absl::bit_cast(0)) + ->RenderUint32("", absl::bit_cast(3202)) ->EndList() ->StartList("repU32") - ->RenderUint32("", bit_cast(3203)) - ->RenderUint32("", bit_cast(0)) + ->RenderUint32("", absl::bit_cast(3203)) + ->RenderUint32("", absl::bit_cast(0)) ->EndList() ->StartList("repI32") ->RenderInt32("", 0) @@ -153,13 +154,13 @@ class ProtostreamObjectSourceTest ->RenderInt32("", 3208) ->EndList() ->StartList("repFix64") - ->RenderUint64("", bit_cast(int64_t{6401})) - ->RenderUint64("", bit_cast(int64_t{0})) + ->RenderUint64("", absl::bit_cast(int64_t{6401})) + ->RenderUint64("", absl::bit_cast(int64_t{0})) ->EndList() ->StartList("repU64") - ->RenderUint64("", bit_cast(int64_t{0})) - ->RenderUint64("", bit_cast(int64_t{6402})) - ->RenderUint64("", bit_cast(int64_t{6403})) + ->RenderUint64("", absl::bit_cast(int64_t{0})) + ->RenderUint64("", absl::bit_cast(int64_t{6402})) + ->RenderUint64("", absl::bit_cast(int64_t{6403})) ->EndList() ->StartList("repI64") ->RenderInt64("", 6404L) @@ -321,13 +322,13 @@ TEST_P(ProtostreamObjectSourceTest, Primitives) { primitive.set_bool_(true); ow_.StartObject("") - ->RenderUint32("fix32", bit_cast(3201)) - ->RenderUint32("u32", bit_cast(3202)) + ->RenderUint32("fix32", absl::bit_cast(3201)) + ->RenderUint32("u32", absl::bit_cast(3202)) ->RenderInt32("i32", 3203) ->RenderInt32("sf32", 3204) ->RenderInt32("s32", 3205) - ->RenderUint64("fix64", bit_cast(int64_t{6401})) - ->RenderUint64("u64", bit_cast(int64_t{6402})) + ->RenderUint64("fix64", absl::bit_cast(int64_t{6401})) + ->RenderUint64("u64", absl::bit_cast(int64_t{6402})) ->RenderInt64("i64", 6403L) ->RenderInt64("sf64", 6404L) ->RenderInt64("s64", 6405L) @@ -478,8 +479,8 @@ TEST_P(ProtostreamObjectSourceTest, int repeat = 10000; for (int i = 0; i < repeat; ++i) { Book_Label* label = book.add_labels(); - label->set_key(StrCat("i", i)); - label->set_value(StrCat("v", i)); + label->set_key(absl::StrCat("i", i)); + label->set_value(absl::StrCat("v", i)); } // Make sure StartList and EndList are called exactly once (see b/18227499 for @@ -584,7 +585,7 @@ TEST_P(ProtostreamObjectSourceTest, CyclicMessageDepthTest) { for (int i = 0; i < 63; ++i) { Author* next = current_author->add_friend_(); next->set_id(i); - next->set_name(StrCat("author_name_", i)); + next->set_name(absl::StrCat("author_name_", i)); next->set_alive(true); current_author = next; } @@ -592,12 +593,12 @@ TEST_P(ProtostreamObjectSourceTest, CyclicMessageDepthTest) { // Recursive message with depth (65) > max (max is 64). for (int i = 0; i < 64; ++i) { Cyclic* next = current->mutable_m_cyclic(); - next->set_m_str(StrCat("count_", i)); + next->set_m_str(absl::StrCat("count_", i)); current = next; } - util::Status status = ExecuteTest(cyclic, Cyclic::descriptor()); - EXPECT_TRUE(util::IsInvalidArgument(status)); + absl::Status status = ExecuteTest(cyclic, Cyclic::descriptor()); + EXPECT_TRUE(absl::IsInvalidArgument(status)); } class ProtostreamObjectSourceMapsTest : public ProtostreamObjectSourceTest { @@ -942,8 +943,8 @@ TEST_P(ProtostreamObjectSourceAnysTest, MissingTypeUrlError) { // We start the "AnyOut" part and then fail when we hit the Any object. ow_.StartObject(""); - util::Status status = ExecuteTest(out, AnyOut::descriptor()); - EXPECT_TRUE(util::IsInternal(status)); + absl::Status status = ExecuteTest(out, AnyOut::descriptor()); + EXPECT_TRUE(absl::IsInternal(status)); } TEST_P(ProtostreamObjectSourceAnysTest, UnknownTypeServiceError) { @@ -958,8 +959,8 @@ TEST_P(ProtostreamObjectSourceAnysTest, UnknownTypeServiceError) { // We start the "AnyOut" part and then fail when we hit the Any object. ow_.StartObject(""); - util::Status status = ExecuteTest(out, AnyOut::descriptor()); - EXPECT_TRUE(util::IsInternal(status)); + absl::Status status = ExecuteTest(out, AnyOut::descriptor()); + EXPECT_TRUE(absl::IsInternal(status)); } TEST_P(ProtostreamObjectSourceAnysTest, UnknownTypeError) { @@ -974,8 +975,8 @@ TEST_P(ProtostreamObjectSourceAnysTest, UnknownTypeError) { // We start the "AnyOut" part and then fail when we hit the Any object. ow_.StartObject(""); - util::Status status = ExecuteTest(out, AnyOut::descriptor()); - EXPECT_TRUE(util::IsInternal(status)); + absl::Status status = ExecuteTest(out, AnyOut::descriptor()); + EXPECT_TRUE(absl::IsInternal(status)); } class ProtostreamObjectSourceStructTest : public ProtostreamObjectSourceTest { @@ -1107,8 +1108,8 @@ TEST_P(ProtostreamObjectSourceTimestampTest, InvalidTimestampBelowMinTest) { ts->set_seconds(kTimestampMinSeconds - 1); ow_.StartObject(""); - util::Status status = ExecuteTest(out, TimestampDuration::descriptor()); - EXPECT_TRUE(util::IsInternal(status)); + absl::Status status = ExecuteTest(out, TimestampDuration::descriptor()); + EXPECT_TRUE(absl::IsInternal(status)); } TEST_P(ProtostreamObjectSourceTimestampTest, InvalidTimestampAboveMaxTest) { @@ -1118,8 +1119,8 @@ TEST_P(ProtostreamObjectSourceTimestampTest, InvalidTimestampAboveMaxTest) { ts->set_seconds(kTimestampMaxSeconds + 1); ow_.StartObject(""); - util::Status status = ExecuteTest(out, TimestampDuration::descriptor()); - EXPECT_TRUE(util::IsInternal(status)); + absl::Status status = ExecuteTest(out, TimestampDuration::descriptor()); + EXPECT_TRUE(absl::IsInternal(status)); } TEST_P(ProtostreamObjectSourceTimestampTest, InvalidDurationBelowMinTest) { @@ -1129,8 +1130,8 @@ TEST_P(ProtostreamObjectSourceTimestampTest, InvalidDurationBelowMinTest) { dur->set_seconds(kDurationMinSeconds - 1); ow_.StartObject(""); - util::Status status = ExecuteTest(out, TimestampDuration::descriptor()); - EXPECT_TRUE(util::IsInternal(status)); + absl::Status status = ExecuteTest(out, TimestampDuration::descriptor()); + EXPECT_TRUE(absl::IsInternal(status)); } TEST_P(ProtostreamObjectSourceTimestampTest, InvalidDurationAboveMaxTest) { @@ -1140,8 +1141,8 @@ TEST_P(ProtostreamObjectSourceTimestampTest, InvalidDurationAboveMaxTest) { dur->set_seconds(kDurationMaxSeconds + 1); ow_.StartObject(""); - util::Status status = ExecuteTest(out, TimestampDuration::descriptor()); - EXPECT_TRUE(util::IsInternal(status)); + absl::Status status = ExecuteTest(out, TimestampDuration::descriptor()); + EXPECT_TRUE(absl::IsInternal(status)); } TEST_P(ProtostreamObjectSourceTimestampTest, TimestampDurationDefaultValue) { diff --git a/src/google/protobuf/util/internal/protostream_objectwriter.cc b/src/google/protobuf/util/internal/protostream_objectwriter.cc index ecb219e06e51..11c97665031c 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter.cc +++ b/src/google/protobuf/util/internal/protostream_objectwriter.cc @@ -36,12 +36,14 @@ #include #include -#include #include #include -#include -#include -#include +#include "absl/base/call_once.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/strip.h" +#include "absl/time/time.h" #include #include #include @@ -57,7 +59,7 @@ namespace protobuf { namespace util { namespace converter { -using util::Status; +using ::absl::Status; using ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite; using std::placeholders::_1; @@ -115,19 +117,19 @@ ProtoStreamObjectWriter::~ProtoStreamObjectWriter() { namespace { // Utility method to split a string representation of Timestamp or Duration and // return the parts. -void SplitSecondsAndNanos(StringPiece input, StringPiece* seconds, - StringPiece* nanos) { +void SplitSecondsAndNanos(absl::string_view input, absl::string_view* seconds, + absl::string_view* nanos) { size_t idx = input.rfind('.'); if (idx != std::string::npos) { *seconds = input.substr(0, idx); *nanos = input.substr(idx + 1); } else { *seconds = input; - *nanos = StringPiece(); + *nanos = absl::string_view(); } } -Status GetNanosFromStringPiece(StringPiece s_nanos, +Status GetNanosFromStringPiece(absl::string_view s_nanos, const char* parse_failure_message, const char* exceeded_limit_message, int32_t* nanos) { @@ -135,7 +137,7 @@ Status GetNanosFromStringPiece(StringPiece s_nanos, // Count the number of leading 0s and consume them. int num_leading_zeros = 0; - while (s_nanos.Consume("0")) { + while (absl::ConsumePrefix(&s_nanos, "0")) { num_leading_zeros++; } int32_t i_nanos = 0; @@ -144,14 +146,14 @@ Status GetNanosFromStringPiece(StringPiece s_nanos, // conversion to 'nanos', rather than a double, so that there is no // loss of precision. if (!s_nanos.empty() && !safe_strto32(s_nanos, &i_nanos)) { - return util::InvalidArgumentError(parse_failure_message); + return absl::InvalidArgumentError(parse_failure_message); } if (i_nanos > kNanosPerSecond || i_nanos < 0) { - return util::InvalidArgumentError(exceeded_limit_message); + return absl::InvalidArgumentError(exceeded_limit_message); } // s_nanos should only have digits. No whitespace. - if (s_nanos.find_first_not_of("0123456789") != StringPiece::npos) { - return util::InvalidArgumentError(parse_failure_message); + if (s_nanos.find_first_not_of("0123456789") != absl::string_view::npos) { + return absl::InvalidArgumentError(parse_failure_message); } if (i_nanos > 0) { @@ -191,7 +193,7 @@ Status GetNanosFromStringPiece(StringPiece s_nanos, conversion = 1; break; default: - return util::InvalidArgumentError(exceeded_limit_message); + return absl::InvalidArgumentError(exceeded_limit_message); } *nanos = i_nanos * conversion; } @@ -199,6 +201,26 @@ Status GetNanosFromStringPiece(StringPiece s_nanos, return Status(); } +// If successful, stores the offset in seconds in "value" and returns true. +// Caller must ensure the first character of "offset" is "+" or "-". +bool ParseTimezoneOffset(absl::string_view offset, int* value) { + GOOGLE_DCHECK(offset[0] == '+' || offset[0] == '-'); + // Format of the offset: +DD:DD or -DD:DD. E.g., +08:00. + if (offset.length() != 6 || offset[3] != ':') { + return false; + } + int hours = 0, minutes = 0; + if (!safe_strto32(offset.substr(1, 2), &hours) || + !safe_strto32(offset.substr(4, 2), &minutes) || hours < 0 || + hours >= 24 || minutes < 0 || minutes >= 60) { + return false; + } + *value = (hours * 60 + minutes) * 60; + if (offset[0] == '-') { + *value = -*value; + } + return true; +} } // namespace ProtoStreamObjectWriter::AnyWriter::AnyWriter(ProtoStreamObjectWriter* parent) @@ -213,7 +235,7 @@ ProtoStreamObjectWriter::AnyWriter::AnyWriter(ProtoStreamObjectWriter* parent) ProtoStreamObjectWriter::AnyWriter::~AnyWriter() {} -void ProtoStreamObjectWriter::AnyWriter::StartObject(StringPiece name) { +void ProtoStreamObjectWriter::AnyWriter::StartObject(absl::string_view name) { ++depth_; // If an object writer is absent, that means we have not called StartAny() // before reaching here, which happens when we have data before the "@type" @@ -260,7 +282,7 @@ bool ProtoStreamObjectWriter::AnyWriter::EndObject() { return true; } -void ProtoStreamObjectWriter::AnyWriter::StartList(StringPiece name) { +void ProtoStreamObjectWriter::AnyWriter::StartList(absl::string_view name) { ++depth_; if (ow_ == nullptr) { // Save data before the "@type" field for later replay. @@ -292,7 +314,7 @@ void ProtoStreamObjectWriter::AnyWriter::EndList() { } void ProtoStreamObjectWriter::AnyWriter::RenderDataPiece( - StringPiece name, const DataPiece& value) { + absl::string_view name, const DataPiece& value) { // Start an Any only at depth_ 0. Other RenderDataPiece calls with "@type" // should go to the contained ow_ as they indicate nested Anys. if (depth_ == 0 && ow_ == nullptr && name == "@type") { @@ -330,7 +352,7 @@ void ProtoStreamObjectWriter::AnyWriter::StartAny(const DataPiece& value) { if (value.type() == DataPiece::TYPE_STRING) { type_url_ = std::string(value.str()); } else { - util::StatusOr s = value.ToString(); + absl::StatusOr s = value.ToString(); if (!s.ok()) { parent_->InvalidValue("String", s.status().message()); invalid_ = true; @@ -339,7 +361,7 @@ void ProtoStreamObjectWriter::AnyWriter::StartAny(const DataPiece& value) { type_url_ = s.value(); } // Resolve the type url, and report an error if we failed to resolve it. - util::StatusOr resolved_type = + absl::StatusOr resolved_type = parent_->typeinfo()->ResolveTypeUrl(type_url_); if (!resolved_type.ok()) { parent_->InvalidValue("Any", resolved_type.status().message()); @@ -393,7 +415,7 @@ void ProtoStreamObjectWriter::AnyWriter::WriteAny() { // There are uninterpreted data, but we never got a "@type" field. if (!invalid_) { parent_->InvalidValue("Any", - StrCat("Missing @type for any field in ", + absl::StrCat("Missing @type for any field in ", parent_->master_type_.name())); invalid_ = true; } @@ -434,7 +456,7 @@ void ProtoStreamObjectWriter::AnyWriter::Event::DeepCopy() { // string value stays valid, we make a copy of the string value and update // DataPiece to reference our own copy. if (value_.type() == DataPiece::TYPE_STRING) { - StrAppend(&value_storage_, value_.str()); + absl::StrAppend(&value_storage_, value_.str()); value_ = DataPiece(value_storage_, value_.use_strict_base64_decoding()); } else if (value_.type() == DataPiece::TYPE_BYTES) { value_storage_ = value_.ToBytes().value(); @@ -478,13 +500,13 @@ ProtoStreamObjectWriter::Item::Item(ProtoStreamObjectWriter::Item* parent, } bool ProtoStreamObjectWriter::Item::InsertMapKeyIfNotPresent( - StringPiece map_key) { + absl::string_view map_key) { return InsertIfNotPresent(map_keys_.get(), std::string(map_key)); } ProtoStreamObjectWriter* ProtoStreamObjectWriter::StartObject( - StringPiece name) { + absl::string_view name) { if (invalid_depth() > 0) { IncrementInvalidDepth(); return this; @@ -680,7 +702,7 @@ ProtoStreamObjectWriter* ProtoStreamObjectWriter::EndObject() { ProtoStreamObjectWriter* ProtoStreamObjectWriter::StartList( - StringPiece name) { + absl::string_view name) { if (invalid_depth() > 0) { IncrementInvalidDepth(); return this; @@ -788,7 +810,7 @@ ProtoStreamObjectWriter* ProtoStreamObjectWriter::StartList( } // Report an error. - InvalidValue("Map", StrCat("Cannot have repeated items ('", name, + InvalidValue("Map", absl::StrCat("Cannot have repeated items ('", name, "') within a map.")); return this; } @@ -885,7 +907,7 @@ ProtoStreamObjectWriter* ProtoStreamObjectWriter::StartList( Push(name, Item::MESSAGE, false, true); return this; } - InvalidValue("Map", StrCat("Cannot bind a list to map for field '", + InvalidValue("Map", absl::StrCat("Cannot bind a list to map for field '", name, "'.")); IncrementInvalidDepth(); return this; @@ -921,7 +943,7 @@ Status ProtoStreamObjectWriter::RenderStructValue(ProtoStreamObjectWriter* ow, switch (data.type()) { case DataPiece::TYPE_INT32: { if (ow->options_.struct_integers_as_strings) { - util::StatusOr int_value = data.ToInt32(); + absl::StatusOr int_value = data.ToInt32(); if (int_value.ok()) { ow->ProtoWriter::RenderDataPiece( "string_value", @@ -934,7 +956,7 @@ Status ProtoStreamObjectWriter::RenderStructValue(ProtoStreamObjectWriter* ow, } case DataPiece::TYPE_UINT32: { if (ow->options_.struct_integers_as_strings) { - util::StatusOr int_value = data.ToUint32(); + absl::StatusOr int_value = data.ToUint32(); if (int_value.ok()) { ow->ProtoWriter::RenderDataPiece( "string_value", @@ -949,10 +971,10 @@ Status ProtoStreamObjectWriter::RenderStructValue(ProtoStreamObjectWriter* ow, // If the option to treat integers as strings is set, then render them as // strings. Otherwise, fallback to rendering them as double. if (ow->options_.struct_integers_as_strings) { - util::StatusOr int_value = data.ToInt64(); + absl::StatusOr int_value = data.ToInt64(); if (int_value.ok()) { ow->ProtoWriter::RenderDataPiece( - "string_value", DataPiece(StrCat(int_value.value()), true)); + "string_value", DataPiece(absl::StrCat(int_value.value()), true)); return Status(); } } @@ -963,10 +985,10 @@ Status ProtoStreamObjectWriter::RenderStructValue(ProtoStreamObjectWriter* ow, // If the option to treat integers as strings is set, then render them as // strings. Otherwise, fallback to rendering them as double. if (ow->options_.struct_integers_as_strings) { - util::StatusOr int_value = data.ToUint64(); + absl::StatusOr int_value = data.ToUint64(); if (int_value.ok()) { ow->ProtoWriter::RenderDataPiece( - "string_value", DataPiece(StrCat(int_value.value()), true)); + "string_value", DataPiece(absl::StrCat(int_value.value()), true)); return Status(); } } @@ -975,7 +997,7 @@ Status ProtoStreamObjectWriter::RenderStructValue(ProtoStreamObjectWriter* ow, } case DataPiece::TYPE_FLOAT: { if (ow->options_.struct_integers_as_strings) { - util::StatusOr float_value = data.ToFloat(); + absl::StatusOr float_value = data.ToFloat(); if (float_value.ok()) { ow->ProtoWriter::RenderDataPiece( "string_value", @@ -988,7 +1010,7 @@ Status ProtoStreamObjectWriter::RenderStructValue(ProtoStreamObjectWriter* ow, } case DataPiece::TYPE_DOUBLE: { if (ow->options_.struct_integers_as_strings) { - util::StatusOr double_value = data.ToDouble(); + absl::StatusOr double_value = data.ToDouble(); if (double_value.ok()) { ow->ProtoWriter::RenderDataPiece( "string_value", @@ -1012,7 +1034,7 @@ Status ProtoStreamObjectWriter::RenderStructValue(ProtoStreamObjectWriter* ow, break; } default: { - return util::InvalidArgumentError( + return absl::InvalidArgumentError( "Invalid struct data type. Only number, string, boolean or null " "values are supported."); } @@ -1025,28 +1047,57 @@ Status ProtoStreamObjectWriter::RenderTimestamp(ProtoStreamObjectWriter* ow, const DataPiece& data) { if (data.type() == DataPiece::TYPE_NULL) return Status(); if (data.type() != DataPiece::TYPE_STRING) { - return util::InvalidArgumentError( - StrCat("Invalid data type for timestamp, value is ", + return absl::InvalidArgumentError( + absl::StrCat("Invalid data type for timestamp, value is ", data.ValueAsStringOrDefault(""))); } - StringPiece value(data.str()); + absl::string_view value(data.str()); - int64_t seconds; - int32_t nanos; - if (!::google::protobuf::internal::ParseTime(value.ToString(), &seconds, - &nanos)) { - return util::InvalidArgumentError(StrCat("Invalid time format: ", value)); + int timezone_offset_seconds = 0; + if (HasSuffixString(value, "Z")) { + value = value.substr(0, value.size() - 1); + } else { + size_t pos = value.find_last_of("+-"); + if (pos == std::string::npos || + !ParseTimezoneOffset(value.substr(pos), &timezone_offset_seconds)) { + return Status(absl::StatusCode::kInvalidArgument, + "Illegal timestamp format; timestamps must end with 'Z' " + "or have a valid timezone offset."); + } + value = value.substr(0, pos); } + absl::string_view s_secs, s_nanos; + SplitSecondsAndNanos(value, &s_secs, &s_nanos); + absl::Time tm; + std::string err; + if (!absl::ParseTime(kRfc3339TimeFormatNoPadding, s_secs, &tm, &err)) { + return Status(absl::StatusCode::kInvalidArgument, + absl::StrCat("Invalid time format: ", err)); + } + + int32_t nanos = 0; + Status nanos_status = GetNanosFromStringPiece( + s_nanos, "Invalid time format, failed to parse nano seconds", + "Timestamp value exceeds limits", &nanos); + if (!nanos_status.ok()) { + return nanos_status; + } + + int64_t seconds = absl::ToUnixSeconds(tm) - timezone_offset_seconds; + if (seconds > kTimestampMaxSeconds || seconds < kTimestampMinSeconds) { + return Status(absl::StatusCode::kInvalidArgument, + "Timestamp value exceeds limits"); + } ow->ProtoWriter::RenderDataPiece("seconds", DataPiece(seconds)); ow->ProtoWriter::RenderDataPiece("nanos", DataPiece(nanos)); return Status(); } -static inline util::Status RenderOneFieldPath(ProtoStreamObjectWriter* ow, - StringPiece path) { +static inline absl::Status RenderOneFieldPath(ProtoStreamObjectWriter* ow, + absl::string_view path) { ow->ProtoWriter::RenderDataPiece( "paths", DataPiece(ConvertFieldMaskPath(path, &ToSnakeCase), true)); return Status(); @@ -1056,8 +1107,8 @@ Status ProtoStreamObjectWriter::RenderFieldMask(ProtoStreamObjectWriter* ow, const DataPiece& data) { if (data.type() == DataPiece::TYPE_NULL) return Status(); if (data.type() != DataPiece::TYPE_STRING) { - return util::InvalidArgumentError( - StrCat("Invalid data type for field mask, value is ", + return absl::InvalidArgumentError( + absl::StrCat("Invalid data type for field mask, value is ", data.ValueAsStringOrDefault(""))); } @@ -1072,15 +1123,15 @@ Status ProtoStreamObjectWriter::RenderDuration(ProtoStreamObjectWriter* ow, const DataPiece& data) { if (data.type() == DataPiece::TYPE_NULL) return Status(); if (data.type() != DataPiece::TYPE_STRING) { - return util::InvalidArgumentError( - StrCat("Invalid data type for duration, value is ", + return absl::InvalidArgumentError( + absl::StrCat("Invalid data type for duration, value is ", data.ValueAsStringOrDefault(""))); } - StringPiece value(data.str()); + absl::string_view value(data.str()); if (!HasSuffixString(value, "s")) { - return util::InvalidArgumentError( + return absl::InvalidArgumentError( "Illegal duration format; duration must end with 's'"); } value = value.substr(0, value.size() - 1); @@ -1090,11 +1141,11 @@ Status ProtoStreamObjectWriter::RenderDuration(ProtoStreamObjectWriter* ow, value = value.substr(1); } - StringPiece s_secs, s_nanos; + absl::string_view s_secs, s_nanos; SplitSecondsAndNanos(value, &s_secs, &s_nanos); uint64_t unsigned_seconds; if (!safe_strtou64(s_secs, &unsigned_seconds)) { - return util::InvalidArgumentError( + return absl::InvalidArgumentError( "Invalid duration format, failed to parse seconds"); } @@ -1110,7 +1161,7 @@ Status ProtoStreamObjectWriter::RenderDuration(ProtoStreamObjectWriter* ow, int64_t seconds = sign * unsigned_seconds; if (seconds > kDurationMaxSeconds || seconds < kDurationMinSeconds || nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) { - return util::InvalidArgumentError("Duration value exceeds limits"); + return absl::InvalidArgumentError("Duration value exceeds limits"); } ow->ProtoWriter::RenderDataPiece("seconds", DataPiece(seconds)); @@ -1126,7 +1177,7 @@ Status ProtoStreamObjectWriter::RenderWrapperType(ProtoStreamObjectWriter* ow, } ProtoStreamObjectWriter* ProtoStreamObjectWriter::RenderDataPiece( - StringPiece name, const DataPiece& data) { + absl::string_view name, const DataPiece& data) { Status status; if (invalid_depth() > 0) return this; @@ -1145,7 +1196,7 @@ ProtoStreamObjectWriter* ProtoStreamObjectWriter::RenderDataPiece( status = (*type_renderer)(this, data); if (!status.ok()) { InvalidValue(master_type_.name(), - StrCat("Field '", name, "', ", status.message())); + absl::StrCat("Field '", name, "', ", status.message())); } ProtoWriter::EndObject(); return this; @@ -1191,7 +1242,7 @@ ProtoStreamObjectWriter* ProtoStreamObjectWriter::RenderDataPiece( status = (*type_renderer)(this, data); if (!status.ok()) { InvalidValue(field->type_url(), - StrCat("Field '", name, "', ", status.message())); + absl::StrCat("Field '", name, "', ", status.message())); } Pop(); return this; @@ -1225,7 +1276,7 @@ ProtoStreamObjectWriter* ProtoStreamObjectWriter::RenderDataPiece( status = (*type_renderer)(this, data); if (!status.ok()) { InvalidValue(field->type_url(), - StrCat("Field '", name, "', ", status.message())); + absl::StrCat("Field '", name, "', ", status.message())); } Pop(); } @@ -1260,7 +1311,7 @@ ProtoStreamObjectWriter* ProtoStreamObjectWriter::RenderDataPiece( // represented by the key. std::unordered_map* ProtoStreamObjectWriter::renderers_ = nullptr; -PROTOBUF_NAMESPACE_ID::internal::once_flag writer_renderers_init_; +absl::once_flag writer_renderers_init_; void ProtoStreamObjectWriter::InitRendererMap() { renderers_ = new std::unordered_mapInsertMapKeyIfNotPresent(unnormalized_name)) { listener()->InvalidName( location(), unnormalized_name, - StrCat("Repeated map key: '", unnormalized_name, + absl::StrCat("Repeated map key: '", unnormalized_name, "' is already set.")); return false; } @@ -1339,7 +1389,7 @@ bool ProtoStreamObjectWriter::ValidMapKey(StringPiece unnormalized_name) { } void ProtoStreamObjectWriter::Push( - StringPiece name, Item::ItemType item_type, bool is_placeholder, + absl::string_view name, Item::ItemType item_type, bool is_placeholder, bool is_list) { is_list ? ProtoWriter::StartList(name) : ProtoWriter::StartObject(name); diff --git a/src/google/protobuf/util/internal/protostream_objectwriter.h b/src/google/protobuf/util/internal/protostream_objectwriter.h index ce2517f9e9ff..dc8700236697 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter.h +++ b/src/google/protobuf/util/internal/protostream_objectwriter.h @@ -42,14 +42,14 @@ #include #include #include -#include +#include "absl/status/status.h" #include #include #include #include #include #include -#include + // Must be included last. #include @@ -151,22 +151,25 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { strings::ByteSink* output, ErrorListener* listener, const ProtoStreamObjectWriter::Options& options = ProtoStreamObjectWriter::Options::Defaults()); + ProtoStreamObjectWriter() = delete; + ProtoStreamObjectWriter(const ProtoStreamObjectWriter&) = delete; + ProtoStreamObjectWriter& operator=(const ProtoStreamObjectWriter&) = delete; ~ProtoStreamObjectWriter() override; // ObjectWriter methods. - ProtoStreamObjectWriter* StartObject(StringPiece name) override; + ProtoStreamObjectWriter* StartObject(absl::string_view name) override; ProtoStreamObjectWriter* EndObject() override; - ProtoStreamObjectWriter* StartList(StringPiece name) override; + ProtoStreamObjectWriter* StartList(absl::string_view name) override; ProtoStreamObjectWriter* EndList() override; // Renders a DataPiece 'value' into a field whose wire type is determined // from the given field 'name'. - ProtoStreamObjectWriter* RenderDataPiece(StringPiece name, + ProtoStreamObjectWriter* RenderDataPiece(absl::string_view name, const DataPiece& data) override; protected: // Function that renders a well known type with modified behavior. - typedef util::Status (*TypeRenderer)(ProtoStreamObjectWriter*, + typedef absl::Status (*TypeRenderer)(ProtoStreamObjectWriter*, const DataPiece&); // Handles writing Anys out using nested object writers and the like. @@ -176,7 +179,7 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { ~AnyWriter(); // Passes a StartObject call through to the Any writer. - void StartObject(StringPiece name); + void StartObject(absl::string_view name); // Passes an EndObject call through to the Any. Returns true if the any // handled the EndObject call, false if the Any is now all done and is no @@ -184,13 +187,13 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { bool EndObject(); // Passes a StartList call through to the Any writer. - void StartList(StringPiece name); + void StartList(absl::string_view name); // Passes an EndList call through to the Any writer. void EndList(); // Renders a data piece on the any. - void RenderDataPiece(StringPiece name, const DataPiece& value); + void RenderDataPiece(absl::string_view name, const DataPiece& value); private: // Before the "@type" field is encountered, we store all incoming data @@ -209,11 +212,11 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { explicit Event(Type type) : type_(type), value_(DataPiece::NullData()) {} // Constructor for START_OBJECT and START_LIST events. - explicit Event(Type type, StringPiece name) + explicit Event(Type type, absl::string_view name) : type_(type), name_(name), value_(DataPiece::NullData()) {} // Constructor for RENDER_DATA_PIECE events. - explicit Event(StringPiece name, const DataPiece& value) + explicit Event(absl::string_view name, const DataPiece& value) : type_(RENDER_DATA_PIECE), name_(name), value_(value) { DeepCopy(); } @@ -299,6 +302,9 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { // Constructor for a field of a message. Item(Item* parent, ItemType item_type, bool is_placeholder, bool is_list); + Item() = delete; + Item(const Item&) = delete; + Item& operator=(const Item&) = delete; ~Item() override {} @@ -318,7 +324,7 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { // The hash set (map_keys_) is ONLY used to keep track of map keys. // Return true if insert successfully; returns false if the map key was // already present. - bool InsertMapKeyIfNotPresent(StringPiece map_key); + bool InsertMapKeyIfNotPresent(absl::string_view map_key); bool is_placeholder() const { return is_placeholder_; } bool is_list() const { return is_list_; } @@ -345,8 +351,6 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { // Conveys whether this Item is a list or not. This is used to send // StartList or EndList calls to underlying ObjectWriter. bool is_list_; - - GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(Item); }; ProtoStreamObjectWriter(const TypeInfo* typeinfo, @@ -375,24 +379,24 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { // Renders google.protobuf.Value in struct.proto. It picks the right oneof // type based on value's type. - static util::Status RenderStructValue(ProtoStreamObjectWriter* ow, + static absl::Status RenderStructValue(ProtoStreamObjectWriter* ow, const DataPiece& data); // Renders google.protobuf.Timestamp value. - static util::Status RenderTimestamp(ProtoStreamObjectWriter* ow, + static absl::Status RenderTimestamp(ProtoStreamObjectWriter* ow, const DataPiece& data); // Renders google.protobuf.FieldMask value. - static util::Status RenderFieldMask(ProtoStreamObjectWriter* ow, + static absl::Status RenderFieldMask(ProtoStreamObjectWriter* ow, const DataPiece& data); // Renders google.protobuf.Duration value. - static util::Status RenderDuration(ProtoStreamObjectWriter* ow, + static absl::Status RenderDuration(ProtoStreamObjectWriter* ow, const DataPiece& data); // Renders wrapper message types for primitive types in // google/protobuf/wrappers.proto. - static util::Status RenderWrapperType(ProtoStreamObjectWriter* ow, + static absl::Status RenderWrapperType(ProtoStreamObjectWriter* ow, const DataPiece& data); static void InitRendererMap(); @@ -405,7 +409,7 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { // is of element type MAP or STRUCT_MAP. // It also calls the appropriate error callback and unnormalzied_name is used // for error string. - bool ValidMapKey(StringPiece unnormalized_name); + bool ValidMapKey(absl::string_view unnormalized_name); // Pushes an item on to the stack. Also calls either StartObject or StartList // on the underlying ObjectWriter depending on whether is_list is false or @@ -413,7 +417,7 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { // is_placeholder conveys whether the item is a placeholder item or not. // Placeholder items are pushed when adding auxiliary types' StartObject or // StartList calls. - void Push(StringPiece name, Item::ItemType item_type, + void Push(absl::string_view name, Item::ItemType item_type, bool is_placeholder, bool is_list); @@ -439,8 +443,6 @@ class PROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { // Reference to the options that control this class's behavior. const ProtoStreamObjectWriter::Options options_; - - GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoStreamObjectWriter); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/protostream_objectwriter_test.cc b/src/google/protobuf/util/internal/protostream_objectwriter_test.cc index 24040f4e0d8d..672bf8b72030 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter_test.cc +++ b/src/google/protobuf/util/internal/protostream_objectwriter_test.cc @@ -41,8 +41,8 @@ #include #include #include -#include #include +#include "absl/strings/string_view.h" #include #include #include @@ -543,17 +543,17 @@ TEST_P(ProtoStreamObjectWriterTest, InfinityInputTest) { ResetTypeInfo(Primitive::descriptor()); - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_INT32"), - StringPiece("\"Infinity\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_INT32"), + absl::string_view("\"Infinity\""))) .With(Args<0>(HasObjectLocation("i32"))); - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_UINT32"), - StringPiece("\"Infinity\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_UINT32"), + absl::string_view("\"Infinity\""))) .With(Args<0>(HasObjectLocation("u32"))); - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_SFIXED64"), - StringPiece("\"-Infinity\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_SFIXED64"), + absl::string_view("\"-Infinity\""))) .With(Args<0>(HasObjectLocation("sf64"))); - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_BOOL"), - StringPiece("\"Infinity\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_BOOL"), + absl::string_view("\"Infinity\""))) .With(Args<0>(HasObjectLocation("bool"))); ow_->StartObject("") @@ -576,17 +576,17 @@ TEST_P(ProtoStreamObjectWriterTest, NaNInputTest) { ResetTypeInfo(Primitive::descriptor()); - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_INT32"), - StringPiece("\"NaN\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_INT32"), + absl::string_view("\"NaN\""))) .With(Args<0>(HasObjectLocation("i32"))); - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_UINT32"), - StringPiece("\"NaN\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_UINT32"), + absl::string_view("\"NaN\""))) .With(Args<0>(HasObjectLocation("u32"))); - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_SFIXED64"), - StringPiece("\"NaN\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_SFIXED64"), + absl::string_view("\"NaN\""))) .With(Args<0>(HasObjectLocation("sf64"))); - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_BOOL"), - StringPiece("\"NaN\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_BOOL"), + absl::string_view("\"NaN\""))) .With(Args<0>(HasObjectLocation("bool"))); ow_->StartObject("") @@ -659,8 +659,8 @@ TEST_P(ProtoStreamObjectWriterTest, NonRepeatedExplicitPrimitiveList) { EXPECT_CALL( listener_, - InvalidName(_, StringPiece("name"), - StringPiece( + InvalidName(_, absl::string_view("name"), + absl::string_view( "Proto field is not repeating, cannot start list."))) .With(Args<0>(HasObjectLocation("author"))); ow_->StartObject("") @@ -734,8 +734,8 @@ TEST_P(ProtoStreamObjectWriterTest, EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("friend"), - StringPiece( + _, absl::string_view("friend"), + absl::string_view( "Starting an object in a repeated field but the parent object " "is not a list"))) .With(Args<0>(HasObjectLocation("author"))) @@ -810,8 +810,8 @@ TEST_P(ProtoStreamObjectWriterTest, NonRepeatedExplicitMessageList) { EXPECT_CALL( listener_, - InvalidName(_, StringPiece("publisher"), - StringPiece( + InvalidName(_, absl::string_view("publisher"), + absl::string_view( "Proto field is not repeating, cannot start list."))) .With(Args<0>(HasObjectLocation(""))); ow_->StartObject("") @@ -833,8 +833,8 @@ TEST_P(ProtoStreamObjectWriterTest, NonRepeatedExplicitMessageList) { TEST_P(ProtoStreamObjectWriterTest, UnknownFieldAtRoot) { Book empty; - EXPECT_CALL(listener_, InvalidName(_, StringPiece("unknown"), - StringPiece("Cannot find field."))) + EXPECT_CALL(listener_, InvalidName(_, absl::string_view("unknown"), + absl::string_view("Cannot find field."))) .With(Args<0>(HasObjectLocation(""))); ow_->StartObject("")->RenderString("unknown", "Nope!")->EndObject(); CheckOutput(empty, 0); @@ -851,8 +851,8 @@ TEST_P(ProtoStreamObjectWriterTest, UnknownFieldAtAuthorFriend) { Author* luke = paul->add_friend_(); luke->set_name("Luke"); - EXPECT_CALL(listener_, InvalidName(_, StringPiece("address"), - StringPiece("Cannot find field."))) + EXPECT_CALL(listener_, InvalidName(_, absl::string_view("address"), + absl::string_view("Cannot find field."))) .With(Args<0>(HasObjectLocation("author.friend[1]"))); ow_->StartObject("") ->StartObject("author") @@ -877,8 +877,8 @@ TEST_P(ProtoStreamObjectWriterTest, UnknownFieldAtAuthorFriend) { TEST_P(ProtoStreamObjectWriterTest, UnknownObjectAtRoot) { Book empty; - EXPECT_CALL(listener_, InvalidName(_, StringPiece("unknown"), - StringPiece("Cannot find field."))) + EXPECT_CALL(listener_, InvalidName(_, absl::string_view("unknown"), + absl::string_view("Cannot find field."))) .With(Args<0>(HasObjectLocation(""))); ow_->StartObject("")->StartObject("unknown")->EndObject()->EndObject(); CheckOutput(empty, 0); @@ -890,8 +890,8 @@ TEST_P(ProtoStreamObjectWriterTest, UnknownObjectAtAuthor) { author->set_name("William"); author->add_pseudonym("Bill"); - EXPECT_CALL(listener_, InvalidName(_, StringPiece("wife"), - StringPiece("Cannot find field."))) + EXPECT_CALL(listener_, InvalidName(_, absl::string_view("wife"), + absl::string_view("Cannot find field."))) .With(Args<0>(HasObjectLocation("author"))); ow_->StartObject("") ->StartObject("author") @@ -908,8 +908,8 @@ TEST_P(ProtoStreamObjectWriterTest, UnknownObjectAtAuthor) { TEST_P(ProtoStreamObjectWriterTest, UnknownListAtRoot) { Book empty; - EXPECT_CALL(listener_, InvalidName(_, StringPiece("unknown"), - StringPiece("Cannot find field."))) + EXPECT_CALL(listener_, InvalidName(_, absl::string_view("unknown"), + absl::string_view("Cannot find field."))) .With(Args<0>(HasObjectLocation(""))); ow_->StartObject("")->StartList("unknown")->EndList()->EndObject(); CheckOutput(empty, 0); @@ -921,8 +921,8 @@ TEST_P(ProtoStreamObjectWriterTest, UnknownListAtPublisher) { Publisher* publisher = expected.mutable_publisher(); publisher->set_name("propaganda"); - EXPECT_CALL(listener_, InvalidName(_, StringPiece("alliance"), - StringPiece("Cannot find field."))) + EXPECT_CALL(listener_, InvalidName(_, absl::string_view("alliance"), + absl::string_view("Cannot find field."))) .With(Args<0>(HasObjectLocation("publisher"))); ow_->StartObject("") ->StartObject("publisher") @@ -987,8 +987,8 @@ TEST_P(ProtoStreamObjectWriterTest, IgnoreUnknownObjectAtRoot) { options_.ignore_unknown_fields = true; ResetProtoWriter(); - EXPECT_CALL(listener_, InvalidName(_, StringPiece("unknown"), - StringPiece("Cannot find field."))) + EXPECT_CALL(listener_, InvalidName(_, absl::string_view("unknown"), + absl::string_view("Cannot find field."))) .Times(0); ow_->StartObject("")->StartObject("unknown")->EndObject()->EndObject(); CheckOutput(empty, 0); @@ -1056,10 +1056,10 @@ TEST_P(ProtoStreamObjectWriterTest, EXPECT_CALL( listener_, InvalidValue(_, - StringPiece( + absl::string_view( "type.googleapis.com/" "proto_util_converter.testing.Proto3Message.NestedEnum"), - StringPiece("\"someunknownvalueyouwillneverknow\""))) + absl::string_view("\"someunknownvalueyouwillneverknow\""))) .With(Args<0>(HasObjectLocation("enum_value"))); ow_->StartObject("") ->RenderString("enumValue", "someunknownvalueyouwillneverknow") @@ -1083,7 +1083,7 @@ TEST_P(ProtoStreamObjectWriterTest, MissingRequiredField) { expected.set_title("My Title"); expected.set_allocated_publisher(new Publisher()); - EXPECT_CALL(listener_, MissingField(_, StringPiece("name"))) + EXPECT_CALL(listener_, MissingField(_, absl::string_view("name"))) .With(Args<0>(HasObjectLocation("publisher"))); ow_->StartObject("") ->StartObject("publisher") @@ -1096,8 +1096,8 @@ TEST_P(ProtoStreamObjectWriterTest, MissingRequiredField) { TEST_P(ProtoStreamObjectWriterTest, InvalidFieldValueAtRoot) { Book empty; - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_UINT32"), - StringPiece("\"garbage\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_UINT32"), + absl::string_view("\"garbage\""))) .With(Args<0>(HasObjectLocation("length"))); ow_->StartObject("")->RenderString("length", "garbage")->EndObject(); CheckOutput(empty, 0); @@ -1107,11 +1107,11 @@ TEST_P(ProtoStreamObjectWriterTest, MultipleInvalidFieldValues) { Book expected; expected.set_title("My Title"); - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_UINT32"), - StringPiece("\"-400\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_UINT32"), + absl::string_view("\"-400\""))) .With(Args<0>(HasObjectLocation("length"))); - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_INT64"), - StringPiece("\"3.14\""))) + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("TYPE_INT64"), + absl::string_view("\"3.14\""))) .With(Args<0>(HasObjectLocation("published"))); ow_->StartObject("") ->RenderString("length", "-400") @@ -1125,8 +1125,8 @@ TEST_P(ProtoStreamObjectWriterTest, UnnamedFieldAtRoot) { Book empty; EXPECT_CALL(listener_, - InvalidName(_, StringPiece(""), - StringPiece("Proto fields must have a name."))) + InvalidName(_, absl::string_view(""), + absl::string_view("Proto fields must have a name."))) .With(Args<0>(HasObjectLocation(""))); ow_->StartObject("")->RenderFloat("", 3.14)->EndObject(); CheckOutput(empty, 0); @@ -1138,8 +1138,8 @@ TEST_P(ProtoStreamObjectWriterTest, UnnamedFieldAtAuthor) { expected.set_allocated_author(new Author()); EXPECT_CALL(listener_, - InvalidName(_, StringPiece(""), - StringPiece("Proto fields must have a name."))) + InvalidName(_, absl::string_view(""), + absl::string_view("Proto fields must have a name."))) .With(Args<0>(HasObjectLocation("author"))); ow_->StartObject("") ->StartObject("author") @@ -1155,8 +1155,8 @@ TEST_P(ProtoStreamObjectWriterTest, UnnamedListAtRoot) { expected.set_title("noname"); EXPECT_CALL(listener_, - InvalidName(_, StringPiece(""), - StringPiece("Proto fields must have a name."))) + InvalidName(_, absl::string_view(""), + absl::string_view("Proto fields must have a name."))) .With(Args<0>(HasObjectLocation(""))); ow_->StartObject("") ->StartList("") @@ -1172,8 +1172,8 @@ TEST_P(ProtoStreamObjectWriterTest, RootNamedObject) { EXPECT_CALL( listener_, - InvalidName(_, StringPiece("oops"), - StringPiece("Root element should not be named."))) + InvalidName(_, absl::string_view("oops"), + absl::string_view("Root element should not be named."))) .With(Args<0>(HasObjectLocation(""))); ow_->StartObject("oops")->RenderString("title", "Annie")->EndObject(); CheckOutput(expected, 7); @@ -1184,8 +1184,8 @@ TEST_P(ProtoStreamObjectWriterTest, RootNamedList) { EXPECT_CALL( listener_, - InvalidName(_, StringPiece("oops"), - StringPiece("Root element should not be named."))) + InvalidName(_, absl::string_view("oops"), + absl::string_view("Root element should not be named."))) .With(Args<0>(HasObjectLocation(""))); ow_->StartList("oops")->RenderString("", "item")->EndList(); CheckOutput(empty, 0); @@ -1195,8 +1195,8 @@ TEST_P(ProtoStreamObjectWriterTest, RootUnnamedField) { Book empty; EXPECT_CALL(listener_, - InvalidName(_, StringPiece(""), - StringPiece("Root element must be a message."))) + InvalidName(_, absl::string_view(""), + absl::string_view("Root element must be a message."))) .With(Args<0>(HasObjectLocation(""))); ow_->RenderBool("", true); CheckOutput(empty, 0); @@ -1206,8 +1206,8 @@ TEST_P(ProtoStreamObjectWriterTest, RootNamedField) { Book empty; EXPECT_CALL(listener_, - InvalidName(_, StringPiece("oops"), - StringPiece("Root element must be a message."))) + InvalidName(_, absl::string_view("oops"), + absl::string_view("Root element must be a message."))) .With(Args<0>(HasObjectLocation(""))); ow_->RenderBool("oops", true); CheckOutput(empty, 0); @@ -1322,9 +1322,10 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: " - "2016-03-07T15:14:23+"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view( + "Field 'ts', Illegal timestamp format; timestamps " + "must end with 'Z' or have a valid timezone offset."))); ow_->StartObject("")->RenderString("ts", "2016-03-07T15:14:23+")->EndObject(); CheckOutput(timestamp); @@ -1337,9 +1338,10 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: " - "2016-03-07T15:14:23+08-10"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view( + "Field 'ts', Illegal timestamp format; timestamps " + "must end with 'Z' or have a valid timezone offset."))); ow_->StartObject("") ->RenderString("ts", "2016-03-07T15:14:23+08-10") @@ -1354,9 +1356,10 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: " - "2016-03-07T15:14:23+24:10"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view( + "Field 'ts', Illegal timestamp format; timestamps " + "must end with 'Z' or have a valid timezone offset."))); ow_->StartObject("") ->RenderString("ts", "2016-03-07T15:14:23+24:10") @@ -1371,9 +1374,10 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: " - "2016-03-07T15:14:23+04:60"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view( + "Field 'ts', Illegal timestamp format; timestamps " + "must end with 'Z' or have a valid timezone offset."))); ow_->StartObject("") ->RenderString("ts", "2016-03-07T15:14:23+04:60") @@ -1387,8 +1391,10 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError1) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: "))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view( + "Field 'ts', Illegal timestamp format; timestamps " + "must end with 'Z' or have a valid timezone offset."))); ow_->StartObject("")->RenderString("ts", "")->EndObject(); CheckOutput(timestamp); @@ -1400,8 +1406,9 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError2) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: Z"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view("Field 'ts', Invalid time format: Failed to " + "parse input"))); ow_->StartObject("")->RenderString("ts", "Z")->EndObject(); CheckOutput(timestamp); @@ -1413,9 +1420,10 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError3) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: " - "1970-01-01T00:00:00.ABZ"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view( + "Field 'ts', Invalid time format, failed to parse nano " + "seconds"))); ow_->StartObject("") ->RenderString("ts", "1970-01-01T00:00:00.ABZ") @@ -1429,9 +1437,8 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError4) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: " - "-8031-10-18T00:00:00.000Z"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view("Field 'ts', Timestamp value exceeds limits"))); ow_->StartObject("") ->RenderString("ts", "-8031-10-18T00:00:00.000Z") @@ -1445,9 +1452,10 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError5) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: " - "2015-11-23T03:37:35.033155 Z"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view( + "Field 'ts', Invalid time format, failed to parse nano " + "seconds"))); ow_->StartObject("") // Whitespace in the Timestamp nanos is not allowed. @@ -1462,9 +1470,10 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError6) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: " - "2015-11-23T03:37:35.033155 1234Z"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view( + "Field 'ts', Invalid time format, failed to parse nano " + "seconds"))); ow_->StartObject("") // Whitespace in the Timestamp nanos is not allowed. @@ -1479,9 +1488,10 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError7) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: " - "2015-11-23T03:37:35.033abc155Z"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view( + "Field 'ts', Invalid time format, failed to parse nano " + "seconds"))); ow_->StartObject("") // Non-numeric characters in the Timestamp nanos is not allowed. @@ -1496,9 +1506,8 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError8) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece("Field 'ts', Invalid time format: " - "0-12-31T23:59:59.000Z"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view("Field 'ts', Timestamp value exceeds limits"))); ow_->StartObject("") ->RenderString("ts", "0-12-31T23:59:59.000Z") @@ -1522,8 +1531,8 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidDurationError1) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Duration"), - StringPiece( + _, absl::string_view("type.googleapis.com/google.protobuf.Duration"), + absl::string_view( "Field 'dur', Illegal duration format; duration must " "end with 's'"))); @@ -1537,8 +1546,8 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidDurationError2) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Duration"), - StringPiece( + _, absl::string_view("type.googleapis.com/google.protobuf.Duration"), + absl::string_view( "Field 'dur', Invalid duration format, failed to parse " "seconds"))); @@ -1552,8 +1561,8 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidDurationError3) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Duration"), - StringPiece("Field 'dur', Invalid duration format, failed to " + _, absl::string_view("type.googleapis.com/google.protobuf.Duration"), + absl::string_view("Field 'dur', Invalid duration format, failed to " "parse nano seconds"))); ow_->StartObject("")->RenderString("dur", "123.DEFs")->EndObject(); @@ -1566,8 +1575,8 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidDurationError4) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Duration"), - StringPiece("Field 'dur', Duration value exceeds limits"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Duration"), + absl::string_view("Field 'dur', Duration value exceeds limits"))); ow_->StartObject("")->RenderString("dur", "315576000002s")->EndObject(); CheckOutput(duration); @@ -1579,8 +1588,8 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidDurationError5) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Duration"), - StringPiece("Field 'dur', Duration value exceeds limits"))); + _, absl::string_view("type.googleapis.com/google.protobuf.Duration"), + absl::string_view("Field 'dur', Duration value exceeds limits"))); ow_->StartObject("")->RenderString("dur", "0.1000000001s")->EndObject(); CheckOutput(duration); @@ -1592,8 +1601,8 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), - StringPiece( + _, absl::string_view("type.googleapis.com/google.protobuf.Timestamp"), + absl::string_view( "Field 'ts', Invalid data type for timestamp, value is 1"))) .With(Args<0>(HasObjectLocation("ts"))); ow_->StartObject("")->RenderInt32("ts", 1)->EndObject(); @@ -1606,8 +1615,8 @@ TEST_P(ProtoStreamObjectWriterTimestampDurationTest, EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Duration"), - StringPiece( + _, absl::string_view("type.googleapis.com/google.protobuf.Duration"), + absl::string_view( "Field 'dur', Invalid data type for duration, value is 1"))) .With(Args<0>(HasObjectLocation("dur"))); ow_->StartObject("")->RenderInt32("dur", 1)->EndObject(); @@ -1666,8 +1675,8 @@ TEST_P(ProtoStreamObjectWriterStructTest, StructRenderSuccess) { TEST_P(ProtoStreamObjectWriterStructTest, StructNullInputSuccess) { StructType struct_type; EXPECT_CALL(listener_, - InvalidName(_, StringPiece(""), - StringPiece("Proto fields must have a name."))) + InvalidName(_, absl::string_view(""), + absl::string_view("Proto fields must have a name."))) .With(Args<0>(HasObjectLocation(""))); ow_->StartObject("")->RenderNull("")->EndObject(); CheckOutput(struct_type); @@ -1678,8 +1687,8 @@ TEST_P(ProtoStreamObjectWriterStructTest, StructInvalidInputFailure) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.Struct"), - StringPiece("true"))) + _, absl::string_view("type.googleapis.com/google.protobuf.Struct"), + absl::string_view("true"))) .With(Args<0>(HasObjectLocation("object"))); ow_->StartObject("")->RenderBool("object", true)->EndObject(); @@ -1710,8 +1719,8 @@ TEST_P(ProtoStreamObjectWriterStructTest, StructValuePreservesNull) { TEST_P(ProtoStreamObjectWriterStructTest, SimpleRepeatedStructMapKeyTest) { EXPECT_CALL(listener_, - InvalidName(_, StringPiece("gBike"), - StringPiece( + InvalidName(_, absl::string_view("gBike"), + absl::string_view( "Repeated map key: 'gBike' is already set."))); ow_->StartObject("") ->StartObject("object") @@ -1724,8 +1733,8 @@ TEST_P(ProtoStreamObjectWriterStructTest, SimpleRepeatedStructMapKeyTest) { TEST_P(ProtoStreamObjectWriterStructTest, RepeatedStructMapListKeyTest) { EXPECT_CALL( listener_, - InvalidName(_, StringPiece("k1"), - StringPiece("Repeated map key: 'k1' is already set."))); + InvalidName(_, absl::string_view("k1"), + absl::string_view("Repeated map key: 'k1' is already set."))); ow_->StartObject("") ->StartObject("object") ->RenderString("k1", "v1") @@ -1739,8 +1748,8 @@ TEST_P(ProtoStreamObjectWriterStructTest, RepeatedStructMapListKeyTest) { TEST_P(ProtoStreamObjectWriterStructTest, RepeatedStructMapObjectKeyTest) { EXPECT_CALL( listener_, - InvalidName(_, StringPiece("k1"), - StringPiece("Repeated map key: 'k1' is already set."))); + InvalidName(_, absl::string_view("k1"), + absl::string_view("Repeated map key: 'k1' is already set."))); ow_->StartObject("") ->StartObject("object") ->StartObject("k1") @@ -1842,8 +1851,8 @@ TEST_P(ProtoStreamObjectWriterMapTest, MapShouldNotAcceptList) { MapIn mm; EXPECT_CALL( listener_, - InvalidValue(_, StringPiece("Map"), - StringPiece( + InvalidValue(_, absl::string_view("Map"), + absl::string_view( "Cannot bind a list to map for field 'map_input'."))); ow_->StartObject("") ->StartList("map_input") @@ -1886,8 +1895,8 @@ TEST_P(ProtoStreamObjectWriterMapTest, MapShouldIgnoreNullValueEntry) { TEST_P(ProtoStreamObjectWriterMapTest, RepeatedMapKeyTest) { EXPECT_CALL( listener_, - InvalidName(_, StringPiece("k1"), - StringPiece("Repeated map key: 'k1' is already set."))); + InvalidName(_, absl::string_view("k1"), + absl::string_view("Repeated map key: 'k1' is already set."))); ow_->StartObject("") ->RenderString("other", "test") ->StartObject("map_input") @@ -2051,7 +2060,7 @@ TEST_P(ProtoStreamObjectWriterAnyTest, TypeUrlAtEnd) { } // Same as TypeUrlAtEnd, but use temporary string values to make sure we don't -// mistakenly store StringPiece objects pointing to invalid memory. +// mistakenly store absl::string_view objects pointing to invalid memory. TEST_P(ProtoStreamObjectWriterAnyTest, TypeUrlAtEndWithTemporaryStrings) { Book book; book.set_title("C++"); @@ -2120,8 +2129,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails1) { EXPECT_CALL( listener_, - InvalidValue(_, StringPiece("Any"), - StringPiece("Missing @type for any field in " + InvalidValue(_, absl::string_view("Any"), + absl::string_view("Missing @type for any field in " "proto_util_converter.testing.AnyOut"))); ow_->StartObject("") @@ -2138,8 +2147,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails2) { EXPECT_CALL( listener_, - InvalidValue(_, StringPiece("Any"), - StringPiece("Missing @type for any field in " + InvalidValue(_, absl::string_view("Any"), + absl::string_view("Missing @type for any field in " "proto_util_converter.testing.AnyOut"))); ow_->StartObject("") @@ -2156,8 +2165,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails3) { EXPECT_CALL( listener_, - InvalidValue(_, StringPiece("Any"), - StringPiece("Missing @type for any field in " + InvalidValue(_, absl::string_view("Any"), + absl::string_view("Missing @type for any field in " "proto_util_converter.testing.AnyOut"))); ow_->StartObject("") @@ -2174,8 +2183,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithInvalidTypeUrlFails) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("Any"), - StringPiece("Invalid type URL, type URLs must be of the form " + _, absl::string_view("Any"), + absl::string_view("Invalid type URL, type URLs must be of the form " "'type.googleapis.com/', got: " "type.other.com/some.Type"))); @@ -2192,8 +2201,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithUnknownTypeFails) { AnyOut any; EXPECT_CALL(listener_, - InvalidValue(_, StringPiece("Any"), - StringPiece( + InvalidValue(_, absl::string_view("Any"), + absl::string_view( "Invalid type URL, unknown type: some.Type"))); ow_->StartObject("") ->StartObject("any") @@ -2210,8 +2219,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyIncorrectInputTypeFails) { EXPECT_CALL( listener_, InvalidValue(_, - StringPiece("type.googleapis.com/google.protobuf.Any"), - StringPiece("1"))); + absl::string_view("type.googleapis.com/google.protobuf.Any"), + absl::string_view("1"))); ow_->StartObject("")->RenderInt32("any", 1)->EndObject(); CheckOutput(any); } @@ -2225,8 +2234,12 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyAcceptsNull) { } TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypeErrorTest) { - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("Any"), - StringPiece("Invalid time format: "))); + EXPECT_CALL(listener_, + InvalidValue(_, absl::string_view("Any"), + absl::string_view("Illegal timestamp format; " + "timestamps must end with " + "'Z' or have a valid " + "timezone offset."))); AnyOut any; google::protobuf::Any* any_type = any.mutable_any(); @@ -2337,8 +2350,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("Any"), - StringPiece("Expect a \"value\" field for well-known types."))); + _, absl::string_view("Any"), + absl::string_view("Expect a \"value\" field for well-known types."))); AnyOut any; google::protobuf::Any* any_type = any.mutable_any(); any_type->set_type_url("type.googleapis.com/google.protobuf.Value"); @@ -2364,8 +2377,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypesNoValueFieldForObject) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("Any"), - StringPiece("Expect a \"value\" field for well-known types."))); + _, absl::string_view("Any"), + absl::string_view("Expect a \"value\" field for well-known types."))); AnyOut any; google::protobuf::Any* any_type = any.mutable_any(); any_type->set_type_url("type.googleapis.com/google.protobuf.Value"); @@ -2392,8 +2405,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypesNoValueFieldForArray) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("Any"), - StringPiece("Expect a \"value\" field for well-known types."))); + _, absl::string_view("Any"), + absl::string_view("Expect a \"value\" field for well-known types."))); AnyOut any; google::protobuf::Any* any_type = any.mutable_any(); any_type->set_type_url("type.googleapis.com/google.protobuf.Value"); @@ -2419,8 +2432,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypesNoValueFieldForArray) { // } TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypesExpectObjectForStruct) { EXPECT_CALL(listener_, - InvalidValue(_, StringPiece("Any"), - StringPiece("Expect a JSON object."))); + InvalidValue(_, absl::string_view("Any"), + absl::string_view("Expect a JSON object."))); AnyOut any; google::protobuf::Any* any_type = any.mutable_any(); any_type->set_type_url("type.googleapis.com/google.protobuf.Struct"); @@ -2444,8 +2457,8 @@ TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypesExpectObjectForStruct) { // } TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypesExpectObjectForAny) { EXPECT_CALL(listener_, - InvalidValue(_, StringPiece("Any"), - StringPiece("Expect a JSON object."))); + InvalidValue(_, absl::string_view("Any"), + absl::string_view("Expect a JSON object."))); AnyOut any; google::protobuf::Any* any_type = any.mutable_any(); any_type->set_type_url("type.googleapis.com/google.protobuf.Any"); @@ -2704,8 +2717,8 @@ TEST_P(ProtoStreamObjectWriterFieldMaskTest, MoreCloseThanOpenParentheses) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.FieldMask"), - StringPiece("Field 'single_mask', Invalid FieldMask 'a(b,c))'. " + _, absl::string_view("type.googleapis.com/google.protobuf.FieldMask"), + absl::string_view("Field 'single_mask', Invalid FieldMask 'a(b,c))'. " "Cannot find matching '(' for all ')'."))); ow_->StartObject(""); @@ -2718,8 +2731,8 @@ TEST_P(ProtoStreamObjectWriterFieldMaskTest, MoreOpenThanCloseParentheses) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.FieldMask"), - StringPiece( + _, absl::string_view("type.googleapis.com/google.protobuf.FieldMask"), + absl::string_view( "Field 'single_mask', Invalid FieldMask 'a(((b,c)'. Cannot " "find matching ')' for all '('."))); @@ -2750,8 +2763,8 @@ TEST_P(ProtoStreamObjectWriterFieldMaskTest, EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.FieldMask"), - StringPiece( + _, absl::string_view("type.googleapis.com/google.protobuf.FieldMask"), + absl::string_view( "Field 'single_mask', Invalid FieldMask " "'path.to.map[\"key1\"]a,path.to.map[\"key2\"]'. " "Map keys should be at the end of a path segment."))); @@ -2766,8 +2779,8 @@ TEST_P(ProtoStreamObjectWriterFieldMaskTest, MapKeyMustEnd) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.FieldMask"), - StringPiece("Field 'single_mask', Invalid FieldMask " + _, absl::string_view("type.googleapis.com/google.protobuf.FieldMask"), + absl::string_view("Field 'single_mask', Invalid FieldMask " "'path.to.map[\"key1\"'. Map keys should be " "represented as [\"some_key\"]."))); @@ -2780,8 +2793,8 @@ TEST_P(ProtoStreamObjectWriterFieldMaskTest, MapKeyMustBeEscapedCorrectly) { EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("type.googleapis.com/google.protobuf.FieldMask"), - StringPiece("Field 'single_mask', Invalid FieldMask " + _, absl::string_view("type.googleapis.com/google.protobuf.FieldMask"), + absl::string_view("Field 'single_mask', Invalid FieldMask " "'path.to.map[\"ke\"y1\"]'. Map keys should be " "represented as [\"some_key\"]."))); @@ -2861,8 +2874,8 @@ TEST_P(ProtoStreamObjectWriterOneOfsTest, EXPECT_CALL( listener_, InvalidValue( - _, StringPiece("oneof"), - StringPiece( + _, absl::string_view("oneof"), + absl::string_view( "oneof field 'data' is already set. Cannot set 'intData'"))); ow_->StartObject(""); @@ -2874,8 +2887,8 @@ TEST_P(ProtoStreamObjectWriterOneOfsTest, TEST_P(ProtoStreamObjectWriterOneOfsTest, MultipleOneofsFailForMessageTypesPrimitiveFirstTest) { // Test for setting primitive oneof field first and then message field. - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("oneof"), - StringPiece( + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("oneof"), + absl::string_view( "oneof field 'data' is already set. " "Cannot set 'messageData'"))); @@ -2891,8 +2904,8 @@ TEST_P(ProtoStreamObjectWriterOneOfsTest, TEST_P(ProtoStreamObjectWriterOneOfsTest, MultipleOneofsFailForMessageTypesMessageFirstTest) { // Test for setting message oneof field first and then primitive field. - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("oneof"), - StringPiece( + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("oneof"), + absl::string_view( "oneof field 'data' is already set. " "Cannot set 'strData'"))); @@ -2907,8 +2920,8 @@ TEST_P(ProtoStreamObjectWriterOneOfsTest, TEST_P(ProtoStreamObjectWriterOneOfsTest, MultipleOneofsFailForStructTypesPrimitiveFirstTest) { - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("oneof"), - StringPiece( + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("oneof"), + absl::string_view( "oneof field 'data' is already set. " "Cannot set 'structData'"))); @@ -2923,8 +2936,8 @@ TEST_P(ProtoStreamObjectWriterOneOfsTest, TEST_P(ProtoStreamObjectWriterOneOfsTest, MultipleOneofsFailForStructTypesStructFirstTest) { - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("oneof"), - StringPiece( + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("oneof"), + absl::string_view( "oneof field 'data' is already set. " "Cannot set 'strData'"))); @@ -2939,8 +2952,8 @@ TEST_P(ProtoStreamObjectWriterOneOfsTest, TEST_P(ProtoStreamObjectWriterOneOfsTest, MultipleOneofsFailForStructValueTypesTest) { - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("oneof"), - StringPiece( + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("oneof"), + absl::string_view( "oneof field 'data' is already set. " "Cannot set 'valueData'"))); @@ -2957,8 +2970,8 @@ TEST_P(ProtoStreamObjectWriterOneOfsTest, TEST_P(ProtoStreamObjectWriterOneOfsTest, MultipleOneofsFailForWellKnownTypesPrimitiveFirstTest) { - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("oneof"), - StringPiece( + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("oneof"), + absl::string_view( "oneof field 'data' is already set. " "Cannot set 'tsData'"))); @@ -2971,8 +2984,8 @@ TEST_P(ProtoStreamObjectWriterOneOfsTest, TEST_P(ProtoStreamObjectWriterOneOfsTest, MultipleOneofsFailForWellKnownTypesWktFirstTest) { - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("oneof"), - StringPiece( + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("oneof"), + absl::string_view( "oneof field 'data' is already set. " "Cannot set 'intData'"))); @@ -2985,8 +2998,8 @@ TEST_P(ProtoStreamObjectWriterOneOfsTest, TEST_P(ProtoStreamObjectWriterOneOfsTest, MultipleOneofsFailForWellKnownTypesAndMessageTest) { - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("oneof"), - StringPiece( + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("oneof"), + absl::string_view( "oneof field 'data' is already set. " "Cannot set 'messageData'"))); @@ -3002,8 +3015,8 @@ TEST_P(ProtoStreamObjectWriterOneOfsTest, TEST_P(ProtoStreamObjectWriterOneOfsTest, MultipleOneofsFailForOneofWithinAnyTest) { - EXPECT_CALL(listener_, InvalidValue(_, StringPiece("oneof"), - StringPiece( + EXPECT_CALL(listener_, InvalidValue(_, absl::string_view("oneof"), + absl::string_view( "oneof field 'data' is already set. " "Cannot set 'intData'"))); diff --git a/src/google/protobuf/util/internal/structured_objectwriter.h b/src/google/protobuf/util/internal/structured_objectwriter.h index f6f7c89e5786..c9129e5dccb0 100644 --- a/src/google/protobuf/util/internal/structured_objectwriter.h +++ b/src/google/protobuf/util/internal/structured_objectwriter.h @@ -33,8 +33,9 @@ #include -#include #include +#include "absl/base/casts.h" +#include #include // Must be included last. @@ -58,6 +59,8 @@ namespace converter { // Derived classes could be thread-unsafe. class PROTOBUF_EXPORT StructuredObjectWriter : public ObjectWriter { public: + StructuredObjectWriter(const StructuredObjectWriter&) = delete; + StructuredObjectWriter& operator=(const StructuredObjectWriter&) = delete; ~StructuredObjectWriter() override {} protected: @@ -72,12 +75,15 @@ class PROTOBUF_EXPORT StructuredObjectWriter : public ObjectWriter { explicit BaseElement(BaseElement* parent) : parent_(parent), level_(parent == nullptr ? 0 : parent->level() + 1) {} + BaseElement() = delete; + BaseElement(const BaseElement&) = delete; + BaseElement& operator=(const BaseElement&) = delete; virtual ~BaseElement() {} // Releases ownership of the parent and returns a pointer to it. template ElementType* pop() { - return down_cast(parent_.release()); + return google::protobuf::internal::DownCast(parent_.release()); } // Returns true if this element is the root. @@ -97,8 +103,6 @@ class PROTOBUF_EXPORT StructuredObjectWriter : public ObjectWriter { // Number of hops to the root Element. // The root Element has nullptr parent_ and a level_ of 0. const int level_; - - GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(BaseElement); }; StructuredObjectWriter() {} @@ -108,7 +112,6 @@ class PROTOBUF_EXPORT StructuredObjectWriter : public ObjectWriter { private: // Do not add any data members to this class. - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StructuredObjectWriter); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/type_info.cc b/src/google/protobuf/util/internal/type_info.cc index b6cf5366ae48..3ca11d95d2c3 100644 --- a/src/google/protobuf/util/internal/type_info.cc +++ b/src/google/protobuf/util/internal/type_info.cc @@ -35,12 +35,11 @@ #include #include -#include -#include -#include +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" #include #include -#include namespace google { namespace protobuf { @@ -59,19 +58,19 @@ class TypeInfoForTypeResolver : public TypeInfo { DeleteCachedTypes(&cached_enums_); } - util::StatusOr ResolveTypeUrl( - StringPiece type_url) const override { - std::map::iterator it = + absl::StatusOr ResolveTypeUrl( + absl::string_view type_url) const override { + std::map::iterator it = cached_types_.find(type_url); if (it != cached_types_.end()) { return it->second; } - // Stores the string value so it can be referenced using StringPiece in the - // cached_types_ map. + // Stores the string value so it can be referenced using absl::string_view + // in the cached_types_ map. const std::string& string_type_url = *string_storage_.insert(std::string(type_url)).first; std::unique_ptr type(new google::protobuf::Type()); - util::Status status = + absl::Status status = type_resolver_->ResolveMessageType(string_type_url, type.get()); StatusOrType result = status.ok() ? StatusOrType(type.release()) : StatusOrType(status); @@ -80,25 +79,25 @@ class TypeInfoForTypeResolver : public TypeInfo { } const google::protobuf::Type* GetTypeByTypeUrl( - StringPiece type_url) const override { + absl::string_view type_url) const override { StatusOrType result = ResolveTypeUrl(type_url); return result.ok() ? result.value() : NULL; } const google::protobuf::Enum* GetEnumByTypeUrl( - StringPiece type_url) const override { - std::map::iterator it = + absl::string_view type_url) const override { + std::map::iterator it = cached_enums_.find(type_url); if (it != cached_enums_.end()) { return it->second.ok() ? it->second.value() : NULL; } - // Stores the string value so it can be referenced using StringPiece in the - // cached_enums_ map. + // Stores the string value so it can be referenced using absl::string_view + // in the cached_enums_ map. const std::string& string_type_url = *string_storage_.insert(std::string(type_url)).first; std::unique_ptr enum_type( new google::protobuf::Enum()); - util::Status status = + absl::Status status = type_resolver_->ResolveEnumType(string_type_url, enum_type.get()); StatusOrEnum result = status.ok() ? StatusOrEnum(enum_type.release()) : StatusOrEnum(status); @@ -108,15 +107,15 @@ class TypeInfoForTypeResolver : public TypeInfo { const google::protobuf::Field* FindField( const google::protobuf::Type* type, - StringPiece camel_case_name) const override { + absl::string_view camel_case_name) const override { std::map::const_iterator it = indexed_types_.find(type); const CamelCaseNameTable& camel_case_name_table = (it == indexed_types_.end()) ? PopulateNameLookupTable(type, &indexed_types_[type]) : it->second; - StringPiece name = FindWithDefault( - camel_case_name_table, camel_case_name, StringPiece()); + absl::string_view name = FindWithDefault( + camel_case_name_table, camel_case_name, absl::string_view()); if (name.empty()) { // Didn't find a mapping. Use whatever provided. name = camel_case_name; @@ -125,13 +124,13 @@ class TypeInfoForTypeResolver : public TypeInfo { } private: - typedef util::StatusOr StatusOrType; - typedef util::StatusOr StatusOrEnum; - typedef std::map CamelCaseNameTable; + typedef absl::StatusOr StatusOrType; + typedef absl::StatusOr StatusOrEnum; + typedef std::map CamelCaseNameTable; template - static void DeleteCachedTypes(std::map* cached_types) { - for (typename std::map::iterator it = + static void DeleteCachedTypes(std::map* cached_types) { + for (typename std::map::iterator it = cached_types->begin(); it != cached_types->end(); ++it) { if (it->second.ok()) { @@ -145,9 +144,9 @@ class TypeInfoForTypeResolver : public TypeInfo { CamelCaseNameTable* camel_case_name_table) const { for (int i = 0; i < type->fields_size(); ++i) { const google::protobuf::Field& field = type->fields(i); - StringPiece name = field.name(); - StringPiece camel_case_name = field.json_name(); - const StringPiece* existing = InsertOrReturnExisting( + absl::string_view name = field.name(); + absl::string_view camel_case_name = field.json_name(); + const absl::string_view* existing = InsertOrReturnExisting( camel_case_name_table, camel_case_name, name); if (existing && *existing != name) { GOOGLE_LOG(WARNING) << "Field '" << name << "' and '" << *existing @@ -164,8 +163,8 @@ class TypeInfoForTypeResolver : public TypeInfo { // cached_types_, cached_enums_. mutable std::set string_storage_; - mutable std::map cached_types_; - mutable std::map cached_enums_; + mutable std::map cached_types_; + mutable std::map cached_enums_; mutable std::map indexed_types_; diff --git a/src/google/protobuf/util/internal/type_info.h b/src/google/protobuf/util/internal/type_info.h index 257df5bad681..d224071081e9 100644 --- a/src/google/protobuf/util/internal/type_info.h +++ b/src/google/protobuf/util/internal/type_info.h @@ -33,10 +33,11 @@ #include #include -#include -#include +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" #include -#include + // Must be included last. #include @@ -50,6 +51,8 @@ namespace converter { class PROTOBUF_EXPORT TypeInfo { public: TypeInfo() {} + TypeInfo(const TypeInfo&) = delete; + TypeInfo& operator=(const TypeInfo&) = delete; virtual ~TypeInfo() {} // Resolves a type url into a Type. If the type url is invalid, returns @@ -57,34 +60,31 @@ class PROTOBUF_EXPORT TypeInfo { // corresponding type cannot be found, returns a NOT_FOUND error status. // // This TypeInfo class retains the ownership of the returned pointer. - virtual util::StatusOr ResolveTypeUrl( - StringPiece type_url) const = 0; + virtual absl::StatusOr ResolveTypeUrl( + absl::string_view type_url) const = 0; // Resolves a type url into a Type. Like ResolveTypeUrl() but returns // NULL if the type url is invalid or the type cannot be found. // // This TypeInfo class retains the ownership of the returned pointer. virtual const google::protobuf::Type* GetTypeByTypeUrl( - StringPiece type_url) const = 0; + absl::string_view type_url) const = 0; // Resolves a type url for an enum. Returns NULL if the type url is // invalid or the type cannot be found. // // This TypeInfo class retains the ownership of the returned pointer. virtual const google::protobuf::Enum* GetEnumByTypeUrl( - StringPiece type_url) const = 0; + absl::string_view type_url) const = 0; // Looks up a field in the specified type given a CamelCase name. virtual const google::protobuf::Field* FindField( const google::protobuf::Type* type, - StringPiece camel_case_name) const = 0; + absl::string_view camel_case_name) const = 0; // Creates a TypeInfo object that looks up type information from a // TypeResolver. Caller takes ownership of the returned pointer. static TypeInfo* NewTypeInfo(TypeResolver* type_resolver); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeInfo); }; } // namespace converter diff --git a/src/google/protobuf/util/internal/utility.cc b/src/google/protobuf/util/internal/utility.cc index 484ec7e9d12d..39fe37a9b455 100644 --- a/src/google/protobuf/util/internal/utility.cc +++ b/src/google/protobuf/util/internal/utility.cc @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -42,6 +43,9 @@ #include #include #include +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" #include #include @@ -56,7 +60,7 @@ namespace converter { bool GetBoolOptionOrDefault( const RepeatedPtrField& options, - StringPiece option_name, bool default_value) { + absl::string_view option_name, bool default_value) { const google::protobuf::Option* opt = FindOptionOrNull(options, option_name); if (opt == nullptr) { return default_value; @@ -66,7 +70,7 @@ bool GetBoolOptionOrDefault( int64_t GetInt64OptionOrDefault( const RepeatedPtrField& options, - StringPiece option_name, int64_t default_value) { + absl::string_view option_name, int64_t default_value) { const google::protobuf::Option* opt = FindOptionOrNull(options, option_name); if (opt == nullptr) { return default_value; @@ -76,7 +80,7 @@ int64_t GetInt64OptionOrDefault( double GetDoubleOptionOrDefault( const RepeatedPtrField& options, - StringPiece option_name, double default_value) { + absl::string_view option_name, double default_value) { const google::protobuf::Option* opt = FindOptionOrNull(options, option_name); if (opt == nullptr) { return default_value; @@ -86,7 +90,7 @@ double GetDoubleOptionOrDefault( std::string GetStringOptionOrDefault( const RepeatedPtrField& options, - StringPiece option_name, StringPiece default_value) { + absl::string_view option_name, absl::string_view default_value) { const google::protobuf::Option* opt = FindOptionOrNull(options, option_name); if (opt == nullptr) { return std::string(default_value); @@ -125,7 +129,7 @@ std::string GetStringFromAny(const google::protobuf::Any& any) { return s.value(); } -StringPiece GetTypeWithoutUrl(StringPiece type_url) { +absl::string_view GetTypeWithoutUrl(absl::string_view type_url) { if (type_url.size() > kTypeUrlSize && type_url[kTypeUrlSize] == '/') { return type_url.substr(kTypeUrlSize + 1); } else { @@ -137,13 +141,13 @@ StringPiece GetTypeWithoutUrl(StringPiece type_url) { } } -std::string GetFullTypeWithUrl(StringPiece simple_type) { - return StrCat(kTypeServiceBaseUrl, "/", simple_type); +std::string GetFullTypeWithUrl(absl::string_view simple_type) { + return absl::StrCat(kTypeServiceBaseUrl, "/", simple_type); } const google::protobuf::Option* FindOptionOrNull( const RepeatedPtrField& options, - StringPiece option_name) { + absl::string_view option_name) { for (int i = 0; i < options.size(); ++i) { const google::protobuf::Option& opt = options.Get(i); if (opt.name() == option_name) { @@ -154,7 +158,7 @@ const google::protobuf::Option* FindOptionOrNull( } const google::protobuf::Field* FindFieldInTypeOrNull( - const google::protobuf::Type* type, StringPiece field_name) { + const google::protobuf::Type* type, absl::string_view field_name) { if (type != nullptr) { for (int i = 0; i < type->fields_size(); ++i) { const google::protobuf::Field& field = type->fields(i); @@ -167,7 +171,7 @@ const google::protobuf::Field* FindFieldInTypeOrNull( } const google::protobuf::Field* FindJsonFieldInTypeOrNull( - const google::protobuf::Type* type, StringPiece json_name) { + const google::protobuf::Type* type, absl::string_view json_name) { if (type != nullptr) { for (int i = 0; i < type->fields_size(); ++i) { const google::protobuf::Field& field = type->fields(i); @@ -193,7 +197,7 @@ const google::protobuf::Field* FindFieldInTypeByNumberOrNull( } const google::protobuf::EnumValue* FindEnumValueByNameOrNull( - const google::protobuf::Enum* enum_type, StringPiece enum_name) { + const google::protobuf::Enum* enum_type, absl::string_view enum_name) { if (enum_type != nullptr) { for (int i = 0; i < enum_type->enumvalue_size(); ++i) { const google::protobuf::EnumValue& enum_value = enum_type->enumvalue(i); @@ -219,7 +223,7 @@ const google::protobuf::EnumValue* FindEnumValueByNumberOrNull( } const google::protobuf::EnumValue* FindEnumValueByNameWithoutUnderscoreOrNull( - const google::protobuf::Enum* enum_type, StringPiece enum_name) { + const google::protobuf::Enum* enum_type, absl::string_view enum_name) { if (enum_type != nullptr) { for (int i = 0; i < enum_type->enumvalue_size(); ++i) { const google::protobuf::EnumValue& enum_value = enum_type->enumvalue(i); @@ -233,7 +237,7 @@ const google::protobuf::EnumValue* FindEnumValueByNameWithoutUnderscoreOrNull( // Make the name uppercase. for (std::string::iterator it = enum_name_without_underscore.begin(); it != enum_name_without_underscore.end(); ++it) { - *it = ascii_toupper(*it); + *it = absl::ascii_toupper(*it); } if (enum_name_without_underscore == enum_name) { @@ -244,14 +248,14 @@ const google::protobuf::EnumValue* FindEnumValueByNameWithoutUnderscoreOrNull( return nullptr; } -std::string EnumValueNameToLowerCamelCase(StringPiece input) { +std::string EnumValueNameToLowerCamelCase(absl::string_view input) { std::string input_string(input); std::transform(input_string.begin(), input_string.end(), input_string.begin(), ::tolower); return ToCamelCase(input_string); } -std::string ToCamelCase(StringPiece input) { +std::string ToCamelCase(absl::string_view input) { bool capitalize_next = false; bool was_cap = true; bool is_cap = false; @@ -260,7 +264,7 @@ std::string ToCamelCase(StringPiece input) { result.reserve(input.size()); for (size_t i = 0; i < input.size(); ++i, was_cap = is_cap) { - is_cap = ascii_isupper(input[i]); + is_cap = absl::ascii_isupper(input[i]); if (input[i] == '_') { capitalize_next = true; if (!result.empty()) first_word = false; @@ -272,37 +276,37 @@ std::string ToCamelCase(StringPiece input) { // 2) followed by a lowercase: "...ABc..." if (!result.empty() && is_cap && (!was_cap || - (i + 1 < input.size() && ascii_islower(input[i + 1])))) { + (i + 1 < input.size() && absl::ascii_islower(input[i + 1])))) { first_word = false; result.push_back(input[i]); } else { - result.push_back(ascii_tolower(input[i])); + result.push_back(absl::ascii_tolower(input[i])); continue; } } else if (capitalize_next) { capitalize_next = false; - if (ascii_islower(input[i])) { - result.push_back(ascii_toupper(input[i])); + if (absl::ascii_islower(input[i])) { + result.push_back(absl::ascii_toupper(input[i])); continue; } else { result.push_back(input[i]); continue; } } else { - result.push_back(ascii_tolower(input[i])); + result.push_back(absl::ascii_tolower(input[i])); } } return result; } -std::string ToSnakeCase(StringPiece input) { +std::string ToSnakeCase(absl::string_view input) { bool was_not_underscore = false; // Initialize to false for case 1 (below) bool was_not_cap = false; std::string result; result.reserve(input.size() << 1); for (size_t i = 0; i < input.size(); ++i) { - if (ascii_isupper(input[i])) { + if (absl::ascii_isupper(input[i])) { // Consider when the current character B is capitalized: // 1) At beginning of input: "B..." => "b..." // (e.g. "Biscuit" => "biscuit") @@ -315,11 +319,11 @@ std::string ToSnakeCase(StringPiece input) { if (was_not_underscore && // case 1 out (was_not_cap || // case 2 in, case 3 out (i + 1 < input.size() && // case 3 out - ascii_islower(input[i + 1])))) { // case 4 in + absl::ascii_islower(input[i + 1])))) { // case 4 in // We add an underscore for case 2 and case 4. result.push_back('_'); } - result.push_back(ascii_tolower(input[i])); + result.push_back(absl::ascii_tolower(input[i])); was_not_underscore = true; was_not_cap = false; } else { @@ -332,7 +336,7 @@ std::string ToSnakeCase(StringPiece input) { } std::set* well_known_types_ = nullptr; -PROTOBUF_NAMESPACE_ID::internal::once_flag well_known_types_init_; +absl::once_flag well_known_types_init_; const char* well_known_types_name_array_[] = { "google.protobuf.Timestamp", "google.protobuf.Duration", "google.protobuf.DoubleValue", "google.protobuf.FloatValue", @@ -344,20 +348,18 @@ const char* well_known_types_name_array_[] = { void DeleteWellKnownTypes() { delete well_known_types_; } void InitWellKnownTypes() { - well_known_types_ = new std::set; - for (int i = 0; i < GOOGLE_ARRAYSIZE(well_known_types_name_array_); ++i) { - well_known_types_->insert(well_known_types_name_array_[i]); - } + well_known_types_ = + new std::set(std::begin(well_known_types_name_array_), + std::end(well_known_types_name_array_)); google::protobuf::internal::OnShutdown(&DeleteWellKnownTypes); } bool IsWellKnownType(const std::string& type_name) { - PROTOBUF_NAMESPACE_ID::internal::call_once(well_known_types_init_, - InitWellKnownTypes); + absl::call_once(well_known_types_init_, InitWellKnownTypes); return ContainsKey(*well_known_types_, type_name); } -bool IsValidBoolString(StringPiece bool_string) { +bool IsValidBoolString(absl::string_view bool_string) { return bool_string == "true" || bool_string == "false" || bool_string == "1" || bool_string == "0"; } @@ -392,7 +394,7 @@ std::string FloatAsString(float value) { return DoubleAsString(value); } -bool SafeStrToFloat(StringPiece str, float* value) { +bool SafeStrToFloat(absl::string_view str, float* value) { double double_value; if (!safe_strtod(str, &double_value)) { return false; diff --git a/src/google/protobuf/util/internal/utility.h b/src/google/protobuf/util/internal/utility.h index 59da234a38c9..fe429ac178da 100644 --- a/src/google/protobuf/util/internal/utility.h +++ b/src/google/protobuf/util/internal/utility.h @@ -42,8 +42,11 @@ #include #include #include -#include -#include +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" + // Must be included last. #include @@ -61,25 +64,25 @@ static const int64_t kTypeUrlSize = 19; // When the option with the given name is not found, default_value is returned. PROTOBUF_EXPORT bool GetBoolOptionOrDefault( const RepeatedPtrField& options, - StringPiece option_name, bool default_value); + absl::string_view option_name, bool default_value); // Returns int64 option value. If the option isn't found, returns the // default_value. PROTOBUF_EXPORT int64_t GetInt64OptionOrDefault( const RepeatedPtrField& options, - StringPiece option_name, int64_t default_value); + absl::string_view option_name, int64_t default_value); // Returns double option value. If the option isn't found, returns the // default_value. PROTOBUF_EXPORT double GetDoubleOptionOrDefault( const RepeatedPtrField& options, - StringPiece option_name, double default_value); + absl::string_view option_name, double default_value); // Returns string option value. If the option isn't found, returns the // default_value. PROTOBUF_EXPORT std::string GetStringOptionOrDefault( const RepeatedPtrField& options, - StringPiece option_name, StringPiece default_value); + absl::string_view option_name, absl::string_view default_value); // Returns a boolean value contained in Any type. // TODO(skarvaje): Make these utilities dealing with Any types more generic, @@ -98,8 +101,8 @@ PROTOBUF_EXPORT std::string GetStringFromAny(const google::protobuf::Any& any); // Returns the type string without the url prefix. e.g.: If the passed type is // 'type.googleapis.com/tech.type.Bool', the returned value is 'tech.type.Bool'. -PROTOBUF_EXPORT StringPiece GetTypeWithoutUrl( - StringPiece type_url); +PROTOBUF_EXPORT absl::string_view GetTypeWithoutUrl( + absl::string_view type_url); // Returns the simple_type with the base type url (kTypeServiceBaseUrl) // prefixed. @@ -108,23 +111,23 @@ PROTOBUF_EXPORT StringPiece GetTypeWithoutUrl( // GetFullTypeWithUrl("google.protobuf.Timestamp") returns the string // "type.googleapis.com/google.protobuf.Timestamp". PROTOBUF_EXPORT std::string GetFullTypeWithUrl( - StringPiece simple_type); + absl::string_view simple_type); // Finds and returns option identified by name and option_name within the // provided map. Returns nullptr if none found. const google::protobuf::Option* FindOptionOrNull( const RepeatedPtrField& options, - StringPiece option_name); + absl::string_view option_name); // Finds and returns the field identified by field_name in the passed tech Type // object. Returns nullptr if none found. const google::protobuf::Field* FindFieldInTypeOrNull( - const google::protobuf::Type* type, StringPiece field_name); + const google::protobuf::Type* type, absl::string_view field_name); // Similar to FindFieldInTypeOrNull, but this looks up fields with given // json_name. const google::protobuf::Field* FindJsonFieldInTypeOrNull( - const google::protobuf::Type* type, StringPiece json_name); + const google::protobuf::Type* type, absl::string_view json_name); // Similar to FindFieldInTypeOrNull, but this looks up fields by number. const google::protobuf::Field* FindFieldInTypeByNumberOrNull( @@ -133,7 +136,7 @@ const google::protobuf::Field* FindFieldInTypeByNumberOrNull( // Finds and returns the EnumValue identified by enum_name in the passed tech // Enum object. Returns nullptr if none found. const google::protobuf::EnumValue* FindEnumValueByNameOrNull( - const google::protobuf::Enum* enum_type, StringPiece enum_name); + const google::protobuf::Enum* enum_type, absl::string_view enum_name); // Finds and returns the EnumValue identified by value in the passed tech // Enum object. Returns nullptr if none found. @@ -145,23 +148,23 @@ const google::protobuf::EnumValue* FindEnumValueByNumberOrNull( // For Ex. if enum_name is ACTIONANDADVENTURE it can get accepted if // EnumValue's name is action_and_adventure or ACTION_AND_ADVENTURE. const google::protobuf::EnumValue* FindEnumValueByNameWithoutUnderscoreOrNull( - const google::protobuf::Enum* enum_type, StringPiece enum_name); + const google::protobuf::Enum* enum_type, absl::string_view enum_name); // Converts input to camel-case and returns it. -PROTOBUF_EXPORT std::string ToCamelCase(const StringPiece input); +PROTOBUF_EXPORT std::string ToCamelCase(const absl::string_view input); // Converts enum name string to camel-case and returns it. -std::string EnumValueNameToLowerCamelCase(const StringPiece input); +std::string EnumValueNameToLowerCamelCase(const absl::string_view input); // Converts input to snake_case and returns it. -PROTOBUF_EXPORT std::string ToSnakeCase(StringPiece input); +PROTOBUF_EXPORT std::string ToSnakeCase(absl::string_view input); // Returns true if type_name represents a well-known type. PROTOBUF_EXPORT bool IsWellKnownType(const std::string& type_name); // Returns true if 'bool_string' represents a valid boolean value. Only "true", // "false", "0" and "1" are allowed. -PROTOBUF_EXPORT bool IsValidBoolString(StringPiece bool_string); +PROTOBUF_EXPORT bool IsValidBoolString(absl::string_view bool_string); // Returns true if "field" is a protobuf map field based on its type. PROTOBUF_EXPORT bool IsMap(const google::protobuf::Field& field, @@ -177,7 +180,7 @@ PROTOBUF_EXPORT std::string FloatAsString(float value); // Convert from int32, int64, uint32, uint64, double or float to string. template std::string ValueAsString(T value) { - return StrCat(value); + return absl::StrCat(value); } template <> @@ -192,7 +195,7 @@ inline std::string ValueAsString(double value) { // Converts a string to float. Unlike safe_strtof, conversion will fail if the // value fits into double but not float (e.g., DBL_MAX). -PROTOBUF_EXPORT bool SafeStrToFloat(StringPiece str, float* value); +PROTOBUF_EXPORT bool SafeStrToFloat(absl::string_view str, float* value); } // namespace converter } // namespace util diff --git a/src/google/protobuf/util/json_util.cc b/src/google/protobuf/util/json_util.cc index 23f2900b6c3f..98de2dd02ea6 100644 --- a/src/google/protobuf/util/json_util.cc +++ b/src/google/protobuf/util/json_util.cc @@ -31,10 +31,11 @@ #include #include -#include -#include +#include "absl/base/call_once.h" +#include "absl/status/status.h" #include -#include +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" #include #include #include @@ -59,7 +60,7 @@ namespace protobuf { namespace util { using ::google::protobuf::io::zc_sink_internal::ZeroCopyStreamByteSink; -util::Status BinaryToJsonStream(TypeResolver* resolver, +absl::Status BinaryToJsonStream(TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* binary_input, io::ZeroCopyOutputStream* json_output, @@ -90,7 +91,7 @@ util::Status BinaryToJsonStream(TypeResolver* resolver, } } -util::Status BinaryToJsonString(TypeResolver* resolver, +absl::Status BinaryToJsonString(TypeResolver* resolver, const std::string& type_url, const std::string& binary_input, std::string* json_output, @@ -107,41 +108,41 @@ class StatusErrorListener : public converter::ErrorListener { StatusErrorListener() {} ~StatusErrorListener() override {} - util::Status GetStatus() { return status_; } + absl::Status GetStatus() { return status_; } void InvalidName(const converter::LocationTrackerInterface& loc, - StringPiece unknown_name, - StringPiece message) override { + absl::string_view unknown_name, + absl::string_view message) override { std::string loc_string = GetLocString(loc); if (!loc_string.empty()) { loc_string.append(" "); } - status_ = util::InvalidArgumentError( - StrCat(loc_string, unknown_name, ": ", message)); + status_ = absl::InvalidArgumentError( + absl::StrCat(loc_string, unknown_name, ": ", message)); } void InvalidValue(const converter::LocationTrackerInterface& loc, - StringPiece type_name, - StringPiece value) override { - status_ = util::InvalidArgumentError( - StrCat(GetLocString(loc), ": invalid value ", std::string(value), + absl::string_view type_name, + absl::string_view value) override { + status_ = absl::InvalidArgumentError( + absl::StrCat(GetLocString(loc), ": invalid value ", std::string(value), " for type ", std::string(type_name))); } void MissingField(const converter::LocationTrackerInterface& loc, - StringPiece missing_name) override { - status_ = util::InvalidArgumentError(StrCat( + absl::string_view missing_name) override { + status_ = absl::InvalidArgumentError(absl::StrCat( GetLocString(loc), ": missing field ", std::string(missing_name))); } private: - util::Status status_; + absl::Status status_; std::string GetLocString(const converter::LocationTrackerInterface& loc) { std::string loc_string = loc.ToString(); StripWhitespace(&loc_string); if (!loc_string.empty()) { - loc_string = StrCat("(", loc_string, ")"); + loc_string = absl::StrCat("(", loc_string, ")"); } return loc_string; } @@ -150,7 +151,7 @@ class StatusErrorListener : public converter::ErrorListener { }; } // namespace -util::Status JsonToBinaryStream(TypeResolver* resolver, +absl::Status JsonToBinaryStream(TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* json_input, io::ZeroCopyOutputStream* binary_output, @@ -176,16 +177,16 @@ util::Status JsonToBinaryStream(TypeResolver* resolver, while (json_input->Next(&buffer, &length)) { if (length == 0) continue; RETURN_IF_ERROR(parser.Parse( - StringPiece(static_cast(buffer), length))); + absl::string_view(static_cast(buffer), length))); } RETURN_IF_ERROR(parser.FinishParse()); return listener.GetStatus(); } -util::Status JsonToBinaryString(TypeResolver* resolver, +absl::Status JsonToBinaryString(TypeResolver* resolver, const std::string& type_url, - StringPiece json_input, + absl::string_view json_input, std::string* binary_output, const JsonParseOptions& options) { io::ArrayInputStream input_stream(json_input.data(), json_input.size()); @@ -197,7 +198,7 @@ util::Status JsonToBinaryString(TypeResolver* resolver, namespace { const char* kTypeUrlPrefix = "type.googleapis.com"; TypeResolver* generated_type_resolver_ = nullptr; -PROTOBUF_NAMESPACE_ID::internal::once_flag generated_type_resolver_init_; +absl::once_flag generated_type_resolver_init_; std::string GetTypeUrl(const Message& message) { return std::string(kTypeUrlPrefix) + "/" + @@ -215,13 +216,12 @@ void InitGeneratedTypeResolver() { } TypeResolver* GetGeneratedTypeResolver() { - PROTOBUF_NAMESPACE_ID::internal::call_once(generated_type_resolver_init_, - InitGeneratedTypeResolver); + absl::call_once(generated_type_resolver_init_, InitGeneratedTypeResolver); return generated_type_resolver_; } } // namespace -util::Status MessageToJsonString(const Message& message, std::string* output, +absl::Status MessageToJsonString(const Message& message, std::string* output, const JsonOptions& options) { const DescriptorPool* pool = message.GetDescriptor()->file()->pool(); @@ -229,7 +229,7 @@ util::Status MessageToJsonString(const Message& message, std::string* output, pool == DescriptorPool::generated_pool() ? GetGeneratedTypeResolver() : NewTypeResolverForDescriptorPool(kTypeUrlPrefix, pool); - util::Status result = + absl::Status result = BinaryToJsonString(resolver, GetTypeUrl(message), message.SerializeAsString(), output, options); if (pool != DescriptorPool::generated_pool()) { @@ -238,7 +238,7 @@ util::Status MessageToJsonString(const Message& message, std::string* output, return result; } -util::Status JsonStringToMessage(StringPiece input, Message* message, +absl::Status JsonStringToMessage(absl::string_view input, Message* message, const JsonParseOptions& options) { const DescriptorPool* pool = message->GetDescriptor()->file()->pool(); @@ -247,10 +247,10 @@ util::Status JsonStringToMessage(StringPiece input, Message* message, ? GetGeneratedTypeResolver() : NewTypeResolverForDescriptorPool(kTypeUrlPrefix, pool); std::string binary; - util::Status result = JsonToBinaryString(resolver, GetTypeUrl(*message), + absl::Status result = JsonToBinaryString(resolver, GetTypeUrl(*message), input, &binary, options); if (result.ok() && !message->ParseFromString(binary)) { - result = util::InvalidArgumentError( + result = absl::InvalidArgumentError( "JSON transcoder produced invalid protobuf output."); } if (pool != DescriptorPool::generated_pool()) { diff --git a/src/google/protobuf/util/json_util.h b/src/google/protobuf/util/json_util.h index 6a5a4482b83f..838712082577 100644 --- a/src/google/protobuf/util/json_util.h +++ b/src/google/protobuf/util/json_util.h @@ -35,8 +35,8 @@ #include -#include -#include +#include "absl/status/status.h" +#include "absl/strings/string_view.h" #include #include @@ -97,11 +97,11 @@ typedef JsonPrintOptions JsonOptions; // // Please note that non-OK statuses are not a stable output of this API and // subject to change without notice. -PROTOBUF_EXPORT util::Status MessageToJsonString(const Message& message, +PROTOBUF_EXPORT absl::Status MessageToJsonString(const Message& message, std::string* output, const JsonOptions& options); -inline util::Status MessageToJsonString(const Message& message, +inline absl::Status MessageToJsonString(const Message& message, std::string* output) { return MessageToJsonString(message, output, JsonOptions()); } @@ -112,10 +112,10 @@ inline util::Status MessageToJsonString(const Message& message, // // Please note that non-OK statuses are not a stable output of this API and // subject to change without notice. -PROTOBUF_EXPORT util::Status JsonStringToMessage( - StringPiece input, Message* message, const JsonParseOptions& options); +PROTOBUF_EXPORT absl::Status JsonStringToMessage( + absl::string_view input, Message* message, const JsonParseOptions& options); -inline util::Status JsonStringToMessage(StringPiece input, +inline absl::Status JsonStringToMessage(absl::string_view input, Message* message) { return JsonStringToMessage(input, message, JsonParseOptions()); } @@ -129,12 +129,12 @@ inline util::Status JsonStringToMessage(StringPiece input, // // Please note that non-OK statuses are not a stable output of this API and // subject to change without notice. -PROTOBUF_EXPORT util::Status BinaryToJsonStream( +PROTOBUF_EXPORT absl::Status BinaryToJsonStream( TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* binary_input, io::ZeroCopyOutputStream* json_output, const JsonPrintOptions& options); -inline util::Status BinaryToJsonStream(TypeResolver* resolver, +inline absl::Status BinaryToJsonStream(TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* binary_input, io::ZeroCopyOutputStream* json_output) { @@ -142,12 +142,12 @@ inline util::Status BinaryToJsonStream(TypeResolver* resolver, JsonPrintOptions()); } -PROTOBUF_EXPORT util::Status BinaryToJsonString( +PROTOBUF_EXPORT absl::Status BinaryToJsonString( TypeResolver* resolver, const std::string& type_url, const std::string& binary_input, std::string* json_output, const JsonPrintOptions& options); -inline util::Status BinaryToJsonString(TypeResolver* resolver, +inline absl::Status BinaryToJsonString(TypeResolver* resolver, const std::string& type_url, const std::string& binary_input, std::string* json_output) { @@ -163,12 +163,12 @@ inline util::Status BinaryToJsonString(TypeResolver* resolver, // // Please note that non-OK statuses are not a stable output of this API and // subject to change without notice. -PROTOBUF_EXPORT util::Status JsonToBinaryStream( +PROTOBUF_EXPORT absl::Status JsonToBinaryStream( TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* json_input, io::ZeroCopyOutputStream* binary_output, const JsonParseOptions& options); -inline util::Status JsonToBinaryStream( +inline absl::Status JsonToBinaryStream( TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* json_input, io::ZeroCopyOutputStream* binary_output) { @@ -176,14 +176,14 @@ inline util::Status JsonToBinaryStream( JsonParseOptions()); } -PROTOBUF_EXPORT util::Status JsonToBinaryString( +PROTOBUF_EXPORT absl::Status JsonToBinaryString( TypeResolver* resolver, const std::string& type_url, - StringPiece json_input, std::string* binary_output, + absl::string_view json_input, std::string* binary_output, const JsonParseOptions& options); -inline util::Status JsonToBinaryString(TypeResolver* resolver, +inline absl::Status JsonToBinaryString(TypeResolver* resolver, const std::string& type_url, - StringPiece json_input, + absl::string_view json_input, std::string* binary_output) { return JsonToBinaryString(resolver, type_url, json_input, binary_output, JsonParseOptions()); diff --git a/src/google/protobuf/util/json_util_test.cc b/src/google/protobuf/util/json_util_test.cc index 8df9880a5cdc..714d4f1337d7 100644 --- a/src/google/protobuf/util/json_util_test.cc +++ b/src/google/protobuf/util/json_util_test.cc @@ -45,9 +45,10 @@ #include #include #include -#include -#include -#include +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/escaping.h" +#include "absl/strings/string_view.h" #include #include #include @@ -81,12 +82,14 @@ using ::proto3::TestOneof; using ::proto3::TestWrapper; using ::proto_util_converter::testing::MapIn; using ::testing::ElementsAre; +using ::testing::IsEmpty; using ::testing::Not; +using ::testing::Pair; using ::testing::SizeIs; // TODO(b/234474291): Use the gtest versions once that's available in OSS. MATCHER_P(IsOkAndHolds, inner, - StrCat("is OK and holds ", testing::PrintToString(inner))) { + absl::StrCat("is OK and holds ", testing::PrintToString(inner))) { if (!arg.ok()) { *result_listener << arg.status(); return false; @@ -94,19 +97,19 @@ MATCHER_P(IsOkAndHolds, inner, return testing::ExplainMatchResult(inner, *arg, result_listener); } -util::Status GetStatus(const util::Status& s) { return s; } +absl::Status GetStatus(const absl::Status& s) { return s; } template -util::Status GetStatus(const util::StatusOr& s) { +absl::Status GetStatus(const absl::StatusOr& s) { return s.status(); } MATCHER_P(StatusIs, status, - StrCat(".status() is ", testing::PrintToString(status))) { + absl::StrCat(".status() is ", testing::PrintToString(status))) { return GetStatus(arg).code() == status; } -#define EXPECT_OK(x) EXPECT_THAT(x, StatusIs(util::StatusCode::kOk)) -#define ASSERT_OK(x) ASSERT_THAT(x, StatusIs(util::StatusCode::kOk)) +#define EXPECT_OK(x) EXPECT_THAT(x, StatusIs(absl::StatusCode::kOk)) +#define ASSERT_OK(x) ASSERT_THAT(x, StatusIs(absl::StatusCode::kOk)) enum class Codec { kReflective, @@ -115,7 +118,7 @@ enum class Codec { class JsonTest : public testing::TestWithParam { protected: - util::StatusOr ToJson(const Message& proto, + absl::StatusOr ToJson(const Message& proto, JsonPrintOptions options = {}) { if (GetParam() == Codec::kReflective) { std::string result; @@ -130,14 +133,14 @@ class JsonTest : public testing::TestWithParam { RETURN_IF_ERROR(BinaryToJsonStream( resolver_.get(), - StrCat("type.googleapis.com/", proto.GetTypeName()), &in, &out, + absl::StrCat("type.googleapis.com/", proto.GetTypeName()), &in, &out, options)); return result; } // The out parameter comes first since `json` tends to be a very long string, // and clang-format does a poor job if it is not the last parameter. - util::Status ToProto(Message& proto, StringPiece json, + absl::Status ToProto(Message& proto, absl::string_view json, JsonParseOptions options = {}) { if (GetParam() == Codec::kReflective) { return JsonStringToMessage(json, &proto, options); @@ -149,17 +152,17 @@ class JsonTest : public testing::TestWithParam { RETURN_IF_ERROR(JsonToBinaryStream( resolver_.get(), - StrCat("type.googleapis.com/", proto.GetTypeName()), &in, &out, + absl::StrCat("type.googleapis.com/", proto.GetTypeName()), &in, &out, options)); if (!proto.ParseFromString(result)) { - return util::InternalError("wire format parse failed"); + return absl::InternalError("wire format parse failed"); } - return util::OkStatus(); + return absl::OkStatus(); } template - util::StatusOr ToProto(StringPiece json, + absl::StatusOr ToProto(absl::string_view json, JsonParseOptions options = {}) { Proto proto; RETURN_IF_ERROR(ToProto(proto, json, options)); @@ -546,7 +549,7 @@ TEST_P(JsonTest, RepeatedMapKey) { "twiceKey": 0, "twiceKey": 1 } - })json"), StatusIs(util::StatusCode::kInvalidArgument)); + })json"), StatusIs(absl::StatusCode::kInvalidArgument)); } TEST_P(JsonTest, ParsePrimitiveMapIn) { @@ -608,7 +611,7 @@ TEST_P(JsonTest, RepeatedOneofKeys) { "oneofInt32Value": 1, "oneofStringValue": "foo" })json"), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); } TEST_P(JsonTest, TestParseIgnoreUnknownFields) { @@ -627,10 +630,10 @@ TEST_P(JsonTest, TestParseIgnoreUnknownFields) { TEST_P(JsonTest, TestParseErrors) { // Parsing should fail if the field name can not be recognized. EXPECT_THAT(ToProto(R"({"unknownName": 0})"), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); // Parsing should fail if the value is invalid. EXPECT_THAT(ToProto(R"("{"int32Value": 2147483648})"), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); } TEST_P(JsonTest, TestDynamicMessage) { @@ -792,14 +795,14 @@ TEST_P(JsonTest, TestParsingBrokenAny) { } } )json"), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); TestAny m2; m2.mutable_value(); EXPECT_THAT(ToJson(m2), IsOkAndHolds(R"({"value":{}})")); m2.mutable_value()->set_value("garbage"); // The ESF parser does not return InvalidArgument for this error. - EXPECT_THAT(ToJson(m2), Not(StatusIs(util::StatusCode::kOk))); + EXPECT_THAT(ToJson(m2), Not(StatusIs(absl::StatusCode::kOk))); m2.Clear(); m2.mutable_value()->set_type_url("type.googleapis.com/proto3.TestMessage"); @@ -871,7 +874,7 @@ TEST_P(JsonTest, ParseWrappers) { } TEST_P(JsonTest, TestParsingUnknownAnyFields) { - StringPiece input = R"json( + absl::string_view input = R"json( { "value": { "@type": "type.googleapis.com/proto3.TestMessage", @@ -882,7 +885,7 @@ TEST_P(JsonTest, TestParsingUnknownAnyFields) { )json"; EXPECT_THAT(ToProto(input), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); JsonParseOptions options; options.ignore_unknown_fields = true; @@ -903,10 +906,10 @@ TEST_P(JsonTest, TestHugeBareString) { } TEST_P(JsonTest, TestParsingUnknownEnumsProto2) { - StringPiece input = R"json({"ayuLmao": "UNKNOWN_VALUE"})json"; + absl::string_view input = R"json({"ayuLmao": "UNKNOWN_VALUE"})json"; EXPECT_THAT(ToProto(input), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); JsonParseOptions options; options.ignore_unknown_fields = true; @@ -917,10 +920,10 @@ TEST_P(JsonTest, TestParsingUnknownEnumsProto2) { TEST_P(JsonTest, TestParsingUnknownEnumsProto3) { TestMessage m; - StringPiece input = R"json({"enum_value":"UNKNOWN_VALUE"})json"; + absl::string_view input = R"json({"enum_value":"UNKNOWN_VALUE"})json"; m.set_enum_value(proto3::BAR); - ASSERT_THAT(ToProto(m, input), StatusIs(util::StatusCode::kInvalidArgument)); + ASSERT_THAT(ToProto(m, input), StatusIs(absl::StatusCode::kInvalidArgument)); EXPECT_EQ(m.enum_value(), proto3::BAR); // Keep previous value JsonParseOptions options; @@ -931,7 +934,7 @@ TEST_P(JsonTest, TestParsingUnknownEnumsProto3) { TEST_P(JsonTest, TestParsingUnknownEnumsProto3FromInt) { TestMessage m; - StringPiece input = R"json({"enum_value":12345})json"; + absl::string_view input = R"json({"enum_value":12345})json"; m.set_enum_value(proto3::BAR); ASSERT_OK(ToProto(m, input)); @@ -946,34 +949,34 @@ TEST_P(JsonTest, TestParsingUnknownEnumsProto3FromInt) { // Trying to pass an object as an enum field value is always treated as an // error TEST_P(JsonTest, TestParsingUnknownEnumsProto3FromObject) { - StringPiece input = R"json({"enum_value": {}})json"; + absl::string_view input = R"json({"enum_value": {}})json"; EXPECT_THAT(ToProto(input), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); JsonParseOptions options; options.ignore_unknown_fields = true; EXPECT_THAT(ToProto(input, options), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); } TEST_P(JsonTest, TestParsingUnknownEnumsProto3FromArray) { - StringPiece input = R"json({"enum_value": []})json"; + absl::string_view input = R"json({"enum_value": []})json"; EXPECT_THAT(ToProto(input), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); JsonParseOptions options; options.ignore_unknown_fields = true; EXPECT_THAT(ToProto(input, options), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); } TEST_P(JsonTest, TestParsingEnumCaseSensitive) { TestMessage m; m.set_enum_value(proto3::FOO); EXPECT_THAT(ToProto(m, R"json({"enum_value": "bar"})json"), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); // Default behavior is case-sensitive, so keep previous value. EXPECT_EQ(m.enum_value(), proto3::FOO); } @@ -1230,7 +1233,7 @@ TEST_P(JsonTest, EmptyValue) { TEST_P(JsonTest, TrailingGarbage) { EXPECT_THAT(ToProto("{}garbage"), - StatusIs(util::StatusCode::kInvalidArgument)); + StatusIs(absl::StatusCode::kInvalidArgument)); } TEST_P(JsonTest, ListList) { @@ -1273,7 +1276,7 @@ TEST_P(JsonTest, HtmlEscape) { TEST_P(JsonTest, FieldOrder) { // $ protoscope -s <<< "3: 3 22: 2 1: 1 22: 2" std::string out; - util::Status s = BinaryToJsonString( + absl::Status s = BinaryToJsonString( resolver_.get(), "type.googleapis.com/proto3.TestMessage", "\x18\x03\xb0\x01\x02\x08\x01\xb0\x01\x02", &out); ASSERT_OK(s); @@ -1288,6 +1291,20 @@ TEST_P(JsonTest, FieldOrder) { } } +// JSON values get special treatment when it comes to pre-existing values in +// their repeated fields, when parsing through their dedicated syntax. +TEST_P(JsonTest, ClearPreExistingRepeatedInJsonValues) { + google::protobuf::ListValue l; + l.add_values()->set_string_value("hello"); + ASSERT_OK(JsonStringToMessage("[]", &l)); + EXPECT_THAT(l.values(), IsEmpty()); + + google::protobuf::Struct s; + (*s.mutable_fields())["hello"].set_string_value("world"); + ASSERT_OK(JsonStringToMessage("{}", &s)); + EXPECT_THAT(s.fields(), IsEmpty()); +} + } // namespace } // namespace util } // namespace protobuf diff --git a/src/google/protobuf/util/message_differencer.cc b/src/google/protobuf/util/message_differencer.cc index 35e4ed02bff4..32834d43c36c 100644 --- a/src/google/protobuf/util/message_differencer.cc +++ b/src/google/protobuf/util/message_differencer.cc @@ -55,6 +55,8 @@ #include #include #include +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include #include @@ -2000,14 +2002,14 @@ void MessageDifferencer::StreamReporter::PrintPath( continue; } } else { - printer_->PrintRaw(StrCat(specific_field.unknown_field_number)); + printer_->PrintRaw(absl::StrCat(specific_field.unknown_field_number)); } if (left_side && specific_field.index >= 0) { - printer_->Print("[$name$]", "name", StrCat(specific_field.index)); + printer_->Print("[$name$]", "name", absl::StrCat(specific_field.index)); } if (!left_side && specific_field.new_index >= 0) { printer_->Print("[$name$]", "name", - StrCat(specific_field.new_index)); + absl::StrCat(specific_field.new_index)); } } } @@ -2072,19 +2074,19 @@ void MessageDifferencer::StreamReporter::PrintUnknownFieldValue( std::string output; switch (unknown_field->type()) { case UnknownField::TYPE_VARINT: - output = StrCat(unknown_field->varint()); + output = absl::StrCat(unknown_field->varint()); break; case UnknownField::TYPE_FIXED32: - output = StrCat( - "0x", strings::Hex(unknown_field->fixed32(), strings::ZERO_PAD_8)); + output = absl::StrCat( + "0x", absl::Hex(unknown_field->fixed32(), absl::kZeroPad8)); break; case UnknownField::TYPE_FIXED64: - output = StrCat( - "0x", strings::Hex(unknown_field->fixed64(), strings::ZERO_PAD_16)); + output = absl::StrCat( + "0x", absl::Hex(unknown_field->fixed64(), absl::kZeroPad16)); break; case UnknownField::TYPE_LENGTH_DELIMITED: output = StringPrintf( - "\"%s\"", CEscape(unknown_field->length_delimited()).c_str()); + "\"%s\"", absl::CEscape(unknown_field->length_delimited()).c_str()); break; case UnknownField::TYPE_GROUP: // TODO(kenton): Print the contents of the group like we do for @@ -2126,7 +2128,7 @@ void MessageDifferencer::StreamReporter::PrintMapKey( if (key_string.empty()) { key_string = "''"; } - printer_->PrintRaw(StrCat("[", key_string, "]")); + printer_->PrintRaw(absl::StrCat("[", key_string, "]")); } } diff --git a/src/google/protobuf/util/time_util.cc b/src/google/protobuf/util/time_util.cc index ee6406d7a1a0..9b14eca474c4 100644 --- a/src/google/protobuf/util/time_util.cc +++ b/src/google/protobuf/util/time_util.cc @@ -36,8 +36,10 @@ #include #include #include +#include "absl/strings/str_cat.h" #include -#include +#include "absl/time/clock.h" +#include "absl/time/time.h" // Must go after other includes. #include @@ -134,15 +136,40 @@ std::string FormatNanos(int32_t nanos) { } std::string FormatTime(int64_t seconds, int32_t nanos) { - return ::google::protobuf::internal::FormatTime(seconds, nanos); + static const char kTimestampFormat[] = "%E4Y-%m-%dT%H:%M:%S"; + + timespec spec; + spec.tv_sec = seconds; + // We only use absl::FormatTime to format the seconds part because we need + // finer control over the precision of nanoseconds. + spec.tv_nsec = 0; + std::string result = absl::FormatTime( + kTimestampFormat, absl::TimeFromTimespec(spec), absl::UTCTimeZone()); + // We format the nanoseconds part separately to meet the precision + // requirement. + if (nanos != 0) { + result += "." + FormatNanos(nanos); + } + result += "Z"; + return result; } bool ParseTime(const std::string& value, int64_t* seconds, int32_t* nanos) { - return ::google::protobuf::internal::ParseTime(value, seconds, nanos); + absl::Time result; + if (!absl::ParseTime(absl::RFC3339_full, value, &result, nullptr)) { + return false; + } + timespec spec = absl::ToTimespec(result); + *seconds = spec.tv_sec; + *nanos = spec.tv_nsec; + return true; } void CurrentTime(int64_t* seconds, int32_t* nanos) { - return ::google::protobuf::internal::GetCurrentTime(seconds, nanos); + absl::Time now = absl::Now(); + timespec spec = absl::ToTimespec(now); + *seconds = spec.tv_sec; + *nanos = spec.tv_nsec; } // Truncates the remainder part after division. @@ -207,7 +234,7 @@ std::string TimeUtil::ToString(const Duration& duration) { seconds = -seconds; nanos = -nanos; } - result += StrCat(seconds); + result += absl::StrCat(seconds); if (nanos != 0) { result += "." + FormatNanos(nanos); } diff --git a/src/google/protobuf/util/time_util.h b/src/google/protobuf/util/time_util.h index bc45f70cde0d..b91df462847a 100644 --- a/src/google/protobuf/util/time_util.h +++ b/src/google/protobuf/util/time_util.h @@ -120,9 +120,6 @@ class PROTOBUF_EXPORT TimeUtil { static std::string ToString(const Duration& duration); static bool FromString(const std::string& value, Duration* timestamp); -#ifdef GetCurrentTime -#undef GetCurrentTime // Visual Studio has macro GetCurrentTime -#endif // Gets the current UTC time. static Timestamp GetCurrentTime(); // Returns the Time representing "1970-01-01 00:00:00". diff --git a/src/google/protobuf/util/type_resolver.h b/src/google/protobuf/util/type_resolver.h index b2e7b43d76e2..da1e1d26a615 100644 --- a/src/google/protobuf/util/type_resolver.h +++ b/src/google/protobuf/util/type_resolver.h @@ -37,8 +37,8 @@ #include #include -#include -#include +#include "absl/status/status.h" + // Must be included last. #include @@ -57,11 +57,11 @@ class PROTOBUF_EXPORT TypeResolver { virtual ~TypeResolver() {} // Resolves a type url for a message type. - virtual util::Status ResolveMessageType( + virtual absl::Status ResolveMessageType( const std::string& type_url, google::protobuf::Type* message_type) = 0; // Resolves a type url for an enum type. - virtual util::Status ResolveEnumType(const std::string& type_url, + virtual absl::Status ResolveEnumType(const std::string& type_url, google::protobuf::Enum* enum_type) = 0; private: diff --git a/src/google/protobuf/util/type_resolver_util.cc b/src/google/protobuf/util/type_resolver_util.cc index 8be0efbe7baa..8de463789a15 100644 --- a/src/google/protobuf/util/type_resolver_util.cc +++ b/src/google/protobuf/util/type_resolver_util.cc @@ -35,10 +35,11 @@ #include #include #include -#include +#include "absl/status/status.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include #include -#include // clang-format off #include @@ -70,38 +71,38 @@ class DescriptorPoolTypeResolver : public TypeResolver { const DescriptorPool* pool) : url_prefix_(url_prefix), pool_(pool) {} - util::Status ResolveMessageType(const std::string& type_url, + absl::Status ResolveMessageType(const std::string& type_url, Type* type) override { std::string type_name; - util::Status status = ParseTypeUrl(type_url, &type_name); + absl::Status status = ParseTypeUrl(type_url, &type_name); if (!status.ok()) { return status; } const Descriptor* descriptor = pool_->FindMessageTypeByName(type_name); if (descriptor == NULL) { - return util::NotFoundError("Invalid type URL, unknown type: " + + return absl::NotFoundError("Invalid type URL, unknown type: " + type_name); } ConvertDescriptor(descriptor, type); - return util::Status(); + return absl::Status(); } - util::Status ResolveEnumType(const std::string& type_url, + absl::Status ResolveEnumType(const std::string& type_url, Enum* enum_type) override { std::string type_name; - util::Status status = ParseTypeUrl(type_url, &type_name); + absl::Status status = ParseTypeUrl(type_url, &type_name); if (!status.ok()) { return status; } const EnumDescriptor* descriptor = pool_->FindEnumTypeByName(type_name); if (descriptor == NULL) { - return util::InvalidArgumentError("Invalid type URL, unknown type: " + + return absl::InvalidArgumentError("Invalid type URL, unknown type: " + type_name); } ConvertEnumDescriptor(descriptor, enum_type); - return util::Status(); + return absl::Status(); } private: @@ -303,30 +304,30 @@ class DescriptorPoolTypeResolver : public TypeResolver { return url_prefix_ + "/" + descriptor->full_name(); } - util::Status ParseTypeUrl(const std::string& type_url, + absl::Status ParseTypeUrl(const std::string& type_url, std::string* type_name) { if (type_url.substr(0, url_prefix_.size() + 1) != url_prefix_ + "/") { - return util::InvalidArgumentError( - StrCat("Invalid type URL, type URLs must be of the form '", + return absl::InvalidArgumentError( + absl::StrCat("Invalid type URL, type URLs must be of the form '", url_prefix_, "/', got: ", type_url)); } *type_name = type_url.substr(url_prefix_.size() + 1); - return util::Status(); + return absl::Status(); } std::string DefaultValueAsString(const FieldDescriptor* descriptor) { switch (descriptor->cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: - return StrCat(descriptor->default_value_int32()); + return absl::StrCat(descriptor->default_value_int32()); break; case FieldDescriptor::CPPTYPE_INT64: - return StrCat(descriptor->default_value_int64()); + return absl::StrCat(descriptor->default_value_int64()); break; case FieldDescriptor::CPPTYPE_UINT32: - return StrCat(descriptor->default_value_uint32()); + return absl::StrCat(descriptor->default_value_uint32()); break; case FieldDescriptor::CPPTYPE_UINT64: - return StrCat(descriptor->default_value_uint64()); + return absl::StrCat(descriptor->default_value_uint64()); break; case FieldDescriptor::CPPTYPE_FLOAT: return SimpleFtoa(descriptor->default_value_float()); @@ -339,7 +340,7 @@ class DescriptorPoolTypeResolver : public TypeResolver { break; case FieldDescriptor::CPPTYPE_STRING: if (descriptor->type() == FieldDescriptor::TYPE_BYTES) { - return CEscape(descriptor->default_value_string()); + return absl::CEscape(descriptor->default_value_string()); } else { return descriptor->default_value_string(); } diff --git a/src/google/protobuf/wire_format.h b/src/google/protobuf/wire_format.h index 68586721ef71..a5fe685d0130 100644 --- a/src/google/protobuf/wire_format.h +++ b/src/google/protobuf/wire_format.h @@ -42,7 +42,7 @@ #include #include -#include +#include "absl/base/casts.h" #include #include #include @@ -338,7 +338,7 @@ inline WireFormatLite::WireType WireFormat::WireTypeForFieldType( // Some compilers don't like enum -> enum casts, so we implicit_cast to // int first. return WireFormatLite::WireTypeForFieldType( - static_cast(implicit_cast(type))); + static_cast(absl::implicit_cast(type))); } inline uint32_t WireFormat::MakeTag(const FieldDescriptor* field) { @@ -351,7 +351,7 @@ inline size_t WireFormat::TagSize(int field_number, // int first. return WireFormatLite::TagSize( field_number, - static_cast(implicit_cast(type))); + static_cast(absl::implicit_cast(type))); } inline void WireFormat::VerifyUTF8String(const char* data, int size, diff --git a/src/google/protobuf/wire_format_lite.cc b/src/google/protobuf/wire_format_lite.cc index 5ab1ca16c61a..ce78639e476e 100644 --- a/src/google/protobuf/wire_format_lite.cc +++ b/src/google/protobuf/wire_format_lite.cc @@ -44,6 +44,7 @@ #include #include #include +#include "absl/strings/str_cat.h" #include @@ -593,8 +594,8 @@ bool WireFormatLite::ReadBytes(io::CodedInputStream* input, std::string** p) { return ReadBytesToString(input, *p); } -void PrintUTF8ErrorLog(StringPiece message_name, - StringPiece field_name, const char* operation_str, +void PrintUTF8ErrorLog(absl::string_view message_name, + absl::string_view field_name, const char* operation_str, bool emit_stacktrace) { std::string stacktrace; (void)emit_stacktrace; // Parameter is used by Google-internal code. @@ -602,13 +603,13 @@ void PrintUTF8ErrorLog(StringPiece message_name, if (!field_name.empty()) { if (!message_name.empty()) { quoted_field_name = - StrCat(" '", message_name, ".", field_name, "'"); + absl::StrCat(" '", message_name, ".", field_name, "'"); } else { - quoted_field_name = StrCat(" '", field_name, "'"); + quoted_field_name = absl::StrCat(" '", field_name, "'"); } } std::string error_message = - StrCat("String field", quoted_field_name, + absl::StrCat("String field", quoted_field_name, " contains invalid UTF-8 data " "when ", operation_str, diff --git a/src/google/protobuf/wire_format_lite.h b/src/google/protobuf/wire_format_lite.h index f3097702bf60..754ea9d4e8c0 100644 --- a/src/google/protobuf/wire_format_lite.h +++ b/src/google/protobuf/wire_format_lite.h @@ -48,7 +48,7 @@ #include #include #include -#include +#include "absl/base/casts.h" #include #include #include @@ -821,19 +821,19 @@ inline size_t WireFormatLite::TagSize(int field_number, } inline uint32_t WireFormatLite::EncodeFloat(float value) { - return bit_cast(value); + return absl::bit_cast(value); } inline float WireFormatLite::DecodeFloat(uint32_t value) { - return bit_cast(value); + return absl::bit_cast(value); } inline uint64_t WireFormatLite::EncodeDouble(double value) { - return bit_cast(value); + return absl::bit_cast(value); } inline double WireFormatLite::DecodeDouble(uint64_t value) { - return bit_cast(value); + return absl::bit_cast(value); } // ZigZag Transform: Encodes signed integers so that they can be diff --git a/src/google/protobuf/wire_format_unittest.inc b/src/google/protobuf/wire_format_unittest.inc index d583ddde19a0..871a7035c252 100644 --- a/src/google/protobuf/wire_format_unittest.inc +++ b/src/google/protobuf/wire_format_unittest.inc @@ -43,7 +43,7 @@ #include #include #include -#include +#include "absl/base/casts.h" #include #include #include @@ -63,15 +63,15 @@ TEST(WireFormatTest, EnumsInSync) { // Verify that WireFormatLite::FieldType and WireFormatLite::CppType match // FieldDescriptor::Type and FieldDescriptor::CppType. - EXPECT_EQ(implicit_cast(FieldDescriptor::MAX_TYPE), - implicit_cast(WireFormatLite::MAX_FIELD_TYPE)); - EXPECT_EQ(implicit_cast(FieldDescriptor::MAX_CPPTYPE), - implicit_cast(WireFormatLite::MAX_CPPTYPE)); + EXPECT_EQ(absl::implicit_cast(FieldDescriptor::MAX_TYPE), + absl::implicit_cast(WireFormatLite::MAX_FIELD_TYPE)); + EXPECT_EQ(absl::implicit_cast(FieldDescriptor::MAX_CPPTYPE), + absl::implicit_cast(WireFormatLite::MAX_CPPTYPE)); for (int i = 1; i <= WireFormatLite::MAX_FIELD_TYPE; i++) { - EXPECT_EQ(implicit_cast(FieldDescriptor::TypeToCppType( + EXPECT_EQ(absl::implicit_cast(FieldDescriptor::TypeToCppType( static_cast(i))), - implicit_cast(WireFormatLite::FieldTypeToCppType( + absl::implicit_cast(WireFormatLite::FieldTypeToCppType( static_cast(i)))); } } diff --git a/src/google/protobuf/wrappers.pb.cc b/src/google/protobuf/wrappers.pb.cc index 7f78690e6e99..e68627fb4123 100644 --- a/src/google/protobuf/wrappers.pb.cc +++ b/src/google/protobuf/wrappers.pb.cc @@ -264,7 +264,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fwrappers_2eproto[] PROT "erspb\370\001\001\242\002\003GPB\252\002\036Google.Protobuf.WellKno" "wnTypesb\006proto3" ; -static ::_pbi::once_flag descriptor_table_google_2fprotobuf_2fwrappers_2eproto_once; +static ::absl::once_flag descriptor_table_google_2fprotobuf_2fwrappers_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_google_2fprotobuf_2fwrappers_2eproto = { false, false, 455, descriptor_table_protodef_google_2fprotobuf_2fwrappers_2eproto, "google/protobuf/wrappers.proto", diff --git a/src/google/protobuf/wrappers.pb.h b/src/google/protobuf/wrappers.pb.h index 1766742722b4..53d1c294fff1 100644 --- a/src/google/protobuf/wrappers.pb.h +++ b/src/google/protobuf/wrappers.pb.h @@ -191,7 +191,7 @@ class PROTOBUF_EXPORT DoubleValue final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.DoubleValue"; } protected: @@ -339,7 +339,7 @@ class PROTOBUF_EXPORT FloatValue final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.FloatValue"; } protected: @@ -487,7 +487,7 @@ class PROTOBUF_EXPORT Int64Value final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Int64Value"; } protected: @@ -635,7 +635,7 @@ class PROTOBUF_EXPORT UInt64Value final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.UInt64Value"; } protected: @@ -783,7 +783,7 @@ class PROTOBUF_EXPORT Int32Value final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.Int32Value"; } protected: @@ -931,7 +931,7 @@ class PROTOBUF_EXPORT UInt32Value final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.UInt32Value"; } protected: @@ -1079,7 +1079,7 @@ class PROTOBUF_EXPORT BoolValue final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.BoolValue"; } protected: @@ -1227,7 +1227,7 @@ class PROTOBUF_EXPORT StringValue final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.StringValue"; } protected: @@ -1380,7 +1380,7 @@ class PROTOBUF_EXPORT BytesValue final : private: friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + static ::absl::string_view FullMessageName() { return "google.protobuf.BytesValue"; } protected: