tre.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import std/re
  2. import std/assertions
  3. proc testAll() =
  4. doAssert match("(a b c)", rex"\( .* \)")
  5. doAssert match("WHiLe", re("while", {reIgnoreCase}))
  6. doAssert "0158787".match(re"\d+")
  7. doAssert "ABC 0232".match(re"\w+\s+\d+")
  8. doAssert "ABC".match(rex"\d+ | \w+")
  9. {.push warnings:off.}
  10. doAssert matchLen("key", re"\b[a-zA-Z_]+[a-zA-Z_0-9]*\b") == 3
  11. {.pop.}
  12. var pattern = re"[a-z0-9]+\s*=\s*[a-z0-9]+"
  13. doAssert matchLen("key1= cal9", pattern) == 11
  14. doAssert find("_____abc_______", re"abc") == 5
  15. doAssert findBounds("_____abc_______", re"abc") == (5,7)
  16. var matches: array[6, string]
  17. if match("abcdefg", re"c(d)ef(g)", matches, 2):
  18. doAssert matches[0] == "d"
  19. doAssert matches[1] == "g"
  20. else:
  21. doAssert false
  22. if "abc" =~ re"(a)bcxyz|(\w+)":
  23. doAssert matches[1] == "abc"
  24. else:
  25. doAssert false
  26. if "abc" =~ re"(cba)?.*":
  27. doAssert matches[0] == ""
  28. else: doAssert false
  29. if "abc" =~ re"().*":
  30. doAssert matches[0] == ""
  31. else: doAssert false
  32. doAssert "var1=key; var2=key2".endsWith(re"\w+=\w+")
  33. doAssert("var1=key; var2=key2".replacef(re"(\w+)=(\w+)", "$1<-$2$2") ==
  34. "var1<-keykey; var2<-key2key2")
  35. doAssert("var1=key; var2=key2".replace(re"(\w+)=(\w+)", "$1<-$2$2") ==
  36. "$1<-$2$2; $1<-$2$2")
  37. var accum: seq[string] = @[]
  38. for word in split("00232this02939is39an22example111", re"\d+"):
  39. accum.add(word)
  40. doAssert(accum == @["", "this", "is", "an", "example", ""])
  41. accum = @[]
  42. for word in split("00232this02939is39an22example111", re"\d+", maxsplit=2):
  43. accum.add(word)
  44. doAssert(accum == @["", "this", "is39an22example111"])
  45. accum = @[]
  46. for word in split("AAA : : BBB", re"\s*:\s*"):
  47. accum.add(word)
  48. doAssert(accum == @["AAA", "", "BBB"])
  49. doAssert(split("abc", re"") == @["a", "b", "c"])
  50. doAssert(split("", re"") == @[])
  51. doAssert(split("a;b;c", re";") == @["a", "b", "c"])
  52. doAssert(split(";a;b;c", re";") == @["", "a", "b", "c"])
  53. doAssert(split(";a;b;c;", re";") == @["", "a", "b", "c", ""])
  54. doAssert(split("a;b;c;", re";") == @["a", "b", "c", ""])
  55. doAssert(split("00232this02939is39an22example111", re"\d+", maxsplit=2) == @["", "this", "is39an22example111"])
  56. for x in findAll("abcdef", re"^{.}", 3):
  57. doAssert x == "d"
  58. accum = @[]
  59. for x in findAll("abcdef", re".", 3):
  60. accum.add(x)
  61. doAssert(accum == @["d", "e", "f"])
  62. doAssert("XYZ".find(re"^\d*") == 0)
  63. doAssert("XYZ".match(re"^\d*") == true)
  64. block:
  65. var matches: array[16, string]
  66. if match("abcdefghijklmnop", re"(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)", matches):
  67. for i in 0..matches.high:
  68. doAssert matches[i] == $chr(i + 'a'.ord)
  69. else:
  70. doAssert false
  71. block: # Buffer based RE
  72. var cs: cstring = "_____abc_______"
  73. doAssert(cs.find(re"abc", bufSize=15) == 5)
  74. doAssert(cs.matchLen(re"_*abc", bufSize=15) == 8)
  75. doAssert(cs.matchLen(re"abc", start=5, bufSize=15) == 3)
  76. doAssert(cs.matchLen(re"abc", start=5, bufSize=7) == -1)
  77. doAssert(cs.matchLen(re"abc_*", start=5, bufSize=10) == 5)
  78. var accum: seq[string] = @[]
  79. for x in cs.findAll(re"[a-z]", start=3, bufSize=15):
  80. accum.add($x)
  81. doAssert(accum == @["a","b","c"])
  82. block: # bug #9306
  83. doAssert replace("bar", re"^", "foo") == "foobar"
  84. doAssert replace("foo", re"$", "bar") == "foobar"
  85. block: # bug #9437
  86. doAssert replace("foo", re"", "-") == "-f-o-o-"
  87. doAssert replace("ooo", re"o", "-") == "---"
  88. block: # bug #14468
  89. accum = @[]
  90. for word in split("this is an example", re"\b"):
  91. accum.add(word)
  92. doAssert(accum == @["this", " ", "is", " ", "an", " ", "example"])
  93. testAll()