aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
diff options
context:
space:
mode:
authorGravatar Jon Skeet <skeet@pobox.com>2015-06-12 11:18:37 +0100
committerGravatar Jon Skeet <skeet@pobox.com>2015-06-12 11:18:37 +0100
commit43527448595b2e19a03747c494d1b33bced6086a (patch)
tree5a15780e964c608c141fd2be4c52182cc028bb8a /src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
parent8fcde2cabd02ffe6ea061e259f67241f277368de (diff)
Use Length to check string/bytes fields for emptiness - it's faster than an equality check.
Diffstat (limited to 'src/google/protobuf/compiler/csharp/csharp_primitive_field.cc')
-rw-r--r--src/google/protobuf/compiler/csharp/csharp_primitive_field.cc8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
index 69056790..e48c6ba9 100644
--- a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
+++ b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
@@ -52,6 +52,10 @@ PrimitiveFieldGenerator::PrimitiveFieldGenerator(
// TODO(jonskeet): Make this cleaner...
is_value_type = descriptor->type() != FieldDescriptor::TYPE_STRING
&& descriptor->type() != FieldDescriptor::TYPE_BYTES;
+ if (!is_value_type) {
+ variables_["has_property_check"] = variables_["property_name"] + ".Length != 0";
+ variables_["other_has_property_check"] = "other." + variables_["property_name"] + ".Length != 0";
+ }
}
PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {
@@ -90,9 +94,11 @@ void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) {
}
void PrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) {
+ // Note: invoke the property setter rather than writing straight to the field,
+ // so that we can normalize "null to empty" for strings and bytes.
printer->Print(
variables_,
- "$name$_ = input.Read$capitalized_type_name$();\n");
+ "$property_name$ = input.Read$capitalized_type_name$();\n");
}
void PrimitiveFieldGenerator::GenerateSerializationCode(io::Printer* printer) {