tstrutil.nim 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. discard """
  2. output: "ha/home/a1xyz/usr/bin"
  3. """
  4. # test the new strutils module
  5. import
  6. strutils
  7. import macros
  8. template rejectParse(e) =
  9. try:
  10. discard e
  11. raise newException(AssertionError, "This was supposed to fail: $#!" % astToStr(e))
  12. except ValueError: discard
  13. proc testStrip() =
  14. write(stdout, strip(" ha "))
  15. proc testRemoveSuffix =
  16. var s = "hello\n\r"
  17. s.removeSuffix
  18. assert s == "hello"
  19. s.removeSuffix
  20. assert s == "hello"
  21. s = "hello\n\n"
  22. s.removeSuffix
  23. assert s == "hello"
  24. s = "hello\r"
  25. s.removeSuffix
  26. assert s == "hello"
  27. s = "hello \n there"
  28. s.removeSuffix
  29. assert s == "hello \n there"
  30. s = "hello"
  31. s.removeSuffix("llo")
  32. assert s == "he"
  33. s.removeSuffix('e')
  34. assert s == "h"
  35. s = "hellos"
  36. s.removeSuffix({'s','z'})
  37. assert s == "hello"
  38. s.removeSuffix({'l','o'})
  39. assert s == "he"
  40. s = "aeiou"
  41. s.removeSuffix("")
  42. assert s == "aeiou"
  43. s = ""
  44. s.removeSuffix("")
  45. assert s == ""
  46. s = " "
  47. s.removeSuffix
  48. assert s == " "
  49. s = " "
  50. s.removeSuffix("")
  51. assert s == " "
  52. s = " "
  53. s.removeSuffix(" ")
  54. assert s == " "
  55. s = " "
  56. s.removeSuffix(' ')
  57. assert s == ""
  58. # Contrary to Chomp in other languages
  59. # empty string does not change behaviour
  60. s = "hello\r\n\r\n"
  61. s.removeSuffix("")
  62. assert s == "hello\r\n\r\n"
  63. proc testRemovePrefix =
  64. var s = "\n\rhello"
  65. s.removePrefix
  66. assert s == "hello"
  67. s.removePrefix
  68. assert s == "hello"
  69. s = "\n\nhello"
  70. s.removePrefix
  71. assert s == "hello"
  72. s = "\rhello"
  73. s.removePrefix
  74. assert s == "hello"
  75. s = "hello \n there"
  76. s.removePrefix
  77. assert s == "hello \n there"
  78. s = "hello"
  79. s.removePrefix("hel")
  80. assert s == "lo"
  81. s.removePrefix('l')
  82. assert s == "o"
  83. s = "hellos"
  84. s.removePrefix({'h','e'})
  85. assert s == "llos"
  86. s.removePrefix({'l','o'})
  87. assert s == "s"
  88. s = "aeiou"
  89. s.removePrefix("")
  90. assert s == "aeiou"
  91. s = ""
  92. s.removePrefix("")
  93. assert s == ""
  94. s = " "
  95. s.removePrefix
  96. assert s == " "
  97. s = " "
  98. s.removePrefix("")
  99. assert s == " "
  100. s = " "
  101. s.removePrefix(" ")
  102. assert s == " "
  103. s = " "
  104. s.removePrefix(' ')
  105. assert s == ""
  106. # Contrary to Chomp in other languages
  107. # empty string does not change behaviour
  108. s = "\r\n\r\nhello"
  109. s.removePrefix("")
  110. assert s == "\r\n\r\nhello"
  111. proc main() =
  112. testStrip()
  113. testRemoveSuffix()
  114. testRemovePrefix()
  115. for p in split("/home/a1:xyz:/usr/bin", {':'}):
  116. write(stdout, p)
  117. proc testDelete =
  118. var s = "0123456789ABCDEFGH"
  119. delete(s, 4, 5)
  120. assert s == "01236789ABCDEFGH"
  121. delete(s, s.len-1, s.len-1)
  122. assert s == "01236789ABCDEFG"
  123. delete(s, 0, 0)
  124. assert s == "1236789ABCDEFG"
  125. proc testIsAlphaNumeric =
  126. assert isAlphaNumeric("abcdABC1234") == true
  127. assert isAlphaNumeric("a") == true
  128. assert isAlphaNumeric("abcABC?1234") == false
  129. assert isAlphaNumeric("abcABC 1234") == false
  130. assert isAlphaNumeric(".") == false
  131. testIsAlphaNumeric()
  132. proc testIsDigit =
  133. assert isDigit("1") == true
  134. assert isDigit("1234") == true
  135. assert isDigit("abcABC?1234") == false
  136. assert isDigit(".") == false
  137. assert isDigit(":") == false
  138. testIsDigit()
  139. proc testFind =
  140. assert "0123456789ABCDEFGH".find('A') == 10
  141. assert "0123456789ABCDEFGH".find('A', 5) == 10
  142. assert "0123456789ABCDEFGH".find('A', 5, 10) == 10
  143. assert "0123456789ABCDEFGH".find('A', 5, 9) == -1
  144. assert "0123456789ABCDEFGH".find("A") == 10
  145. assert "0123456789ABCDEFGH".find("A", 5) == 10
  146. assert "0123456789ABCDEFGH".find("A", 5, 10) == 10
  147. assert "0123456789ABCDEFGH".find("A", 5, 9) == -1
  148. assert "0123456789ABCDEFGH".find({'A'..'C'}) == 10
  149. assert "0123456789ABCDEFGH".find({'A'..'C'}, 5) == 10
  150. assert "0123456789ABCDEFGH".find({'A'..'C'}, 5, 10) == 10
  151. assert "0123456789ABCDEFGH".find({'A'..'C'}, 5, 9) == -1
  152. proc testRFind =
  153. assert "0123456789ABCDEFGAH".rfind('A') == 17
  154. assert "0123456789ABCDEFGAH".rfind('A', 13) == 10
  155. assert "0123456789ABCDEFGAH".rfind('H', 13) == -1
  156. assert "0123456789ABCDEFGAH".rfind("A") == 17
  157. assert "0123456789ABCDEFGAH".rfind("A", 13) == 10
  158. assert "0123456789ABCDEFGAH".rfind("H", 13) == -1
  159. assert "0123456789ABCDEFGAH".rfind({'A'..'C'}) == 17
  160. assert "0123456789ABCDEFGAH".rfind({'A'..'C'}, 13) == 12
  161. assert "0123456789ABCDEFGAH".rfind({'G'..'H'}, 13) == -1
  162. proc testSplitLines() =
  163. let fixture = "a\nb\rc\r\nd"
  164. assert len(fixture.splitLines) == 4
  165. assert splitLines(fixture) == @["a", "b", "c", "d"]
  166. assert splitLines(fixture, keepEol=true) == @["a\n", "b\r", "c\r\n", "d"]
  167. proc testCountLines =
  168. proc assertCountLines(s: string) = assert s.countLines == s.splitLines.len
  169. assertCountLines("")
  170. assertCountLines("\n")
  171. assertCountLines("\n\n")
  172. assertCountLines("abc")
  173. assertCountLines("abc\n123")
  174. assertCountLines("abc\n123\n")
  175. assertCountLines("\nabc\n123")
  176. assertCountLines("\nabc\n123\n")
  177. proc testParseInts =
  178. # binary
  179. assert "0b1111".parseBinInt == 15
  180. assert "0B1111".parseBinInt == 15
  181. assert "1111".parseBinInt == 15
  182. assert "1110".parseBinInt == 14
  183. assert "1_1_1_1".parseBinInt == 15
  184. assert "0b1_1_1_1".parseBinInt == 15
  185. rejectParse "".parseBinInt
  186. rejectParse "_".parseBinInt
  187. rejectParse "0b".parseBinInt
  188. rejectParse "0b1234".parseBinInt
  189. # hex
  190. assert "0x72".parseHexInt == 114
  191. assert "0X72".parseHexInt == 114
  192. assert "#72".parseHexInt == 114
  193. assert "72".parseHexInt == 114
  194. assert "FF".parseHexInt == 255
  195. assert "ff".parseHexInt == 255
  196. assert "fF".parseHexInt == 255
  197. assert "0x7_2".parseHexInt == 114
  198. rejectParse "".parseHexInt
  199. rejectParse "_".parseHexInt
  200. rejectParse "0x".parseHexInt
  201. rejectParse "0xFFG".parseHexInt
  202. rejectParse "reject".parseHexInt
  203. # octal
  204. assert "0o17".parseOctInt == 15
  205. assert "0O17".parseOctInt == 15
  206. assert "17".parseOctInt == 15
  207. assert "10".parseOctInt == 8
  208. assert "0o1_0_0".parseOctInt == 64
  209. rejectParse "".parseOctInt
  210. rejectParse "_".parseOctInt
  211. rejectParse "0o".parseOctInt
  212. rejectParse "9".parseOctInt
  213. rejectParse "0o9".parseOctInt
  214. rejectParse "reject".parseOctInt
  215. testDelete()
  216. testFind()
  217. testRFind()
  218. testSplitLines()
  219. testCountLines()
  220. testParseInts()
  221. assert(insertSep($1000_000) == "1_000_000")
  222. assert(insertSep($232) == "232")
  223. assert(insertSep($12345, ',') == "12,345")
  224. assert(insertSep($0) == "0")
  225. assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)
  226. assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)
  227. assert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5)
  228. assert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3)
  229. assert(editDistance("prefix__hallo_suffix", "prefix") == 14)
  230. assert(editDistance("prefix__hallo_suffix", "suffix") == 14)
  231. assert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2)
  232. assert(editDistance("main", "malign") == 2)
  233. assert "/1/2/3".rfind('/') == 4
  234. assert "/1/2/3".rfind('/', 1) == 0
  235. assert "/1/2/3".rfind('0') == -1
  236. assert(toHex(100i16, 32) == "00000000000000000000000000000064")
  237. assert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C")
  238. assert "".parseHexStr == ""
  239. assert "00Ff80".parseHexStr == "\0\xFF\x80"
  240. try:
  241. discard "00Ff8".parseHexStr
  242. assert false, "Should raise ValueError"
  243. except ValueError:
  244. discard
  245. try:
  246. discard "0k".parseHexStr
  247. assert false, "Should raise ValueError"
  248. except ValueError:
  249. discard
  250. assert "".toHex == ""
  251. assert "\x00\xFF\x80".toHex == "00FF80"
  252. assert "0123456789abcdef".parseHexStr.toHex == "0123456789ABCDEF"
  253. assert(' '.repeat(8)== " ")
  254. assert(" ".repeat(8) == " ")
  255. assert(spaces(8) == " ")
  256. assert(' '.repeat(0) == "")
  257. assert(" ".repeat(0) == "")
  258. assert(spaces(0) == "")
  259. # bug #8911
  260. when true:
  261. static:
  262. let a = ""
  263. let a2 = a.replace("\n", "\\n")
  264. when true:
  265. static:
  266. let b = "b"
  267. let b2 = b.replace("\n", "\\n")
  268. when true:
  269. let c = ""
  270. let c2 = c.replace("\n", "\\n")
  271. main()
  272. #OUT ha/home/a1xyz/usr/bin