123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- discard """
- output: ""
- """
- import strscans, strutils
- block ParsePasswd:
- proc parsePasswd(content: string): seq[string] =
- result = @[]
- var idx = 0
- while true:
- var entry = ""
- if scanp(content, idx, +(~{'\L', '\0'} -> entry.add($_)), '\L'):
- result.add entry
- else:
- break
- const etc_passwd = """root:x:0:0:root:/root:/bin/bash
- daemon:x:1:1:daemon:/usr/sbin:/bin/sh
- bin:x:2:2:bin:/bin:/bin/sh
- sys:x:3:3:sys:/dev:/bin/sh
- nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
- messagebus:x:103:107::/var/run/dbus:/bin/false
- """
- const parsed_etc_passwd = @[
- "root:x:0:0:root:/root:/bin/bash",
- "daemon:x:1:1:daemon:/usr/sbin:/bin/sh",
- "bin:x:2:2:bin:/bin:/bin/sh",
- "sys:x:3:3:sys:/dev:/bin/sh",
- "nobody:x:65534:65534:nobody:/nonexistent:/bin/sh",
- "messagebus:x:103:107::/var/run/dbus:/bin/false",
- ]
- doAssert etc_passwd.parsePasswd == parsed_etc_passwd
- block LastNot:
- var idx : int
- idx = 0
- doAssert scanp("foo", idx, 'f', 'o', ~'a')
- idx = 0
- doAssert scanp("foo", idx, 'f', 'o', ~'o') == false
- idx = 0
- doAssert scanp("foox", idx, 'f', 'o', ~'o') == false
- idx = 0
- doAssert scanp("foox", idx, 'f', 'o', ~'a')
- block LastOptional:
- var idx = 0
- doAssert scanp("foo", idx, 'f', 'o', 'o', ?'o')
- block Tuple:
- var idx = 0
- doAssert scanp("foo", idx, ('f', 'o', 'o'))
- block NotWithOptional:
- var idx : int
- idx = 0
- doAssert scanp("bc", idx, ~(?'b', 'c')) == false
- idx = 0
- doAssert scanp("c", idx, ~(?'b', 'c')) == false
- idx = 0
- doAssert scanp("b", idx, ~(?'b', 'c'))
- block NotEmpty:
- var idx = 0
- doAssert scanp("", idx, ~()) == false
- block EmptyTuple:
- var idx = 0
- doAssert scanp("ab", idx, 'a', (), 'b')
- block Arrow:
- let text = "foo;bar;baz;"
- var idx = 0
- doAssert scanp(text, idx, +(~{';','\0'} -> (discard $_)), ';')
- doAssert scanp(text, idx, +(~{';','\0'} -> (discard $_)), ';')
- doAssert scanp(text, idx, +(~{';','\0'} -> (discard $_)), ';')
- doAssert scanp(text, idx, +(~{';','\0'} -> (discard $_)), ';') == false
- block issue15064:
- var nick1, msg1: string
- doAssert scanf("<abcd> a", "<$+> $+", nick1, msg1)
- doAssert nick1 == "abcd"
- doAssert msg1 == "a"
- var nick2, msg2: string
- doAssert(not scanf("<abcd> ", "<$+> $+", nick2, msg2))
- var nick3, msg3: string
- doAssert scanf("<abcd> ", "<$+> $*", nick3, msg3)
- doAssert nick3 == "abcd"
- doAssert msg3 == ""
- block:
- proc twoDigits(input: string; x: var int; start: int): int =
- if start+1 < input.len and input[start] == '0' and input[start+1] == '0':
- result = 2
- x = 13
- else:
- result = 0
- proc someSep(input: string; start: int; seps: set[char] = {';', ',', '-', '.'}): int =
- result = 0
- while start+result < input.len and input[start+result] in seps: inc result
- proc demangle(s: string; res: var string; start: int): int =
- while result+start < s.len and s[result+start] in {'_', '@'}: inc result
- res = ""
- while result+start < s.len and s[result+start] > ' ' and s[result+start] != '_':
- res.add s[result+start]
- inc result
- while result+start < s.len and s[result+start] > ' ':
- inc result
- proc parseGDB(resp: string): seq[string] =
- const
- digits = {'0'..'9'}
- hexdigits = digits + {'a'..'f', 'A'..'F'}
- whites = {' ', '\t', '\C', '\L'}
- result = @[]
- var idx = 0
- while true:
- var prc = ""
- var info = ""
- if scanp(resp, idx, *`whites`, '#', *`digits`, +`whites`, ?("0x", *`hexdigits`, " in "),
- demangle($input, prc, $index), *`whites`, '(', * ~ ')', ')',
- *`whites`, "at ", +(~{'\C', '\L'} -> info.add($_))):
- result.add prc & " " & info
- else:
- break
- var key, val: string
- var intval: int
- var floatval: float
- doAssert scanf("abc:: xyz 89 33.25", "$w$s::$s$w$s$i $f", key, val, intval, floatVal)
- doAssert key == "abc"
- doAssert val == "xyz"
- doAssert intval == 89
- doAssert floatVal == 33.25
- var binval: int
- var octval: int
- var hexval: int
- doAssert scanf("0b0101 0o1234 0xabcd", "$b$s$o$s$h", binval, octval, hexval)
- doAssert binval == 0b0101
- doAssert octval == 0o1234
- doAssert hexval == 0xabcd
- let xx = scanf("$abc", "$$$i", intval)
- doAssert xx == false
- let xx2 = scanf("$1234", "$$$i", intval)
- doAssert xx2
- let yy = scanf(";.--Breakpoint00 [output]",
- "$[someSep]Breakpoint${twoDigits}$[someSep({';','.','-'})] [$+]$.",
- intVal, key)
- doAssert yy
- doAssert key == "output"
- doAssert intVal == 13
- var ident = ""
- var idx = 0
- let zz = scanp("foobar x x x xWZ", idx, +{'a'..'z'} -> add(ident, $_), *(*{
- ' ', '\t'}, "x"), ~'U', "Z")
- doAssert zz
- doAssert ident == "foobar"
- const digits = {'0'..'9'}
- var year = 0
- var idx2 = 0
- if scanp("201655-8-9", idx2, `digits`{4, 6} -> (year = year * 10 + ord($_) -
- ord('0')), "-8", "-9"):
- doAssert year == 201655
- const gdbOut = """
- #0 @foo_96013_1208911747@8 (x0=...)
- at c:/users/anwender/projects/nim/temp.nim:11
- #1 0x00417754 in tempInit000 () at c:/users/anwender/projects/nim/temp.nim:13
- #2 0x0041768d in NimMainInner ()
- at c:/users/anwender/projects/nim/lib/system.nim:2605
- #3 0x004176b1 in NimMain ()
- at c:/users/anwender/projects/nim/lib/system.nim:2613
- #4 0x004176db in main (argc=1, args=0x712cc8, env=0x711ca8)
- at c:/users/anwender/projects/nim/lib/system.nim:2620"""
- const result = @["foo c:/users/anwender/projects/nim/temp.nim:11",
- "tempInit000 c:/users/anwender/projects/nim/temp.nim:13",
- "NimMainInner c:/users/anwender/projects/nim/lib/system.nim:2605",
- "NimMain c:/users/anwender/projects/nim/lib/system.nim:2613",
- "main c:/users/anwender/projects/nim/lib/system.nim:2620"]
- doAssert parseGDB(gdbOut) == result
- # bug #6487
- var count = 0
- proc test(): string =
- inc count
- result = ",123123"
- var a: int
- discard scanf(test(), ",$i", a)
- doAssert count == 1
- block:
- let input = """1-3 s: abc
- 15-18 9: def
- 15-18 A: ghi
- 15-18 _: jkl
- """
- var
- lo, hi: int
- w: string
- c: char
- res: int
- for line in input.splitLines:
- if line.scanf("$i-$i $c: $w", lo, hi, c, w):
- inc res
- doAssert res == 4
- block:
- #whenscanf testing
- let input = """1-3 s: abc
- 15-18 9: def
- 15-18 A: ghi
- 15-18 _: jkl
- """
- proc twoDigits(input: string; x: var int; start: int): int =
- if start+1 < input.len and input[start] == '0' and input[start+1] == '0':
- result = 2
- x = 13
- else:
- result = 0
- proc someSep(input: string; start: int; seps: set[char] = {';', ',', '-', '.'}): int =
- result = 0
- while start+result < input.len and input[start+result] in seps: inc result
- var res = 0
- for line in input.splitLines:
- let (success, lo, hi, c ,w) = scanTuple(line, "$i-$i $c: $w")
- if success:
- inc res
- doAssert res == 4
- let (_, key, val, intval, floatVal) = scanTuple("abc:: xyz 89 33.25", "$w$s::$s$w$s$i $f")
- doAssert key == "abc"
- doAssert val == "xyz"
- doAssert intval == 89
- doAssert floatVal == 33.25
- let (_, binVal, octVal, hexVal) = scanTuple("0b0101 0o1234 0xabcd", "$b$s$o$s$h", binval, octval, hexval)
- doAssert binval == 0b0101
- doAssert octval == 0o1234
- doAssert hexval == 0xabcd
- var (xx,_) = scanTuple("$abc", "$$$i")
- doAssert xx == false
- let (xx2, _) = block: scanTuple("$1234", "$$$i")
- doAssert xx2
- var (yy, intval2, key2) = scanTuple(";.--Breakpoint00 [output]",
- "$[someSep]Breakpoint${twoDigits}$[someSep({';','.','-'})] [$+]$.",
- int)
- doAssert yy
- doAssert key2 == "output"
- doAssert intVal2 == 13
|