|
1 # |
|
2 # Copyright (c) 2015, Mahlon E. Smith <mahlon@martini.nu> |
|
3 # All rights reserved. |
|
4 # Redistribution and use in source and binary forms, with or without |
|
5 # modification, are permitted provided that the following conditions are met: |
|
6 # |
|
7 # * Redistributions of source code must retain the above copyright |
|
8 # notice, this list of conditions and the following disclaimer. |
|
9 # |
|
10 # * Redistributions in binary form must reproduce the above copyright |
|
11 # notice, this list of conditions and the following disclaimer in the |
|
12 # documentation and/or other materials provided with the distribution. |
|
13 # |
|
14 # * Neither the name of Mahlon E. Smith nor the names of his |
|
15 # contributors may be used to endorse or promote products derived |
|
16 # from this software without specific prior written permission. |
|
17 # |
|
18 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY |
|
19 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
21 # DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY |
|
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
28 |
|
29 ## This module implements a simple TNetstring parser and serializer. |
|
30 ## TNetString stands for "tagged netstring" and is a modification of Dan |
|
31 ## Bernstein's netstrings specification. TNetstrings allow for the same data |
|
32 ## structures as JSON but in a format that is resistant to buffer overflows |
|
33 ## and backward compatible with original netstrings. They make no assumptions |
|
34 ## about string contents, allowing for easy transmission of binary data mixed |
|
35 ## with strongly typed values. |
|
36 |
|
37 ## See http://cr.yp.to/proto/netstrings.txt and http://tnetstrings.org/ for additional information. |
|
38 ## |
|
39 ## This module borrows heavily (in both usage and code) from the nim JSON stdlib |
|
40 ## (json.nim) -- (c) Copyright 2015 Andreas Rumpf, Dominik Picheta. |
|
41 ## |
|
42 ## Usage example: |
|
43 ## |
|
44 ## .. code-block:: nim |
|
45 ## |
|
46 ## let |
|
47 ## tnetstr = "52:4:test,3:1.3^4:key2,4:true!6:things,12:1:1#1:2#1:3#]}" |
|
48 ## tnetobj = parse_tnetstring( tnetstr ) |
|
49 ## |
|
50 ## # tnetobj is now equivalent to the structure: |
|
51 ## # @[(key: test, val: 1.3), (key: key2, val: true), (key: things, val: @[1, 2, 3])] |
|
52 ## |
|
53 ## assert ( tnetobj.kind == TNetstringObject ) |
|
54 ## echo tnetobj[ "test" ] |
|
55 ## echo tnetobj[ "key2" ] |
|
56 ## for item in tnetobj[ "things" ]: |
|
57 ## echo item |
|
58 ## |
|
59 ## Results in: |
|
60 ## |
|
61 ## .. code-block:: nim |
|
62 ## |
|
63 ## 1.3 |
|
64 ## true |
|
65 ## 1 |
|
66 ## 2 |
|
67 ## 3 |
|
68 ## |
|
69 ## This module can also be used to reasonably create a serialized |
|
70 ## TNetstring, suitable for network transmission: |
|
71 ## |
|
72 ## .. code-block:: nim |
|
73 ## |
|
74 ## let |
|
75 ## number = 1000 |
|
76 ## list = @[ "thing1", "thing2" ] |
|
77 ## tnettop = newTNetstringArray() # top-level array |
|
78 ## tnetsub = newTNetstringArray() # sub array |
|
79 ## |
|
80 ## tnettop.add( newTNetstringInt(number) ) |
|
81 ## for item in list: |
|
82 ## tnetsub.add( newTNetstringString(item) ) |
|
83 ## tnettop.add( tnetsub ) |
|
84 ## |
|
85 ## # Equivalent to: @[1000, @[thing1, thing2]] |
|
86 ## echo dump_tnetstring( tnettop ) |
|
87 ## |
|
88 ## Results in: |
|
89 ## |
|
90 ## .. code-block:: nim |
|
91 ## |
|
92 ## 29:4:1000#18:6:thing1,6:thing2,]] |
|
93 ## |
|
94 |
|
95 import |
|
96 hashes, |
|
97 parseutils, |
|
98 strutils |
|
99 |
|
100 const version = "0.1.0" |
|
101 |
|
102 type |
|
103 TNetstringKind* = enum ## enumeration of all valid types |
|
104 TNetstringString, ## a string literal |
|
105 TNetstringInt, ## an integer literal |
|
106 TNetstringFloat, ## a float literal |
|
107 TNetstringBool, ## a ``true`` or ``false`` value |
|
108 TNetstringNull, ## the value ``null`` |
|
109 TNetstringObject, ## an object: the ``}`` token |
|
110 TNetstringArray ## an array: the ``]`` token |
|
111 |
|
112 TNetstringNode* = ref TNetstringNodeObj |
|
113 TNetstringNodeObj* {.acyclic.} = object |
|
114 extra: string |
|
115 case kind*: TNetstringKind |
|
116 of TNetstringString: |
|
117 str*: string |
|
118 of TNetstringInt: |
|
119 num*: BiggestInt |
|
120 of TNetstringFloat: |
|
121 fnum*: float |
|
122 of TNetstringBool: |
|
123 bval*: bool |
|
124 of TNetstringNull: |
|
125 nil |
|
126 of TNetstringObject: |
|
127 fields*: seq[ tuple[key: string, val: TNetstringNode] ] |
|
128 of TNetstringArray: |
|
129 elems*: seq[ TNetstringNode ] |
|
130 |
|
131 TNetstringParseError* = object of ValueError ## Raised for a TNetstring error |
|
132 |
|
133 |
|
134 proc raiseParseErr*( t: TNetstringNode, msg: string ) {.noinline, noreturn.} = |
|
135 ## Raises a `TNetstringParseError` exception. |
|
136 raise newException( TNetstringParseError, msg ) |
|
137 |
|
138 |
|
139 proc newTNetstringString*( s: string ): TNetstringNode = |
|
140 ## Create a new String typed TNetstringNode. |
|
141 new( result ) |
|
142 result.kind = TNetstringString |
|
143 result.str = s |
|
144 |
|
145 |
|
146 proc newTNetstringInt*( i: BiggestInt ): TNetstringNode = |
|
147 ## Create a new Integer typed TNetstringNode. |
|
148 new( result ) |
|
149 result.kind = TNetstringInt |
|
150 result.num = i |
|
151 |
|
152 |
|
153 proc newTNetstringFloat*( f: float ): TNetstringNode = |
|
154 ## Create a new Float typed TNetstringNode. |
|
155 new( result ) |
|
156 result.kind = TNetstringFloat |
|
157 result.fnum = f |
|
158 |
|
159 |
|
160 proc newTNetstringBool*( b: bool ): TNetstringNode = |
|
161 ## Create a new Boolean typed TNetstringNode. |
|
162 new( result ) |
|
163 result.kind = TNetstringBool |
|
164 result.bval = b |
|
165 |
|
166 |
|
167 proc newTNetstringNull*(): TNetstringNode = |
|
168 ## Create a new nil typed TNetstringNode. |
|
169 new( result ) |
|
170 result.kind = TNetstringNull |
|
171 |
|
172 |
|
173 proc newTNetstringObject*(): TNetstringNode = |
|
174 ## Create a new Object typed TNetstringNode. |
|
175 new( result ) |
|
176 result.kind = TNetstringObject |
|
177 result.fields = @[] |
|
178 |
|
179 |
|
180 proc newTNetstringArray*(): TNetstringNode = |
|
181 ## Create a new Array typed TNetstringNode. |
|
182 new( result ) |
|
183 result.kind = TNetstringArray |
|
184 result.elems = @[] |
|
185 |
|
186 |
|
187 proc parse_tnetstring*( data: string ): TNetstringNode = |
|
188 ## Given an encoded tnetstring, parse and return a TNetstringNode. |
|
189 var |
|
190 length: int |
|
191 kind: char |
|
192 payload: string |
|
193 extra: string |
|
194 |
|
195 let sep_pos = data.skipUntil( ':' ) |
|
196 if sep_pos == data.len: raiseParseErr( result, "Invalid data: No separator token found." ) |
|
197 |
|
198 try: |
|
199 length = data[ 0 .. sep_pos - 1 ].parseInt |
|
200 kind = data[ sep_pos + length + 1 ] |
|
201 payload = data[ sep_pos + 1 .. sep_pos + length ] |
|
202 extra = data[ sep_pos + length + 2 .. ^1 ] |
|
203 |
|
204 except ValueError, IndexError: |
|
205 var msg = getCurrentExceptionMsg() |
|
206 raiseParseErr( result, msg ) |
|
207 |
|
208 case kind: |
|
209 of ',': |
|
210 result = newTNetstringString( payload ) |
|
211 |
|
212 of '#': |
|
213 try: |
|
214 result = newTNetstringInt( payload.parseBiggestInt ) |
|
215 except ValueError: |
|
216 var msg = getCurrentExceptionMsg() |
|
217 raiseParseErr( result, msg ) |
|
218 |
|
219 of '^': |
|
220 try: |
|
221 result = newTNetstringFloat( payload.parseFloat ) |
|
222 except ValueError: |
|
223 var msg = getCurrentExceptionMsg() |
|
224 raiseParseErr( result, msg ) |
|
225 |
|
226 of '!': |
|
227 result = newTNetstringBool( payload == "true" ) |
|
228 |
|
229 of '~': |
|
230 if length != 0: raiseParseErr( result, "Invalid data: Payload must be 0 length for null." ) |
|
231 result = newTNetstringNull() |
|
232 |
|
233 of ']': |
|
234 result = newTNetstringArray() |
|
235 |
|
236 var subnode = parse_tnetstring( payload ) |
|
237 result.elems.add( subnode ) |
|
238 |
|
239 while subnode.extra != "": |
|
240 subnode = parse_tnetstring( subnode.extra ) |
|
241 result.elems.add( subnode ) |
|
242 |
|
243 of '}': |
|
244 result = newTNetstringObject() |
|
245 var key = parse_tnetstring( payload ) |
|
246 |
|
247 if ( key.extra == "" ): raiseParseErr( result, "Invalid data: Unbalanced tuple." ) |
|
248 if ( key.kind != TNetstringString ): raiseParseErr( result, "Invalid data: Object keys must be strings." ) |
|
249 |
|
250 var value = parse_tnetstring( key.extra ) |
|
251 result.fields = @[] |
|
252 result.fields.add( (key: key.str, val: value) ) |
|
253 |
|
254 while value.extra != "": |
|
255 var subkey = parse_tnetstring( value.extra ) |
|
256 if ( subkey.extra == "" ): raiseParseErr( result, "Invalid data: Unbalanced tuple." ) |
|
257 if ( subkey.kind != TNetstringString ): raiseParseErr( result, "Invalid data: Object keys must be strings." ) |
|
258 |
|
259 value = parse_tnetstring( subkey.extra ) |
|
260 result.fields.add( (key: subkey.str, val: value) ) |
|
261 |
|
262 else: |
|
263 raiseParseErr( result, "Invalid data: Unknown tnetstring type '$1'." % $kind ) |
|
264 |
|
265 result.extra = extra |
|
266 |
|
267 |
|
268 iterator items*( node: TNetstringNode ): TNetstringNode = |
|
269 ## Iterator for the items of `node`. `node` has to be a TNetstringArray. |
|
270 assert node.kind == TNetstringArray |
|
271 for i in items( node.elems ): |
|
272 yield i |
|
273 |
|
274 |
|
275 iterator mitems*( node: var TNetstringNode ): var TNetstringNode = |
|
276 ## Iterator for the items of `node`. `node` has to be a TNetstringArray. Items can be |
|
277 ## modified. |
|
278 assert node.kind == TNetstringArray |
|
279 for i in mitems( node.elems ): |
|
280 yield i |
|
281 |
|
282 |
|
283 iterator pairs*( node: TNetstringNode ): tuple[ key: string, val: TNetstringNode ] = |
|
284 ## Iterator for the child elements of `node`. `node` has to be a TNetstringObject. |
|
285 assert node.kind == TNetstringObject |
|
286 for key, val in items( node.fields ): |
|
287 yield ( key, val ) |
|
288 |
|
289 |
|
290 iterator mpairs*( node: var TNetstringNode ): var tuple[ key: string, val: TNetstringNode ] = |
|
291 ## Iterator for the child elements of `node`. `node` has to be a TNetstringObject. |
|
292 ## Items can be modified. |
|
293 assert node.kind == TNetstringObject |
|
294 for keyVal in mitems( node.fields ): |
|
295 yield keyVal |
|
296 |
|
297 |
|
298 proc `$`*( node: TNetstringNode ): string = |
|
299 ## Delegate stringification of `TNetstringNode` to its underlying object. |
|
300 return case node.kind: |
|
301 of TNetstringString: |
|
302 $node.str |
|
303 of TNetstringInt: |
|
304 $node.num |
|
305 of TNetstringFloat: |
|
306 $node.fnum |
|
307 of TNetstringBool: |
|
308 $node.bval |
|
309 of TNetstringNull: |
|
310 "(nil)" |
|
311 of TNetstringArray: |
|
312 $node.elems |
|
313 of TNetstringObject: |
|
314 $node.fields |
|
315 |
|
316 |
|
317 proc `==`* ( a, b: TNetstringNode ): bool = |
|
318 ## Check two TNetstring nodes for equality. |
|
319 if a.isNil: |
|
320 if b.isNil: return true |
|
321 return false |
|
322 elif b.isNil or a.kind != b.kind: |
|
323 return false |
|
324 else: |
|
325 return case a.kind |
|
326 of TNetstringString: |
|
327 a.str == b.str |
|
328 of TNetstringInt: |
|
329 a.num == b.num |
|
330 of TNetstringFloat: |
|
331 a.fnum == b.fnum |
|
332 of TNetstringBool: |
|
333 a.bval == b.bval |
|
334 of TNetstringNull: |
|
335 true |
|
336 of TNetstringArray: |
|
337 a.elems == b.elems |
|
338 of TNetstringObject: |
|
339 a.fields == b.fields |
|
340 |
|
341 |
|
342 proc copy*( node: TNetstringNode ): TNetstringNode = |
|
343 ## Perform a deep copy of TNetstringNode. |
|
344 new( result ) |
|
345 result.kind = node.kind |
|
346 result.extra = node.extra |
|
347 |
|
348 case node.kind |
|
349 of TNetstringString: |
|
350 result.str = node.str |
|
351 of TNetstringInt: |
|
352 result.num = node.num |
|
353 of TNetstringFloat: |
|
354 result.fnum = node.fnum |
|
355 of TNetstringBool: |
|
356 result.bval = node.bval |
|
357 of TNetstringNull: |
|
358 discard |
|
359 of TNetstringArray: |
|
360 result.elems = @[] |
|
361 for item in items( node ): |
|
362 result.elems.add( copy(item) ) |
|
363 of TNetstringObject: |
|
364 result.fields = @[] |
|
365 for key, value in items( node.fields ): |
|
366 result.fields.add( (key, copy(value)) ) |
|
367 |
|
368 |
|
369 proc delete*( node: TNetstringNode, key: string ) = |
|
370 ## Deletes ``node[key]`` preserving the order of the other (key, value)-pairs. |
|
371 assert( node.kind == TNetstringObject ) |
|
372 for i in 0..node.fields.len - 1: |
|
373 if node.fields[i].key == key: |
|
374 node.fields.delete( i ) |
|
375 return |
|
376 raise newException( IndexError, "key not in object" ) |
|
377 |
|
378 |
|
379 proc hash*( node: TNetstringNode ): THash = |
|
380 ## Compute the hash for a TNetstringString node |
|
381 return case node.kind |
|
382 of TNetstringString: |
|
383 hash( node.str ) |
|
384 of TNetstringInt: |
|
385 hash( node.num ) |
|
386 of TNetstringFloat: |
|
387 hash( node.fnum ) |
|
388 of TNetstringBool: |
|
389 hash( node.bval.int ) |
|
390 of TNetstringNull: |
|
391 hash( 0 ) |
|
392 of TNetstringArray: |
|
393 hash( node.elems ) |
|
394 of TNetstringObject: |
|
395 hash( node.fields ) |
|
396 |
|
397 |
|
398 proc len*( node: TNetstringNode ): int = |
|
399 ## If `node` is a `TNetstringArray`, it returns the number of elements. |
|
400 ## If `node` is a `TNetstringObject`, it returns the number of pairs. |
|
401 ## If `node` is a `TNetstringString`, it returns strlen. |
|
402 ## Else it returns 0. |
|
403 return case node.kind |
|
404 of TNetstringString: |
|
405 node.str.len |
|
406 of TNetstringArray: |
|
407 node.elems.len |
|
408 of TNetstringObject: |
|
409 node.fields.len |
|
410 else: |
|
411 0 |
|
412 |
|
413 |
|
414 proc `[]`*( node: TNetstringNode, name: string ): TNetstringNode = |
|
415 ## Gets a field from a `TNetstringNode`, which must not be nil. |
|
416 ## If the value at `name` does not exist, returns nil |
|
417 assert( not isNil(node) ) |
|
418 assert( node.kind == TNetstringObject ) |
|
419 for key, item in node: |
|
420 if key == name: |
|
421 return item |
|
422 return nil |
|
423 |
|
424 |
|
425 proc `[]`*( node: TNetstringNode, index: int ): TNetstringNode = |
|
426 ## Gets the node at `index` in an Array. Result is undefined if `index` |
|
427 ## is out of bounds. |
|
428 assert( not isNil(node) ) |
|
429 assert( node.kind == TNetstringArray ) |
|
430 return node.elems[ index ] |
|
431 |
|
432 |
|
433 proc hasKey*( node: TNetstringNode, key: string ): bool = |
|
434 ## Checks if `key` exists in `node`. |
|
435 assert( node.kind == TNetstringObject ) |
|
436 for k, item in items( node.fields ): |
|
437 if k == key: return true |
|
438 |
|
439 |
|
440 proc add*( parent, child: TNetstringNode ) = |
|
441 ## Appends `child` to a TNetstringArray node `parent`. |
|
442 assert( parent.kind == TNetstringArray ) |
|
443 parent.elems.add( child ) |
|
444 |
|
445 |
|
446 proc add*( node: TNetstringNode, key: string, val: TNetstringNode ) = |
|
447 ## Adds ``(key, val)`` pair to the TNetstringObject `node`. |
|
448 ## For speed reasons no check for duplicate keys is performed. |
|
449 ## (Note, ``[]=`` performs the check.) |
|
450 assert( node.kind == TNetstringObject ) |
|
451 node.fields.add( (key, val) ) |
|
452 |
|
453 |
|
454 proc `[]=`*( node: TNetstringNode, index: int, val: TNetstringNode ) = |
|
455 ## Sets an index for a `TNetstringArray`. |
|
456 assert( node.kind == TNetstringArray ) |
|
457 node.elems[ index ] = val |
|
458 |
|
459 |
|
460 proc `[]=`*( node: TNetstringNode, key: string, val: TNetstringNode ) = |
|
461 ## Sets a field from a `TNetstringObject`. Performs a check for duplicate keys. |
|
462 assert( node.kind == TNetstringObject ) |
|
463 for i in 0 .. node.fields.len - 1: |
|
464 if node.fields[i].key == key: |
|
465 node.fields[i].val = val |
|
466 return |
|
467 node.fields.add( (key, val) ) |
|
468 |
|
469 |
|
470 proc dump_tnetstring*( node: TNetstringNode ): string = |
|
471 ## Renders a TNetstring `node` as a regular string. |
|
472 case node.kind |
|
473 of TNetstringString: |
|
474 result = $( node.str.len ) & ':' & node.str & ',' |
|
475 of TNetstringInt: |
|
476 let str = $( node.num ) |
|
477 result = $( str.len ) & ':' & str & '#' |
|
478 of TNetstringFloat: |
|
479 let str = $( node.fnum ) |
|
480 result = $( str.len ) & ':' & str & '^' |
|
481 of TNetstringBool: |
|
482 result = if node.bval: "4:true!" else: "5:false!" |
|
483 of TNetstringNull: |
|
484 result = "0:~" |
|
485 of TNetstringArray: |
|
486 result = "" |
|
487 for n in node.items: |
|
488 result = result & n.dump_tnetstring |
|
489 result = $( result.len ) & ':' & result & ']' |
|
490 of TNetstringObject: |
|
491 result = "" |
|
492 for key, val in node.pairs: |
|
493 result = result & $( key.len ) & ':' & key & ',' # key |
|
494 result = result & val.dump_tnetstring # val |
|
495 result = $( result.len ) & ':' & result & '}' |
|
496 |
|
497 |
|
498 # |
|
499 # Tests! |
|
500 # |
|
501 when isMainModule: |
|
502 |
|
503 # Expected exceptions |
|
504 # |
|
505 try: |
|
506 discard parse_tnetstring( "totally invalid" ) |
|
507 except TNetstringParseError: |
|
508 doAssert( true, "invalid tnetstring" ) |
|
509 try: |
|
510 discard parse_tnetstring( "what:ever" ) |
|
511 except TNetstringParseError: |
|
512 doAssert( true, "bad length" ) |
|
513 try: |
|
514 discard parse_tnetstring( "3:yep~" ) |
|
515 except TNetstringParseError: |
|
516 doAssert( true, "null w/ > 0 length" ) |
|
517 try: |
|
518 discard parse_tnetstring( "8:1:1#1:1#}" ) |
|
519 except TNetstringParseError: |
|
520 doAssert( true, "hash with non-string key" ) |
|
521 try: |
|
522 discard parse_tnetstring( "7:4:test,}" ) |
|
523 except TNetstringParseError: |
|
524 doAssert( true, "hash with odd number of elements" ) |
|
525 try: |
|
526 discard parse_tnetstring( "2:25*" ) |
|
527 except TNetstringParseError: |
|
528 doAssert( true, "unknown netstring tag" ) |
|
529 |
|
530 # Equality |
|
531 # |
|
532 let tnet_int = parse_tnetstring( "1:1#" ) |
|
533 doAssert( tnet_int == tnet_int ) |
|
534 doAssert( tnet_int == parse_tnetstring( "1:1#" ) ) |
|
535 doAssert( parse_tnetstring( "0:~" ) == parse_tnetstring( "0:~" ) ) |
|
536 |
|
537 # Type detection |
|
538 # |
|
539 doAssert( tnet_int.kind == TNetstringInt ) |
|
540 doAssert( parse_tnetstring( "1:a," ).kind == TNetstringString ) |
|
541 doAssert( parse_tnetstring( "3:1.0^" ).kind == TNetstringFloat ) |
|
542 doAssert( parse_tnetstring( "5:false!" ).kind == TNetstringBool ) |
|
543 doAssert( parse_tnetstring( "0:~" ).kind == TNetstringNull ) |
|
544 doAssert( parse_tnetstring( "9:2:hi,1:1#}" ).kind == TNetstringObject ) |
|
545 doAssert( parse_tnetstring( "8:1:1#1:2#]" ).kind == TNetstringArray ) |
|
546 |
|
547 # Iteration (both array and tuple) |
|
548 # |
|
549 var |
|
550 keys: array[ 2, string ] |
|
551 vals: array[ 4, string ] |
|
552 k_idx = 0 |
|
553 idx = 0 |
|
554 for key, val in parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" ): |
|
555 keys[ idx ] = key |
|
556 idx = idx + 1 |
|
557 for item in val: |
|
558 vals[ k_idx ] = item.str |
|
559 k_idx = k_idx + 1 |
|
560 doAssert( keys == ["hi","there"] ) |
|
561 doassert( vals == ["a","b","c","d"] ) |
|
562 |
|
563 # Deep copies |
|
564 # |
|
565 var original = parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" ) |
|
566 var copied = original.copy |
|
567 doAssert( original == copied ) |
|
568 doAssert( original.repr != copied.repr ) |
|
569 doAssert( original.fields.pop.val.elems.pop.repr != copied.fields.pop.val.elems.pop.repr ) |
|
570 |
|
571 # Key deletion |
|
572 # |
|
573 var tnet_obj = parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" ) |
|
574 tnet_obj.delete( "hi" ) |
|
575 doAssert( tnet_obj.fields.len == 1 ) |
|
576 |
|
577 # Hashing |
|
578 # |
|
579 doAssert( tnet_int.hash == 1.hash ) |
|
580 doAssert( parse_tnetstring( "4:true!" ).hash == hash( true.int ) ) |
|
581 |
|
582 # Length checks. |
|
583 # |
|
584 tnet_obj = parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" ) |
|
585 doAssert( parse_tnetstring( "0:~" ).len == 0 ) |
|
586 doAssert( tnet_obj.len == 2 ) |
|
587 doAssert( parse_tnetstring( "8:1:1#1:2#]" ).len == 2 ) |
|
588 doAssert( parse_tnetstring( "5:hallo," ).len == 5 ) |
|
589 |
|
590 # Index accessors |
|
591 # |
|
592 tnet_obj = parse_tnetstring( "20:1:1#1:2#1:3#1:4#1:5#]" ) |
|
593 doAssert( tnet_obj[ 2 ].num == 3 ) |
|
594 |
|
595 # Key accessors |
|
596 # |
|
597 tnet_obj = parse_tnetstring( "11:2:hi,3:yep,}" ) |
|
598 doAssert( $tnet_obj["hi"] == "yep" ) |
|
599 doAssert( tnet_obj.has_key( "hi" ) == true ) |
|
600 doAssert( tnet_obj.has_key( "nope-not-here" ) == false ) |
|
601 |
|
602 # Adding elements to an existing TNetstring array |
|
603 # |
|
604 var tnet_array = newTNetstringArray() |
|
605 for i in 1 .. 10: |
|
606 tnet_obj = newTNetstringInt( i ) |
|
607 tnet_array.add( tnet_obj ) |
|
608 tnet_array[ 6 ] = newTNetstringString( "yep" ) |
|
609 doAssert( tnet_array.len == 10 ) |
|
610 doAssert( tnet_array[ 4 ].num == 5 ) |
|
611 doAssert( tnet_array[ 6 ].str == "yep" ) |
|
612 |
|
613 # Adding pairs to an existing TNetstring aobject. |
|
614 # |
|
615 tnet_obj = newTNetstringObject() |
|
616 tnet_obj.add( "yo", newTNetstringInt(1) ) |
|
617 tnet_obj.add( "yep", newTNetstringInt(2) ) |
|
618 doAssert( tnet_obj["yo"].num == 1 ) |
|
619 doAssert( tnet_obj["yep"].num == 2 ) |
|
620 doAssert( tnet_obj.len == 2 ) |
|
621 tnet_obj[ "more" ] = newTNetstringInt(1) |
|
622 tnet_obj[ "yo" ] = newTNetstringInt(1) # dup check |
|
623 doAssert( tnet_obj.len == 3 ) |
|
624 |
|
625 # Serialization. |
|
626 # |
|
627 var tstr = "308:9:givenName,6:Mahlon,16:departmentNumber,22:Information Technology," & |
|
628 "5:title,19:Senior Technologist,13:accountConfig,48:7:vmemail,4:true!7:allpage," & |
|
629 "5:false!7:galhide,0:~}13:homeDirectory,14:/home/m/mahlon,3:uid,6:mahlon,9:yubi" & |
|
630 "KeyId,12:vvidhghkhehj,5:gecos,12:Mahlon Smith,2:sn,5:Smith,14:employeeNumber,5:12921#}" |
|
631 tnet_obj = parse_tnetstring( tstr ) |
|
632 doAssert( tstr == tnet_obj.dump_tnetstring ) |
|
633 |
|
634 echo "* Tests passed!" |
|
635 |
|
636 |
|
637 while true and defined( testing ): |
|
638 for line in readline( stdin ).split_lines: |
|
639 let input = line.strip |
|
640 try: |
|
641 var tnetstring = parse_tnetstring( input ) |
|
642 echo " parsed --> ", tnetstring |
|
643 echo " serialized --> ", tnetstring.dump_tnetstring, "\n" |
|
644 except TNetstringParseError: |
|
645 echo input, " --> ", getCurrentExceptionMsg() |
|
646 |