11import JSONSerializerSwift
2- // PASTE GENERATED CODE BELOW :
3-
4- // Generated by the protocol buffer compiler. DO NOT EDIT!
5- // source: template.proto
6-
7- /**
8- * @fileoverview Generated Protocol Buffer code for file template.proto.
9- */
102
113public func == ( a: DemoMessage , b: DemoMessage ) -> Bool {
124 return (
13- a. demoDouble == b. demoDouble
14- && a. demoInt32 == b. demoInt32
15- && a. demoInt64 == b. demoInt64
16- && a. demoBool == b. demoBool
17- && a. demoString == b. demoString
18- && a. demoNestedMessage == b. demoNestedMessage
19- && a. demoRepeated == b. demoRepeated
20- && a. demoRepeatedNestedMessage == b. demoRepeatedNestedMessage
5+ a. sizeInBytes == b. sizeInBytes
6+ && a. demoDouble == b. demoDouble
7+ && a. demoInt32 == b. demoInt32
8+ && a. demoInt64 == b. demoInt64
9+ && a. demoBool == b. demoBool
10+ && a. demoString == b. demoString
11+ && a. demoNestedMessage == b. demoNestedMessage
12+ && a. demoRepeated == b. demoRepeated
13+ && a. demoRepeatedNestedMessage == b. demoRepeatedNestedMessage
2114 )
2215}
2316
2417public func == ( a: DemoMessage . DemoNestedMessage , b: DemoMessage . DemoNestedMessage ) -> Bool {
2518 return (
26- a. nestedString == b. nestedString
27- && a. nestedInt32 == b. nestedInt32
19+ a. sizeInBytes == b. sizeInBytes
20+ && a. nestedString == b. nestedString
21+ && a. nestedInt32 == b. nestedInt32
2822 )
2923}
3024
3125public class DemoMessage : Equatable {
26+ public let sizeInBytes : Int
3227 public let demoDouble : Float64 ?
3328 public let demoInt32 : Int ?
3429 public let demoInt64 : Int ?
@@ -38,7 +33,8 @@ public class DemoMessage: Equatable {
3833 public let demoRepeated : [ String ]
3934 public let demoRepeatedNestedMessage : [ DemoNestedMessage ] !
4035
41- init ( demoDouble: Float64 ? , demoInt32: Int ? , demoInt64: Int ? , demoBool: Bool ? , demoString: String ? , demoNestedMessage: DemoNestedMessage ? , demoRepeated: [ String ] , demoRepeatedNestedMessage: [ DemoNestedMessage ] ! ) {
36+ init ( sizeInBytes: Int , demoDouble: Float64 ? , demoInt32: Int ? , demoInt64: Int ? , demoBool: Bool ? , demoString: String ? , demoNestedMessage: DemoNestedMessage ? , demoRepeated: [ String ] , demoRepeatedNestedMessage: [ DemoNestedMessage ] ! ) {
37+ self . sizeInBytes = sizeInBytes
4238 self . demoDouble = demoDouble
4339 self . demoInt32 = demoInt32
4440 self . demoInt64 = demoInt64
@@ -90,6 +86,7 @@ public class DemoMessage: Equatable {
9086
9187 if let v = self . demoNestedMessage {
9288 w. writeTag ( 50 )
89+ w. writeVarInt ( v. sizeInBytes)
9390 v. toWriter ( w)
9491 }
9592
@@ -100,6 +97,7 @@ public class DemoMessage: Equatable {
10097
10198 for v in self . demoRepeatedNestedMessage {
10299 w. writeTag ( 66 )
100+ w. writeVarInt ( v. sizeInBytes)
103101 v. toWriter ( w)
104102 }
105103
@@ -142,30 +140,68 @@ public class DemoMessage: Equatable {
142140 case 42 :
143141 demoString = r. readString ( )
144142 case 50 :
143+ let limit = r. pushLimit ( r. readVarInt ( ) )
145144 demoNestedMessage = DemoMessage . DemoNestedMessage. fromReader ( r)
145+ r. popLimit ( limit)
146146 case 58 :
147147 demoRepeated. append ( r. readString ( ) )
148148 case 66 :
149+ let limit = r. pushLimit ( r. readVarInt ( ) )
149150 demoRepeatedNestedMessage. append ( DemoMessage . DemoNestedMessage. fromReader ( r) )
151+ r. popLimit ( limit)
150152 default :
151153 break loop
152154 }
153155 }
154156
155157 r. popTagMap ( )
156158
157- return DemoMessage ( demoDouble: demoDouble, demoInt32: demoInt32, demoInt64: demoInt64, demoBool: demoBool, demoString: demoString, demoNestedMessage: demoNestedMessage, demoRepeated: demoRepeated, demoRepeatedNestedMessage: demoRepeatedNestedMessage)
159+ let sizeInBytes = DemoMessage . sizeOf ( demoDouble, demoInt32: demoInt32, demoInt64: demoInt64, demoBool: demoBool, demoString: demoString, demoNestedMessage: demoNestedMessage, demoRepeated: demoRepeated, demoRepeatedNestedMessage: demoRepeatedNestedMessage)
160+ return DemoMessage ( sizeInBytes: sizeInBytes, demoDouble: demoDouble, demoInt32: demoInt32, demoInt64: demoInt64, demoBool: demoBool, demoString: demoString, demoNestedMessage: demoNestedMessage, demoRepeated: demoRepeated, demoRepeatedNestedMessage: demoRepeatedNestedMessage)
161+ }
162+
163+ class func sizeOf( demoDouble: Float64 ? , demoInt32: Int ? , demoInt64: Int ? , demoBool: Bool ? , demoString: String ? , demoNestedMessage: DemoNestedMessage ? , demoRepeated: [ String ] , demoRepeatedNestedMessage: [ DemoNestedMessage ] ! ) -> Int {
164+ var n = 0
165+
166+ if let _ = demoDouble {
167+ n += 1 + 8
168+ }
169+ if let v = demoInt32 {
170+ n += 1 + sizeOfVarInt( Int ( v) )
171+ }
172+ if let v = demoInt64 {
173+ n += 1 + sizeOfVarInt( Int ( v) )
174+ }
175+ if let _ = demoBool {
176+ n += 1 + 1
177+ }
178+ if let v = demoString {
179+ n += 1 + sizeOfString( v)
180+ }
181+ if let v = demoNestedMessage {
182+ n += 1 + sizeOfVarInt( v. sizeInBytes) + v. sizeInBytes
183+ }
184+ for v in demoRepeated {
185+ n += 1 + sizeOfString( v)
186+ }
187+ for v in demoRepeatedNestedMessage {
188+ n += 1 + sizeOfVarInt( v. sizeInBytes) + v. sizeInBytes
189+ }
190+
191+ return n
158192 }
159193
160194 public class func builder( ) -> DemoMessageBuilder {
161195 return DemoMessageBuilder ( )
162196 }
163197
164198 public class DemoNestedMessage : Equatable {
199+ public let sizeInBytes : Int
165200 public let nestedString : String ?
166201 public let nestedInt32 : Int ?
167202
168- init ( nestedString: String ? , nestedInt32: Int ? ) {
203+ init ( sizeInBytes: Int , nestedString: String ? , nestedInt32: Int ? ) {
204+ self . sizeInBytes = sizeInBytes
169205 self . nestedString = nestedString
170206 self . nestedInt32 = nestedInt32
171207 }
@@ -215,7 +251,21 @@ public class DemoMessage: Equatable {
215251
216252 r. popTagMap ( )
217253
218- return DemoNestedMessage ( nestedString: nestedString, nestedInt32: nestedInt32)
254+ let sizeInBytes = DemoNestedMessage . sizeOf ( nestedString, nestedInt32: nestedInt32)
255+ return DemoNestedMessage ( sizeInBytes: sizeInBytes, nestedString: nestedString, nestedInt32: nestedInt32)
256+ }
257+
258+ class func sizeOf( nestedString: String ? , nestedInt32: Int ? ) -> Int {
259+ var n = 0
260+
261+ if let v = nestedString {
262+ n += 1 + sizeOfString( v)
263+ }
264+ if let v = nestedInt32 {
265+ n += 1 + sizeOfVarInt( Int ( v) )
266+ }
267+
268+ return n
219269 }
220270
221271 public class func builder( ) -> DemoNestedMessageBuilder {
@@ -255,7 +305,8 @@ public class DemoMessage: Equatable {
255305 }
256306
257307 public func build( ) -> DemoNestedMessage {
258- return DemoNestedMessage ( nestedString: nestedString, nestedInt32: nestedInt32)
308+ let sizeInBytes = DemoNestedMessage . sizeOf ( nestedString, nestedInt32: nestedInt32)
309+ return DemoNestedMessage ( sizeInBytes: sizeInBytes, nestedString: nestedString, nestedInt32: nestedInt32)
259310 }
260311 }
261312
@@ -365,7 +416,23 @@ public class DemoMessageBuilder {
365416 }
366417
367418 public func build( ) -> DemoMessage {
368- return DemoMessage ( demoDouble: demoDouble, demoInt32: demoInt32, demoInt64: demoInt64, demoBool: demoBool, demoString: demoString, demoNestedMessage: demoNestedMessage, demoRepeated: demoRepeated, demoRepeatedNestedMessage: demoRepeatedNestedMessage)
419+ let sizeInBytes = DemoMessage . sizeOf ( demoDouble, demoInt32: demoInt32, demoInt64: demoInt64, demoBool: demoBool, demoString: demoString, demoNestedMessage: demoNestedMessage, demoRepeated: demoRepeated, demoRepeatedNestedMessage: demoRepeatedNestedMessage)
420+ return DemoMessage ( sizeInBytes: sizeInBytes, demoDouble: demoDouble, demoInt32: demoInt32, demoInt64: demoInt64, demoBool: demoBool, demoString: demoString, demoNestedMessage: demoNestedMessage, demoRepeated: demoRepeated, demoRepeatedNestedMessage: demoRepeatedNestedMessage)
369421 }
370422}
371423
424+ private func sizeOfVarInt( v: Int ) -> Int {
425+ var n = 0
426+ var x = v
427+ repeat {
428+ x = x >> 7
429+ n++
430+ } while ( x > 0 )
431+ return n
432+ }
433+
434+ private func sizeOfString( s: String ) -> Int {
435+ let b = s. utf8. count
436+ return sizeOfVarInt ( b) + b
437+ }
438+
0 commit comments