diff options
author | Jon Skeet <skeet@pobox.com> | 2015-06-12 11:18:37 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2015-06-12 11:18:37 +0100 |
commit | 43527448595b2e19a03747c494d1b33bced6086a (patch) | |
tree | 5a15780e964c608c141fd2be4c52182cc028bb8a /src/google | |
parent | 8fcde2cabd02ffe6ea061e259f67241f277368de (diff) | |
download | protobuf-43527448595b2e19a03747c494d1b33bced6086a.tar.gz protobuf-43527448595b2e19a03747c494d1b33bced6086a.tar.bz2 protobuf-43527448595b2e19a03747c494d1b33bced6086a.zip |
Use Length to check string/bytes fields for emptiness - it's faster than an equality check.
Diffstat (limited to 'src/google')
-rw-r--r-- | src/google/protobuf/compiler/csharp/csharp_primitive_field.cc | 8 |
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) { |