8000 refactoring · jrtkcoder/phpredis@388e4a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 388e4a8

Browse files
committed
refactoring
New implementation of redis_parse_info_response function without estrndup using and with long/double/string values
1 parent 058753e commit 388e4a8

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

library.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -923,54 +923,50 @@ PHP_REDIS_API void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *
923923
PHP_REDIS_API void
924924
redis_parse_info_response(char *response, zval *z_ret)
925925
{
926-
char *key, *value, *p, *cur, *pos;
927-
int is_numeric;
926+
char *cur, *pos;
928927

929928
array_init(z_ret);
930929

931930
cur = response;
932931
while(1) {
933932
/* skip comments and empty lines */
934-
if(*cur == '#' || *cur == '\r') {
935-
if(!(cur = strchr(cur, '\n')))
933+
if (*cur == '#' || *cur == '\r') {
934+
if ((cur = strstr(cur, _NL)) == NULL) {
936935
break;
937-
cur++;
936+
}
937+
cur += 2;
938938
continue;
939939
}
940940

941941
/* key */
942-
pos = strchr(cur, ':');
943-
if(pos == NULL) {
942+
if ((pos = strchr(cur, ':')) == NULL) {
944943
break;
945944
}
946-
key = estrndup(cur, pos - cur);
945+
char *key = cur;
946+
int key_len = pos - cur;
947+
key[key_len] = '\0';
947948

948949
/* value */
949950
cur = pos + 1;
950-
pos = strchr(cur, '\r');
951-
if(pos == NULL) {
952-
efree(key);
951+
if ((pos = strstr(cur, _NL)) == NULL) {
953952
break;
954953
}
955-
value = estrndup(cur, pos - cur);
956-
pos += 2; /* \r, \n */
957-
cur = pos;
958-
959-
is_numeric = 1;
960-
for(p = value; *p; ++p) {
961-
if(*p < '0' || *p > '9') {
962-
is_numeric = 0;
963-
break;
964-
}
965-
}
966-
967-
if(is_numeric == 1) {
968-
add_assoc_long(z_ret, key, atol(value));
954+
char *value = cur;
955+
int value_len = pos - cur;
956+
value[value_len] = '\0';
957+
958+
double dval;
959+
zend_long lval;
960+
zend_uchar type = is_numeric_string(value, value_len, &lval, &dval, 0);
961+
if (type == IS_LONG) {
962+
add_assoc_long_ex(z_ret, key, key_len, lval);
963+
} else if (type == IS_DOUBLE) {
964+
add_assoc_double_ex(z_ret, key, key_len, dval);
969965
} else {
970-
add_assoc_string(z_ret, key, value);
966+
add_assoc_stringl_ex(z_ret, key, key_len, value, value_len);
971967
}
972-
efree(value);
973-
efree(key);
968+
969+
cur = pos + 2; /* \r, \n */
974970
}
975971
}
976972

0 commit comments

Comments
 (0)
0