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

Skip to content

Commit 68136a2

Browse files
committed
Refactor redis_parse_client_list_response
1 parent 39e7c09 commit 68136a2

1 file changed

Lines changed: 33 additions & 84 deletions

File tree

library.c

Lines changed: 33 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,95 +1193,44 @@ redis_client_list_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zva
11931193
PHP_REDIS_API void
11941194
redis_parse_client_list_response(char *response, zval *z_ret)
11951195
{
1196-
char *p, *lpos, *kpos = NULL, *vpos = NULL, *p2, *key, *value;
1197-
int klen = 0, done = 0, is_numeric;
1198-
zval z_sub_result;
1196+
char *p1, *s1 = NULL;
11991197

1200-
/* Allocate for response and our user */
1201-
array_init(z_ret);
1202-
array_init(&z_sub_result);
1203-
1204-
// Pointers for parsing
1205-
p = response;
1206-
lpos = response;
1207-
1208-
/* While we've got more to parse */
1209-
while(!done) {
1210-
/* What character are we on */
1211-
switch(*p) {
1212-
/* We're done */
1213-
case '\0':
1214-
done = 1;
1215-
break;
1216-
/* \n, ' ' mean we can pull a k/v pair */
1217-
case '\n':
1218-
case ' ':
1219-
/* Grab our value */
1220-
vpos = lpos;
1221-
1222-
/* There is some communication error or Redis bug if we don't
1223-
have a key and value, but check anyway. */
1224-
if(kpos && vpos) {
1225-
/* Allocate, copy in our key */
1226-
key = estrndup(kpos, klen);
1227-
1228-
/* Allocate, copy in our value */
1229-
value = estrndup(lpos, p - lpos);
1230-
1231-
/* Treat numbers as numbers, strings as strings */
1232-
is_numeric = 1;
1233-
for(p2 = value; *p2; ++p2) {
1234-
if(*p2 < '0' || *p2 > '9') {
1235-
is_numeric = 0;
1198+
if ((p1 = strtok_r(response, _NL, &s1)) != NULL) {
1199+
array_init(z_ret);
1200+
do {
1201+
char *p2, *s2 = NULL;
1202+
zval z_sub;
1203+
1204+
if ((p2 = strtok_r(p1, " ", &s2)) != NULL) {
1205+
array_init(&z_sub);
1206+
do {
1207+
char *p;
1208+
zend_uchar type;
1209+
zend_long lval;
1210+
double dval;
1211+
if ((p = strchr(p2, '=')) != NULL) {
1212+
type = is_numeric_string(p + 1, s2 - p - 1, &lval, &dval, 0);
1213+
switch (type) {
1214+
case IS_LONG:
1215+
add_assoc_long_ex(&z_sub, p2, p - p2, lval);
1216+
break;
1217+
case IS_DOUBLE:
1218+
add_assoc_double_ex(&z_sub, p2, p - p2, dval);
12361219
break;
1220+
default:
1221+
add_assoc_stringl_ex(&z_sub, p2, p - p2, p + 1, s2 - p - 1);
12371222
}
1238-
}
1239-
1240-
/* Add as a long or string, depending */
1241-
if(is_numeric == 1) {
1242-
add_assoc_long(&z_sub_result, key, atol(value));
12431223
} else {
1244-
add_assoc_string(&z_sub_result, key, value);
1245-
}
1246-
efree(value);
1247-
// If we hit a '\n', then we can add this user to our list
1248-
if(*p == '\n') {
1249-
/* Add our user */
1250-
add_next_index_zval(z_ret, &z_sub_result);
1251-
1252-
/* If we have another user, make another one */
1253-
if(*(p+1) != '\0') {
1254-
array_init(&z_sub_result);
1255-
}
1224+
add_next_index_string(&z_sub, p2);
12561225
}
1257-
1258-
// Free our key
1259-
efree(key);
1260-
} else {
1261-
// Something is wrong
1262-
zval_dtor(z_ret);
1263-
ZVAL_BOOL(z_ret, 0);
1264-
return;
1265-
}
1266-
1267-
/* Move forward */
1268-
lpos = p + 1;
1269-
1270-
break;
1271-
/* We can pull the key and null terminate at our sep */
1272-
case '=':
1273-
/* Key, key length */
1274-
kpos = lpos;
1275-
klen = p - lpos;
1276-
1277-
/* Move forward */
1278-
lpos = p + 1;
1279-
1280-
break;
1281-
}
1282-
1283-
/* Increment */
1284-
p++;
1226+
} while ((p2 = strtok_r(NULL, " ", &s2)) != NULL);
1227+
} else {
1228+
ZVAL_FALSE(&z_sub);
1229+
}
1230+
add_next_index_zval(z_ret, &z_sub);
1231+
} while ((p1 = strtok_r(NULL, _NL, &s1)) != NULL);
1232+
} else {
1233+
ZVAL_FALSE(z_ret);
12851234
}
12861235
}
12871236

0 commit comments

Comments
 (0)