Skip to content

Commit 312dd87

Browse files
committed
Optionally include empty last line when string ends with newline.
1 parent b0c93e7 commit 312dd87

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/org/rascalmpl/library/String.rsc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ str trimFinalNewlines(str input, list[str] lineseps = newLineCharacters) {
767767
}
768768
769769
@synopsis{Split a string in <text, newline> pairs for each line.}
770-
list[tuple[str, str]] separateLines(str input, list[str] lineseps = newLineCharacters) {
770+
list[tuple[str, str]] separateLines(str input, bool includeEmptyLastLine = false, list[str] lineseps = newLineCharacters) {
771771
orderedSeps = reverse(sort(lineseps, bySize));
772772
773773
list[tuple[str, str]] lines = [];
@@ -781,8 +781,8 @@ list[tuple[str, str]] separateLines(str input, list[str] lineseps = newLineChara
781781
}
782782
783783
// last line
784-
if (str nl <- orderedSeps, nl == input[-size(nl)..]) {
785-
lines += <input[next..next+size(nl)], "">;
784+
if (next < size(input) || includeEmptyLastLine) {
785+
lines += <input[next..], "">;
786786
}
787787
788788
return lines;
@@ -793,8 +793,8 @@ str mergeLines(list[tuple[str, str]] lines)
793793
= ("" | it + line + sep | <line, sep> <- lines);
794794
795795
@synopsis{Process the text of a string per line, maintaining the original newline characters.}
796-
str perLine(str input, str(str) lineFunc, list[str] lineseps = newLineCharacters)
797-
= mergeLines([<lineFunc(l), nl> | <l, nl> <- separateLines(input, lineseps=lineseps)]);
796+
str perLine(str input, str(str) lineFunc, bool includeEmptyLastLine = false, list[str] lineseps = newLineCharacters)
797+
= mergeLines([<lineFunc(l), nl> | <l, nl> <- separateLines(input, includeEmptyLastLine=includeEmptyLastLine, lineseps=lineseps)]);
798798
799799
@synopsis{Trim trailing non-newline whitespace from each line in a multi-line string.}
800800
str trimTrailingWhitespace(str input, list[str] lineseps = newLineCharacters) {

src/org/rascalmpl/library/lang/rascal/tests/library/String.rsc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,18 @@ test bool trimFinalNewlineTestWhiteSpace() = trimFinalNewlines("a\n\n\nb\n\n ")
282282
test bool trimTrailingWhitespaceTest() = trimTrailingWhitespace("a \nb\t\n c \n") == "a\nb\n c\n";
283283

284284
// perLine
285-
test bool perLineTest() = perLine("a\nb\r\nc\n\r\n", str(str line) { return line + "x"; }) == "ax\nbx\r\ncx\nx\r\nx";
285+
test bool perLineTest() = perLine("a\nb\r\nc\n\r\n", str(str line) { return line + "x"; }) == "ax\nbx\r\ncx\nx\r\n";
286+
287+
// separateLines
288+
test bool separateLinesTestSimple() = separateLines("a\nb\r\nc\n\r\n") == [<"a", "\n">, <"b", "\r\n">, <"c", "\n">, <"", "\r\n">];
289+
test bool separateLinesTestSimpleWithLast() = separateLines("a\nb\r\nc\n\r\n", includeEmptyLastLine=true) == [<"a", "\n">, <"b", "\r\n">, <"c", "\n">, <"", "\r\n">, <"", "">];
290+
test bool separateLinesTestNoFinalNewline() = separateLines("a\nb\r\nc") == [<"a", "\n">, <"b", "\r\n">, <"c", "">];
291+
test bool separateLinesTestNoFinalNewlineNoEmpty() = separateLines("a\nb\r\nc", includeEmptyLastLine=true) == [<"a", "\n">, <"b", "\r\n">, <"c", "">];
292+
test bool separateLinesTestOnlyNewlines() = separateLines("\n\r\n\n\r\n") == [<"", "\n">, <"", "\r\n">, <"", "\n">, <"", "\r\n">];
293+
test bool separateLinesTestNoNewlines() = separateLines("abc") == [<"abc", "">];
294+
295+
// substrings
296+
test bool substringsTestEmpty() = substrings("") == {};
297+
test bool substringsTestSingle() = substrings("a") == {};
298+
test bool substringsTestTwo() = substrings("ab") == {"a", "b"};
299+
test bool substringsTestThree() = substrings("abc") == {"a", "b", "c", "ab", "bc"};

0 commit comments

Comments
 (0)