tre.nim 3.7 KB

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