Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 982bd13

Browse files
committed
Refactor redis_parse_info_response
1 parent 393c0bd commit 982bd13

1 file changed

Lines changed: 26 additions & 43 deletions

File tree

library.c

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,50 +1111,33 @@ PHP_REDIS_API int redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *r
11111111
PHP_REDIS_API void
11121112
redis_parse_info_response(char *response, zval *z_ret)
11131113
{
1114-
char *cur, *pos;
1115-
1116-
array_init(z_ret);
1114+
char *p1, *s1 = NULL;
11171115

1118-
cur = response;
1119-
while(1) {
1120-
/* skip comments and empty lines */
1121-
if (*cur == '#' || *cur == '\r') {
1122-
if ((cur = strstr(cur, _NL)) == NULL) {
1123-
break;
1116+
ZVAL_FALSE(z_ret);
1117+
if ((p1 = php_strtok_r(response, _NL, &s1)) != NULL) {
1118+
array_init(z_ret);
1119+
do {
1120+
if (*p1 == '#') continue;
1121+
char *p;
1122+
zend_uchar type;
1123+
zend_long lval;
1124+
double dval;
1125+
if ((p = strchr(p1, ':')) != NULL) {
1126+
type = is_numeric_string(p + 1, strlen(p + 1), &lval, &dval, 0);
1127+
switch (type) {
1128+
case IS_LONG:
1129+
add_assoc_long_ex(z_ret, p1, p - p1, lval);
1130+
break;
1131+
case IS_DOUBLE:
1132+
add_assoc_double_ex(z_ret, p1, p - p1, dval);
1133+
break;
1134+
default:
1135+
add_assoc_string_ex(z_ret, p1, p - p1, p + 1);
1136+
}
1137+
} else {
1138+
add_next_index_string(z_ret, p1);
11241139
}
1125-
cur += 2;
1126-
continue;
1127-
}
1128-
1129-
/* key */
1130-
if ((pos = strchr(cur, ':')) == NULL) {
1131-
break;
1132-
}
1133-
char *key = cur;
1134-
int key_len = pos - cur;
1135-
key[key_len] = '\0';
1136-
1137-
/* value */
1138-
cur = pos + 1;
1139-
if ((pos = strstr(cur, _NL)) == NULL) {
1140-
break;
1141-
}
1142-
char *value = cur;
1143-
int value_len = pos - cur;
1144-
value[value_len] = '\0';
1145-
1146-
double dval;
1147-
zend_long lval;
1148-
zend_uchar type = is_numeric_string(value, value_len, &lval, &dval, 0);
1149-
if (type == IS_LONG) {
1150-
add_assoc_long_ex(z_ret, key, key_len, lval);
1151-
} else if (type == IS_DOUBLE) {
1152-
add_assoc_double_ex(z_ret, key, key_len, dval);
1153-
} else {
1154-
add_assoc_stringl_ex(z_ret, key, key_len, value, value_len);
1155-
}
1156-
1157-
cur = pos + 2; /* \r, \n */
1140+
} while ((p1 = php_strtok_r(NULL, _NL, &s1)) != NULL);
11581141
}
11591142
}
11601143

@@ -1211,7 +1194,7 @@ redis_parse_client_list_response(char *response, zval *z_ret)
12111194
zend_long lval;
12121195
double dval;
12131196
if ((p = strchr(p2, '=')) != NULL) {
1214-
type = is_numeric_string(p + 1, s2 - p - 1, &lval, &dval, 0);
1197+
type = is_numeric_string(p + 1, strlen(p + 1), &lval, &dval, 0);
12151198
switch (type) {
12161199
case IS_LONG:
12171200
add_assoc_long_ex(&z_sub, p2, p - p2, lval);

0 commit comments

Comments
 (0)