Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1981. Added SplitNewLine method to the TextFormatter. #1982

Merged
merged 1 commit into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Terminal.Gui/Core/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,50 @@ static ustring ReplaceCRLFWithSpace (ustring str)
return ustring.Make (runes);
}

/// <summary>
/// Splits all newlines in the <paramref name="text"/> into a list
/// and supports both CRLF and LF, preserving the ending newline.
/// </summary>
/// <param name="text">The text.</param>
/// <returns>A list of text without the newline characters.</returns>
public static List<ustring> SplitNewLine (ustring text)
{
var runes = text.ToRuneList ();
var lines = new List<ustring> ();
var start = 0;
var end = 0;

for (int i = 0; i < runes.Count; i++) {
end = i;
switch (runes [i]) {
case '\n':
lines.Add (ustring.Make (runes.GetRange (start, end - start)));
i++;
start = i;
break;

case '\r':
if ((i + 1) < runes.Count && runes [i + 1] == '\n') {
lines.Add (ustring.Make (runes.GetRange (start, end - start)));
i += 2;
start = i;
} else {
lines.Add (ustring.Make (runes.GetRange (start, end - start)));
i++;
start = i;
}
break;
}
}
if (runes.Count > 0 && lines.Count == 0) {
lines.Add (ustring.Make (runes));
} else if (runes.Count > 0 && start < runes.Count) {
lines.Add (ustring.Make (runes.GetRange (start, runes.Count - start)));
} else {
lines.Add (ustring.Make (""));
}
return lines;
}

/// <summary>
/// Adds trailing whitespace or truncates <paramref name="text"/>
Expand Down
42 changes: 42 additions & 0 deletions UnitTests/TextFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4012,5 +4012,47 @@ public void Format_With_PreserveTrailingSpaces_And_Without_PreserveTrailingSpace
Assert.Equal ("Line2", formated [1]);
Assert.Equal ("Line3", formated [^1]);
}

[Fact]
public void SplitNewLine_Ending_Without_NewLine_Probably_CRLF ()
{
var text = $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界";
var splited = TextFormatter.SplitNewLine (text);
Assert.Equal ("First Line 界", splited [0]);
Assert.Equal ("Second Line 界", splited [1]);
Assert.Equal ("Third Line 界", splited [^1]);
}

[Fact]
public void SplitNewLine_Ending_With_NewLine_Probably_CRLF ()
{
var text = $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界{Environment.NewLine}";
var splited = TextFormatter.SplitNewLine (text);
Assert.Equal ("First Line 界", splited [0]);
Assert.Equal ("Second Line 界", splited [1]);
Assert.Equal ("Third Line 界", splited [2]);
Assert.Equal ("", splited [^1]);
}

[Fact]
public void SplitNewLine_Ending_Without_NewLine_Only_LF ()
{
var text = $"First Line 界\nSecond Line 界\nThird Line 界";
var splited = TextFormatter.SplitNewLine (text);
Assert.Equal ("First Line 界", splited [0]);
Assert.Equal ("Second Line 界", splited [1]);
Assert.Equal ("Third Line 界", splited [^1]);
}

[Fact]
public void SplitNewLine_Ending_With_NewLine_Only_LF ()
{
var text = $"First Line 界\nSecond Line 界\nThird Line 界\n";
var splited = TextFormatter.SplitNewLine (text);
Assert.Equal ("First Line 界", splited [0]);
Assert.Equal ("Second Line 界", splited [1]);
Assert.Equal ("Third Line 界", splited [2]);
Assert.Equal ("", splited [^1]);
}
}
}