when copy&pasting your code, i get an IndexError. The problem starts here:
proc decode(this: Decoder, source: string) : (BencodeType, int) =
var curchar = source[0]
var idx = 0
while idx < source.len:
curchar = source[idx]
case curchar
of 'i':
let pair = this.decode_i(source[idx..source.len])
let obj = pair[0]
let nextobjpos = pair[1]
idx += nextobjpos
return (obj, idx)
of 'l':
let pair = this.decode_l(source[idx..source.len])
let obj = pair[0]
let nextobjpos = pair[1]
idx += nextobjpos
return (obj, idx)
of 'd':
let pair = this.decode_d(source[idx..source.len])
let obj = pair[0]
let nextobjpos = pair[1]
idx += nextobjpos
return (obj, idx)
else:
let pair = this.decode_s(source[idx..source.len])
let obj = pair[0]
let nextobjpos = pair[1]
idx += nextobjpos
return (obj, idx)
source.len must give that error on string slicing, it should be len(source)-1. If i change that, the testcode compiles and works ... it seems.
The next problem is, that the decode_s method is only working, when len(source) < 10, otherwise:
testObjects[BencodeType(kind: btString, s:"this is a test")] = "14:this is a test"