Skip to content

Commit

Permalink
Fix value parsing with =
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabben committed Feb 20, 2023
1 parent 53cfdef commit ca23567
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/sam_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,20 @@ template stringHasSep(s: string, index: int, sep: char): bool =
template isWhitespace(s: string, index: int): bool =
s[index] in Whitespace

template checkValueEndIndex() {.dirty.} =
valueEndIndex = last-1
if valueEndIndex == s.high or s[valueEndIndex+1] in Whitespace:
yield ((keyStartIndex, keyEndIndex), (valueStartIndex, valueEndIndex))
keyStartIndex = -1
keyEndIndex = -1
valueStartIndex = -1
valueEndIndex = -1
else:
valueEndIndex = -1

iterator keyValueSplit(s: string, sep: char, startFrom: int): (tuple[start: int, finish: int], tuple[start: int, finish: int]) =
## Common code for split procs
var last = startFrom
const sepLen = 1

var
keyStartIndex = -1
Expand All @@ -234,21 +244,18 @@ iterator keyValueSplit(s: string, sep: char, startFrom: int): (tuple[start: int,

while last <= len(s):
var first = last
while last < len(s) and not stringHasSep(s, last, sep) and not isWhitespace(s, last):
while last < len(s) and not isWhitespace(s, last) and not stringHasSep(s, last, sep):
inc(last)

if keyStartIndex == -1 and keyEndIndex == -1:
keyStartIndex = first
keyEndIndex = last-1
elif valueStartIndex == -1 and valueEndIndex == -1:
elif valueStartIndex == -1:
valueStartIndex = first
valueEndIndex = last-1
yield ((keyStartIndex, keyEndIndex), (valueStartIndex, valueEndIndex))
keyStartIndex = -1
keyEndIndex = -1
valueStartIndex = -1
valueEndIndex = -1
inc(last, sepLen)
checkValueEndIndex()
elif valueEndIndex == -1:
checkValueEndIndex()
inc(last)

{.push inline.}

Expand Down
6 changes: 6 additions & 0 deletions tests/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ block:
doAssert answer.dest.pub == "XXX"
doAssert answer.dest.priv == "YYY"

block:
let answer = Answer.fromString("DEST REPLY PUB=XX=X PRIV=YY=Y")
doAssert answer.kind == DestReply
doAssert answer.dest.pub == "XX=X"
doAssert answer.dest.priv == "YY=Y"

block:
let answer = Answer.fromString("PONG")
doAssert answer.kind == Pong
Expand Down

0 comments on commit ca23567

Please sign in to comment.