Skip to content

Commit ac8664b

Browse files
committed
Name "NextSectionWhilePunc" correctly
1 parent d5377b7 commit ac8664b

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed

CSharpMath.Rendering.Text.Tests/TextLaTeXParserTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@ private Action<TextAtom> CheckAtom<T>
2020
[InlineData("abc")]
2121
[InlineData("abc", "123")]
2222
[InlineData("12", "a.m.")]
23+
[InlineData("1,", "2,", "3")]
24+
[InlineData("1,,", "2,,", "3")]
25+
[InlineData("1,,,")]
26+
[InlineData("1,,,", "2")]
27+
[InlineData("1()", "a")]
28+
[InlineData("a,", "b,", "c")]
29+
[InlineData("a,,", "b,,", "c")]
30+
[InlineData("1/", "2/", "3")]
31+
[InlineData("!@*()")]
2332
[InlineData("ก", "ข", "ฃ")]
2433
[InlineData("中", "文")]
2534
[InlineData("😀", "Text")]
2635
[InlineData("Chinese", "中", "文")]
2736
[InlineData("Chinese", "中", "12345", "文", "😄")]
37+
[InlineData("a", "😀", "😄", "b")]
2838
public void Text(params string[] text) {
2939
var input = string.Concat(text);
3040
var atom = Parse(input);
@@ -222,6 +232,16 @@ public void Accent(string command, string accent) {
222232
[InlineData(@"\$\[\$$$\$", @"\$", @"\$", true, @"\$", @"\$\[\$ \]\$")]
223233
[InlineData(@"\$$$\$\]\$", @"\$", @"\$", true, @"\$", @"\$\[\$ \]\$")]
224234
[InlineData(@"\$\[\$\]\$", @"\$", @"\$", true, @"\$", @"\$\[\$ \]\$")]
235+
236+
// https://github.com/verybadcat/CSharpMath/issues/113
237+
//[InlineData(@",$,$,", @",", @",,", false, @"", @",\(,, \)")]
238+
//[InlineData(@",\(,$,", @",", @",,", false, @"", @",\(,, \)")]
239+
//[InlineData(@",$,\),", @",", @",,", false, @"", @",\(,, \)")]
240+
//[InlineData(@",\(,\),", @",", @",,", false, @"", @",\(,, \)")]
241+
//[InlineData(@",$$,$$,", @",", @",,", true, @"", @",\[,, \]")]
242+
//[InlineData(@",\[,$$,", @",", @",,", true, @"", @",\[,, \]")]
243+
//[InlineData(@",$$,\],", @",", @",,", true, @"", @",\[,, \]")]
244+
//[InlineData(@",\[,\],", @",", @",,", true, @"", @",\[,, \]")]
225245
public void Math(string input, string? textBefore, string math, bool display, string? textAfter, string output) {
226246
var atom = Parse(input);
227247
var list = new List<TextAtom>();

CSharpMath.Rendering/Text/TextLaTeXParser.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ Result CheckDollarCount(int startAt, ref int endAt, TextAtomListBuilder atoms) {
7272
displayMath = null;
7373
break;
7474
case null:
75-
mathLaTeX.Clear();
7675
displayMath = false;
7776
break;
7877
}
@@ -89,7 +88,6 @@ Result CheckDollarCount(int startAt, ref int endAt, TextAtomListBuilder atoms) {
8988
case false:
9089
return "Cannot close inline math mode with $$";
9190
case null:
92-
mathLaTeX.Clear();
9391
displayMath = true;
9492
break;
9593
}
@@ -157,12 +155,13 @@ Result<Color> ReadColor(ReadOnlySpan<char> latexInput, ref ReadOnlySpan<char> se
157155
Ok(value) :
158156
Err("Invalid color: " + color.ToString())
159157
);
160-
ReadOnlySpan<char> NextSectionUntilPunc(ReadOnlySpan<char> latexInput, ref ReadOnlySpan<char> section) {
158+
///<summary>Get punctutation after current section</summary>
159+
ReadOnlySpan<char> NextSectionWhilePunc(ReadOnlySpan<char> latexInput, ref ReadOnlySpan<char> section) {
161160
int start = endAt;
162161
ReadOnlySpan<char> specialChars = stackalloc[] { '#', '$', '%', '&', '\\', '^', '_', '{', '}', '~' };
163162
while (NextSection(latexInput, ref section))
164163
if (wordKind != WordKind.Punc || specialChars.IndexOf(section[0]) != -1) {
165-
//We have overlooked by one
164+
// We have overlooked by one when non-punctuation or special character is encountered
166165
PreviousSection(latexInput, ref section);
167166
break;
168167
}
@@ -173,7 +172,7 @@ ReadOnlySpan<char> NextSectionUntilPunc(ReadOnlySpan<char> latexInput, ref ReadO
173172
if (textSection.Is('$')) {
174173
if (backslashEscape)
175174
if (displayMath != null) mathLaTeX.Append(@"\$");
176-
else atoms.Text("$", NextSectionUntilPunc(latex, ref textSection));
175+
else atoms.Text("$", NextSectionWhilePunc(latex, ref textSection));
177176
else {
178177
dollarCount++;
179178
continue;
@@ -254,7 +253,7 @@ ReadOnlySpan<char> NextSectionUntilPunc(ReadOnlySpan<char> latexInput, ref ReadO
254253
//Need to allocate in the end :(
255254
//Don't look ahead for punc; we are looking for one char only
256255
atoms.Text(textSection[0].ToString(), default);
257-
} else atoms.Text(textSection.ToString(), NextSectionUntilPunc(latex, ref textSection));
256+
} else atoms.Text(textSection.ToString(), NextSectionWhilePunc(latex, ref textSection));
258257
break;
259258
}
260259
afterCommand = false;
@@ -320,13 +319,11 @@ ReadOnlySpan<char> NextSectionUntilPunc(ReadOnlySpan<char> latexInput, ref ReadO
320319
atoms.ControlSpace();
321320
break;
322321
case "(":
323-
mathLaTeX = new StringBuilder();
324322
displayMath = false;
325323
break;
326324
case ")":
327325
return "Cannot close inline math mode outside of math mode";
328326
case "[":
329-
mathLaTeX = new StringBuilder();
330327
displayMath = true;
331328
break;
332329
case "]":
@@ -431,7 +428,7 @@ ReadOnlySpan<char> NextSectionUntilPunc(ReadOnlySpan<char> latexInput, ref ReadO
431428
}
432429
//case "textasciicircum", "textless", ...
433430
case var textSymbol when TextLaTeXSettings.PredefinedTextSymbols.TryGetValue(textSymbol, out var replaceResult):
434-
atoms.Text(replaceResult, NextSectionUntilPunc(latex, ref textSection));
431+
atoms.Text(replaceResult, NextSectionWhilePunc(latex, ref textSection));
435432
break;
436433
case var command:
437434
if (displayMath != null) mathLaTeX.Append(command); //don't eat the command when parsing math

CSharpMath.sln

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpMath.Evaluation.Tests
101101
EndProject
102102
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpMath.Forms.Example.Ooui", "CSharpMath.Forms.Example\CSharpMath.Forms.Example.Ooui\CSharpMath.Forms.Example.Ooui.csproj", "{338E9ACF-EB26-4D5B-9ADB-B0C4588D71E1}"
103103
EndProject
104+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "AngouriMath\Tests\UnitTests\UnitTests.csproj", "{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}"
105+
EndProject
104106
Global
105107
GlobalSection(SharedMSBuildProjectFiles) = preSolution
106108
CSharpMath.Apple\CSharpMath.Apple.projitems*{11d4e6c7-c8e2-449c-a1e7-18bbbce4e6f3}*SharedItemsImports = 5
@@ -1512,6 +1514,54 @@ Global
15121514
{338E9ACF-EB26-4D5B-9ADB-B0C4588D71E1}.Release|x64.Build.0 = Release|Any CPU
15131515
{338E9ACF-EB26-4D5B-9ADB-B0C4588D71E1}.Release|x86.ActiveCfg = Release|Any CPU
15141516
{338E9ACF-EB26-4D5B-9ADB-B0C4588D71E1}.Release|x86.Build.0 = Release|Any CPU
1517+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
1518+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
1519+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
1520+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
1521+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
1522+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
1523+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1524+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
1525+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
1526+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
1527+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
1528+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
1529+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
1530+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|Any CPU.Build.0 = Debug|Any CPU
1531+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|ARM.ActiveCfg = Debug|Any CPU
1532+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|ARM.Build.0 = Debug|Any CPU
1533+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
1534+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|iPhone.Build.0 = Debug|Any CPU
1535+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1536+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
1537+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|x64.ActiveCfg = Debug|Any CPU
1538+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|x64.Build.0 = Debug|Any CPU
1539+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|x86.ActiveCfg = Debug|Any CPU
1540+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.AppStore|x86.Build.0 = Debug|Any CPU
1541+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1542+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|Any CPU.Build.0 = Debug|Any CPU
1543+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|ARM.ActiveCfg = Debug|Any CPU
1544+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|ARM.Build.0 = Debug|Any CPU
1545+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|iPhone.ActiveCfg = Debug|Any CPU
1546+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|iPhone.Build.0 = Debug|Any CPU
1547+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1548+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
1549+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|x64.ActiveCfg = Debug|Any CPU
1550+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|x64.Build.0 = Debug|Any CPU
1551+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|x86.ActiveCfg = Debug|Any CPU
1552+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Debug|x86.Build.0 = Debug|Any CPU
1553+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|Any CPU.ActiveCfg = Release|Any CPU
1554+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|Any CPU.Build.0 = Release|Any CPU
1555+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|ARM.ActiveCfg = Release|Any CPU
1556+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|ARM.Build.0 = Release|Any CPU
1557+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|iPhone.ActiveCfg = Release|Any CPU
1558+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|iPhone.Build.0 = Release|Any CPU
1559+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
1560+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
1561+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|x64.ActiveCfg = Release|Any CPU
1562+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|x64.Build.0 = Release|Any CPU
1563+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|x86.ActiveCfg = Release|Any CPU
1564+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450}.Release|x86.Build.0 = Release|Any CPU
15151565
EndGlobalSection
15161566
GlobalSection(SolutionProperties) = preSolution
15171567
HideSolutionNode = FALSE
@@ -1553,6 +1603,7 @@ Global
15531603
{F3FE15B5-1995-4444-85FF-6CAC564A9113} = {74FBDC58-E93A-4DCE-B83F-AA936EE57B31}
15541604
{06241755-626A-4992-9CAE-C5A5745CB83C} = {125C1FE9-F684-4E87-A9EF-969FD1E2D726}
15551605
{338E9ACF-EB26-4D5B-9ADB-B0C4588D71E1} = {C365BD30-D4E7-444A-A66D-25AAB9C67038}
1606+
{1EE81B65-9B82-43B2-8C2C-4FC691F2D450} = {74FBDC58-E93A-4DCE-B83F-AA936EE57B31}
15561607
EndGlobalSection
15571608
GlobalSection(ExtensibilityGlobals) = postSolution
15581609
SolutionGuid = {3C9A56A6-4EA3-4228-B064-E4789B282032}

0 commit comments

Comments
 (0)