tstrscans.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. discard """
  2. output: ""
  3. """
  4. import strscans
  5. block ParsePasswd:
  6. proc parsePasswd(content: string): seq[string] =
  7. result = @[]
  8. var idx = 0
  9. while true:
  10. var entry = ""
  11. if scanp(content, idx, +(~{'\L', '\0'} -> entry.add($_)), '\L'):
  12. result.add entry
  13. else:
  14. break
  15. const etc_passwd = """root:x:0:0:root:/root:/bin/bash
  16. daemon:x:1:1:daemon:/usr/sbin:/bin/sh
  17. bin:x:2:2:bin:/bin:/bin/sh
  18. sys:x:3:3:sys:/dev:/bin/sh
  19. nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
  20. messagebus:x:103:107::/var/run/dbus:/bin/false
  21. """
  22. const parsed_etc_passwd = @[
  23. "root:x:0:0:root:/root:/bin/bash",
  24. "daemon:x:1:1:daemon:/usr/sbin:/bin/sh",
  25. "bin:x:2:2:bin:/bin:/bin/sh",
  26. "sys:x:3:3:sys:/dev:/bin/sh",
  27. "nobody:x:65534:65534:nobody:/nonexistent:/bin/sh",
  28. "messagebus:x:103:107::/var/run/dbus:/bin/false",
  29. ]
  30. doAssert etc_passwd.parsePasswd == parsed_etc_passwd
  31. block LastNot:
  32. var idx : int
  33. idx = 0
  34. doAssert scanp("foo", idx, 'f', 'o', ~'a')
  35. idx = 0
  36. doAssert scanp("foo", idx, 'f', 'o', ~'o') == false
  37. idx = 0
  38. doAssert scanp("foox", idx, 'f', 'o', ~'o') == false
  39. idx = 0
  40. doAssert scanp("foox", idx, 'f', 'o', ~'a')
  41. block LastOptional:
  42. var idx = 0
  43. doAssert scanp("foo", idx, 'f', 'o', 'o', ?'o')
  44. block Tuple:
  45. var idx = 0
  46. doAssert scanp("foo", idx, ('f', 'o', 'o'))
  47. block NotWithOptional:
  48. var idx : int
  49. idx = 0
  50. doAssert scanp("bc", idx, ~(?'b', 'c')) == false
  51. idx = 0
  52. doAssert scanp("c", idx, ~(?'b', 'c')) == false
  53. idx = 0
  54. doAssert scanp("b", idx, ~(?'b', 'c'))
  55. block NotEmpty:
  56. var idx = 0
  57. doAssert scanp("", idx, ~()) == false
  58. block EmptyTuple:
  59. var idx = 0
  60. doAssert scanp("ab", idx, 'a', (), 'b')
  61. block Arrow:
  62. let text = "foo;bar;baz;"
  63. var idx = 0
  64. doAssert scanp(text, idx, +(~{';','\0'} -> (discard $_)), ';')
  65. doAssert scanp(text, idx, +(~{';','\0'} -> (discard $_)), ';')
  66. doAssert scanp(text, idx, +(~{';','\0'} -> (discard $_)), ';')
  67. doAssert scanp(text, idx, +(~{';','\0'} -> (discard $_)), ';') == false
  68. block issue15064:
  69. var nick1, msg1: string
  70. doAssert scanf("<abcd> a", "<$+> $+", nick1, msg1)
  71. doAssert nick1 == "abcd"
  72. doAssert msg1 == "a"
  73. var nick2, msg2: string
  74. doAssert(not scanf("<abcd> ", "<$+> $+", nick2, msg2))
  75. var nick3, msg3: string
  76. doAssert scanf("<abcd> ", "<$+> $*", nick3, msg3)
  77. doAssert nick3 == "abcd"
  78. doAssert msg3 == ""