summary refs log tree commit diff stats
path: root/tests/stdlib/tstrscans.nim
diff options
context:
space:
mode:
authorJason Beetham <beefers331@gmail.com>2020-12-12 15:29:20 -0700
committerGitHub <noreply@github.com>2020-12-12 23:29:20 +0100
commit5a58440ebe4ba794f0127ebb6ad33824a92dfb58 (patch)
treef5eaa0476d456b6efd81c57c65b5adc784a5819a /tests/stdlib/tstrscans.nim
parentd15f63a0f2d431a516601530196ac01c26e00f53 (diff)
downloadNim-5a58440ebe4ba794f0127ebb6ad33824a92dfb58.tar.gz
Added strscans.scanTuple (#16300)
* Added since and changelog
Diffstat (limited to 'tests/stdlib/tstrscans.nim')
-rw-r--r--tests/stdlib/tstrscans.nim51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/stdlib/tstrscans.nim b/tests/stdlib/tstrscans.nim
index 8ca167837..d443c5dda 100644
--- a/tests/stdlib/tstrscans.nim
+++ b/tests/stdlib/tstrscans.nim
@@ -227,3 +227,54 @@ block:
     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
\ No newline at end of file