diff options
author | Pierre Joye <pajoye@php.net> | 2011-07-27 00:14:02 +0000 |
---|---|---|
committer | Pierre Joye <pajoye@php.net> | 2011-07-27 00:14:02 +0000 |
commit | 6e4435d628474d6fe395a742832cee730fbb5911 (patch) | |
tree | d54e735b3ccacaaf2dc263004c0df9c1b52cfdf2 /Zend/zend_strtod.c | |
parent | 57536d31ebdeeeeacd3bf2ef78302b97a79675bd (diff) | |
download | php-git-6e4435d628474d6fe395a742832cee730fbb5911.tar.gz |
- add binary suport, FR #50638, as defined in RFC https://wiki.php.net/rfc/binnotation4ints, patch by Jonah Harris
Diffstat (limited to 'Zend/zend_strtod.c')
-rw-r--r-- | Zend/zend_strtod.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Zend/zend_strtod.c b/Zend/zend_strtod.c index 62113bcd60..3f41f85a28 100644 --- a/Zend/zend_strtod.c +++ b/Zend/zend_strtod.c @@ -2639,6 +2639,44 @@ ZEND_API double zend_oct_strtod(const char *str, const char **endptr) return value; } +ZEND_API double zend_bin_strtod(const char *str, const char **endptr) +{ + const char *s = str; + char c; + double value = 0; + int any = 0; + + if ('0' == *s && ('b' == s[1] || 'B' == s[1])) { + s += 2; + } + + while ((c = *s++)) { + /* + * Verify the validity of the current character as a base-2 digit. In + * the event that an invalid digit is found, halt the conversion and + * return the portion which has been converted thus far. + */ + if ('0' == c || '1' == c) + value = value * 2 + c - '0'; + else + break; + + any = 1; + } + + /* + * As with many strtoX implementations, should the subject sequence be + * empty or not well-formed, no conversion is performed and the original + * value of str is stored in *endptr, provided that endptr is not a null + * pointer. + */ + if (NULL != endptr) { + *endptr = (char *)(any ? s - 1 : str); + } + + return value; +} + /* * Local variables: * tab-width: 4 |