Fix problem with floatvalue-scores in PHP 5.4 #189

Merged
merged 5 commits into from Aug 16, 2012
View
@@ -259,8 +259,7 @@ redis_cmd_format_static(char **ret, char *keyword, char *format, ...) {
case 'f':
case 'F': {
double d = va_arg(ap, double);
- dbl_str = _php_math_number_format(d, 8, '.', '\x00');
- dbl_len = strlen(dbl_str);
+ REDIS_DOUBLE_TO_STRING(dbl_str, dbl_len, d)
smart_str_append_long(&buf, dbl_len);
smart_str_appendl(&buf, _NL, sizeof(_NL) - 1);
smart_str_appendl(&buf, dbl_str, dbl_len);
@@ -328,8 +327,7 @@ redis_cmd_format(char **ret, char *format, ...) {
case 'F':
case 'f': {
double d = va_arg(ap, double);
- dbl_str = _php_math_number_format(d, 8, '.', '\x00');
- dbl_len = strlen(dbl_str);
+ REDIS_DOUBLE_TO_STRING(dbl_str, dbl_len, d)
smart_str_append_long(&buf, dbl_len);
smart_str_appendl(&buf, _NL, sizeof(_NL) - 1);
smart_str_appendl(&buf, dbl_str, dbl_len);
View
@@ -42,3 +42,15 @@ redis_key_prefix(RedisSock *redis_sock, char **key, int *key_len TSRMLS_DC);
PHPAPI int
redis_unserialize(RedisSock *redis_sock, const char *val, int val_len, zval **return_value TSRMLS_DC);
+#if ZEND_MODULE_API_NO >= 20100000
+#define REDIS_DOUBLE_TO_STRING(dbl_str, dbl_len, dbl) \
+ char dbl_decsep; \
+ dbl_decsep = '.'; \
+ dbl_str = _php_math_number_format_ex(dbl, 8, &dbl_decsep, 1, NULL, 0); \
+ dbl_len = strlen(dbl_str);
+#else
+#define REDIS_DOUBLE_TO_STRING(dbl_str, dbl_len, dbl) \
+ dbl_str = _php_math_number_format(dbl, 8, '.', '\x00'); \
+ dbl_len = strlen(dbl_str);
+#endif
+
View
@@ -3662,8 +3662,7 @@ PHP_METHOD(Redis, zAdd) {
/* add score */
score = Z_DVAL_P(z_args[i]);
- dbl_str = _php_math_number_format(score, 8, '.', '\x00');
- dbl_len = strlen(dbl_str);
+ REDIS_DOUBLE_TO_STRING(dbl_str, dbl_len, score)
smart_str_appendc(&buf, '$');
smart_str_append_long(&buf, dbl_len);
smart_str_appendl(&buf, _NL, sizeof(_NL) - 1);
View
@@ -1794,6 +1794,22 @@ public function testZX() {
$this->redis->delete('key2');
$this->redis->delete('key3');
+ $this->redis->zadd('key1', 2000.1, 'one');
+ $this->redis->zadd('key1', 3000.1, 'two');
+ $this->redis->zadd('key1', 4000.1, 'three');
+
+ $ret = $this->redis->zRange('key1', 0, -1, TRUE);
+ $this->assertTrue(count($ret) === 3);
+ $retValues = array_keys($ret);
+
+ $this->assertTrue(array('one', 'two', 'three') === $retValues);
+
+ // + 0 converts from string to float OR integer
+ $this->assertTrue(is_float($ret['one'] + 0));
+ $this->assertTrue(is_float($ret['two'] + 0));
+ $this->assertTrue(is_float($ret['three'] + 0));
+
+ $this->redis->delete('key1');
// ZREMRANGEBYRANK
$this->redis->zAdd('key1', 1, 'one');