Skip to content
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
48 changes: 27 additions & 21 deletions src/CommonNovel/Noder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,40 @@ public partial class Compiler
/// <returns>Nodes</returns>
public static string[] Noder(string input)
{
string[] lines = [];
string[] nodes = [];
ReadOnlySpan<char> inputSpan = input.AsSpan();
List<string> nodeList = [];

using (StringReader reader = new(input))
int start = 0;
for (int i = 0; i < inputSpan.Length; i++)
{
string? line;
while ((line = reader.ReadLine()) != null)
{
lines = [.. lines, line];
}
}
int? end = null;
if (inputSpan[i] != '\n') continue;

int i = 0;
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line))
// "\n\n" (LF) or "\r\n\r\n" (CRLF), "\n \t\n" (tabs, spaces) => Split
for (int j = i + 1; j < inputSpan.Length; j++)
{
i++;
continue;
if (inputSpan[j] == '\n')
{
end = j + 1;
break;
}
if (char.IsWhiteSpace(inputSpan[j]) || (inputSpan[i] == '\r'))
continue;
else
break;
}

Array.Resize(ref nodes, i + 1);
if (string.IsNullOrWhiteSpace(nodes[i]))
nodes[i] = line;
else
nodes[i] += Environment.NewLine + line;
if (end is null) continue;

ReadOnlySpan<char> node = inputSpan[start..end.Value].Trim();
nodeList.Add(node.ToString());
Comment thread
Lemon73-Computing marked this conversation as resolved.

start = end.Value;
}
Comment thread
Lemon73-Computing marked this conversation as resolved.

return nodes;
if (start < inputSpan.Length)
nodeList.Add(inputSpan[start..].Trim().ToString());

return nodeList.ToArray();
}
}
25 changes: 18 additions & 7 deletions tests/CommonNovel.Tests/Noder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Diagnostics.CodeAnalysis;

namespace CommonNovel.Tests;

public partial class CompilerUnitTest
Expand All @@ -17,31 +19,40 @@ public void TestNoder(string input, string[] expectedNodes)
}
}

private static readonly string Nl = Environment.NewLine;

[ExcludeFromCodeCoverage]
public static IEnumerable<object[]> NoderTestData =>
new List<object[]>
{
new object[] // CRLF
{
"- Alice\r\n[Hi, Bob!]\r\n\r\n- Bob\r\n[Alice!]\r\n\r\n",
(string[])[
$"- Alice{Nl}[Hi, Bob!]", // Future -> "\r\n"
$"- Bob{Nl}[Alice!]" // Future -> "\r\n"
"- Alice\r\n[Hi, Bob!]",
"- Bob\r\n[Alice!]"
]
},
new object[] // LF
{
"- Alice\n[Hi, Bob!]\n\n- Bob\n[Alice!]\n\n",
(string[])[
$"- Alice{Nl}[Hi, Bob!]", // Future -> '\n'
$"- Bob{Nl}[Alice!]" // Future -> '\n'
"- Alice\n[Hi, Bob!]",
"- Bob\n[Alice!]"
]
},
new object[] // three enters
{
"\r\n\r\n\r\n",
(string[])[] // Future -> [ "", "" ]
(string[])[ "", "" ]
},
new object[] // single element
{
"- Alice",
(string[])[ "- Alice" ]
},
new object[] // spaces and tabs between newline-codes (\n or \r\n)
{
"\r\n \t \r\n",
(string[])[ "" ]
}
};
}
5 changes: 4 additions & 1 deletion tests/CommonNovel.Tests/Parser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace CommonNovel.Tests;
using System.Diagnostics.CodeAnalysis;

namespace CommonNovel.Tests;

public partial class CompilerUnitTest
{
Expand All @@ -20,6 +22,7 @@ public void TestParser(string inputNode, string[][] expectedAST)
}
}

[ExcludeFromCodeCoverage]
public static IEnumerable<object[]> ParserTestData =>
new List<object[]>
{
Expand Down
Loading