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
15 changes: 15 additions & 0 deletions Extensions.Test/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,19 @@
List<string> result = [.. "ab --\u00ADforce".NominalWordWrap(5f, 1f)];
CollectionAssert.AreEqual(new List<string> { "ab --", "force" }, result);
}

[TestMethod]
public void NominalWordWrapDoesNotBreakAtANoBreakSpace()
{
// 10 chars per line; "Total: 100 kg" would break before "kg", but U+00A0 keeps the number with its unit.
List<string> result = [.. "Total: 100\u00A0kg".NominalWordWrap(10f, 1f)];
CollectionAssert.AreEqual(new List<string> { "Total:", "100\u00A0kg" }, result);

Check warning on line 642 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=AaEzIsK75AOFwQ9qH66L&open=AaEzIsK75AOFwQ9qH66L&pullRequest=164
}

[TestMethod]
public void NominalWordWrapKeepsNoBreakSpacesUnchanged()
{
List<string> result = [.. "a\u00A0b c\u2007d e\u202Ff".NominalWordWrap(100f, 1f)];
CollectionAssert.AreEqual(new List<string> { "a\u00A0b c\u2007d e\u202Ff" }, result);

Check warning on line 649 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=AaEzIsK75AOFwQ9qH66M&open=AaEzIsK75AOFwQ9qH66M&pullRequest=164
}
}
41 changes: 37 additions & 4 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 @@ -223,7 +223,8 @@
/// <param name="nominalGlyphWidth">The nominal (assumed uniform) width of a single glyph, in the same units as <paramref name="wrapWidth"/>.</param>
/// <returns>
/// The wrapped lines. Existing line breaks (Unix, Windows, or Mac) are honored as forced breaks and blank lines are
/// preserved. Runs of whitespace within a line are collapsed to a single space. A break may also occur after a visible
/// preserved. Runs of whitespace within a line are collapsed to a single space. The no-break spaces U+00A0, U+2007 and
/// U+202F are not break points and are kept unchanged. A break may also occur after a visible
/// hyphen (the hyphen stays on the upper line) or at a soft hyphen (<c>­</c>), which renders as a hyphen only when
/// a break lands there and is otherwise removed. Words that still cannot fit are hard-broken as a last resort so that
/// no line exceeds the computed width, except that honoring a soft hyphen at a line boundary may add a single
Expand Down Expand Up @@ -264,7 +265,7 @@
/// </summary>
private const char SoftHyphen = '­';

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

Check warning on line 268 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 268 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 +298,7 @@

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

Check warning on line 301 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 301 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 @@ -338,9 +339,7 @@
private static List<(string Text, bool GlueSpace, bool HyphenBefore, bool CanBreak)> BuildAtoms(string segment)
{
List<(string Text, bool GlueSpace, bool HyphenBefore, bool CanBreak)> atoms = [];
string[] words = segment.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries);

foreach (string word in words)
foreach (string word in SplitAtBreakingWhiteSpace(segment))
{
List<(string Text, bool HyphenBefore)> chunks = ChunkWord(word);
for (int c = 0; c < chunks.Count; c++)
Expand All @@ -360,6 +359,40 @@
return atoms;
}

/// <summary>
/// Splits a segment into words at whitespace that allows a line break, dropping empty entries.
/// The no-break spaces U+00A0, U+2007 and U+202F are whitespace but forbid a break, so they stay inside their word.
/// </summary>
private static List<string> SplitAtBreakingWhiteSpace(string segment)
{
List<string> words = [];
int start = -1;
for (int i = 0; i < segment.Length; i++)
{
char ch = segment[i];
bool breaking = char.IsWhiteSpace(ch) && ch is not '\u00A0' and not '\u2007' and not '\u202F';
if (breaking)
{
if (start >= 0)
{
words.Add(segment.Substring(start, i - start));
start = -1;
}
}
else if (start < 0)
{
start = i;
}
}

if (start >= 0)
{
words.Add(segment.Substring(start));
}

return words;
}

private static List<(string Text, bool HyphenBefore)> ChunkWord(string word)
{
// Split a whitespace-delimited word into break-units at hyphenation opportunities:
Expand Down
Loading