4141#include " absl/strings/str_format.h"
4242#include " absl/strings/str_join.h"
4343#include " absl/strings/str_replace.h"
44+ #include " absl/strings/str_split.h"
4445#include " absl/strings/strip.h"
4546
4647#include < algorithm>
@@ -79,16 +80,6 @@ static const int kNumKeyword = sizeof(kKeyword) / sizeof(char*);
7980
8081namespace {
8182
82- // The mode of operation for bytes fields. Historically JSPB always carried
83- // bytes as JS {string}, containing base64 content by convention. With binary
84- // and proto3 serialization the new convention is to represent it as binary
85- // data in Uint8Array. See b/26173701 for background on the migration.
86- enum BytesMode {
87- BYTES_DEFAULT, // Default type for getBytesField to return.
88- BYTES_B64, // Explicitly coerce to base64 string where needed.
89- BYTES_U8, // Explicitly coerce to Uint8Array where needed.
90- };
91-
9283bool IsReserved (absl::string_view ident) {
9384 for (int i = 0 ; i < kNumKeyword ; i++) {
9485 if (ident == kKeyword [i]) {
@@ -1159,7 +1150,7 @@ static const char* kRepeatedFieldArrayName = "repeatedFields_";
11591150std::string RepeatedFieldsArrayName (const GeneratorOptions& options,
11601151 const Descriptor* desc) {
11611152 return HasRepeatedFields (options, desc)
1162- ? (desc->name () + " ." + kRepeatedFieldArrayName )
1153+ ? absl::StrCat (desc->name (), " ." , kRepeatedFieldArrayName )
11631154 : " null" ;
11641155}
11651156
@@ -1177,7 +1168,7 @@ static const char* kOneofGroupArrayName = "oneofGroups_";
11771168std::string OneofFieldsArrayName (const GeneratorOptions& options,
11781169 const Descriptor* desc) {
11791170 return HasOneofFields (desc)
1180- ? (desc->name () + " ." + kOneofGroupArrayName )
1171+ ? absl::StrCat (desc->name (), " ." , kOneofGroupArrayName )
11811172 : " null" ;
11821173}
11831174
@@ -1250,7 +1241,7 @@ std::string JSExtensionsObjectName(const GeneratorOptions& options,
12501241 const FileDescriptor* from_file,
12511242 const Descriptor* desc) {
12521243 if (options.WantEs6 ()) {
1253- return TypeNames::JsName (desc->name ()) + " .extensions" ;
1244+ return TypeNames::JsName (std::string ( desc->name () )) + " .extensions" ;
12541245 } else if (desc->full_name () == " google.protobuf.bridge.MessageSet" ) {
12551246 // TODO(haberman): fix this for the kImportCommonJs case.
12561247 return " jspb.Message.messageSetExtensions" ;
@@ -2054,7 +2045,7 @@ void Generator::GenerateClassEs6(const GeneratorOptions& options,
20542045
20552046
20562047 std::string prefix = (desc->containing_type () == nullptr ) ?
2057- " export " : (" static " + desc->name () + " = " );
2048+ " export " : absl::StrCat (" static " , desc->name (), " = " );
20582049
20592050 printer->Print (" \n " );
20602051 printer->Print (
@@ -2320,7 +2311,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
23202311
23212312 const char * if_guard_start = options.WantEs6 () ? " " : " if (jspb.Message.GENERATE_TO_OBJECT) {\n " ;
23222313 const char * if_guard_end = options.WantEs6 () ? " " : " }\n " ;
2323- const std::string classSymbol = options.WantEs6 () ? desc->name () : GetMessagePath (options, desc);
2314+ const std::string classSymbol = options.WantEs6 () ? std::string ( desc->name () ) : GetMessagePath (options, desc);
23242315
23252316 printer->Print (
23262317 " \n "
@@ -3137,7 +3128,7 @@ void Generator::GenerateRepeatedPrimitiveHelperMethods(
31373128 // clang-format off
31383129 printer->Print (
31393130 " /**\n "
3140- " * Adds a value to the repeated field $field_name $ \n "
3131+ " * Adds a value to the repeated field $addername $ \n "
31413132 " *\n "
31423133 " * @param {$optionaltype$} value\n "
31433134 " * @param {number=} opt_index\n "
@@ -3186,7 +3177,7 @@ void Generator::GenerateRepeatedMessageHelperMethods(
31863177
31873178 printer->Print (
31883179 " /**\n "
3189- " * Adds a value to the repeated field $field_name$ \n "
3180+ " * Adds a value to the repeated field fieldName \n "
31903181 " *\n "
31913182 " * @param {!$optionaltype$=} opt_value\n "
31923183 " * @param {number=} opt_index\n "
@@ -3195,7 +3186,7 @@ void Generator::GenerateRepeatedMessageHelperMethods(
31953186 " $methodstart$(opt_value, opt_index) {\n "
31963187 " return jspb.Message.addTo$repeatedtag$WrapperField(" ,
31973188 " optionaltype" , JSTypeName (options, field, BYTES_DEFAULT),
3198- " field_name " , field->name (),
3189+ " fieldName " , field->name (),
31993190 " class" , classSymbol,
32003191 " methodstart" , adderMethodStart,
32013192 " addername" , adderName,
@@ -3265,7 +3256,7 @@ void Generator::GenerateClassDeserializeBinary(const GeneratorOptions& options,
32653256 // TODO(cfallin): Handle lazy decoding when requested by field option and/or
32663257 // by default for 'bytes' fields and packed repeated fields.
32673258
3268- const std::string classSymbol = desc->name ();
3259+ const absl::string_view classSymbol = desc->name ();
32693260
32703261 printer->Print (
32713262 " /**\n "
@@ -3279,7 +3270,7 @@ void Generator::GenerateClassDeserializeBinary(const GeneratorOptions& options,
32793270 " var reader = new jspb.BinaryReader(bytes);\n "
32803271 " var msg = new $class$;\n "
32813272 " return $class$.deserializeBinaryFromReader(msg, reader);\n " ,
3282- " methodstart" , this ->MethodStartStatic (options, classSymbol.c_str (), " deserializeBinary" ),
3273+ " methodstart" , this ->MethodStartStatic (options, classSymbol.data (), " deserializeBinary" ),
32833274 " class" , classSymbol);
32843275
32853276 GenerateMethodEnd (options, printer);
@@ -3304,7 +3295,7 @@ printer->Print(
33043295 " }\n "
33053296 " var field = reader.getFieldNumber();\n "
33063297 " switch (field) {\n " ,
3307- " methodstart" , MethodStartStatic (options, classSymbol.c_str (), " deserializeBinaryFromReader" ));
3298+ " methodstart" , MethodStartStatic (options, classSymbol.data (), " deserializeBinaryFromReader" ));
33083299
33093300 for (int i = 0 ; i < desc->field_count (); i++) {
33103301 if (!IgnoreField (desc->field (i))) {
@@ -3432,7 +3423,7 @@ void Generator::GenerateClassSerializeBinary(const GeneratorOptions& options,
34323423 io::Printer* printer,
34333424 const Descriptor* desc) const {
34343425
3435- const std::string classSymbol = desc->name ();
3426+ const absl::string_view classSymbol = desc->name ();
34363427
34373428 printer->Print (
34383429 " /**\n "
@@ -3444,7 +3435,7 @@ void Generator::GenerateClassSerializeBinary(const GeneratorOptions& options,
34443435 " var writer = new jspb.BinaryWriter();\n "
34453436 " this.constructor.serializeBinaryToWriter(this, writer);\n "
34463437 " return writer.getResultBuffer();\n " ,
3447- " methodstart" , MethodStart (options, classSymbol.c_str (), " serializeBinary" ),
3438+ " methodstart" , MethodStart (options, classSymbol.data (), " serializeBinary" ),
34483439 " class" , classSymbol);
34493440
34503441 GenerateMethodEnd (options, printer);
@@ -3461,7 +3452,7 @@ void Generator::GenerateClassSerializeBinary(const GeneratorOptions& options,
34613452 " */\n "
34623453 " $methodstart$(message, writer) {\n "
34633454 " var f = undefined;\n " ,
3464- " methodstart" , MethodStartStatic (options, classSymbol.c_str (), " serializeBinaryToWriter" ),
3455+ " methodstart" , MethodStartStatic (options, classSymbol.data (), " serializeBinaryToWriter" ),
34653456 " class" , classSymbol);
34663457
34673458for (int i = 0 ; i < desc->field_count (); i++) {
@@ -3640,9 +3631,9 @@ void Generator::GenerateEnum(const GeneratorOptions& options,
36403631 // TODO(reddaly): If the enum is defined at top-level, we need
36413632 // 'const <EnumName> = ' instead of '<EnumType> = '
36423633 const std::string enumNameForDefinition = options.WantEs6 () ? (
3643- enumNamePrefix + enumdesc->name ()
3634+ absl::StrCat ( enumNamePrefix, enumdesc->name () )
36443635 ) : (
3645- GetEnumPathPrefix (options, enumdesc) + enumdesc->name ()
3636+ absl::StrCat ( GetEnumPathPrefix (options, enumdesc), enumdesc->name () )
36463637 );
36473638 printer->Print (
36483639 " /**\n "
@@ -3695,7 +3686,7 @@ void Generator::GenerateExtension(const GeneratorOptions& options,
36953686 " * @type {!jspb.ExtensionFieldInfo<$extensionType$>}\n "
36963687 " */\n "
36973688 " $class$.$name$ = new jspb.ExtensionFieldInfo(\n " ,
3698- " class" , extension_scope_name, " name" , extension_object_field_name, " extensionType" ,
3689+ " class" , extension_scope_name, " name" , extension_object_field_name, " extensionType" ,
36993690 JSFieldTypeAnnotation (options, field,
37003691 /* is_setter_argument = */ false ,
37013692 /* force_present = */ true ,
@@ -3876,7 +3867,7 @@ void Generator::GenerateFileAndDeps(
38763867 const FileDescriptor* root, std::set<const FileDescriptor*>* all_files,
38773868 std::set<const FileDescriptor*>* generated) const {
38783869 // ES6 must use kOneOutputFilePerInputFile.
3879- GOOGLE_CHECK_NE (GeneratorOptions::kOneOutputFilePerInputFile ,
3870+ ABSL_CHECK_NE (GeneratorOptions::kOneOutputFilePerInputFile ,
38803871 options.output_mode ());
38813872 TypeNames type_names = TypeNames::NonEs6TypeNames (options);
38823873
@@ -3994,12 +3985,11 @@ TypeNames TypeNames::Es6TypeNames(
39943985 ReservedForLocalIdentifiers (codegen_file, reserved_aliases);
39953986 // RegisterTypesDefinedInGeneratedFile(codegen_file, full_name_to_alias);
39963987
3997- auto pick_name = [&](const std::string full_name, const std::string ideal_name) -> void {
3998- std::string base_candidate = ideal_name;
3988+ auto pick_name = [&](absl::string_view full_name, absl::string_view ideal_name) -> void {
3989+ std::string base_candidate = std::string ( ideal_name) ;
39993990 for (int i = -1 ; i < 10000 ; i++) {
40003991 if (i == 0 ) {
4001- base_candidate = full_name;
4002- ReplaceCharacters (&base_candidate, " ." , ' _' );
3992+ base_candidate = absl::StrReplaceAll (full_name, {{" ." , " _" }});
40033993 }
40043994 std::string candidate = base_candidate;
40053995 if (i > 0 ) {
@@ -4076,36 +4066,36 @@ std::string TypeNames::JsExpression(const google::protobuf::EnumDescriptor& desc
40764066}
40774067
40784068std::string TypeNames::SubmessageTypeRef (const FieldDescriptor* field) const {
4079- GOOGLE_CHECK (field->cpp_type () == FieldDescriptor::CPPTYPE_MESSAGE);
4080- GOOGLE_CHECK (this ->codegen_file == nullptr ||
4069+ ABSL_CHECK (field->cpp_type () == FieldDescriptor::CPPTYPE_MESSAGE);
4070+ ABSL_CHECK (this ->codegen_file == nullptr ||
40814071 this ->codegen_file == field->file ());
4082- GOOGLE_CHECK_NOTNULL (field->message_type ());
4072+ ABSL_CHECK (field->message_type () != nullptr );
40834073 return JsExpression (*field->message_type ());
40844074}
40854075
4086- std::string TypeNames::JsExpression (const std::string& full_name) const {
4087- GOOGLE_CHECK_OK (this ->options .WantEs6 ());
4076+ std::string TypeNames::JsExpression (absl::string_view full_name) const {
4077+ ABSL_CHECK (this ->options .WantEs6 ());
40884078
4089- auto iter = this ->map_ .find (full_name);
4079+ auto iter = this ->map_ .find (std::string ( full_name) );
40904080 if (iter != this ->map_ .end ()) {
40914081 return iter->second ;
40924082 }
40934083 // See if the parent full_name is available. If it is, use it as the prefix.
4094- auto parts = google::protobuf::Split (full_name, " ." , false );
4084+ const std::vector<std::string> parts = absl::StrSplit (full_name, " ." );
40954085 if (parts.size () > 1 ) {
40964086 std::vector<std::string> parent_parts = {parts.begin (), parts.end () - 1 };
4097- auto parent_path = google::protobuf::JoinStrings (
4087+ auto parent_path = absl::StrJoin (
40984088 parent_parts,
40994089 " ." );
4100- return this ->JsExpression (parent_path) + " ." + parts[parts.size () - 1 ];
4090+ return absl::StrCat ( this ->JsExpression (parent_path), " ." , parts[parts.size () - 1 ]) ;
41014091 }
4102- return std::string (" INVALID TYPE NAME " ) + full_name;
4092+ return absl::StrCat (" INVALID TYPE NAME " , full_name) ;
41034093}
41044094
4105- std::string TypeNames::JsName (const std::string& full_name) {
4095+ std::string TypeNames::JsName (absl::string_view full_name) {
41064096 // TODO(reddaly): There should probably be some logic to rename messages that
41074097 // conflict with reserved names, right?
4108- auto parts = google::protobuf::Split (full_name, " ." , false );
4098+ const std::vector<std::string> parts = absl::StrSplit (full_name, " ." );
41094099 return parts[parts.size ()-1 ];
41104100}
41114101
0 commit comments