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 | 4d0f1bfeb437c8f4c5b3513f14225d5e0a48b756 (patch) | |
| tree | 7f848582272f95b69ede1554eee06712d5bcf997 /Zend/zend_strtod.c | |
| parent | 823d7a0534c3eaec7c8f6ff4889240df77baa58e (diff) | |
| download | php-git-4d0f1bfeb437c8f4c5b3513f14225d5e0a48b756.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 |
