Skip to content

Commit 6f6bf21

Browse files
author
Nicolas Laurent
committed
simplify newline normalization logic
1 parent 21bbfda commit 6f6bf21

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/main/java/org/truffleruby/parser/lexer/HeredocTerm.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,17 @@ public int parseString(RubyLexer lexer) {
115115
if ((flags & STR_FUNC_EXPAND) == 0) {
116116
// heredocs without string interpolation
117117

118-
// TODO what's in lbuf?
119118
do { // iterate on lines, while end marker not found
120-
Rope lbuf = lexer.lexb;
119+
final Rope lbuf = lexer.lexb;
121120
int pend = lexer.lex_pend;
122121

123-
// Adjust pend so that it doesn't cover the final newline, excepted if the line
124-
// only has a single \n, or has both \n and \r - in which case a single \n wil be included.
125-
// TODO: this logic is insane - why is it necessary? - can it be refactored?
126-
if (pend > p) {
122+
// Remove trailing newline, it will be appended later in normalized form (single \n).
123+
if (pend > 0) {
127124
switch (lexer.p(pend - 1)) {
128125
case '\n':
129126
pend--;
130-
if (pend == p || lexer.p(pend - 1) == '\r') {
131-
pend++;
132-
break;
127+
if (pend > 0 && lexer.p(pend - 1) == '\r') {
128+
pend--;
133129
}
134130
break;
135131
case '\r':
@@ -156,10 +152,8 @@ public int parseString(RubyLexer lexer) {
156152
str = builder;
157153
}
158154

159-
// if the newline wasn't included in the append, add it now
160-
if (pend < lexer.lex_pend) {
161-
str.append('\n');
162-
}
155+
// append the newline that we removed earlier
156+
str.append('\n');
163157
lexer.lex_goto_eol();
164158

165159
if (lexer.getHeredocIndent() > 0) {

0 commit comments

Comments
 (0)