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
26 changes: 26 additions & 0 deletions Extensions.Test/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,32 @@ public void NominalWordWrapThrowsArgumentOutOfRangeExceptionWhenGlyphWidthNotPos
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => input.NominalWordWrap(100f, 0f).ToList());
}

[TestMethod]
public void NominalWordWrapThrowsArgumentOutOfRangeExceptionWhenWrapWidthIsNaN()
{
string input = "hello world foo";
ArgumentOutOfRangeException exception = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => input.NominalWordWrap(float.NaN, 1f).ToList());
Assert.AreEqual("wrapWidth", exception.ParamName);
}

[TestMethod]
public void NominalWordWrapThrowsArgumentOutOfRangeExceptionWhenGlyphWidthIsNaN()
{
string input = "hello world foo";
ArgumentOutOfRangeException exception = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => input.NominalWordWrap(10f, float.NaN).ToList());
Assert.AreEqual("nominalGlyphWidth", exception.ParamName);
}

[TestMethod]
[DataRow(float.PositiveInfinity, 1f)]
[DataRow(float.MaxValue, float.Epsilon)]
public void NominalWordWrapKeepsEachLineWholeWhenWidthRatioIsUnbounded(float wrapWidth, float nominalGlyphWidth)
{
string input = "hello world foo\nbar baz";
List<string> result = [.. input.NominalWordWrap(wrapWidth, nominalGlyphWidth)];
Assert.AreSequenceEqual(new List<string> { "hello world foo", "bar baz" }, result);
}

[TestMethod]
public void NominalWordWrapBreaksAfterVisibleHyphen()
{
Expand Down
10 changes: 7 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 @@ -235,12 +235,13 @@
{
Ensure.NotNull(text);

if (wrapWidth <= 0)
// NaN fails every comparison, so it has to be rejected explicitly.
if (float.IsNaN(wrapWidth) || wrapWidth <= 0)
{
throw new ArgumentOutOfRangeException(nameof(wrapWidth), wrapWidth, "Wrap width must be greater than zero.");
}

if (nominalGlyphWidth <= 0)
if (float.IsNaN(nominalGlyphWidth) || nominalGlyphWidth <= 0)
{
throw new ArgumentOutOfRangeException(nameof(nominalGlyphWidth), nominalGlyphWidth, "Nominal glyph width must be greater than zero.");
}
Expand All @@ -250,7 +251,10 @@
return [];
}

int maxCharsPerLine = Math.Max(1, (int)Math.Floor(wrapWidth / nominalGlyphWidth));
// Clamp before casting: float-to-int conversion does not saturate before .NET 9, so an infinite or huge ratio
// would otherwise become int.MinValue and wrap one character per line.
double ratio = Math.Floor((double)wrapWidth / nominalGlyphWidth);
int maxCharsPerLine = ratio >= int.MaxValue ? int.MaxValue : Math.Max(1, (int)ratio);
return NominalWordWrapIterator(text, maxCharsPerLine);
}

Expand All @@ -259,7 +263,7 @@
/// </summary>
private const char SoftHyphen = '­';

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

Check warning on line 266 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

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

Check warning on line 266 in Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

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

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

Check warning on line 299 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 299 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 Down
Loading