ref: 8ef76e0e497a1f6fe2f34f55ff5781db4e8fac03
parent: a67e53ab83967e47c51b305e684aba6687e2632e
author: Ori Bernstein <[email protected]>
date: Tue Jan 21 18:31:35 EST 2014
Add 'charval()' function. This returns the value of a character in any base <= 16
--- a/libstd/chartype.myr
+++ b/libstd/chartype.myr
@@ -21,6 +21,8 @@
const tolower : (c : char -> char)
const toupper : (c : char -> char)
const totitle : (c : char -> char)
+
+ generic charval : (c : char, base : int -> @a::(tcint,tcnum,tctest))
;;
/*
@@ -1207,3 +1209,19 @@
-> c
}
+generic charval = {c, base -> @a::(tcnum,tcint,tctest)
+ var v = -1
+
+ if c >= '0' && c <= '9'
+ v = (c - '0') castto(@a::(tcint,tcnum,tctest))
+ elif c >= 'a' && c <= 'z'
+ v = (c - 'a' + 10) castto(@a::(tcint,tcnum,tctest))
+ elif c >= 'A' && c <= 'Z'
+ v = (c - 'A' + 10) castto(@a::(tcint,tcnum,tctest))
+ ;;
+
+ if v < 0 || v > (base castto(@a::(tcint,tcnum,tctest)))
+ -> -1
+ ;;
+ -> v
+}