tstrutils.nim 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. import std/strutils
  5. from stdtest/testutils import disableVm
  6. import std/assertions
  7. # xxx each instance of `disableVm` and `when not defined js:` should eventually be fixed
  8. template rejectParse(e) =
  9. try:
  10. discard e
  11. raise newException(AssertionDefect, "This was supposed to fail: $#!" % astToStr(e))
  12. except ValueError: discard
  13. template main() =
  14. block: # strip
  15. doAssert strip(" ha ") == "ha"
  16. doAssert strip(" foofoofoo ") == "foofoofoo"
  17. doAssert strip("sfoofoofoos", chars = {'s'}) == "foofoofoo"
  18. doAssert strip("barfoofoofoobar", chars = {'b', 'a', 'r'}) == "foofoofoo"
  19. doAssert strip("stripme but don't strip this stripme",
  20. chars = {'s', 't', 'r', 'i', 'p', 'm', 'e'}) ==
  21. " but don't strip this "
  22. doAssert strip("sfoofoofoos", leading = false, chars = {'s'}) == "sfoofoofoo"
  23. doAssert strip("sfoofoofoos", trailing = false, chars = {'s'}) == "foofoofoos"
  24. block:
  25. let a = "xxxxxx"
  26. doAssert a.strip(chars={'x'}).len == 0
  27. doAssert "".strip(chars={'x'}).len == 0
  28. doAssert " ".strip(chars={'x'}) == " "
  29. doAssert "xxx xxx".strip(chars={'x'}) == " "
  30. doAssert "xxx wind".strip(chars={'x'}) == " wind"
  31. doAssert "xxx iii".strip(chars={'i'}) == "xxx "
  32. doAssert "x".strip(leading = false, chars={'x'}).len == 0
  33. doAssert "x".strip(trailing = false, chars={'x'}).len == 0
  34. doAssert "x".strip(leading = false, trailing = false, chars={'x'}) == "x"
  35. block: # split
  36. var ret: seq[string] # or use `toSeq` or `collect`
  37. for p in split("/home/a1:xyz:/usr/bin", {':'}): ret.add p
  38. doAssert ret == @["/home/a1", "xyz", "/usr/bin"]
  39. let s = " this is an example "
  40. let s2 = ":this;is;an:example;;"
  41. doAssert s.split() == @["", "this", "is", "an", "example", "", ""]
  42. doAssert s2.split(seps = {':', ';'}) == @["", "this", "is", "an", "example",
  43. "", ""]
  44. doAssert s.split(maxsplit = 4) == @["", "this", "is", "an", "example "]
  45. doAssert s.split(' ', maxsplit = 1) == @["", "this is an example "]
  46. doAssert s.split(" ", maxsplit = 4) == @["", "this", "is", "an", "example "]
  47. block: # splitLines
  48. let fixture = "a\nb\rc\r\nd"
  49. doAssert len(fixture.splitLines) == 4
  50. doAssert splitLines(fixture) == @["a", "b", "c", "d"]
  51. doAssert splitLines(fixture, keepEol=true) == @["a\n", "b\r", "c\r\n", "d"]
  52. block: # rsplit
  53. doAssert rsplit("foo bar", seps = Whitespace) == @["foo", "bar"]
  54. doAssert rsplit(" foo bar", seps = Whitespace, maxsplit = 1) == @[" foo", "bar"]
  55. doAssert rsplit(" foo bar ", seps = Whitespace, maxsplit = 1) == @[
  56. " foo bar", ""]
  57. doAssert rsplit(":foo:bar", sep = ':') == @["", "foo", "bar"]
  58. doAssert rsplit(":foo:bar", sep = ':', maxsplit = 2) == @["", "foo", "bar"]
  59. doAssert rsplit(":foo:bar", sep = ':', maxsplit = 3) == @["", "foo", "bar"]
  60. doAssert rsplit("foothebar", sep = "the") == @["foo", "bar"]
  61. block: # splitWhitespace
  62. let s = " this is an example "
  63. doAssert s.splitWhitespace() == @["this", "is", "an", "example"]
  64. doAssert s.splitWhitespace(maxsplit = 1) == @["this", "is an example "]
  65. doAssert s.splitWhitespace(maxsplit = 2) == @["this", "is", "an example "]
  66. doAssert s.splitWhitespace(maxsplit = 3) == @["this", "is", "an", "example "]
  67. doAssert s.splitWhitespace(maxsplit = 4) == @["this", "is", "an", "example"]
  68. block: # removeSuffix
  69. var s = "hello\n\r"
  70. s.removeSuffix
  71. doAssert s == "hello"
  72. s.removeSuffix
  73. doAssert s == "hello"
  74. s = "hello\n\n"
  75. s.removeSuffix
  76. doAssert s == "hello"
  77. s = "hello\r"
  78. s.removeSuffix
  79. doAssert s == "hello"
  80. s = "hello \n there"
  81. s.removeSuffix
  82. doAssert s == "hello \n there"
  83. s = "hello"
  84. s.removeSuffix("llo")
  85. doAssert s == "he"
  86. s.removeSuffix('e')
  87. doAssert s == "h"
  88. s = "hellos"
  89. s.removeSuffix({'s','z'})
  90. doAssert s == "hello"
  91. s.removeSuffix({'l','o'})
  92. doAssert s == "he"
  93. s = "aeiou"
  94. s.removeSuffix("")
  95. doAssert s == "aeiou"
  96. s = ""
  97. s.removeSuffix("")
  98. doAssert s == ""
  99. s = " "
  100. s.removeSuffix
  101. doAssert s == " "
  102. s = " "
  103. s.removeSuffix("")
  104. doAssert s == " "
  105. s = " "
  106. s.removeSuffix(" ")
  107. doAssert s == " "
  108. s = " "
  109. s.removeSuffix(' ')
  110. doAssert s == ""
  111. # Contrary to Chomp in other languages
  112. # empty string does not change behaviour
  113. s = "hello\r\n\r\n"
  114. s.removeSuffix("")
  115. doAssert s == "hello\r\n\r\n"
  116. block: # removePrefix
  117. var s = "\n\rhello"
  118. s.removePrefix
  119. doAssert s == "hello"
  120. s.removePrefix
  121. doAssert s == "hello"
  122. s = "\n\nhello"
  123. s.removePrefix
  124. doAssert s == "hello"
  125. s = "\rhello"
  126. s.removePrefix
  127. doAssert s == "hello"
  128. s = "hello \n there"
  129. s.removePrefix
  130. doAssert s == "hello \n there"
  131. s = "hello"
  132. s.removePrefix("hel")
  133. doAssert s == "lo"
  134. s.removePrefix('l')
  135. doAssert s == "o"
  136. s = "hellos"
  137. s.removePrefix({'h','e'})
  138. doAssert s == "llos"
  139. s.removePrefix({'l','o'})
  140. doAssert s == "s"
  141. s = "aeiou"
  142. s.removePrefix("")
  143. doAssert s == "aeiou"
  144. s = ""
  145. s.removePrefix("")
  146. doAssert s == ""
  147. s = " "
  148. s.removePrefix
  149. doAssert s == " "
  150. s = " "
  151. s.removePrefix("")
  152. doAssert s == " "
  153. s = " "
  154. s.removePrefix(" ")
  155. doAssert s == " "
  156. s = " "
  157. s.removePrefix(' ')
  158. doAssert s == ""
  159. # Contrary to Chomp in other languages
  160. # empty string does not change behaviour
  161. s = "\r\n\r\nhello"
  162. s.removePrefix("")
  163. doAssert s == "\r\n\r\nhello"
  164. block: # delete(slice)
  165. var s = "0123456789ABCDEFGH"
  166. delete(s, 4 .. 5)
  167. doAssert s == "01236789ABCDEFGH"
  168. delete(s, s.len-1 .. s.len-1)
  169. doAssert s == "01236789ABCDEFG"
  170. delete(s, 0..0)
  171. doAssert s == "1236789ABCDEFG"
  172. s = ""
  173. doAssertRaises(IndexDefect): delete(s, 0..0)
  174. doAssert s == ""
  175. s = "abc"
  176. doAssertRaises(IndexDefect): delete(s, -1 .. -2)
  177. doAssertRaises(IndexDefect): delete(s, 2..3)
  178. doAssertRaises(IndexDefect): delete(s, 3..2)
  179. delete(s, 2..2)
  180. doAssert s == "ab"
  181. delete(s, 1..0)
  182. doAssert s == "ab"
  183. delete(s, 0..0)
  184. doAssert s == "b"
  185. block: # delete(first, last)
  186. {.push warning[deprecated]:off.}
  187. var s = "0123456789ABCDEFGH"
  188. delete(s, 4, 5)
  189. doAssert s == "01236789ABCDEFGH"
  190. delete(s, s.len-1, s.len-1)
  191. doAssert s == "01236789ABCDEFG"
  192. delete(s, 0, 0)
  193. doAssert s == "1236789ABCDEFG"
  194. {.pop.}
  195. block: # find
  196. const haystack: string = "0123456789ABCDEFGH"
  197. doAssert haystack.find('A') == 10
  198. doAssert haystack.find('A', 5) == 10
  199. doAssert haystack.find('A', 5, 10) == 10
  200. doAssert haystack.find('A', 5, 9) == -1
  201. doAssert haystack.find("A") == 10
  202. doAssert haystack.find("A", 5) == 10
  203. doAssert haystack.find("A", 5, 10) == 10
  204. doAssert haystack.find("A", 5, 9) == -1
  205. doAssert haystack.find({'A'..'C'}) == 10
  206. doAssert haystack.find({'A'..'C'}, 5) == 10
  207. doAssert haystack.find({'A'..'C'}, 5, 10) == 10
  208. doAssert haystack.find({'A'..'C'}, 5, 9) == -1
  209. doAssert haystack.find('A', 0, 0) == -1 # search limited to the first char
  210. doAssert haystack.find('A', 5, 0) == -1 # last < start
  211. doAssert haystack.find('A', 5, 4) == -1 # last < start
  212. block:
  213. const haystack: string = "ABCABABABABCAB"
  214. doAssert haystack.len == 14
  215. # only last argument
  216. doAssert haystack.find("ABC") == 0
  217. doAssert haystack.find("ABC", last=13) == 0 # after the second ABC
  218. doAssert haystack.find("ABC", last=5) == 0 # before the second ABC
  219. # only start argument
  220. doAssert haystack.find("ABC", start=0) == 0
  221. doAssert haystack.find("ABC", start=1) == 9
  222. doAssert haystack.find("ABC", start=9) == 9
  223. doAssert haystack.find("ABC", start=10) == -1
  224. # both start and last arguments
  225. doAssert haystack.find("ABC", start=0, last=14) == 0
  226. doAssert haystack.find("ABC", start=0, last=13) == 0
  227. doAssert haystack.find("ABC", start=0, last=12) == 0
  228. doAssert haystack.find("ABC", start=1, last=13) == 9
  229. doAssert haystack.find("ABC", start=1, last=12) == 9
  230. doAssert haystack.find("ABC", start=1, last=11) == 9
  231. doAssert haystack.find("ABC", start=1, last=10) == -1
  232. doAssert "".find("/") == -1
  233. doAssert "/".find("/") == 0
  234. doAssert "/".find("//") == -1
  235. doAssert "///".find("//", start=3) == -1
  236. # searching for empty string
  237. doAssert "".find("") == 0
  238. doAssert "abc".find("") == 0
  239. doAssert "abc".find("", start=1) == 1
  240. doAssert "abc".find("", start=2) == 2
  241. doAssert "abc".find("", start=3) == 3
  242. doAssert "abc".find("", start=4) == -1
  243. doAssert "abc".find("", start=400) == -1
  244. doAssert "abc".find("", start=1, last=3) == 1
  245. doAssert "abc".find("", start=1, last=2) == 1
  246. doAssert "abc".find("", start=1, last=1) == 1
  247. doAssert "abc".find("", start=1, last=0) == 1
  248. doAssert "abc".find("", start=1, last = -1) == 1
  249. # when last <= start, searching for non-empty string
  250. block:
  251. let last: int = -1 # searching through whole line
  252. doAssert "abcd".find("ab", start=0, last=last) == 0
  253. doAssert "abcd".find("ab", start=1, last=last) == -1
  254. doAssert "abcd".find("bc", start=1, last=last) == 1
  255. doAssert "abcd".find("bc", start=2, last=last) == -1
  256. block:
  257. let last: int = 0
  258. doAssert "abcd".find("ab", start=0, last=last) == -1
  259. doAssert "abcd".find("ab", start=1, last=last) == -1
  260. doAssert "abcd".find("bc", start=1, last=last) == -1
  261. doAssert "abcd".find("bc", start=2, last=last) == -1
  262. block:
  263. let last: int = 1
  264. doAssert "abcd".find("ab", start=0, last=last) == 0
  265. doAssert "abcd".find("ab", start=1, last=last) == -1
  266. doAssert "abcd".find("bc", start=1, last=last) == -1
  267. doAssert "abcd".find("bc", start=2, last=last) == -1
  268. block: # rfind
  269. doAssert "0123456789ABCDEFGAH".rfind('A') == 17
  270. doAssert "0123456789ABCDEFGAH".rfind('A', last=13) == 10
  271. doAssert "0123456789ABCDEFGAH".rfind('H', last=13) == -1
  272. doAssert "0123456789ABCDEFGAH".rfind("A") == 17
  273. doAssert "0123456789ABCDEFGAH".rfind("A", last=13) == 10
  274. doAssert "0123456789ABCDEFGAH".rfind("H", last=13) == -1
  275. doAssert "0123456789ABCDEFGAH".rfind({'A'..'C'}) == 17
  276. doAssert "0123456789ABCDEFGAH".rfind({'A'..'C'}, last=13) == 12
  277. doAssert "0123456789ABCDEFGAH".rfind({'G'..'H'}, last=13) == -1
  278. doAssert "0123456789ABCDEFGAH".rfind('A', start=18) == -1
  279. doAssert "0123456789ABCDEFGAH".rfind('A', start=11, last=17) == 17
  280. doAssert "0123456789ABCDEFGAH".rfind("0", start=0) == 0
  281. doAssert "0123456789ABCDEFGAH".rfind("0", start=1) == -1
  282. doAssert "0123456789ABCDEFGAH".rfind("H", start=11) == 18
  283. doAssert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=5) == 9
  284. doAssert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=10) == -1
  285. doAssert "/1/2/3".rfind('/') == 4
  286. doAssert "/1/2/3".rfind('/', last=1) == 0
  287. doAssert "/1/2/3".rfind('0') == -1
  288. block:
  289. const haystack: string = "ABCABABABABCAB"
  290. doAssert haystack.len == 14
  291. doAssert haystack.rfind("ABC") == 9
  292. doAssert haystack.rfind("ABC", last=13) == 9
  293. doAssert haystack.rfind("ABC", last=12) == 9
  294. doAssert haystack.rfind("ABC", last=11) == 9
  295. doAssert haystack.rfind("ABC", last=10) == 0
  296. doAssert haystack.rfind("ABC", start=0) == 9
  297. doAssert haystack.rfind("ABC", start=1) == 9
  298. doAssert haystack.rfind("ABC", start=9) == 9
  299. doAssert haystack.rfind("ABC", start=10) == -1
  300. doAssert haystack.rfind("ABC", start=0, last=13) == 9
  301. doAssert haystack.rfind("ABC", start=0, last=12) == 9
  302. doAssert haystack.rfind("ABC", start=0, last=11) == 9
  303. doAssert haystack.rfind("ABC", start=0, last=10) == 0
  304. doAssert haystack.rfind("ABC", start=1, last=10) == -1
  305. doAssert "".rfind("/") == -1
  306. doAssert "/".rfind("/") == 0
  307. doAssert "/".rfind("//") == -1
  308. doAssert "///".rfind("//", start=3) == -1
  309. # searching for empty string
  310. doAssert "".rfind("") == 0
  311. doAssert "abc".rfind("") == 3
  312. doAssert "abc".rfind("", start=1) == 3
  313. doAssert "abc".rfind("", start=2) == 3
  314. doAssert "abc".rfind("", start=3) == 3
  315. doAssert "abc".rfind("", start=4) == 4
  316. doAssert "abc".rfind("", start=400) == 400
  317. doAssert "abc".rfind("", start=1, last=3) == 3
  318. doAssert "abc".rfind("", start=1, last=2) == 2
  319. doAssert "abc".rfind("", start=1, last=1) == 1
  320. # This returns the start index instead of the last index
  321. # because start > last
  322. doAssert "abc".rfind("", start=1, last=0) == 1
  323. doAssert "abc".rfind("", start=1, last = -1) == 3
  324. doAssert "abc".rfind("", start=0, last=0) == 0
  325. # when last <= start, searching for non-empty string
  326. block:
  327. let last: int = -1
  328. doAssert "abcd".rfind("ab", start=0, last=last) == 0
  329. doAssert "abcd".rfind("ab", start=1, last=last) == -1
  330. doAssert "abcd".rfind("bc", start=1, last=last) == 1
  331. doAssert "abcd".rfind("bc", start=2, last=last) == -1
  332. block:
  333. let last: int = 0
  334. doAssert "abcd".rfind("ab", start=0, last=last) == -1
  335. doAssert "abcd".rfind("ab", start=1, last=last) == -1
  336. doAssert "abcd".rfind("bc", start=1, last=last) == -1
  337. doAssert "abcd".rfind("bc", start=2, last=last) == -1
  338. block:
  339. let last: int = 1
  340. doAssert "abcd".rfind("ab", start=0, last=last) == 0
  341. doAssert "abcd".rfind("ab", start=1, last=last) == -1
  342. doAssert "abcd".rfind("bc", start=1, last=last) == -1
  343. doAssert "abcd".rfind("bc", start=2, last=last) == -1
  344. block: # trimZeros
  345. var x = "1200"
  346. x.trimZeros()
  347. doAssert x == "1200"
  348. x = "120.0"
  349. x.trimZeros()
  350. doAssert x == "120"
  351. x = "0."
  352. x.trimZeros()
  353. doAssert x == "0"
  354. x = "1.0e2"
  355. x.trimZeros()
  356. doAssert x == "1e2"
  357. x = "78.90"
  358. x.trimZeros()
  359. doAssert x == "78.9"
  360. x = "1.23e4"
  361. x.trimZeros()
  362. doAssert x == "1.23e4"
  363. x = "1.01"
  364. x.trimZeros()
  365. doAssert x == "1.01"
  366. x = "1.1001"
  367. x.trimZeros()
  368. doAssert x == "1.1001"
  369. x = "0.0"
  370. x.trimZeros()
  371. doAssert x == "0"
  372. x = "0.01"
  373. x.trimZeros()
  374. doAssert x == "0.01"
  375. x = "1e0"
  376. x.trimZeros()
  377. doAssert x == "1e0"
  378. x = "1.23"
  379. x.trimZeros()
  380. doAssert x == "1.23"
  381. block: # countLines
  382. proc assertCountLines(s: string) = doAssert s.countLines == s.splitLines.len
  383. assertCountLines("")
  384. assertCountLines("\n")
  385. assertCountLines("\n\n")
  386. assertCountLines("abc")
  387. assertCountLines("abc\n123")
  388. assertCountLines("abc\n123\n")
  389. assertCountLines("\nabc\n123")
  390. assertCountLines("\nabc\n123\n")
  391. block: # parseBinInt, parseHexInt, parseOctInt
  392. # binary
  393. doAssert "0b1111".parseBinInt == 15
  394. doAssert "0B1111".parseBinInt == 15
  395. doAssert "1111".parseBinInt == 15
  396. doAssert "1110".parseBinInt == 14
  397. doAssert "1_1_1_1".parseBinInt == 15
  398. doAssert "0b1_1_1_1".parseBinInt == 15
  399. rejectParse "".parseBinInt
  400. rejectParse "_".parseBinInt
  401. rejectParse "0b".parseBinInt
  402. rejectParse "0b1234".parseBinInt
  403. # hex
  404. doAssert "0x72".parseHexInt == 114
  405. doAssert "0X72".parseHexInt == 114
  406. doAssert "#72".parseHexInt == 114
  407. doAssert "72".parseHexInt == 114
  408. doAssert "FF".parseHexInt == 255
  409. doAssert "ff".parseHexInt == 255
  410. doAssert "fF".parseHexInt == 255
  411. doAssert "0x7_2".parseHexInt == 114
  412. rejectParse "".parseHexInt
  413. rejectParse "_".parseHexInt
  414. rejectParse "0x".parseHexInt
  415. rejectParse "0xFFG".parseHexInt
  416. rejectParse "reject".parseHexInt
  417. # octal
  418. doAssert "0o17".parseOctInt == 15
  419. doAssert "0O17".parseOctInt == 15
  420. doAssert "17".parseOctInt == 15
  421. doAssert "10".parseOctInt == 8
  422. doAssert "0o1_0_0".parseOctInt == 64
  423. rejectParse "".parseOctInt
  424. rejectParse "_".parseOctInt
  425. rejectParse "0o".parseOctInt
  426. rejectParse "9".parseOctInt
  427. rejectParse "0o9".parseOctInt
  428. rejectParse "reject".parseOctInt
  429. block: # parseHexStr
  430. doAssert "".parseHexStr == ""
  431. doAssert "00Ff80".parseHexStr == "\0\xFF\x80"
  432. try:
  433. discard "00Ff8".parseHexStr
  434. doAssert false, "Should raise ValueError"
  435. except ValueError:
  436. discard
  437. try:
  438. discard "0k".parseHexStr
  439. doAssert false, "Should raise ValueError"
  440. except ValueError:
  441. discard
  442. doAssert "".toHex == ""
  443. doAssert "\x00\xFF\x80".toHex == "00FF80"
  444. doAssert "0123456789abcdef".parseHexStr.toHex == "0123456789ABCDEF"
  445. block: # toHex
  446. doAssert(toHex(100i16, 32) == "00000000000000000000000000000064")
  447. doAssert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C")
  448. when not defined js:
  449. doAssert(toHex(high(uint64)) == "FFFFFFFFFFFFFFFF")
  450. doAssert(toHex(high(uint64), 16) == "FFFFFFFFFFFFFFFF")
  451. doAssert(toHex(high(uint64), 32) == "0000000000000000FFFFFFFFFFFFFFFF")
  452. block: # insertSep
  453. doAssert(insertSep($1000_000) == "1_000_000")
  454. doAssert(insertSep($232) == "232")
  455. doAssert(insertSep($12345, ',') == "12,345")
  456. doAssert(insertSep($0) == "0")
  457. block: # repeat, spaces
  458. doAssert(' '.repeat(8) == " ")
  459. doAssert(" ".repeat(8) == " ")
  460. doAssert(spaces(8) == " ")
  461. doAssert(' '.repeat(0) == "")
  462. doAssert(" ".repeat(0) == "")
  463. doAssert(spaces(0) == "")
  464. block: # toBin, toOct
  465. block:# bug #11369
  466. var num: int64 = -1
  467. when not defined js:
  468. doAssert num.toBin(64) == "1111111111111111111111111111111111111111111111111111111111111111"
  469. doAssert num.toOct(24) == "001777777777777777777777"
  470. block: # replace
  471. doAssert "oo".replace("", "abc") == "oo"
  472. # bug #8911
  473. static:
  474. let a = ""
  475. let a2 = a.replace("\n", "\\n")
  476. static:
  477. let b = "b"
  478. let b2 = b.replace("\n", "\\n")
  479. block:
  480. let c = ""
  481. let c2 = c.replace("\n", "\\n")
  482. block: # replaceWord
  483. doAssert "-ld a-ldz -ld".replaceWord("-ld") == " a-ldz "
  484. doAssert "-lda-ldz -ld abc".replaceWord("-ld") == "-lda-ldz abc"
  485. doAssert "-lda-ldz -ld abc".replaceWord("") == "-lda-ldz -ld abc"
  486. block: # multiReplace
  487. doAssert "abba".multiReplace(("a", "b"), ("b", "a")) == "baab"
  488. doAssert "Hello World.".multiReplace(("ello", "ELLO"), ("World.",
  489. "PEOPLE!")) == "HELLO PEOPLE!"
  490. doAssert "aaaa".multiReplace(("a", "aa"), ("aa", "bb")) == "aaaaaaaa"
  491. # `parseEnum`, ref issue #14030
  492. # check enum defined at top level # xxx this is probably irrelevant, and pollutes scope
  493. # for remaining tests
  494. type
  495. Foo = enum
  496. A = -10
  497. B = "bb"
  498. C = (-5, "ccc")
  499. D = 15
  500. E = "ee" # check that we count enum fields correctly
  501. block: # parseEnum
  502. block:
  503. let a = parseEnum[Foo]("A")
  504. let b = parseEnum[Foo]("bb")
  505. let c = parseEnum[Foo]("ccc")
  506. let d = parseEnum[Foo]("D")
  507. let e = parseEnum[Foo]("ee")
  508. doAssert a == A
  509. doAssert b == B
  510. doAssert c == C
  511. doAssert d == D
  512. doAssert e == E
  513. try:
  514. let f = parseEnum[Foo]("Bar")
  515. doAssert false
  516. except ValueError:
  517. discard
  518. # finally using default
  519. let g = parseEnum[Foo]("Bar", A)
  520. doAssert g == A
  521. block: # bug #19463
  522. const CAMPAIGN_TABLE = "wikientries_campaign"
  523. const CHARACTER_TABLE = "wikientries_character"
  524. type Tables = enum
  525. a = CAMPAIGN_TABLE,
  526. b = CHARACTER_TABLE,
  527. let myA = CAMPAIGN_TABLE
  528. doAssert $parseEnum[Tables](myA) == "wikientries_campaign"
  529. block: # check enum defined in block
  530. type
  531. Bar = enum
  532. V
  533. W = "ww"
  534. X = (3, "xx")
  535. Y = 10
  536. Z = "zz" # check that we count enum fields correctly
  537. let a = parseEnum[Bar]("V")
  538. let b = parseEnum[Bar]("ww")
  539. let c = parseEnum[Bar]("xx")
  540. let d = parseEnum[Bar]("Y")
  541. let e = parseEnum[Bar]("zz")
  542. doAssert a == V
  543. doAssert b == W
  544. doAssert c == X
  545. doAssert d == Y
  546. doAssert e == Z
  547. try:
  548. let f = parseEnum[Bar]("Baz")
  549. doAssert false
  550. except ValueError:
  551. discard
  552. # finally using default
  553. let g = parseEnum[Bar]("Baz", V)
  554. doAssert g == V
  555. block: # check ambiguous enum fails to parse
  556. type
  557. Ambig = enum
  558. f1 = "A"
  559. f2 = "B"
  560. f3 = "A"
  561. doAssert not compiles((let a = parseEnum[Ambig]("A")))
  562. block: # check almost ambiguous enum
  563. type
  564. AlmostAmbig = enum
  565. f1 = "someA"
  566. f2 = "someB"
  567. f3 = "SomeA"
  568. let a = parseEnum[AlmostAmbig]("someA")
  569. let b = parseEnum[AlmostAmbig]("someB")
  570. let c = parseEnum[AlmostAmbig]("SomeA")
  571. doAssert a == f1
  572. doAssert b == f2
  573. doAssert c == f3
  574. block: # parseEnum TODO: merge above
  575. type MyEnum = enum enA, enB, enC, enuD, enE
  576. doAssert parseEnum[MyEnum]("enu_D") == enuD
  577. doAssert parseEnum("invalid enum value", enC) == enC
  578. block: # indentation
  579. doAssert 0 == indentation """
  580. hey
  581. low
  582. there
  583. """
  584. doAssert 2 == indentation """
  585. hey
  586. low
  587. there
  588. """
  589. doAssert 2 == indentation """ hey
  590. low
  591. there
  592. """
  593. doAssert 2 == indentation """ hey
  594. low
  595. there"""
  596. doAssert 0 == indentation ""
  597. doAssert 0 == indentation " \n \n"
  598. doAssert 0 == indentation " "
  599. block: # indent
  600. doAssert " foo\n bar".indent(4, "Q") == "QQQQ foo\nQQQQ bar"
  601. block: # unindent
  602. doAssert """~~!!foo
  603. ~~!!bar
  604. ~~!!baz""".unindent(2, "~~!!") == "foo\nbar\nbaz"
  605. doAssert """~~!!foo
  606. ~~!!bar
  607. ~~!!baz""".unindent(2, "~~!!aa") == "~~!!foo\n~~!!bar\n~~!!baz"
  608. doAssert """~~foo
  609. ~~ bar
  610. ~~ baz""".unindent(4, "~") == "foo\n bar\n baz"
  611. doAssert """foo
  612. bar
  613. baz
  614. """.unindent(4) == "foo\nbar\nbaz\n"
  615. doAssert """foo
  616. bar
  617. baz
  618. """.unindent(2) == "foo\n bar\n baz\n"
  619. doAssert """foo
  620. bar
  621. baz
  622. """.unindent(100) == "foo\nbar\nbaz\n"
  623. doAssert """foo
  624. foo
  625. bar
  626. """.unindent() == "foo\nfoo\nbar\n"
  627. block: # formatBiggestFloat
  628. disableVm:
  629. doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000"
  630. when not defined(js):
  631. doAssert formatBiggestFloat(1234.567, ffDecimal, 0) == "1235." # bugs 8242, 12586
  632. doAssert formatBiggestFloat(1234.567, ffDecimal, 1) == "1234.6"
  633. doAssert formatBiggestFloat(0.00000000001, ffDecimal, 11) == "0.00000000001"
  634. doAssert formatBiggestFloat(0.00000000001, ffScientific, 1, ',') in
  635. ["1,0e-11", "1,0e-011"]
  636. block: # formatFloat
  637. disableVm:
  638. # bug #6589
  639. when not defined(js):
  640. doAssert formatFloat(123.456, ffScientific, precision = -1) == "1.234560e+02"
  641. block: # `%`
  642. doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"
  643. doAssert "${1}12 ${-1}$2" % ["a", "b"] == "a12 bb"
  644. doAssert "$animal eats $food." % ["animal", "The cat", "food", "fish"] ==
  645. "The cat eats fish."
  646. block: # formatSize
  647. disableVm:
  648. when not defined(js):
  649. doAssert formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB" # <=== bug #8231
  650. doAssert formatSize((2.234*1024*1024).int) == "2.234MiB"
  651. doAssert formatSize(4096) == "4KiB"
  652. doAssert formatSize(4096, prefix = bpColloquial, includeSpace = true) == "4 kB"
  653. doAssert formatSize(4096, includeSpace = true) == "4 KiB"
  654. doAssert formatSize(5_378_934, prefix = bpColloquial, decimalSep = ',') == "5,13MB"
  655. block: # formatEng
  656. disableVm:
  657. doAssert formatEng(0, 2, trim = false) == "0.00"
  658. doAssert formatEng(0, 2) == "0"
  659. doAssert formatEng(53, 2, trim = false) == "53.00"
  660. doAssert formatEng(0.053, 2, trim = false) == "53.00e-3"
  661. doAssert formatEng(0.053, 4, trim = false) == "53.0000e-3"
  662. doAssert formatEng(0.053, 4, trim = true) == "53e-3"
  663. doAssert formatEng(0.053, 0) == "53e-3"
  664. doAssert formatEng(52731234) == "52.731234e6"
  665. doAssert formatEng(-52731234) == "-52.731234e6"
  666. doAssert formatEng(52731234, 1) == "52.7e6"
  667. doAssert formatEng(-52731234, 1) == "-52.7e6"
  668. doAssert formatEng(52731234, 1, decimalSep = ',') == "52,7e6"
  669. doAssert formatEng(-52731234, 1, decimalSep = ',') == "-52,7e6"
  670. doAssert formatEng(4100, siPrefix = true, unit = "V") == "4.1 kV"
  671. doAssert formatEng(4.1, siPrefix = true, unit = "V",
  672. useUnitSpace = true) == "4.1 V"
  673. doAssert formatEng(4.1, siPrefix = true) == "4.1" # Note lack of space
  674. doAssert formatEng(4100, siPrefix = true) == "4.1 k"
  675. doAssert formatEng(4.1, siPrefix = true, unit = "",
  676. useUnitSpace = true) == "4.1 " # Includes space
  677. doAssert formatEng(4100, siPrefix = true, unit = "") == "4.1 k"
  678. doAssert formatEng(4100) == "4.1e3"
  679. doAssert formatEng(4100, unit = "V", useUnitSpace = true) == "4.1e3 V"
  680. doAssert formatEng(4100, unit = "", useUnitSpace = true) == "4.1e3 "
  681. # Don't use SI prefix as number is too big
  682. doAssert formatEng(3.1e22, siPrefix = true, unit = "a",
  683. useUnitSpace = true) == "31e21 a"
  684. # Don't use SI prefix as number is too small
  685. doAssert formatEng(3.1e-25, siPrefix = true, unit = "A",
  686. useUnitSpace = true) == "310e-27 A"
  687. block: # align
  688. doAssert align("abc", 4) == " abc"
  689. doAssert align("a", 0) == "a"
  690. doAssert align("1232", 6) == " 1232"
  691. doAssert align("1232", 6, '#') == "##1232"
  692. block: # alignLeft
  693. doAssert alignLeft("abc", 4) == "abc "
  694. doAssert alignLeft("a", 0) == "a"
  695. doAssert alignLeft("1232", 6) == "1232 "
  696. doAssert alignLeft("1232", 6, '#') == "1232##"
  697. block: # center
  698. doAssert center("foo", 13) == " foo "
  699. doAssert center("foo", 0) == "foo"
  700. doAssert center("foo", 3, fillChar = 'a') == "foo"
  701. doAssert center("foo", 10, fillChar = '\t') == "\t\t\tfoo\t\t\t\t"
  702. block: # count
  703. doAssert count("foofoofoo", "foofoo") == 1
  704. doAssert count("foofoofoo", "foofoo", overlapping = true) == 2
  705. doAssert count("foofoofoo", 'f') == 3
  706. doAssert count("foofoofoobar", {'f', 'b'}) == 4
  707. block: # isAlphaAscii
  708. doAssert isAlphaAscii('r')
  709. doAssert isAlphaAscii('A')
  710. doAssert(not isAlphaAscii('$'))
  711. block: # isAlphaNumeric
  712. doAssert isAlphaNumeric('3')
  713. doAssert isAlphaNumeric('R')
  714. doAssert(not isAlphaNumeric('!'))
  715. block: # isDigit
  716. doAssert isDigit('3')
  717. doAssert(not isDigit('a'))
  718. doAssert(not isDigit('%'))
  719. block: # isSpaceAscii
  720. doAssert isSpaceAscii('\t')
  721. doAssert isSpaceAscii('\l')
  722. doAssert(not isSpaceAscii('A'))
  723. block: # isEmptyOrWhitespace
  724. doAssert(isEmptyOrWhitespace(""))
  725. doAssert(isEmptyOrWhitespace(" "))
  726. doAssert(isEmptyOrWhitespace("\t\l \v\r\f"))
  727. doAssert(not isEmptyOrWhitespace("ABc \td"))
  728. block: # isLowerAscii
  729. doAssert isLowerAscii('a')
  730. doAssert isLowerAscii('z')
  731. doAssert(not isLowerAscii('A'))
  732. doAssert(not isLowerAscii('5'))
  733. doAssert(not isLowerAscii('&'))
  734. doAssert(not isLowerAscii(' '))
  735. block: # isUpperAscii
  736. doAssert isUpperAscii('A')
  737. doAssert(not isUpperAscii('b'))
  738. doAssert(not isUpperAscii('5'))
  739. doAssert(not isUpperAscii('%'))
  740. block: # unescape
  741. doAssert(unescape(r"\x013", "", "") == "\x013")
  742. block: # join
  743. doAssert join(["foo", "bar", "baz"]) == "foobarbaz"
  744. doAssert join(@["foo", "bar", "baz"], ", ") == "foo, bar, baz"
  745. doAssert join([1, 2, 3]) == "123"
  746. doAssert join(@[1, 2, 3], ", ") == "1, 2, 3"
  747. block: # startsWith / endsWith
  748. var s = "abcdef"
  749. doAssert s.startsWith('a')
  750. doAssert s.startsWith('b') == false
  751. doAssert s.endsWith('f')
  752. doAssert s.endsWith('a') == false
  753. doAssert s.endsWith('\0') == false
  754. block: # nimIdentNormalize
  755. doAssert nimIdentNormalize("") == ""
  756. doAssert nimIdentNormalize("foo") == "foo"
  757. doAssert nimIdentNormalize("foo_bar") == "foobar"
  758. doAssert nimIdentNormalize("Foo_bar") == "Foobar"
  759. doAssert nimIdentNormalize("_Foo_bar") == "_foobar"
  760. block: # bug #19500
  761. doAssert "abc \0 def".find("def") == 6
  762. doAssert "abc \0 def".find('d') == 6
  763. static: main()
  764. main()