tparseutils.nim 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import std/[parseutils, sequtils, sugar]
  2. let input = "$test{} $this is ${an{ example}} "
  3. let expected = @[(ikVar, "test"), (ikStr, "{} "), (ikVar, "this"),
  4. (ikStr, " is "), (ikExpr, "an{ example}"), (ikStr, " ")]
  5. doAssert toSeq(interpolatedFragments(input)) == expected
  6. var value = 0
  7. discard parseHex("0x38", value)
  8. doAssert value == 56
  9. value = -1
  10. doAssert(parseSaturatedNatural("848", value) == 3)
  11. doAssert value == 848
  12. value = -1
  13. discard parseSaturatedNatural("84899999999999999999324234243143142342135435342532453", value)
  14. doAssert value == high(int)
  15. value = -1
  16. discard parseSaturatedNatural("9223372036854775808", value)
  17. doAssert value == high(int)
  18. value = -1
  19. discard parseSaturatedNatural("9223372036854775807", value)
  20. doAssert value == high(int)
  21. value = -1
  22. discard parseSaturatedNatural("18446744073709551616", value)
  23. doAssert value == high(int)
  24. value = -1
  25. discard parseSaturatedNatural("18446744073709551615", value)
  26. doAssert value == high(int)
  27. value = -1
  28. doAssert(parseSaturatedNatural("1_000_000", value) == 9)
  29. doAssert value == 1_000_000
  30. var i64Value: int64
  31. discard parseBiggestInt("9223372036854775807", i64Value)
  32. doAssert i64Value == 9223372036854775807
  33. block:
  34. var f: float
  35. let res = collect:
  36. for x in ["9.123456789012345+","11.123456789012345+","9.123456789012345-","8.123456789012345+","9.12345678901234-","9.123456789012345"]:
  37. (parseFloat(x, f, 0), $f)
  38. doAssert res == @[(17, "9.123456789012344"), (18, "11.123456789012344"),
  39. (17, "9.123456789012344"), (17, "8.123456789012344"),
  40. (16, "9.12345678901234"), (17, "9.123456789012344")]