247
bool AllAscii(const string& text) {
248
for (int i = 0; i < text.size(); i++) {
249
if ((text[i] & 0x80) != 0) {
256
string DefaultValue(const FieldDescriptor* field) {
257
// Switch on cpp_type since we need to know which default_value_* method
258
// of FieldDescriptor to call.
259
switch (field->cpp_type()) {
260
case FieldDescriptor::CPPTYPE_INT32:
261
return SimpleItoa(field->default_value_int32());
262
case FieldDescriptor::CPPTYPE_UINT32:
263
// Need to print as a signed int since Java has no unsigned.
264
return SimpleItoa(static_cast<int32>(field->default_value_uint32()));
265
case FieldDescriptor::CPPTYPE_INT64:
266
return SimpleItoa(field->default_value_int64()) + "L";
267
case FieldDescriptor::CPPTYPE_UINT64:
268
return SimpleItoa(static_cast<int64>(field->default_value_uint64())) +
270
case FieldDescriptor::CPPTYPE_DOUBLE:
271
return SimpleDtoa(field->default_value_double()) + "D";
272
case FieldDescriptor::CPPTYPE_FLOAT:
273
return SimpleFtoa(field->default_value_float()) + "F";
274
case FieldDescriptor::CPPTYPE_BOOL:
275
return field->default_value_bool() ? "true" : "false";
276
case FieldDescriptor::CPPTYPE_STRING:
277
if (field->type() == FieldDescriptor::TYPE_BYTES) {
278
if (field->has_default_value()) {
279
// See comments in Internal.java for gory details.
280
return strings::Substitute(
281
"com.google.protobuf.Internal.bytesDefaultValue(\"$0\")",
282
CEscape(field->default_value_string()));
284
return "com.google.protobuf.ByteString.EMPTY";
287
if (AllAscii(field->default_value_string())) {
288
// All chars are ASCII. In this case CEscape() works fine.
289
return "\"" + CEscape(field->default_value_string()) + "\"";
291
// See comments in Internal.java for gory details.
292
return strings::Substitute(
293
"com.google.protobuf.Internal.stringDefaultValue(\"$0\")",
294
CEscape(field->default_value_string()));
298
case FieldDescriptor::CPPTYPE_ENUM:
299
return ClassName(field->enum_type()) + "." +
300
field->default_value_enum()->name();
302
case FieldDescriptor::CPPTYPE_MESSAGE:
303
return ClassName(field->message_type()) + ".getDefaultInstance()";
305
// No default because we want the compiler to complain if any new
309
GOOGLE_LOG(FATAL) << "Can't get here.";
246
313
} // namespace java
247
314
} // namespace compiler
248
315
} // namespace protobuf