Skip to content

Commit 149c742

Browse files
committed
prefactor: extract yp_newline_list_check_append
1 parent 0d5d759 commit 149c742

File tree

5 files changed

+19
-21
lines changed

5 files changed

+19
-21
lines changed

include/yarp/util/yp_newline_list.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ bool yp_newline_list_init(yp_newline_list_t *list, const char *start, size_t cap
4747
// the offsets succeeds (if one was necessary), otherwise returns false.
4848
bool yp_newline_list_append(yp_newline_list_t *list, const char *cursor);
4949

50+
// Conditionally append a new offset to the newline list, if the value passed in is a newline.
51+
bool yp_newline_list_check_append(yp_newline_list_t *list, const char *cursor);
52+
5053
// Returns the line and column of the given offset. If the offset is not in the
5154
// list, the line and column of the closest offset less than the given offset
5255
// are returned.

src/util/yp_newline_list.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ yp_newline_list_append(yp_newline_list_t *list, const char *cursor) {
3838
return true;
3939
}
4040

41+
// Conditionally append a new offset to the newline list, if the value passed in is a newline.
42+
bool
43+
yp_newline_list_check_append(yp_newline_list_t *list, const char *cursor) {
44+
if (*cursor != '\n') {
45+
return true;
46+
}
47+
return yp_newline_list_append(list, cursor);
48+
}
49+
4150
// Returns the line and column of the given offset, assuming we don't have any
4251
// information about the previous index that we found.
4352
static yp_line_column_t

src/yarp.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6183,9 +6183,7 @@ parser_lex(yp_parser_t *parser) {
61836183
parser->current.end++;
61846184
}
61856185

6186-
if (*parser->current.end == '\n') {
6187-
yp_newline_list_append(&parser->newline_list, parser->current.end);
6188-
}
6186+
yp_newline_list_check_append(&parser->newline_list, parser->current.end);
61896187

61906188
parser->current.end++;
61916189
LEX(YP_TOKEN_STRING_BEGIN);
@@ -6215,9 +6213,7 @@ parser_lex(yp_parser_t *parser) {
62156213

62166214
if (parser->current.end < parser->end) {
62176215
lex_mode_push_regexp(parser, lex_mode_incrementor(*parser->current.end), lex_mode_terminator(*parser->current.end));
6218-
if (parser->current.end == '\n') {
6219-
yp_newline_list_append(&parser->newline_list, parser->current.end);
6220-
}
6216+
yp_newline_list_check_append(&parser->newline_list, parser->current.end);
62216217
parser->current.end++;
62226218
}
62236219

@@ -6465,9 +6461,7 @@ parser_lex(yp_parser_t *parser) {
64656461

64666462
// If the result is an escaped newline, then we need to
64676463
// track that newline.
6468-
if (breakpoint[difference - 1] == '\n') {
6469-
yp_newline_list_append(&parser->newline_list, breakpoint + difference - 1);
6470-
}
6464+
yp_newline_list_check_append(&parser->newline_list, breakpoint + difference - 1);
64716465

64726466
breakpoint = yp_strpbrk(parser, breakpoint + difference, breakpoints, parser->end - (breakpoint + difference));
64736467
continue;
@@ -6580,9 +6574,7 @@ parser_lex(yp_parser_t *parser) {
65806574

65816575
// If the result is an escaped newline, then we need to
65826576
// track that newline.
6583-
if (breakpoint[difference - 1] == '\n') {
6584-
yp_newline_list_append(&parser->newline_list, breakpoint + difference - 1);
6585-
}
6577+
yp_newline_list_check_append(&parser->newline_list, breakpoint + difference - 1);
65866578

65876579
breakpoint = yp_strpbrk(parser, breakpoint + difference, breakpoints, parser->end - (breakpoint + difference));
65886580
continue;
@@ -6673,9 +6665,7 @@ parser_lex(yp_parser_t *parser) {
66736665
parser->current.end = breakpoint + 2;
66746666
yp_newline_list_append(&parser->newline_list, breakpoint + 1);
66756667
} else {
6676-
if (*parser->current.end == '\n') {
6677-
yp_newline_list_append(&parser->newline_list, parser->current.end);
6678-
}
6668+
yp_newline_list_check_append(&parser->newline_list, parser->current.end);
66796669

66806670
parser->current.end = breakpoint + 1;
66816671
}
@@ -6725,9 +6715,7 @@ parser_lex(yp_parser_t *parser) {
67256715

67266716
// If the result is an escaped newline, then we need to
67276717
// track that newline.
6728-
if (breakpoint[difference - 1] == '\n') {
6729-
yp_newline_list_append(&parser->newline_list, breakpoint + difference - 1);
6730-
}
6718+
yp_newline_list_check_append(&parser->newline_list, breakpoint + difference - 1);
67316719

67326720
breakpoint = yp_strpbrk(parser, breakpoint + difference, breakpoints, parser->end - (breakpoint + difference));
67336721
break;
@@ -6898,9 +6886,7 @@ parser_lex(yp_parser_t *parser) {
68986886
yp_unescape_type_t unescape_type = (quote == YP_HEREDOC_QUOTE_SINGLE) ? YP_UNESCAPE_MINIMAL : YP_UNESCAPE_ALL;
68996887
size_t difference = yp_unescape_calculate_difference(breakpoint, parser->end, unescape_type, false, &parser->error_list);
69006888

6901-
if (breakpoint[difference - 1] == '\n') {
6902-
yp_newline_list_append(&parser->newline_list, breakpoint + difference - 1);
6903-
}
6889+
yp_newline_list_check_append(&parser->newline_list, breakpoint + difference - 1);
69046890

69056891
breakpoint = yp_strpbrk(parser, breakpoint + difference, breakpoints, parser->end - (breakpoint + difference));
69066892
}

0 commit comments

Comments
 (0)