tparseutils.nim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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()