Commit 3a2ff982 authored by Seeker's avatar Seeker

Fix for empty lines in squiggly heredocs

parent 6d98ae57
...@@ -1362,26 +1362,29 @@ heredoc_end(parser_state *p) ...@@ -1362,26 +1362,29 @@ heredoc_end(parser_state *p)
node *pair = list->car->cdr; node *pair = list->car->cdr;
const char *str = (char*)pair->car; const char *str = (char*)pair->car;
size_t len = (size_t)pair->cdr; size_t len = (size_t)pair->cdr;
if (counting) { mrb_bool check = counting;
list2 = push(list2, pair); mrb_bool empty = TRUE;
} mrb_bool newline = FALSE;
size_t spaces = 0; size_t spaces = 0;
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
if (counting) {
if (ISSPACE(str[i])) {
++spaces;
}
else {
counting = FALSE;
if (indent == -1 || spaces < indent) {
indent = spaces;
}
}
}
if (str[i] == '\n') { if (str[i] == '\n') {
counting = TRUE; counting = TRUE;
newline = TRUE;
break; break;
} }
if (ISSPACE(str[i])) {
if (counting)
++spaces;
}
else {
counting = FALSE;
empty = FALSE;
}
}
if (check) {
if ((indent == -1 || spaces < indent) && (!empty || !newline))
indent = spaces;
list2 = push(list2, cons((node*)spaces, pair));
} }
} }
else { else {
...@@ -1391,11 +1394,15 @@ heredoc_end(parser_state *p) ...@@ -1391,11 +1394,15 @@ heredoc_end(parser_state *p)
} }
if (indent > 0) { if (indent > 0) {
while (list2) { while (list2) {
node *pair = list2->car; node *n = list2->car;
const char *str = (char*)pair->car; size_t spaces = (size_t)n->car;
size_t len = (size_t)pair->cdr; if (spaces >= indent) {
pair->car = (node*)(str + indent); node *pair = n->cdr;
pair->cdr = (node*)(len - indent); const char *str = (char*)pair->car;
size_t len = (size_t)pair->cdr;
pair->car = (node*)(str + indent);
pair->cdr = (node*)(len - indent);
}
list2 = list2->cdr; list2 = list2->cdr;
} }
} }
......
This diff is collapsed.
...@@ -181,6 +181,12 @@ QQ2 ...@@ -181,6 +181,12 @@ QQ2
UUU2 UUU2
u#{3}u u#{3}u
UUU3 UUU3
v = <<~VVV
\tvvv
\t\t
\t#{123}
VVV
w = %W( 1 #{<<WWW} 3 w = %W( 1 #{<<WWW} 3
www www
...@@ -221,6 +227,7 @@ ZZZ ...@@ -221,6 +227,7 @@ ZZZ
assert_equal "sss\n sss\n", s assert_equal "sss\n sss\n", s
assert_equal "ttt\n ttt\n", t assert_equal "ttt\n ttt\n", t
assert_equal ["u1u\n", "u2u\n", "u\#{3}u\n"], u assert_equal ["u1u\n", "u2u\n", "u\#{3}u\n"], u
assert_equal "\nvvv\n\t\n123\n", v
assert_equal ["1", "www\n", "3", "4", "5"], w assert_equal ["1", "www\n", "3", "4", "5"], w
assert_equal [1, "foo 222 333\n 444\n5\n bar\n6\n", 9], x assert_equal [1, "foo 222 333\n 444\n5\n bar\n6\n", 9], x
assert_equal "", z assert_equal "", z
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment