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