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
18 changes: 18 additions & 0 deletions Extensions.Test/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,24 @@ public void NominalWordWrapClampsToAtLeastOneCharacterPerLine()
CollectionAssert.AreEqual(new List<string> { "a", "b" }, result);
}

[TestMethod]
public void NominalWordWrapHardBreakDoesNotSplitSurrogatePairs()
{
// Each 😀 is a surrogate pair: two UTF-16 code units.
List<string> emoji = [.. "😀😀😀".NominalWordWrap(3f, 1f)];
Assert.AreSequenceEqual(new List<string> { "😀", "😀", "😀" }, emoji);

List<string> mixed = [.. "ab😀cd".NominalWordWrap(3f, 1f)];
Assert.AreSequenceEqual(new List<string> { "ab", "😀c", "d" }, mixed);
}

[TestMethod]
public void NominalWordWrapLetsASurrogatePairOverhangAtWidthOne()
{
List<string> result = [.. "a😀b".NominalWordWrap(1f, 1f)];
Assert.AreSequenceEqual(new List<string> { "a", "😀", "b" }, result);
}

[TestMethod]
public void NominalWordWrapThrowsArgumentNullExceptionWhenTextIsNull()
{
Expand Down
14 changes: 11 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 / 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 / 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 / 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 / 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 / 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 / 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 @@ -227,7 +227,8 @@
/// 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
/// overhanging character.
/// overhanging character. A hard break never splits a surrogate pair, so at a width of one character a line may
/// hold a whole pair (two UTF-16 code units).
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="text"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="wrapWidth"/> or <paramref name="nominalGlyphWidth"/> is not greater than zero.</exception>
Expand Down Expand Up @@ -259,7 +260,7 @@
/// </summary>
private const char SoftHyphen = '­';

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

Check warning on line 263 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

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

Check warning on line 263 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / 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 @@ -292,7 +293,7 @@

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

Check warning on line 296 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this useless assignment to local variable 'atLineStart'.

Check warning on line 296 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this useless assignment to local variable 'atLineStart'.
}
else if (!atLineStart)
{
Expand All @@ -309,8 +310,15 @@
string remaining = atomText;
while (remaining.Length > maxCharsPerLine)
{
yield return remaining.Substring(0, maxCharsPerLine);
remaining = remaining.Substring(maxCharsPerLine);
int cut = maxCharsPerLine;
if (char.IsHighSurrogate(remaining[cut - 1]) && char.IsLowSurrogate(remaining[cut]))
{
// Never split a surrogate pair. At a width of one, let the pair overhang instead.
cut = cut > 1 ? cut - 1 : 2;
}

yield return remaining.Substring(0, cut);
remaining = remaining.Substring(cut);
}

line.Append(remaining);
Expand Down
Loading