Skip to content

Commit

Permalink
added cursor.toString (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq authored Sep 6, 2024
1 parent 6ac3671 commit 964666e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/lib/nifcursors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,22 @@ proc addParRi*(dest: var TokenBuf) =

proc toString*(b: TokenBuf): string =
result = nifstreams.toString(toOpenArray(b.data, 0, b.len-1))

proc span*(c: Cursor): int =
result = 0
var c = c
if c.kind == ParLe:
var nested = 0
while true:
inc c
inc result
if c.kind == ParRi:
if nested == 0: break
dec nested
elif c.kind == ParLe: inc nested
inc c
inc result

proc toString*(b: Cursor): string =
let counter = span(b)
result = nifstreams.toString(toOpenArray(cast[ptr UncheckedArray[PackedToken]](b.p), 0, counter-1))

0 comments on commit 964666e

Please sign in to comment.