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
43 changes: 43 additions & 0 deletions Extensions.Test/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,47 @@
List<string> result = [.. input.NominalWordWrap(1000f, 10f)];
CollectionAssert.AreEqual(new List<string> { "well-known" }, result);
}

[TestMethod]
public void NominalWordWrapKeepsADoubleHyphenDashTogether()
{
// 12 chars per line; "It was late-" would fit, but the break must not fall between the two hyphens.
string input = "It was late--too late to call";
List<string> result = [.. input.NominalWordWrap(12f, 1f)];
CollectionAssert.AreEqual(new List<string> { "It was", "late--too", "late to call" }, result);

Check warning on line 600 in Extensions.Test/StringExtensionsTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Extensions&issues=AaEfbS4QR3z4AgGU3G9P&open=AaEfbS4QR3z4AgGU3G9P&pullRequest=162
}

[TestMethod]
public void NominalWordWrapBreaksAfterTheLastHyphenOfARun()
{
string input = "It was late--too";
List<string> result = [.. input.NominalWordWrap(13f, 1f)];
CollectionAssert.AreEqual(new List<string> { "It was late--", "too" }, result);

Check warning on line 608 in Extensions.Test/StringExtensionsTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Extensions&issues=AaEfbS4QR3z4AgGU3G9Q&open=AaEfbS4QR3z4AgGU3G9Q&pullRequest=162
}

[TestMethod]
public void NominalWordWrapDoesNotBreakAfterAWordInitialHyphen()
{
List<string> number = [.. "ab -5".NominalWordWrap(4f, 1f)];
CollectionAssert.AreEqual(new List<string> { "ab", "-5" }, number);

Check warning on line 615 in Extensions.Test/StringExtensionsTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Extensions&issues=AaEfbS4QR3z4AgGU3G9R&open=AaEfbS4QR3z4AgGU3G9R&pullRequest=162

List<string> flag = [.. "Use the --force flag".NominalWordWrap(10f, 1f)];
CollectionAssert.AreEqual(new List<string> { "Use the", "--force", "flag" }, flag);

Check warning on line 618 in Extensions.Test/StringExtensionsTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Extensions&issues=AaEfbS4QR3z4AgGU3G9S&open=AaEfbS4QR3z4AgGU3G9S&pullRequest=162
}

[TestMethod]
public void NominalWordWrapStillBreaksAfterAHyphenThatFollowsALeadingOne()
{
// "-5-6": the second hyphen has a digit before it, so it is still a break opportunity.
List<string> result = [.. "-5-6".NominalWordWrap(3f, 1f)];
CollectionAssert.AreEqual(new List<string> { "-5-", "6" }, result);

Check warning on line 626 in Extensions.Test/StringExtensionsTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Extensions&issues=AaEfbS4QR3z4AgGU3G9T&open=AaEfbS4QR3z4AgGU3G9T&pullRequest=162
}

[TestMethod]
public void NominalWordWrapDoesNotAddAHyphenAtASoftHyphenAfterALeadingDash()
{
// The soft hyphen after "--" is a break opportunity, but the visible dash already ends the upper line.
List<string> result = [.. "ab --\u00ADforce".NominalWordWrap(5f, 1f)];
CollectionAssert.AreEqual(new List<string> { "ab --", "force" }, result);

Check warning on line 634 in Extensions.Test/StringExtensionsTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Extensions&issues=AaEfbS4QR3z4AgGU3G9U&open=AaEfbS4QR3z4AgGU3G9U&pullRequest=162
}
}
16 changes: 13 additions & 3 deletions Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@
#endif
}

private static Regex LineEndingRegexUnix { get; } = new(@"(?<!\r)\n", RegexOptions.Compiled);

Check warning on line 125 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 125 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.
private static Regex LineEndingRegexWindows { get; } = new(@"\r\n", RegexOptions.Compiled);

Check warning on line 126 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 126 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.
private static Regex LineEndingRegexMac { get; } = new(@"\r(?!\n)", RegexOptions.Compiled);

Check warning on line 127 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 127 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

// One pass over every kind of line ending. Normalizing in several passes lets an earlier pass
// build a sequence the next one reads differently: under Mac, "\r\n\n" became "\r" + "\n", a new CRLF.
Expand Down Expand Up @@ -264,7 +264,7 @@
/// </summary>
private const char SoftHyphen = '­';

private static IEnumerable<string> NominalWordWrapIterator(string text, int maxCharsPerLine)

Check warning on line 267 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed.

Check warning on line 267 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed.
{
string normalized = text.NormalizeLineEndings(LineEndingStyle.Unix);
string[] segments = normalized.Split('\n');
Expand Down Expand Up @@ -297,7 +297,7 @@

yield return line.ToString();
line.Clear();
atLineStart = true;

Check warning on line 300 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove this useless assignment to local variable 'atLineStart'.

Check warning on line 300 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove this useless assignment to local variable 'atLineStart'.
}
else if (!atLineStart)
{
Expand Down Expand Up @@ -364,19 +364,25 @@
{
// Split a whitespace-delimited word into break-units at hyphenation opportunities:
// after a visible hyphen (kept on the left chunk) or at a soft hyphen (removed unless a break lands there).
// A visible hyphen is a break opportunity only when it ends its run of hyphens and some other character
// comes before it in the word, so "--" stays together and "-5" or "--force" is never split after the dash.
List<(string Text, bool HyphenBefore)> chunks = [];
StringBuilder current = new();
bool hyphenBefore = false;
bool seenNonHyphen = false;

foreach (char ch in word)
for (int i = 0; i < word.Length; i++)
{
char ch = word[i];
if (ch == SoftHyphen)
{
if (current.Length > 0)
{
// A visible hyphen that did not end the chunk (a leading "--" before a soft hyphen) already shows at a break here.
bool endsInHyphen = current[^1] == '-';
chunks.Add((current.ToString(), hyphenBefore));
current.Clear();
hyphenBefore = true;
hyphenBefore = !endsInHyphen;
}
else if (chunks.Count > 0 && chunks[^1].Text[^1] != '-')
{
Expand All @@ -390,7 +396,11 @@
}

current.Append(ch);
if (ch == '-')
if (ch != '-')
{
seenNonHyphen = true;
}
else if (seenNonHyphen && (i + 1 >= word.Length || word[i + 1] != '-'))
{
chunks.Add((current.ToString(), hyphenBefore));
current.Clear();
Expand Down
Loading