< Summary

Information
Class: MyNet.Text.Truncation.FixedNumberOfCharactersTextTruncator
Assembly: MyNet.Text
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Truncation/FixedNumberOfCharactersTextTruncator.cs
Tag: 323_28699572109
Line coverage
93%
Covered lines: 28
Uncovered lines: 2
Coverable lines: 30
Total lines: 90
Line coverage: 93.3%
Branch coverage
90%
Covered branches: 20
Total branches: 22
Branch coverage: 90.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
CountAlphaNumeric(...)100%11100%
ApplyCore(...)100%1010100%
TruncateFromRight(...)83.33%6685.71%
TruncateFromLeft(...)83.33%6685.71%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="FixedNumberOfCharactersTextTruncator.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System.Globalization;
 8using System.Linq;
 9
 10namespace MyNet.Text.Truncation;
 11
 8412public sealed class FixedNumberOfCharactersTextTruncator(TextTruncationOptions options) : TextTruncatorBase(options)
 13{
 14    /// <summary>
 15    /// Counts the number of alphanumeric characters in the given string.
 16    /// </summary>
 7217    private static int CountAlphaNumeric(string value) => value.Count(char.IsLetterOrDigit);
 18
 19    /// <inheritdoc/>
 20    protected override string ApplyCore(string input, CultureInfo culture)
 21    {
 8422        if (string.IsNullOrEmpty(input))
 1223            return input;
 24
 7225        var totalAlphaNumeric = CountAlphaNumeric(input);
 26
 7227        return totalAlphaNumeric <= Options.Length
 7228            ? input
 7229            : Options.TruncationString.Length >= Options.Length
 7230                ? Options.Direction == TruncateFrom.Right
 7231                    ? input[..Options.Length]
 7232                    : input[^Options.Length..]
 7233                : Options.Direction switch
 7234                {
 1535                    TruncateFrom.Left => TruncateFromLeft(input),
 1536                    _ => TruncateFromRight(input)
 7237                };
 38    }
 39
 40    /// <summary>
 41    /// Truncate the string from the right, starting from the end of the string and moving towards the start until the d
 42    /// </summary>
 43    /// <param name="input">The input string to truncate.</param>
 44    /// <returns>The truncated string.</returns>
 45    private string TruncateFromRight(string input)
 46    {
 1547        var processed = 0;
 48
 35449        for (var i = 0; i < input.Length; i++)
 50        {
 17751            if (char.IsLetterOrDigit(input[i]))
 52            {
 12953                processed++;
 54            }
 55
 17756            if (processed + Options.TruncationString.Length >= Options.Length)
 57            {
 1558                return TruncateRight(input, i + 1);
 59            }
 60        }
 61
 062        return input;
 63    }
 64
 65    /// <summary>
 66    /// Truncate the string from the left, starting from the end of the string and moving towards the start until the de
 67    /// </summary>
 68    /// <param name="input">The input string to truncate.</param>
 69    /// <returns>The truncated string.</returns>
 70    private string TruncateFromLeft(string input)
 71    {
 1572        var processed = 0;
 73
 33674        for (var i = input.Length - 1; i >= 0; i--)
 75        {
 16876            if (char.IsLetterOrDigit(input[i]))
 77            {
 10878                processed++;
 79            }
 80
 16881            if (processed + Options.TruncationString.Length >= Options.Length)
 82            {
 1583                return TruncateLeft(input, i);
 84            }
 85        }
 86
 087        return input;
 88    }
 89}
 90