tstrbasics.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. discard """
  2. targets: "c cpp js"
  3. matrix: "--gc:refc; --gc:arc"
  4. """
  5. import std/[strbasics, sugar, assertions]
  6. template strip2(input: string, args: varargs[untyped]): untyped =
  7. var a = input
  8. when varargsLen(args) > 0:
  9. strip(a, args)
  10. else:
  11. strip(a)
  12. a
  13. proc main() =
  14. block: # strip
  15. block: # bug #17173
  16. var a = " vhellov "
  17. strip(a)
  18. doAssert a == "vhellov"
  19. doAssert strip2(" vhellov ") == "vhellov"
  20. doAssert strip2(" vhellov ", leading = false) == " vhellov"
  21. doAssert strip2(" vhellov ", trailing = false) == "vhellov "
  22. doAssert strip2("vhellov", chars = {'v'}) == "hello"
  23. doAssert strip2("vhellov", leading = false, chars = {'v'}) == "vhello"
  24. doAssert strip2("blaXbla", chars = {'b', 'a'}) == "laXbl"
  25. doAssert strip2("blaXbla", chars = {'b', 'a', 'l'}) == "X"
  26. doAssert strip2("xxxxxx", chars={'x'}) == ""
  27. doAssert strip2("x", chars={'x'}) == ""
  28. doAssert strip2("x", chars={'1'}) == "x"
  29. doAssert strip2("", chars={'x'}) == ""
  30. doAssert strip2("xxx xxx", chars={'x'}) == " "
  31. doAssert strip2("xxx wind", chars={'x'}) == " wind"
  32. doAssert strip2("xxx iii", chars={'i'}) == "xxx "
  33. block:
  34. var a = "xxx iii"
  35. doAssert a.dup(strip(chars = {'i'})) == "xxx "
  36. doAssert a.dup(strip(chars = {' '})) == "xxx iii"
  37. doAssert a.dup(strip(chars = {'x'})) == " iii"
  38. doAssert a.dup(strip(chars = {'x', ' '})) == "iii"
  39. doAssert a.dup(strip(chars = {'x', 'i'})) == " "
  40. doAssert a.dup(strip(chars = {'x', 'i', ' '})).len == 0
  41. block:
  42. var a = "x i"
  43. doAssert a.dup(strip(chars = {'i'})) == "x "
  44. doAssert a.dup(strip(chars = {' '})) == "x i"
  45. doAssert a.dup(strip(chars = {'x'})) == " i"
  46. doAssert a.dup(strip(chars = {'x', ' '})) == "i"
  47. doAssert a.dup(strip(chars = {'x', 'i'})) == " "
  48. doAssert a.dup(strip(chars = {'x', 'i', ' '})).len == 0
  49. block:
  50. var a = ""
  51. doAssert a.dup(strip(chars = {'i'})).len == 0
  52. doAssert a.dup(strip(chars = {' '})).len == 0
  53. doAssert a.dup(strip(chars = {'x'})).len == 0
  54. doAssert a.dup(strip(chars = {'x', ' '})).len == 0
  55. doAssert a.dup(strip(chars = {'x', 'i'})).len == 0
  56. doAssert a.dup(strip(chars = {'x', 'i', ' '})).len == 0
  57. block:
  58. var a = " "
  59. doAssert a.dup(strip(chars = {'i'})) == " "
  60. doAssert a.dup(strip(chars = {' '})).len == 0
  61. doAssert a.dup(strip(chars = {'x'})) == " "
  62. doAssert a.dup(strip(chars = {'x', ' '})).len == 0
  63. doAssert a.dup(strip(chars = {'x', 'i'})) == " "
  64. doAssert a.dup(strip(chars = {'x', 'i', ' '})).len == 0
  65. block: # setSlice
  66. var a = "Hello, Nim!"
  67. doAssert a.dup(setSlice(7 .. 9)) == "Nim"
  68. doAssert a.dup(setSlice(0 .. 0)) == "H"
  69. doAssert a.dup(setSlice(0 .. 1)) == "He"
  70. doAssert a.dup(setSlice(0 .. 10)) == a
  71. doAssert a.dup(setSlice(1 .. 0)).len == 0
  72. doAssert a.dup(setSlice(20 .. -1)).len == 0
  73. doAssertRaises(AssertionDefect):
  74. discard a.dup(setSlice(-1 .. 1))
  75. doAssertRaises(AssertionDefect):
  76. discard a.dup(setSlice(1 .. 11))
  77. block: # add
  78. var a0 = "hi"
  79. var b0 = "foobar"
  80. when nimvm:
  81. discard # pending bug #15952
  82. else:
  83. a0.add b0.toOpenArray(1,3)
  84. doAssert a0 == "hioob"
  85. proc fn(c: openArray[char]): string =
  86. result.add c
  87. doAssert fn("def") == "def"
  88. doAssert fn(['d','\0', 'f'])[2] == 'f'
  89. static: main()
  90. main()