Skip to content

Effort free video editing!

913 B
Raw Blame
## Numbers read from a whole string, answering whether it was one rather
## than raising: what std/strutils' parseInt and parseFloat take, they take.
## A string that isn't a number leaves the result as it was.

import std/parseutils

func toBiggestInt*(s: string, n: var BiggestInt): bool =
  ## False for text that isn't an integer or is one too large to hold.
  var parsed: BiggestInt
  try:
    let used = parseBiggestInt(s, parsed)
    result = used > 0 and used == s.len
  except ValueError:
    result = false
  if result: n = parsed

func toInt*(s: string, n: var int): bool =
  var big: BiggestInt
  result = toBiggestInt(s, big) and big >= low(int) and big <= high(int)
  if result: n = int(big)

func toFloat*(s: string, f: var float): bool =
  ## "nan" and "inf" are floats here too.
  var parsed: float
  let used = parseFloat(s, parsed)
  result = used > 0 and used == s.len
  if result: f = parsed