瀏覽代碼

Added SplitNewLine method to the TextFormatter. (#1982)

BDisp 2 年之前
父節點
當前提交
31082c001c
共有 2 個文件被更改,包括 86 次插入0 次删除
  1. 44 0
      Terminal.Gui/Core/TextFormatter.cs
  2. 42 0
      UnitTests/TextFormatterTests.cs

+ 44 - 0
Terminal.Gui/Core/TextFormatter.cs

@@ -417,6 +417,50 @@ namespace Terminal.Gui {
 			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"/>

+ 42 - 0
UnitTests/TextFormatterTests.cs

@@ -4012,5 +4012,47 @@ e
 			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]);
+		}
 	}
 }