Skip to content

Commit 45ba2fb

Browse files
committed
ext/standard/scanf: add assignToVariables variable and parameter
This makes the code more obvious
1 parent 41d4456 commit 45ba2fb

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

ext/standard/scanf.c

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ typedef zend_long (*int_string_formater)(const char*, char**, int);
114114
static char *BuildCharSet(CharSet *cset, char *format);
115115
static int CharInSet(CharSet *cset, int ch);
116116
static void ReleaseCharSet(CharSet *cset);
117-
static inline void scan_set_error_return(int numVars, zval *return_value);
117+
static inline void scan_set_error_return(bool assignToVariables, zval *return_value);
118118

119119

120120
/* {{{ BuildCharSet
@@ -313,6 +313,7 @@ static int ValidateFormat(char *format, int numVars, int *totalSubs)
313313
int *nassign = staticAssign;
314314
int objIndex, xpgSize, nspace = STATIC_LIST_SIZE;
315315

316+
bool assignToVariables = numVars;
316317
/*
317318
* Initialize an array that records the number of times a variable
318319
* is assigned to by the format string. We use this to detect if
@@ -361,9 +362,9 @@ static int ValidateFormat(char *format, int numVars, int *totalSubs)
361362
if (gotSequential) {
362363
goto mixedXPG;
363364
}
364-
if ((value < 1) || (numVars && (value > numVars))) {
365+
if ((value < 1) || (assignToVariables && (value > numVars))) {
365366
goto badIndex;
366-
} else if (numVars == 0) {
367+
} else if (!assignToVariables) {
367368
/*
368369
* In the case where no vars are specified, the user can
369370
* specify %9999$ legally, so we have to consider special
@@ -410,7 +411,7 @@ static int ValidateFormat(char *format, int numVars, int *totalSubs)
410411
ch = format++;
411412
}
412413

413-
if (!(flags & SCAN_SUPPRESS) && numVars && (objIndex >= numVars)) {
414+
if (!(flags & SCAN_SUPPRESS) && assignToVariables && (objIndex >= numVars)) {
414415
goto badIndex;
415416
}
416417

@@ -513,7 +514,7 @@ static int ValidateFormat(char *format, int numVars, int *totalSubs)
513514
/*
514515
* Verify that all of the variable were assigned exactly once.
515516
*/
516-
if (numVars == 0) {
517+
if (!assignToVariables) {
517518
if (xpgSize) {
518519
numVars = xpgSize;
519520
} else {
@@ -595,11 +596,13 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
595596
numVars = 0;
596597
}
597598

599+
bool assignToVariables = numVars;
600+
598601
/*
599602
* Check for errors in the format string.
600603
*/
601604
if (ValidateFormat(format, numVars, &totalVars) != SCAN_SUCCESS) {
602-
scan_set_error_return( numVars, return_value );
605+
scan_set_error_return( assignToVariables, return_value );
603606
return SCAN_ERROR_INVALID_FORMAT;
604607
}
605608

@@ -608,7 +611,7 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
608611
/*
609612
* If any variables are passed, make sure they are all passed by reference
610613
*/
611-
if (numVars) {
614+
if (assignToVariables) {
612615
for (i = 0; i < argCount; i++){
613616
ZEND_ASSERT(Z_ISREF(args[i]) && "Parameter must be passed by reference");
614617
}
@@ -618,7 +621,7 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
618621
* Allocate space for the result objects. Only happens when no variables
619622
* are specified
620623
*/
621-
if (!numVars) {
624+
if (!assignToVariables) {
622625
zval tmp;
623626

624627
/* allocate an array for return */
@@ -720,9 +723,9 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
720723
switch (*ch) {
721724
case 'n':
722725
if (!(flags & SCAN_SUPPRESS)) {
723-
if (numVars && objIndex >= argCount) {
726+
if (assignToVariables && objIndex >= argCount) {
724727
break;
725-
} else if (numVars) {
728+
} else if (assignToVariables) {
726729
current = args + objIndex++;
727730
ZEND_TRY_ASSIGN_REF_LONG(current, (zend_long) (string - baseString));
728731
} else {
@@ -838,9 +841,9 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
838841
}
839842
}
840843
if (!(flags & SCAN_SUPPRESS)) {
841-
if (numVars && objIndex >= argCount) {
844+
if (assignToVariables && objIndex >= argCount) {
842845
break;
843-
} else if (numVars) {
846+
} else if (assignToVariables) {
844847
current = args + objIndex++;
845848
ZEND_TRY_ASSIGN_REF_STRINGL(current, string, end - string);
846849
} else {
@@ -878,9 +881,9 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
878881
goto done;
879882
}
880883
if (!(flags & SCAN_SUPPRESS)) {
881-
if (numVars && objIndex >= argCount) {
884+
if (assignToVariables && objIndex >= argCount) {
882885
break;
883-
} else if (numVars) {
886+
} else if (assignToVariables) {
884887
current = args + objIndex++;
885888
ZEND_TRY_ASSIGN_REF_STRINGL(current, string, end - string);
886889
} else {
@@ -897,7 +900,7 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
897900
sch = *string;
898901
string++;
899902
if (!(flags & SCAN_SUPPRESS)) {
900-
if (numVars) {
903+
if (assignToVariables) {
901904
char __buf[2];
902905
__buf[0] = sch;
903906
__buf[1] = '\0';
@@ -1030,19 +1033,19 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
10301033
value = (zend_long) (*fn)(buf, NULL, base);
10311034
if ((flags & SCAN_UNSIGNED) && (value < 0)) {
10321035
snprintf(buf, sizeof(buf), ZEND_ULONG_FMT, value); /* INTL: ISO digit */
1033-
if (numVars && objIndex >= argCount) {
1036+
if (assignToVariables && objIndex >= argCount) {
10341037
break;
1035-
} else if (numVars) {
1038+
} else if (assignToVariables) {
10361039
/* change passed value type to string */
10371040
current = args + objIndex++;
10381041
ZEND_TRY_ASSIGN_REF_STRING(current, buf);
10391042
} else {
10401043
add_index_string(return_value, objIndex++, buf);
10411044
}
10421045
} else {
1043-
if (numVars && objIndex >= argCount) {
1046+
if (assignToVariables && objIndex >= argCount) {
10441047
break;
1045-
} else if (numVars) {
1048+
} else if (assignToVariables) {
10461049
current = args + objIndex++;
10471050
ZEND_TRY_ASSIGN_REF_LONG(current, value);
10481051
} else {
@@ -1144,9 +1147,9 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
11441147
double dvalue;
11451148
*end = '\0';
11461149
dvalue = zend_strtod(buf, NULL);
1147-
if (numVars && objIndex >= argCount) {
1150+
if (assignToVariables && objIndex >= argCount) {
11481151
break;
1149-
} else if (numVars) {
1152+
} else if (assignToVariables) {
11501153
current = args + objIndex++;
11511154
ZEND_TRY_ASSIGN_REF_DOUBLE(current, dvalue);
11521155
} else {
@@ -1162,9 +1165,9 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
11621165
result = SCAN_SUCCESS;
11631166

11641167
if (underflow && (0==nconversions)) {
1165-
scan_set_error_return( numVars, return_value );
1168+
scan_set_error_return( assignToVariables, return_value );
11661169
result = SCAN_ERROR_EOF;
1167-
} else if (numVars) {
1170+
} else if (assignToVariables) {
11681171
zval_ptr_dtor(return_value );
11691172
ZVAL_LONG(return_value, nconversions);
11701173
} else if (nconversions < totalVars) {
@@ -1175,9 +1178,9 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
11751178
/* }}} */
11761179

11771180
/* the compiler choked when i tried to make this a macro */
1178-
static inline void scan_set_error_return(int numVars, zval *return_value) /* {{{ */
1181+
static inline void scan_set_error_return(bool assignToVariables, zval *return_value) /* {{{ */
11791182
{
1180-
if (numVars) {
1183+
if (assignToVariables) {
11811184
ZVAL_LONG(return_value, SCAN_ERROR_EOF); /* EOF marker */
11821185
} else {
11831186
/* convert_to_null calls destructor */

0 commit comments

Comments
 (0)