Skip to content
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
26 changes: 26 additions & 0 deletions Extensions.Test/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,32 @@
CollectionAssert.AreEqual(new List<string> { "hello", "", "world" }, result);
}

[TestMethod]
[DataRow('\v')]
[DataRow('\f')]
[DataRow('\u0085')]
[DataRow('\u2028')]
[DataRow('\u2029')]
public void NominalWordWrapHonorsUnicodeMandatoryBreaks(char separator)
{
string input = "Line one" + separator + "Line two";
List<string> result = [.. input.NominalWordWrap(80f, 1f)];
CollectionAssert.AreEqual(new List<string> { "Line one", "Line two" }, result);

Check warning on line 419 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=AaGdaxiULudwRFKm4kZd&open=AaGdaxiULudwRFKm4kZd&pullRequest=185
}

[TestMethod]
[DataRow('\v')]
[DataRow('\f')]
[DataRow('\u0085')]
[DataRow('\u2028')]
[DataRow('\u2029')]
public void NominalWordWrapPreservesBlankLinesBetweenUnicodeMandatoryBreaks(char separator)
{
string input = "a" + separator + separator + "b";
List<string> result = [.. input.NominalWordWrap(80f, 1f)];
CollectionAssert.AreEqual(new List<string> { "a", "", "b" }, result);

Check warning on line 432 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=AaGdaxiULudwRFKm4kZe&open=AaGdaxiULudwRFKm4kZe&pullRequest=185
}

[TestMethod]
public void NominalWordWrapHonorsWindowsLineBreaks()
{
Expand Down
12 changes: 10 additions & 2 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.
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.

// 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. The no-break spaces U+00A0, U+2007 and
/// preserved, as are the other mandatory breaks: vertical tab, form feed, next line (U+0085), line separator (U+2028)
/// and paragraph separator (U+2029). 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. A soft hyphen is only broken at when its rendered hyphen fits on the
Expand Down Expand Up @@ -271,10 +272,17 @@
/// </summary>
private const char SoftHyphen = '­';

/// <summary>
/// The characters that force a line break (Unicode line-break classes BK and NL in UAX #14) once CR and CRLF have been
/// normalized to LF: line feed, vertical tab, form feed, next line (U+0085), line separator (U+2028) and paragraph
/// separator (U+2029).
/// </summary>
private static readonly char[] MandatoryLineBreaks = ['\n', '\v', '\f', '\u0085', '\u2028', '\u2029'];

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

Check warning on line 282 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 51 to the 15 allowed.

Check warning on line 282 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 51 to the 15 allowed.
{
string normalized = text.NormalizeLineEndings(LineEndingStyle.Unix);
string[] segments = normalized.Split('\n');
string[] segments = normalized.Split(MandatoryLineBreaks);

foreach (string segment in segments)
{
Expand Down Expand Up @@ -332,7 +340,7 @@
yield return line.ToString();
line.Clear();
lineAtoms.Clear();
atLineStart = true;

Check warning on line 343 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 343 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 @@ -428,7 +436,7 @@
return words;
}

private static List<(string Text, bool HyphenBefore)> ChunkWord(string word)

Check warning on line 439 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 17 to the 15 allowed.

Check warning on line 439 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 17 to the 15 allowed.
{
// 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).
Expand Down
Loading