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

Refactor and test StartsAndEndsWithTag method #8909

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions src/Test/Logic/UtilitiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,5 +1019,25 @@ public void UrlDecode1()

Assert.AreEqual("В отеле", s);
}

[TestMethod]
public void StartsAndEndsWithTagTest()
{
const string italicOpen = "<i>";
const string italicClose = "</i>";

// all variation of openings
Assert.IsTrue(Utilities.StartsAndEndsWithTag("<i>Hallo world!</i>", italicOpen, italicClose));
Assert.IsTrue(Utilities.StartsAndEndsWithTag("- <i>Hallo world!</i>", italicOpen, italicClose));
Assert.IsTrue(Utilities.StartsAndEndsWithTag("-<i>Hallo world!</i>", italicOpen, italicClose));
Assert.IsTrue(Utilities.StartsAndEndsWithTag("- ...<i>Hallo world!</i>", italicOpen, italicClose));
Assert.IsTrue(Utilities.StartsAndEndsWithTag("- <i>...Hallo world!</i>", italicOpen, italicClose));

// all variations of closings
Assert.IsTrue(Utilities.StartsAndEndsWithTag("<i>Hallo world!</i>", italicOpen, italicClose));
Assert.IsTrue(Utilities.StartsAndEndsWithTag("<i>Hallo world?</i>", italicOpen, italicClose));
Assert.IsTrue(Utilities.StartsAndEndsWithTag("<i>Hallo world</i>...", italicOpen, italicClose));
Assert.IsTrue(Utilities.StartsAndEndsWithTag("<i>Hallo world</i>-", italicOpen, italicClose));
}
}
}
44 changes: 15 additions & 29 deletions src/libse/Common/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,40 +1215,26 @@ public static bool StartsAndEndsWithTag(string text, string startTag, string end
return false;
}

if (!text.Contains(startTag) || !text.Contains(endTag))
{
return false;
}

while (text.Contains(" "))
{
text = text.Replace(" ", " ");
}

var s1 = "- " + startTag;
var s2 = "-" + startTag;
var s3 = "- ..." + startTag;
var s4 = "- " + startTag + "..."; // - <i>...
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was always false too... as line 1228 would always match first


var e1 = endTag + ".";
var e2 = endTag + "!";
var e3 = endTag + "?";
var e4 = endTag + "...";
var e5 = endTag + "-";
text = text.FixExtraSpaces();

bool isStart = false;
bool isEnd = false;
if (text.StartsWith(startTag, StringComparison.Ordinal) || text.StartsWith(s1, StringComparison.Ordinal) || text.StartsWith(s2, StringComparison.Ordinal) || text.StartsWith(s3, StringComparison.Ordinal) || text.StartsWith(s4, StringComparison.Ordinal))
{
isStart = true;
}
string[] prefixFormats = { "{0}", "- {0}", "-{0}", "- ...{0}", "- {0}..." };
string[] suffixFormats = { "{0}", "{0}.", "{0}!", "{0}?", "{0}...", "{0}-" };

if (text.EndsWith(endTag, StringComparison.Ordinal) || text.EndsWith(e1, StringComparison.Ordinal) || text.EndsWith(e2, StringComparison.Ordinal) || text.EndsWith(e3, StringComparison.Ordinal) || text.EndsWith(e4, StringComparison.Ordinal) || text.EndsWith(e5, StringComparison.Ordinal))
foreach (string prefixFormat in prefixFormats)
{
isEnd = true;
if (text.StartsWith(string.Format(prefixFormat, startTag), StringComparison.Ordinal))
{
foreach (string suffixFormat in suffixFormats)
{
if (text.EndsWith(string.Format(suffixFormat, endTag), StringComparison.Ordinal))
{
return true;
}
}
}
}

return isStart && isEnd;
return false;
}

public static Paragraph GetOriginalParagraph(int index, Paragraph paragraph, List<Paragraph> originalParagraphs)
Expand Down