| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextTruncatorBase.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | |
| | | 10 | | namespace 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> |
| | | 16 | | public 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 | | { |
| | 294 | 33 | | ArgumentNullException.ThrowIfNull(input); |
| | 294 | 34 | | ArgumentNullException.ThrowIfNull(culture); |
| | | 35 | | |
| | 291 | 36 | | 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) |
| | 27 | 54 | | => 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) |
| | 57 | 63 | | => string.Concat(value.AsSpan(0, endIndex), Options.TruncationString); |
| | | 64 | | } |
| | | 65 | | |