@@ -89,7 +89,7 @@ enum BytesMode {
89
89
BYTES_U8, // Explicitly coerce to Uint8Array where needed.
90
90
};
91
91
92
- bool IsReserved (const std::string& ident) {
92
+ bool IsReserved (absl::string_view ident) {
93
93
for (int i = 0 ; i < kNumKeyword ; i++) {
94
94
if (ident == kKeyword [i]) {
95
95
return true ;
@@ -98,9 +98,8 @@ bool IsReserved(const std::string& ident) {
98
98
return false ;
99
99
}
100
100
101
- std::string GetSnakeFilename (const std::string& filename) {
102
- std::string snake_name = filename;
103
- return absl::StrReplaceAll (snake_name, {{" /" , " _" }});
101
+ std::string GetSnakeFilename (absl::string_view filename) {
102
+ return absl::StrReplaceAll (filename, {{" /" , " _" }});
104
103
}
105
104
106
105
// Given a filename like foo/bar/baz.proto, returns the corresponding JavaScript
@@ -135,7 +134,7 @@ std::string GetRootPath(const std::string& from_filename,
135
134
136
135
// Returns the alias we assign to the module of the given .proto filename
137
136
// when importing.
138
- std::string ModuleAlias (const std::string& filename) {
137
+ std::string ModuleAlias (absl::string_view filename) {
139
138
// This scheme could technically cause problems if a file includes any 2 of:
140
139
// foo/bar_baz.proto
141
140
// foo_bar_baz.proto
@@ -157,7 +156,7 @@ std::string GetNamespace(const GeneratorOptions& options,
157
156
if (!options.namespace_prefix .empty ()) {
158
157
return options.namespace_prefix ;
159
158
} else if (!file->package ().empty ()) {
160
- return " proto." + file->package ();
159
+ return absl::StrCat ( " proto." , file->package () );
161
160
} else {
162
161
return " proto" ;
163
162
}
@@ -204,7 +203,7 @@ std::string GetMessagePathPrefix(const GeneratorOptions& options,
204
203
// message descriptor.
205
204
std::string GetMessagePath (const GeneratorOptions& options,
206
205
const Descriptor* descriptor) {
207
- return GetMessagePathPrefix (options, descriptor) + descriptor->name ();
206
+ return absl::StrCat ( GetMessagePathPrefix (options, descriptor), descriptor->name () );
208
207
}
209
208
210
209
// Returns the fully normalized JavaScript path prefix for the given
@@ -219,7 +218,7 @@ std::string GetEnumPathPrefix(const GeneratorOptions& options,
219
218
// enumeration descriptor.
220
219
std::string GetEnumPath (const GeneratorOptions& options,
221
220
const EnumDescriptor* enum_descriptor) {
222
- return GetEnumPathPrefix (options, enum_descriptor) + enum_descriptor->name ();
221
+ return absl::StrCat ( GetEnumPathPrefix (options, enum_descriptor), enum_descriptor->name () );
223
222
}
224
223
225
224
std::string MaybeCrossFileRef (const GeneratorOptions& options,
@@ -230,9 +229,9 @@ std::string MaybeCrossFileRef(const GeneratorOptions& options,
230
229
from_file != to_message->file ()) {
231
230
// Cross-file ref in CommonJS needs to use the module alias instead of
232
231
// the global name.
233
- return ModuleAlias (to_message->file ()->name ()) +
234
- GetNestedMessageName (to_message->containing_type ()) + " ." +
235
- to_message->name ();
232
+ return absl::StrCat ( ModuleAlias (to_message->file ()->name ()),
233
+ GetNestedMessageName (to_message->containing_type ()), " ." ,
234
+ to_message->name ()) ;
236
235
} else {
237
236
// Within a single file we use a full name.
238
237
return GetMessagePath (options, to_message);
@@ -262,7 +261,7 @@ char ToLowerASCII(char c) {
262
261
}
263
262
}
264
263
265
- std::vector<std::string> ParseLowerUnderscore (const std::string& input) {
264
+ std::vector<std::string> ParseLowerUnderscore (absl::string_view input) {
266
265
std::vector<std::string> words;
267
266
std::string running = " " ;
268
267
for (auto c : input) {
@@ -281,7 +280,7 @@ std::vector<std::string> ParseLowerUnderscore(const std::string& input) {
281
280
return words;
282
281
}
283
282
284
- std::vector<std::string> ParseUpperCamel (const std::string& input) {
283
+ std::vector<std::string> ParseUpperCamel (absl::string_view input) {
285
284
std::vector<std::string> words;
286
285
std::string running = " " ;
287
286
for (auto c : input) {
@@ -326,7 +325,7 @@ std::string ToUpperCamel(const std::vector<std::string>& words) {
326
325
// Based on code from descriptor.cc (Thanks Kenton!)
327
326
// Uppercases the entire string, turning ValueName into
328
327
// VALUENAME.
329
- std::string ToEnumCase (const std::string& input) {
328
+ std::string ToEnumCase (absl::string_view input) {
330
329
std::string result;
331
330
result.reserve (input.size ());
332
331
@@ -341,7 +340,7 @@ std::string ToEnumCase(const std::string& input) {
341
340
return result;
342
341
}
343
342
344
- std::string ToLower (const std::string& input) {
343
+ std::string ToLower (absl::string_view input) {
345
344
std::string result;
346
345
result.reserve (input.size ());
347
346
@@ -625,7 +624,7 @@ uint16_t DecodeUTF8Codepoint(uint8_t* bytes, size_t* length) {
625
624
// Returns false if |out| was truncated because |in| contained invalid UTF-8 or
626
625
// codepoints outside the BMP.
627
626
// TODO(b/115551870): Support codepoints outside the BMP.
628
- bool EscapeJSString (const std::string& in, std::string* out) {
627
+ bool EscapeJSString (absl::string_view in, std::string* out) {
629
628
size_t decoded = 0 ;
630
629
for (size_t i = 0 ; i < in.size (); i += decoded) {
631
630
uint16_t codepoint = 0 ;
@@ -697,7 +696,7 @@ bool EscapeJSString(const std::string& in, std::string* out) {
697
696
return true ;
698
697
}
699
698
700
- std::string EscapeBase64 (const std::string& in) {
699
+ std::string EscapeBase64 (absl::string_view in) {
701
700
static const char * kAlphabet =
702
701
" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
703
702
std::string result;
@@ -1074,7 +1073,7 @@ std::string JSFieldTypeAnnotation(const GeneratorOptions& options,
1074
1073
}
1075
1074
1076
1075
std::string JSBinaryMethodType (const FieldDescriptor* field, bool is_writer) {
1077
- std::string name = field->type_name ();
1076
+ std::string name = std::string ( field->type_name () );
1078
1077
if (name[0 ] >= ' a' && name[0 ] <= ' z' ) {
1079
1078
name[0 ] = (name[0 ] - ' a' ) + ' A' ;
1080
1079
}
@@ -1226,9 +1225,9 @@ std::string RelativeTypeName(const FieldDescriptor* field) {
1226
1225
field->cpp_type () == FieldDescriptor::CPPTYPE_MESSAGE);
1227
1226
// For a field with an enum or message type, compute a name relative to the
1228
1227
// path name of the message type containing this field.
1229
- std::string package = field->file ()->package ();
1230
- std::string containing_type = field->containing_type ()->full_name () + " ." ;
1231
- std::string type = (field->cpp_type () == FieldDescriptor::CPPTYPE_ENUM)
1228
+ absl::string_view package = field->file ()->package ();
1229
+ std::string containing_type = absl::StrCat ( field->containing_type ()->full_name (), " ." ) ;
1230
+ absl::string_view type = (field->cpp_type () == FieldDescriptor::CPPTYPE_ENUM)
1232
1231
? field->enum_type ()->full_name ()
1233
1232
: field->message_type ()->full_name ();
1234
1233
@@ -1245,7 +1244,7 @@ std::string RelativeTypeName(const FieldDescriptor* field) {
1245
1244
}
1246
1245
}
1247
1246
1248
- return type.substr (prefix);
1247
+ return std::string ( type) .substr (prefix);
1249
1248
}
1250
1249
1251
1250
std::string JSExtensionsObjectName (const GeneratorOptions& options,
@@ -1285,8 +1284,8 @@ std::string FieldDefinition(const GeneratorOptions& options,
1285
1284
} else {
1286
1285
value_type = ProtoTypeName (options, value_field);
1287
1286
}
1288
- return absl::StrFormat (" map<%s, %s> %s = %d;" , key_type. c_str () ,
1289
- value_type. c_str () , field->name (). c_str (),
1287
+ return absl::StrFormat (" map<%s, %s> %s = %d;" , key_type,
1288
+ value_type, field->name (),
1290
1289
field->number ());
1291
1290
} else {
1292
1291
std::string qualifier =
@@ -3588,8 +3587,8 @@ bool Generator::GenerateFile(const FileDescriptor* file,
3588
3587
std::string filename =
3589
3588
options.output_dir + " /" +
3590
3589
GetJSFilename (options, use_short_name
3591
- ? file->name ().substr (file->name ().rfind (' /' ))
3592
- : file->name ());
3590
+ ? std::string ( file->name ().substr (file->name ().rfind (' /' ) ))
3591
+ : std::string ( file->name () ));
3593
3592
std::unique_ptr<io::ZeroCopyOutputStream> output (context->Open (filename));
3594
3593
ABSL_CHECK (output);
3595
3594
GeneratedCodeInfo annotations;
@@ -3649,12 +3648,12 @@ void Generator::GenerateFile(const GeneratorOptions& options,
3649
3648
}
3650
3649
3651
3650
for (int i = 0 ; i < file->dependency_count (); i++) {
3652
- const std::string& name = file->dependency (i)->name ();
3651
+ const std::string name = std::string ( file->dependency (i)->name () );
3653
3652
printer->Print (
3654
3653
" var $alias$ = require('$file$');\n "
3655
3654
" goog.object.extend(proto, $alias$);\n " ,
3656
3655
" alias" , ModuleAlias (name), " file" ,
3657
- GetRootPath (file->name (), name) + GetJSFilename (options, name));
3656
+ GetRootPath (std::string ( file->name () ), name) + GetJSFilename (options, name));
3658
3657
}
3659
3658
}
3660
3659
0 commit comments