tstrutil.nim 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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', last=13) == 10
  155. assert "0123456789ABCDEFGAH".rfind('H', last=13) == -1
  156. assert "0123456789ABCDEFGAH".rfind("A") == 17
  157. assert "0123456789ABCDEFGAH".rfind("A", last=13) == 10
  158. assert "0123456789ABCDEFGAH".rfind("H", last=13) == -1
  159. assert "0123456789ABCDEFGAH".rfind({'A'..'C'}) == 17
  160. assert "0123456789ABCDEFGAH".rfind({'A'..'C'}, last=13) == 12
  161. assert "0123456789ABCDEFGAH".rfind({'G'..'H'}, last=13) == -1
  162. assert "0123456789ABCDEFGAH".rfind('A', start=18) == -1
  163. assert "0123456789ABCDEFGAH".rfind('A', start=11, last=17) == 17
  164. assert "0123456789ABCDEFGAH".rfind("0", start=0) == 0
  165. assert "0123456789ABCDEFGAH".rfind("0", start=1) == -1
  166. assert "0123456789ABCDEFGAH".rfind("H", start=11) == 18
  167. assert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=5) == 9
  168. assert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=10) == -1
  169. proc testSplitLines() =
  170. let fixture = "a\nb\rc\r\nd"
  171. assert len(fixture.splitLines) == 4
  172. assert splitLines(fixture) == @["a", "b", "c", "d"]
  173. assert splitLines(fixture, keepEol=true) == @["a\n", "b\r", "c\r\n", "d"]
  174. proc testCountLines =
  175. proc assertCountLines(s: string) = assert s.countLines == s.splitLines.len
  176. assertCountLines("")
  177. assertCountLines("\n")
  178. assertCountLines("\n\n")
  179. assertCountLines("abc")
  180. assertCountLines("abc\n123")
  181. assertCountLines("abc\n123\n")
  182. assertCountLines("\nabc\n123")
  183. assertCountLines("\nabc\n123\n")
  184. proc testParseInts =
  185. # binary
  186. assert "0b1111".parseBinInt == 15
  187. assert "0B1111".parseBinInt == 15
  188. assert "1111".parseBinInt == 15
  189. assert "1110".parseBinInt == 14
  190. assert "1_1_1_1".parseBinInt == 15
  191. assert "0b1_1_1_1".parseBinInt == 15
  192. rejectParse "".parseBinInt
  193. rejectParse "_".parseBinInt
  194. rejectParse "0b".parseBinInt
  195. rejectParse "0b1234".parseBinInt
  196. # hex
  197. assert "0x72".parseHexInt == 114
  198. assert "0X72".parseHexInt == 114
  199. assert "#72".parseHexInt == 114
  200. assert "72".parseHexInt == 114
  201. assert "FF".parseHexInt == 255
  202. assert "ff".parseHexInt == 255
  203. assert "fF".parseHexInt == 255
  204. assert "0x7_2".parseHexInt == 114
  205. rejectParse "".parseHexInt
  206. rejectParse "_".parseHexInt
  207. rejectParse "0x".parseHexInt
  208. rejectParse "0xFFG".parseHexInt
  209. rejectParse "reject".parseHexInt
  210. # octal
  211. assert "0o17".parseOctInt == 15
  212. assert "0O17".parseOctInt == 15
  213. assert "17".parseOctInt == 15
  214. assert "10".parseOctInt == 8
  215. assert "0o1_0_0".parseOctInt == 64
  216. rejectParse "".parseOctInt
  217. rejectParse "_".parseOctInt
  218. rejectParse "0o".parseOctInt
  219. rejectParse "9".parseOctInt
  220. rejectParse "0o9".parseOctInt
  221. rejectParse "reject".parseOctInt
  222. testDelete()
  223. testFind()
  224. testRFind()
  225. testSplitLines()
  226. testCountLines()
  227. testParseInts()
  228. assert(insertSep($1000_000) == "1_000_000")
  229. assert(insertSep($232) == "232")
  230. assert(insertSep($12345, ',') == "12,345")
  231. assert(insertSep($0) == "0")
  232. assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)
  233. assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)
  234. assert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5)
  235. assert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3)
  236. assert(editDistance("prefix__hallo_suffix", "prefix") == 14)
  237. assert(editDistance("prefix__hallo_suffix", "suffix") == 14)
  238. assert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2)
  239. assert(editDistance("main", "malign") == 2)
  240. assert "/1/2/3".rfind('/') == 4
  241. assert "/1/2/3".rfind('/', last=1) == 0
  242. assert "/1/2/3".rfind('0') == -1
  243. assert(toHex(100i16, 32) == "00000000000000000000000000000064")
  244. assert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C")
  245. assert "".parseHexStr == ""
  246. assert "00Ff80".parseHexStr == "\0\xFF\x80"
  247. try:
  248. discard "00Ff8".parseHexStr
  249. assert false, "Should raise ValueError"
  250. except ValueError:
  251. discard
  252. try:
  253. discard "0k".parseHexStr
  254. assert false, "Should raise ValueError"
  255. except ValueError:
  256. discard
  257. assert "".toHex == ""
  258. assert "\x00\xFF\x80".toHex == "00FF80"
  259. assert "0123456789abcdef".parseHexStr.toHex == "0123456789ABCDEF"
  260. assert(' '.repeat(8)== " ")
  261. assert(" ".repeat(8) == " ")
  262. assert(spaces(8) == " ")
  263. assert(' '.repeat(0) == "")
  264. assert(" ".repeat(0) == "")
  265. assert(spaces(0) == "")
  266. # bug #11369
  267. var num: int64 = -1
  268. assert num.toBin(64) == "1111111111111111111111111111111111111111111111111111111111111111"
  269. assert num.toOct(24) == "001777777777777777777777"
  270. # bug #8911
  271. when true:
  272. static:
  273. let a = ""
  274. let a2 = a.replace("\n", "\\n")
  275. when true:
  276. static:
  277. let b = "b"
  278. let b2 = b.replace("\n", "\\n")
  279. when true:
  280. let c = ""
  281. let c2 = c.replace("\n", "\\n")
  282. main()
  283. #OUT ha/home/a1xyz/usr/bin