From 4d0f1bfeb437c8f4c5b3513f14225d5e0a48b756 Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Wed, 27 Jul 2011 00:14:02 +0000 Subject: - add binary suport, FR #50638, as defined in RFC https://wiki.php.net/rfc/binnotation4ints, patch by Jonah Harris --- Zend/zend_strtod.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'Zend/zend_strtod.c') 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 -- cgit v1.2.1