Skip to content

Commit

Permalink
imp - Handle dangling quotes
Browse files Browse the repository at this point in the history
---

We need to handle dangling quotes when splitting text.

---

Type: imp
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 15, 2024
1 parent e103e74 commit 5bbab6b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
15 changes: 14 additions & 1 deletion Textify.Tests/General/TextToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ public void TestSplitEncloseDoubleQuotesEdgeCase2()
string TargetString = "test\"\0t est";
var TargetArray = TargetString.SplitEncloseDoubleQuotes();
TargetArray.Length.ShouldBe(2);
TargetArray[1].ShouldBe("\0t est");
TargetArray[1].ShouldBe("est");
}

/// <summary>
Expand All @@ -657,6 +657,19 @@ public void TestSplitEncloseDoubleQuotesEdgeCase3()
TargetArray[2].ShouldBe("Textify Terminaux");
}

/// <summary>
/// Tests splitting a string with double quotes enclosed (edge case regarding single escape with space)
/// </summary>
[TestMethod]
[Description("Querying")]
public void TestSplitEncloseDoubleQuotesEdgeCase4()
{
string TargetString = "test\\\"\0t est\"a fne\"fs";
var TargetArray = TargetString.SplitEncloseDoubleQuotes();
TargetArray.Length.ShouldBe(2);
TargetArray[1].ShouldBe("est\"a fne\"fs");
}

/// <summary>
/// Tests checking to see if the string is numeric
/// </summary>
Expand Down
20 changes: 19 additions & 1 deletion Textify/General/TextTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ public static string[] SplitEncloseDoubleQuotesNoRelease(this string target)
if (target is null)
throw new TextifyException("The target may not be null");

// Build the split matches
List<string> matchesStr = [];
bool inEscape = false;
bool inQuote = false;
StringBuilder builder = new();
for (int i = 0; i < target.Length; i++)
{
// Add a character
char character = target[i];
if (char.IsWhiteSpace(character) && !inEscape && !inQuote)
{
Expand All @@ -72,15 +74,31 @@ public static string[] SplitEncloseDoubleQuotesNoRelease(this string target)
}
else
builder.Append(character);

// Deal with the quotes
if ((character == '\"' || character == '\'' || character == '`') && !inEscape)
inQuote = !inQuote;

// Deal with the escapes
if (character == '\\')
inEscape = true;
else if (inEscape)
inEscape = false;
}

// Add the final portion, but check the quotes
if (builder.Length > 0)
matchesStr.Add(builder.ToString());
{
string final = builder.ToString();
if (inQuote)
{
// Now, split this portion normally with spaces
var splitFinal = final.Split(' ');
matchesStr.AddRange(splitFinal);
}
else
matchesStr.Add(final);
}
return [.. matchesStr];
}

Expand Down

0 comments on commit 5bbab6b

Please sign in to comment.