< Summary

Information
Class: MyNet.Text.Truncation.FixedNumberOfWordsTextTruncator
Assembly: MyNet.Text
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Truncation/FixedNumberOfWordsTextTruncator.cs
Tag: 323_28699572109
Line coverage
95%
Covered lines: 46
Uncovered lines: 2
Coverable lines: 48
Total lines: 138
Line coverage: 95.8%
Branch coverage
90%
Covered branches: 29
Total branches: 32
Branch coverage: 90.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
CountWords(...)100%66100%
ApplyCore(...)100%66100%
TruncateFromRight(...)87.5%8891.66%
TruncateFromLeft(...)83.33%121293.33%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Truncation/FixedNumberOfWordsTextTruncator.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="FixedNumberOfWordsTextTruncator.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System.Globalization;
 8
 9namespace MyNet.Text.Truncation;
 10
 11/// <summary>
 12/// Truncates a string to a fixed number of words, either from the left or the right, depending on the specified truncat
 13/// </summary>
 14/// <param name="options">The options for truncation, including the number of words and the truncation direction.</param
 15public sealed class FixedNumberOfWordsTextTruncator(TextTruncationOptions options)
 9016    : TextTruncatorBase(options)
 17{
 18    /// <summary>
 19    /// Counts the number of words in the input string by iterating through each character and determining when a new wo
 20    /// </summary>
 21    /// <param name="value">The input string to count words in.</param>
 22    /// <returns>The number of words in the input string.</returns>
 23    private static int CountWords(string value)
 24    {
 7825        var count = 0;
 7826        var insideWord = false;
 27
 582028        foreach (var c in value)
 29        {
 283230            if (char.IsWhiteSpace(c))
 31            {
 40832                insideWord = false;
 40833                continue;
 34            }
 35
 242436            if (!insideWord)
 37            {
 47438                insideWord = true;
 47439                count++;
 40            }
 41        }
 42
 7843        return count;
 44    }
 45
 46    /// <inheritdoc/>
 47    protected override string ApplyCore(string input, CultureInfo culture)
 48    {
 9049        if (string.IsNullOrWhiteSpace(input))
 1250            return input;
 51
 7852        var wordCount = CountWords(input);
 53
 7854        return wordCount <= Options.Length
 7855            ? input
 7856            : Options.Direction switch
 7857            {
 2458                TruncateFrom.Left => TruncateFromLeft(input),
 1859                _ => TruncateFromRight(input)
 7860            };
 61    }
 62
 63    /// <summary>
 64    /// Truncates the input string from the right, keeping only the specified number of words from the left.
 65    /// </summary>
 66    /// <param name="input">The input string to truncate.</param>
 67    /// <returns>The truncated string.</returns>
 68    private string TruncateFromRight(string input)
 69    {
 1870        var words = 0;
 1871        var insideWord = false;
 72
 90673        for (var i = 0; i < input.Length; i++)
 74        {
 45375            var isWhiteSpace = char.IsWhiteSpace(input[i]);
 76
 77            switch (isWhiteSpace)
 78            {
 38179                case false when !insideWord:
 9080                    insideWord = true;
 9081                    words++;
 9082                    break;
 83                case true:
 7284                    insideWord = false;
 85                    break;
 86            }
 87
 45388            if (words > Options.Length)
 89            {
 1890                return TruncateRight(input, i - 1);
 91            }
 92        }
 93
 094        return input;
 95    }
 96
 97    /// <summary>
 98    /// Truncates the input string from the left, keeping only the specified number of words from the right.
 99    /// </summary>
 100    /// <param name="input">The input string to truncate.</param>
 101    /// <returns>The truncated string.</returns>
 102    private string TruncateFromLeft(string input)
 103    {
 24104        var words = 0;
 24105        var insideWord = false;
 106
 1344107        for (var i = input.Length - 1; i >= 0; i--)
 108        {
 672109            var isWhiteSpace = char.IsWhiteSpace(input[i]);
 110
 111            switch (isWhiteSpace)
 112            {
 564113                case false when !insideWord:
 120114                    insideWord = true;
 120115                    words++;
 120116                    break;
 117                case true:
 108118                    insideWord = false;
 119                    break;
 120            }
 121
 672122            if (words > Options.Length)
 123            {
 24124                var startIndex = i + 1;
 125
 48126                while (startIndex < input.Length && char.IsWhiteSpace(input[startIndex]))
 127                {
 24128                    startIndex++;
 129                }
 130
 24131                return string.Concat(Options.TruncationString, input[startIndex..].TrimEnd());
 132            }
 133        }
 134
 0135        return input;
 136    }
 137}
 138