diff options
author | Andreas Rumpf <andreas@andreas-desktop> | 2010-01-18 17:06:43 +0100 |
---|---|---|
committer | Andreas Rumpf <andreas@andreas-desktop> | 2010-01-18 17:06:43 +0100 |
commit | b50133b50f12025faf4801558f7e42cd96493da0 (patch) | |
tree | df4e1548c4cbf3a82700bf31585754299b797cbb /lib/pure/strutils.nim | |
parent | 8259d9d15aa34dbbd84bb19150ab16a27378dd88 (diff) | |
download | Nim-b50133b50f12025faf4801558f7e42cd96493da0.tar.gz |
strutils.parseHexInt added
Diffstat (limited to 'lib/pure/strutils.nim')
-rwxr-xr-x | lib/pure/strutils.nim | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 1dfb070bc..5cf1cf6a3 100755 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -326,6 +326,10 @@ proc ParseFloat*(s: string): float {.noSideEffect, procvar.} ## a valid floating point number, `EInvalidValue` is raised. ``NAN``, ## ``INF``, ``-INF`` are also supported (case insensitive comparison). +proc ParseHexInt*(s: string): int {.noSideEffect, procvar.} + ## Parses a hexadecimal integer value contained in `s`. If `s` is not + ## a valid integer, `EInvalidValue` is raised. + # the stringify and format operators: proc toString*[Ty](x: Ty): string {.deprecated.} ## This generic proc is the same as the stringify operator `$`. @@ -731,6 +735,25 @@ proc ParseBiggestInt(s: string): biggestInt = if index == -1: raise newException(EInvalidValue, "invalid integer: " & s) + +proc ParseHexInt(s: string): int = + var i = 0 + if s[i] == '0' and (s[i+1] == 'x' or s[i+1] == 'X'): inc(i, 2) + while true: + case s[i] + of '_': inc(i) + of '0'..'9': + result = result shl 4 or (ord(s[i]) - ord('0')) + inc(i) + of 'a'..'f': + result = result shl 4 or (ord(s[i]) - ord('a') + 10) + inc(i) + of 'A'..'F': + result = result shl 4 or (ord(s[i]) - ord('A') + 10) + inc(i) + of '\0': break + else: raise newException(EInvalidValue, "invalid integer: " & s) + proc ParseFloat(s: string): float = var esign = 1.0 |