tparseutils.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. discard """
  2. targets: "c cpp"
  3. """
  4. import std/[parseutils, sequtils, sugar, formatfloat]
  5. import std/assertions
  6. proc test() =
  7. let input = "$test{} $this is ${an{ example}} "
  8. let expected = @[(ikVar, "test"), (ikStr, "{} "), (ikVar, "this"),
  9. (ikStr, " is "), (ikExpr, "an{ example}"), (ikStr, " ")]
  10. doAssert toSeq(interpolatedFragments(input)) == expected
  11. var value = 0
  12. discard parseHex("0x38", value)
  13. doAssert value == 56
  14. value = -1
  15. doAssert(parseSaturatedNatural("848", value) == 3)
  16. doAssert value == 848
  17. value = -1
  18. discard parseSaturatedNatural("84899999999999999999324234243143142342135435342532453", value)
  19. doAssert value == high(int)
  20. value = -1
  21. discard parseSaturatedNatural("9223372036854775808", value)
  22. doAssert value == high(int)
  23. value = -1
  24. discard parseSaturatedNatural("9223372036854775807", value)
  25. doAssert value == high(int)
  26. value = -1
  27. discard parseSaturatedNatural("18446744073709551616", value)
  28. doAssert value == high(int)
  29. value = -1
  30. discard parseSaturatedNatural("18446744073709551615", value)
  31. doAssert value == high(int)
  32. value = -1
  33. doAssert(parseSaturatedNatural("1_000_000", value) == 9)
  34. doAssert value == 1_000_000
  35. var i64Value: int64
  36. discard parseBiggestInt("9223372036854775807", i64Value)
  37. doAssert i64Value == 9223372036854775807
  38. block:
  39. var f: float
  40. let res = collect:
  41. for x in ["9.123456789012345+","11.123456789012345+","9.123456789012345-","8.123456789012345+","9.12345678901234-","9.123456789012345"]:
  42. (parseFloat(x, f, 0), $f)
  43. doAssert res == @[(17, "9.123456789012344"), (18, "11.123456789012344"),
  44. (17, "9.123456789012344"), (17, "8.123456789012344"),
  45. (16, "9.12345678901234"), (17, "9.123456789012344")]
  46. test()
  47. static: test()
  48. block: # With this included, static: test() crashes the compiler (from a
  49. # VM problem with parseSize calling parseFloat).
  50. var sz: int64
  51. template checkParseSize(s, expectLen, expectVal) =
  52. if (let got = parseSize(s, sz); got != expectLen):
  53. raise newException(IOError, "got len " & $got & " != " & $expectLen)
  54. if sz != expectVal:
  55. raise newException(IOError, "got sz " & $sz & " != " & $expectVal)
  56. # STRING LEN SZ
  57. # Good, complete parses
  58. checkParseSize "1 b" , 4, 1
  59. checkParseSize "1 B" , 4, 1
  60. checkParseSize "1k" , 2, 1000
  61. checkParseSize "1 kib" , 5, 1024
  62. checkParseSize "1 ki" , 4, 1024
  63. checkParseSize "1mi" , 3, 1048576
  64. checkParseSize "1 mi" , 4, 1048576
  65. checkParseSize "1 mib" , 5, 1048576
  66. checkParseSize "1 Mib" , 5, 1048576
  67. checkParseSize "1 MiB" , 5, 1048576
  68. checkParseSize "1.23GiB", 7, 1320702444 # 1320702443.52 rounded
  69. checkParseSize "0.001k" , 6, 1
  70. checkParseSize "0.0004k", 7, 0
  71. checkParseSize "0.0006k", 7, 1
  72. # Incomplete parses
  73. checkParseSize "1 " , 1, 1 # Trailing white IGNORED
  74. checkParseSize "1 B " , 4, 1 # Trailing white IGNORED
  75. checkParseSize "1 B/s" , 4, 1 # Trailing junk IGNORED
  76. checkParseSize "1 kX" , 3, 1000
  77. checkParseSize "1 kiX" , 4, 1024
  78. checkParseSize "1j" , 1, 1 # Unknown prefix IGNORED
  79. checkParseSize "1 jib" , 2, 1 # Unknown prefix post space
  80. checkParseSize "1 ji" , 3, 1
  81. # Bad parses; `sz` should stay last good|incomplete value
  82. checkParseSize "-1b" , 0, 1 # Negative numbers
  83. checkParseSize "abc" , 0, 1 # Non-numeric
  84. checkParseSize " 12" , 0, 1 # Leading white
  85. # Value Edge cases
  86. checkParseSize "9223372036854775807", 19, int64.high