Created
June 17, 2015 19:10
-
-
Save iArnold/d414bbbbc5c3e72fbf6e to your computer and use it in GitHub Desktop.
string-to-integer convertion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string-to-integer: function [ | |
input [string!] | |
return: [integer! none!] | |
/local | |
total [integer!] | |
in-char [char! string!] | |
cipher [integer!] | |
is-number [logic!] | |
][ | |
is-number: false | |
total: 0 | |
forall input [ | |
if is-number [ | |
in-char: first input | |
cipher: switch/default in-char [ | |
#"1" [1] | |
#"2" [2] | |
#"3" [3] | |
#"4" [4] | |
#"5" [5] | |
#"6" [6] | |
#"7" [7] | |
#"8" [8] | |
#"9" [9] | |
#"0" [0] | |
][ | |
none! | |
] | |
either none! = type? cipher [ | |
is-number: false | |
][ | |
total: total * 10 + cipher | |
] | |
] | |
] | |
return either is-number [ total ][ none! ] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Few optimizations: