< Summary

Information
Class: MyNet.Text.Truncation.TextTruncatorBase
Assembly: MyNet.Text
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Truncation/TextTruncatorBase.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 5
Uncovered lines: 0
Coverable lines: 5
Total lines: 65
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Apply(...)100%11100%
TruncateLeft(...)100%11100%
TruncateRight(...)100%11100%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TextTruncatorBase.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9
 10namespace MyNet.Text.Truncation;
 11
 12/// <summary>
 13/// Base class for text truncation, providing common functionality for truncating strings based on specified options.
 14/// </summary>
 15/// <param name="options">The options to use for truncating text.</param>
 16public abstract class TextTruncatorBase(TextTruncationOptions options) : ITextTruncator
 17{
 18    /// <summary>
 19    /// Gets the options used for truncating text.
 20    /// </summary>
 21    protected TextTruncationOptions Options { get; } = options;
 22
 23    /// <summary>
 24    /// Truncate the input string based on the specified options and culture.
 25    /// </summary>
 26    /// <param name="input">The input string to truncate.</param>
 27    /// <param name="culture">The culture to use for truncation.</param>
 28    /// <returns>The truncated string.</returns>
 29    /// <exception cref="ArgumentNullException">Thrown when the input string is null.</exception>
 30    /// <exception cref="ArgumentOutOfRangeException">Thrown when the specified length is less than zero.</exception>
 31    public string Apply(string input, CultureInfo culture)
 32    {
 29433        ArgumentNullException.ThrowIfNull(input);
 29434        ArgumentNullException.ThrowIfNull(culture);
 35
 29136        return ApplyCore(input, culture);
 37    }
 38
 39    /// <summary>
 40    /// When overridden in a derived class, applies the truncation logic to the input string based on the specified cult
 41    /// </summary>
 42    /// <param name="input">The input string to truncate.</param>
 43    /// <param name="culture">The culture to use for truncation.</param>
 44    /// <returns>The truncated string.</returns>
 45    protected abstract string ApplyCore(string input, CultureInfo culture);
 46
 47    /// <summary>
 48    /// Truncates the input string from the left, starting at the specified index, and prepending the truncation string 
 49    /// </summary>
 50    /// <param name="value">The input string to truncate.</param>
 51    /// <param name="startIndex">The index at which to start truncation.</param>
 52    /// <returns>The truncated string.</returns>
 53    protected string TruncateLeft(string value, int startIndex)
 2754        => string.Concat(Options.TruncationString, value.AsSpan(startIndex));
 55
 56    /// <summary>
 57    /// Truncates the input string from the right, ending at the specified index, and appending the truncation string de
 58    /// </summary>
 59    /// <param name="value">The input string to truncate.</param>
 60    /// <param name="endIndex">The index at which to end truncation.</param>
 61    /// <returns>The truncated string.</returns>
 62    protected string TruncateRight(string value, int endIndex)
 5763        => string.Concat(value.AsSpan(0, endIndex), Options.TruncationString);
 64}
 65