tparseutils.nim 3.9 KB

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