split.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import unittest, strutils
  2. include nre
  3. suite "string splitting":
  4. test "splitting strings":
  5. check("1 2 3 4 5 6 ".split(re" ") == @["1", "2", "3", "4", "5", "6", ""])
  6. check("1 2 ".split(re(" ")) == @["1", "", "2", "", ""])
  7. check("1 2".split(re(" ")) == @["1", "2"])
  8. check("foo".split(re("foo")) == @["", ""])
  9. check("".split(re"foo") == @[""])
  10. check("9".split(re"\son\s") == @["9"])
  11. test "captured patterns":
  12. check("12".split(re"(\d)") == @["", "1", "", "2", ""])
  13. test "maxsplit":
  14. check("123".split(re"", maxsplit = 2) == @["1", "23"])
  15. check("123".split(re"", maxsplit = 1) == @["123"])
  16. check("123".split(re"", maxsplit = -1) == @["1", "2", "3"])
  17. test "split with 0-length match":
  18. check("12345".split(re("")) == @["1", "2", "3", "4", "5"])
  19. check("".split(re"") == newSeq[string]())
  20. check("word word".split(re"\b") == @["word", " ", "word"])
  21. check("word\r\lword".split(re"(*ANYCRLF)(?m)$") == @["word", "\r\lword"])
  22. check("слово слово".split(re"(*U)(\b)") == @["", "слово", "", " ", "", "слово", ""])
  23. test "perl split tests":
  24. check("forty-two" .split(re"") .join(",") == "f,o,r,t,y,-,t,w,o")
  25. check("forty-two" .split(re"", 3) .join(",") == "f,o,rty-two")
  26. check("split this string" .split(re" ") .join(",") == "split,this,string")
  27. check("split this string" .split(re" ", 2) .join(",") == "split,this string")
  28. check("try$this$string" .split(re"\$") .join(",") == "try,this,string")
  29. check("try$this$string" .split(re"\$", 2) .join(",") == "try,this$string")
  30. check("comma, separated, values" .split(re", ") .join("|") == "comma|separated|values")
  31. check("comma, separated, values" .split(re", ", 2) .join("|") == "comma|separated, values")
  32. check("Perl6::Camelia::Test" .split(re"::") .join(",") == "Perl6,Camelia,Test")
  33. check("Perl6::Camelia::Test" .split(re"::", 2) .join(",") == "Perl6,Camelia::Test")
  34. check("split,me,please" .split(re",") .join("|") == "split|me|please")
  35. check("split,me,please" .split(re",", 2) .join("|") == "split|me,please")
  36. check("Hello World Goodbye Mars".split(re"\s+") .join(",") == "Hello,World,Goodbye,Mars")
  37. check("Hello World Goodbye Mars".split(re"\s+", 3).join(",") == "Hello,World,Goodbye Mars")
  38. check("Hello test" .split(re"(\s+)") .join(",") == "Hello, ,test")
  39. check("this will be split" .split(re" ") .join(",") == "this,will,be,split")
  40. check("this will be split" .split(re" ", 3) .join(",") == "this,will,be split")
  41. check("a.b" .split(re"\.") .join(",") == "a,b")
  42. check("" .split(re"") .len == 0)
  43. check(":" .split(re"") .len == 1)
  44. test "start position":
  45. check("abc".split(re"", start = 1) == @["b", "c"])
  46. check("abc".split(re"", start = 2) == @["c"])
  47. check("abc".split(re"", start = 3) == newSeq[string]())