tstrutil.nim 7.9 KB

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