Skip to content

Commit 076fbd3

Browse files
committed
Minor fgetcsv cleanup
Reduce some variable scopes.
1 parent 1a81251 commit 076fbd3

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

ext/standard/file.c

+13-14
Original file line numberDiff line numberDiff line change
@@ -2047,10 +2047,10 @@ PHP_FUNCTION(fgetcsv)
20472047

20482048
PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int escape_char, size_t buf_len, char *buf, zval *return_value) /* {{{ */
20492049
{
2050-
char *temp, *tptr, *bptr, *line_end, *limit;
2050+
char *temp, *bptr, *line_end, *limit;
20512051
size_t temp_len, line_end_len;
20522052
int inc_len;
2053-
bool first_field = 1;
2053+
bool first_field = true;
20542054

20552055
ZEND_ASSERT((escape_char >= 0 && escape_char <= UCHAR_MAX) || escape_char == PHP_CSV_NO_ESCAPE);
20562056

@@ -2062,9 +2062,8 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
20622062
/* Strip trailing space from buf, saving end of line in case required for enclosure field */
20632063

20642064
bptr = buf;
2065-
tptr = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len);
2066-
line_end_len = buf_len - (size_t)(tptr - buf);
2067-
line_end = limit = tptr;
2065+
line_end = limit = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len);
2066+
line_end_len = buf_len - (size_t)(limit - buf);
20682067

20692068
/* reserve workspace for building each individual field */
20702069
temp_len = buf_len;
@@ -2078,8 +2077,7 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
20782077

20792078
do {
20802079
char *comp_end, *hunk_begin;
2081-
2082-
tptr = temp;
2080+
char *tptr = temp;
20832081

20842082
inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
20852083
if (inc_len == 1) {
@@ -2096,7 +2094,7 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
20962094
add_next_index_null(return_value);
20972095
break;
20982096
}
2099-
first_field = 0;
2097+
first_field = false;
21002098
/* 2. Read field, leaving bptr pointing at start of next field */
21012099
if (inc_len != 0 && *bptr == enclosure) {
21022100
int state = 0;
@@ -2122,10 +2120,6 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
21222120
ZEND_FALLTHROUGH;
21232121

21242122
case 0: {
2125-
char *new_buf;
2126-
size_t new_len;
2127-
char *new_temp;
2128-
21292123
if (hunk_begin != line_end) {
21302124
memcpy(tptr, hunk_begin, bptr - hunk_begin);
21312125
tptr += (bptr - hunk_begin);
@@ -2138,7 +2132,11 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
21382132

21392133
if (stream == NULL) {
21402134
goto quit_loop_2;
2141-
} else if ((new_buf = php_stream_get_line(stream, NULL, 0, &new_len)) == NULL) {
2135+
}
2136+
2137+
size_t new_len;
2138+
char *new_buf = php_stream_get_line(stream, NULL, 0, &new_len);
2139+
if (!new_buf) {
21422140
/* we've got an unterminated enclosure,
21432141
* assign all the data from the start of
21442142
* the enclosure to end of data to the
@@ -2150,8 +2148,9 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
21502148
RETVAL_FALSE;
21512149
goto out;
21522150
}
2151+
21532152
temp_len += new_len;
2154-
new_temp = erealloc(temp, temp_len);
2153+
char *new_temp = erealloc(temp, temp_len);
21552154
tptr = new_temp + (size_t)(tptr - temp);
21562155
temp = new_temp;
21572156

0 commit comments

Comments
 (0)