src/tnetstring.nim
changeset 12 959dc81335c9
parent 9 c109bf5f2aa4
equal deleted inserted replaced
11:c302a311cedf 12:959dc81335c9
     1 #
     1 #
     2 # Copyright (c) 2015-2018, Mahlon E. Smith <mahlon@martini.nu>
     2 # Copyright (c) 2015-2021, Mahlon E. Smith <mahlon@martini.nu>
     3 # All rights reserved.
     3 # All rights reserved.
     4 # Redistribution and use in source and binary forms, with or without
     4 # Redistribution and use in source and binary forms, with or without
     5 # modification, are permitted provided that the following conditions are met:
     5 # modification, are permitted provided that the following conditions are met:
     6 #
     6 #
     7 #     * Redistributions of source code must retain the above copyright
     7 #     * Redistributions of source code must retain the above copyright
    95 import
    95 import
    96     hashes,
    96     hashes,
    97     parseutils,
    97     parseutils,
    98     strutils
    98     strutils
    99 
    99 
   100 const version = "0.1.2"
       
   101 
       
   102 type 
   100 type 
   103   TNetstringKind* = enum     ## enumeration of all valid types
   101   TNetstringKind* = enum     ## enumeration of all valid types
   104     TNetstringString,        ## a string literal
   102     TNetstringString,        ## a string literal
   105     TNetstringInt,           ## an integer literal
   103     TNetstringInt,           ## an integer literal
   106     TNetstringFloat,         ## a float literal
   104     TNetstringFloat,         ## a float literal
   136   raise newException( TNetstringParseError, msg )
   134   raise newException( TNetstringParseError, msg )
   137 
   135 
   138 
   136 
   139 proc newTNetstringString*( s: string ): TNetstringNode =
   137 proc newTNetstringString*( s: string ): TNetstringNode =
   140     ## Create a new String typed TNetstringNode.
   138     ## Create a new String typed TNetstringNode.
   141     new( result )
   139     result = TNetstringNode( kind: TNetstringString, str: s )
   142     result.kind = TNetstringString
       
   143     result.str = s
       
   144 
   140 
   145 
   141 
   146 proc newTNetstringInt*( i: BiggestInt ): TNetstringNode =
   142 proc newTNetstringInt*( i: BiggestInt ): TNetstringNode =
   147     ## Create a new Integer typed TNetstringNode.
   143     ## Create a new Integer typed TNetstringNode.
   148     new( result )
   144     result = TNetstringNode( kind: TNetstringInt, num: i )
   149     result.kind = TNetstringInt
       
   150     result.num = i
       
   151 
   145 
   152 
   146 
   153 proc newTNetstringFloat*( f: float ): TNetstringNode =
   147 proc newTNetstringFloat*( f: float ): TNetstringNode =
   154     ## Create a new Float typed TNetstringNode.
   148     ## Create a new Float typed TNetstringNode.
   155     new( result )
   149     result = TNetstringNode( kind: TNetstringFloat, fnum: f )
   156     result.kind = TNetstringFloat
       
   157     result.fnum = f
       
   158 
   150 
   159 
   151 
   160 proc newTNetstringBool*( b: bool ): TNetstringNode =
   152 proc newTNetstringBool*( b: bool ): TNetstringNode =
   161     ## Create a new Boolean typed TNetstringNode.
   153     ## Create a new Boolean typed TNetstringNode.
   162     new( result )
   154     result = TNetstringNode( kind: TNetstringBool, bval: b )
   163     result.kind = TNetstringBool
       
   164     result.bval = b
       
   165 
   155 
   166 
   156 
   167 proc newTNetstringNull*(): TNetstringNode =
   157 proc newTNetstringNull*(): TNetstringNode =
   168     ## Create a new nil typed TNetstringNode.
   158     ## Create a new nil typed TNetstringNode.
   169     new( result )
   159     result = TNetstringNode( kind: TNetstringNull )
   170     result.kind = TNetstringNull
       
   171 
   160 
   172 
   161 
   173 proc newTNetstringObject*(): TNetstringNode =
   162 proc newTNetstringObject*(): TNetstringNode =
   174     ## Create a new Object typed TNetstringNode.
   163     ## Create a new Object typed TNetstringNode.
   175     new( result )
   164     result = TNetstringNode( kind: TNetstringObject, fields: @[] )
   176     result.kind = TNetstringObject
       
   177     result.fields = @[]
       
   178 
   165 
   179 
   166 
   180 proc newTNetstringArray*(): TNetstringNode =
   167 proc newTNetstringArray*(): TNetstringNode =
   181     ## Create a new Array typed TNetstringNode.
   168     ## Create a new Array typed TNetstringNode.
   182     new( result )
   169     result = TNetstringNode( kind: TNetstringArray, elems: @[] )
   183     result.kind = TNetstringArray
       
   184     result.elems = @[]
       
   185 
   170 
   186 
   171 
   187 proc getStr*( node: TNetstringNode, default: string = "" ): string =
   172 proc getStr*( node: TNetstringNode, default: string = "" ): string =
   188     ## Retrieves the string value of a `TNetstringString TNetstringNodee`.
   173     ## Retrieves the string value of a `TNetstringString TNetstringNodee`.
   189     ## Returns ``default`` if ``node`` is not a ``TNetstringString``.
   174     ## Returns ``default`` if ``node`` is not a ``TNetstringString``.
   243         length       = data[ 0 .. sep_pos - 1 ].parseInt
   228         length       = data[ 0 .. sep_pos - 1 ].parseInt
   244         kind         = data[ sep_pos + length + 1 ]
   229         kind         = data[ sep_pos + length + 1 ]
   245         payload      = data[ sep_pos + 1 .. sep_pos + length ]
   230         payload      = data[ sep_pos + 1 .. sep_pos + length ]
   246         extra        = data[ sep_pos + length + 2 .. ^1 ]
   231         extra        = data[ sep_pos + length + 2 .. ^1 ]
   247 
   232 
   248     except ValueError, IndexError:
   233     except ValueError, IndexDefect:
   249         var msg = getCurrentExceptionMsg()
   234         var msg = getCurrentExceptionMsg()
   250         raiseParseErr( result, msg )
   235         raiseParseErr( result, msg )
   251 
   236 
   252     case kind:
   237     case kind:
   253         of ',':
   238         of ',':
   382             a.fields == b.fields
   367             a.fields == b.fields
   383 
   368 
   384 
   369 
   385 proc copy*( node: TNetstringNode ): TNetstringNode =
   370 proc copy*( node: TNetstringNode ): TNetstringNode =
   386     ## Perform a deep copy of TNetstringNode.
   371     ## Perform a deep copy of TNetstringNode.
   387     new( result )
   372     result = TNetstringNode( kind: node.kind, extra: node.extra )
   388     result.kind  = node.kind
       
   389     result.extra = node.extra
       
   390 
   373 
   391     case node.kind
   374     case node.kind
   392     of TNetstringString:
   375     of TNetstringString:
   393         result.str = node.str
   376         result.str = node.str
   394     of TNetstringInt:
   377     of TNetstringInt:
   414     assert( node.kind == TNetstringObject )
   397     assert( node.kind == TNetstringObject )
   415     for i in 0..node.fields.len - 1:
   398     for i in 0..node.fields.len - 1:
   416         if node.fields[i].key == key:
   399         if node.fields[i].key == key:
   417             node.fields.delete( i )
   400             node.fields.delete( i )
   418             return
   401             return
   419     raise newException( IndexError, "key not in object" )
   402     raise newException( IndexDefect, "key not in object" )
   420 
   403 
   421 
   404 
   422 proc hash*( node: TNetstringNode ): Hash =
   405 proc hash*( node: TNetstringNode ): Hash =
   423     ## Compute the hash for a TNetstringString node
   406     ## Compute the hash for a TNetstringString node
   424     return case node.kind
   407     return case node.kind