Add new option max_length for strings (issue #107)
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Mon, 20 Feb 2017 13:47:44 +0000 (15:47 +0200)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Mon, 20 Feb 2017 13:47:44 +0000 (15:47 +0200)
Max_size is the allocated size, so users had to add +1 for
the null terminator. Max_length does the +1 automatically
in the generator.

generator/nanopb_generator.py
generator/proto/nanopb.proto
tests/options/options.expected
tests/options/options.proto

index 6e5ebaf..ca60c03 100755 (executable)
@@ -270,6 +270,10 @@ class Field:
         # Parse field options
         if field_options.HasField("max_size"):
             self.max_size = field_options.max_size
+        
+        if desc.type == FieldD.TYPE_STRING and field_options.HasField("max_length"):
+            # max_length overrides max_size for strings
+            self.max_size = field_options.max_length + 1
 
         if field_options.HasField("max_count"):
             self.max_count = field_options.max_count
index f6fe4a2..7d39e1c 100644 (file)
@@ -32,8 +32,13 @@ enum IntSize {
 // fields.
 message NanoPBOptions {
   // Allocated size for 'bytes' and 'string' fields.
+  // For string fields, this should include the space for null terminator.
   optional int32 max_size = 1;
   
+  // Maximum length for 'string' fields. Setting this is equivalent
+  // to setting max_size to a value of length+1.
+  optional int32 max_length = 14;
+  
   // Allocated number of entries in arrays ('repeated' fields)
   optional int32 max_count = 2;
   
index 0769880..9e47e6a 100644 (file)
@@ -1,6 +1,7 @@
 char filesize\[20\];
 char msgsize\[30\];
 char fieldsize\[40\];
+char fieldlen\[41\];
 pb_callback_t int32_callback;
 \sEnumValue1 = 1
 Message5_EnumValue1
index 89bb086..c6ca5e2 100644 (file)
@@ -26,6 +26,7 @@ message Message3
 {
     option (nanopb_msgopt).msgid = 103;
     required string fieldsize = 1 [(nanopb).max_size = 40];
+    required string fieldlen = 2 [(nanopb).max_length = 40];
 }
 
 // Forced callback field