< Summary

Information
Class: MyNet.Text.Truncation.FixedNumberOfWordsTextTruncator
Assembly: MyNet.Text
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Text/Truncation/FixedNumberOfWordsTextTruncator.cs
Tag: 342_31701152804
Line coverage
95%
Covered lines: 46
Uncovered lines: 2
Coverable lines: 48
Total lines: 134
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/5aaaab311aa14541478b9e402357d98b657a23e4/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) continue;
 47437            insideWord = true;
 47438            count++;
 39        }
 40
 7841        return count;
 42    }
 43
 44    /// <inheritdoc/>
 45    protected override string ApplyCore(string input, CultureInfo culture)
 46    {
 9047        if (string.IsNullOrWhiteSpace(input))
 1248            return input;
 49
 7850        var wordCount = CountWords(input);
 51
 7852        return wordCount <= Options.Length
 7853            ? input
 7854            : Options.Direction switch
 7855            {
 2456                TruncateFrom.Left => TruncateFromLeft(input),
 1857                _ => TruncateFromRight(input)
 7858            };
 59    }
 60
 61    /// <summary>
 62    /// Truncates the input string from the right, keeping only the specified number of words from the left.
 63    /// </summary>
 64    /// <param name="input">The input string to truncate.</param>
 65    /// <returns>The truncated string.</returns>
 66    private string TruncateFromRight(string input)
 67    {
 1868        var words = 0;
 1869        var insideWord = false;
 70
 90671        for (var i = 0; i < input.Length; i++)
 72        {
 45373            var isWhiteSpace = char.IsWhiteSpace(input[i]);
 74
 75            switch (isWhiteSpace)
 76            {
 38177                case false when !insideWord:
 9078                    insideWord = true;
 9079                    words++;
 9080                    break;
 81                case true:
 7282                    insideWord = false;
 83                    break;
 84            }
 85
 45386            if (words > Options.Length)
 87            {
 1888                return TruncateRight(input, i - 1);
 89            }
 90        }
 91
 092        return input;
 93    }
 94
 95    /// <summary>
 96    /// Truncates the input string from the left, keeping only the specified number of words from the right.
 97    /// </summary>
 98    /// <param name="input">The input string to truncate.</param>
 99    /// <returns>The truncated string.</returns>
 100    private string TruncateFromLeft(string input)
 101    {
 24102        var words = 0;
 24103        var insideWord = false;
 104
 1344105        for (var i = input.Length - 1; i >= 0; i--)
 106        {
 672107            var isWhiteSpace = char.IsWhiteSpace(input[i]);
 108
 109            switch (isWhiteSpace)
 110            {
 564111                case false when !insideWord:
 120112                    insideWord = true;
 120113                    words++;
 120114                    break;
 115                case true:
 108116                    insideWord = false;
 117                    break;
 118            }
 119
 672120            if (words <= Options.Length) continue;
 24121            var startIndex = i + 1;
 122
 48123            while (startIndex < input.Length && char.IsWhiteSpace(input[startIndex]))
 124            {
 24125                startIndex++;
 126            }
 127
 24128            return string.Concat(Options.TruncationString, input[startIndex..].TrimEnd());
 129        }
 130
 0131        return input;
 132    }
 133}
 134