aboutsummaryrefslogtreecommitdiffstats
path: root/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
diff options
context:
space:
mode:
authorMax Cai <maxtroy@google.com>2013-10-15 18:11:56 +0100
committerMax Cai <maxtroy@google.com>2013-10-15 18:11:56 +0100
commit334a7d1117a576bc6010c14677fb6444639c1dda (patch)
tree08e02cb00d3c80f193e51917fab0017b88093539 /src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
parent63e819adbb4d2d8215f5d3d8297dc50bf2fe3329 (diff)
downloadexternal_protobuf-334a7d1117a576bc6010c14677fb6444639c1dda.zip
external_protobuf-334a7d1117a576bc6010c14677fb6444639c1dda.tar.gz
external_protobuf-334a7d1117a576bc6010c14677fb6444639c1dda.tar.bz2
Fix repeated field merging semantics.
The public doc states that repeated fields are simply concatenated and doesn't impose a different semantics for packed fields. This CL fixes this for packed fields and adds tests covering all cases. Also fixed a bit of missed null-repeated-field treatments. Change-Id: Ie35277bb1a9f0b8171dc9d07b6adf9b9d3308de2
Diffstat (limited to 'src/google/protobuf/compiler/javanano/javanano_primitive_field.cc')
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_primitive_field.cc31
1 files changed, 18 insertions, 13 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
index 8097be8..0562a6e 100644
--- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
+++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
@@ -519,35 +519,40 @@ GenerateMergingCode(io::Printer* printer) const {
" arrayLength++;\n"
"}\n"
"input.rewindToPosition(startPos);\n"
- "this.$name$ = new $type$[arrayLength];\n"
- "for (int i = 0; i < arrayLength; i++) {\n"
- " this.$name$[i] = input.read$capitalized_type$();\n"
+ "int i = this.$name$ == null ? 0 : this.$name$.length;\n"
+ "$type$[] newArray = new $type$[i + arrayLength];\n"
+ "if (i != 0) {\n"
+ " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
"}\n"
+ "for (; i < newArray.length; i++) {\n"
+ " newArray[i] = input.read$capitalized_type$();\n"
+ "}\n"
+ "this.$name$ = newArray;\n"
"input.popLimit(limit);\n");
} else {
printer->Print(variables_,
"int arrayLength = com.google.protobuf.nano.WireFormatNano\n"
" .getRepeatedFieldArrayLength(input, $tag$);\n"
- "int i = this.$name$.length;\n");
+ "int i = this.$name$ == null ? 0 : this.$name$.length;\n");
if (GetJavaType(descriptor_) == JAVATYPE_BYTES) {
printer->Print(variables_,
- "byte[][] newArray = new byte[i + arrayLength][];\n"
- "System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
- "this.$name$ = newArray;\n");
+ "byte[][] newArray = new byte[i + arrayLength][];\n");
} else {
printer->Print(variables_,
- "$type$[] newArray = new $type$[i + arrayLength];\n"
- "System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
- "this.$name$ = newArray;\n");
+ "$type$[] newArray = new $type$[i + arrayLength];\n");
}
printer->Print(variables_,
- "for (; i < this.$name$.length - 1; i++) {\n"
- " this.$name$[i] = input.read$capitalized_type$();\n"
+ "if (i != 0) {\n"
+ " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
+ "}\n"
+ "for (; i < newArray.length - 1; i++) {\n"
+ " newArray[i] = input.read$capitalized_type$();\n"
" input.readTag();\n"
"}\n"
"// Last one without readTag.\n"
- "this.$name$[i] = input.read$capitalized_type$();\n");
+ "newArray[i] = input.read$capitalized_type$();\n"
+ "this.$name$ = newArray;\n");
}
}