| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FixedLengthTextTruncator.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Globalization; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Text.Truncation; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Truncates a string to a fixed length, optionally adding a truncation string (e.g. ellipsis) at the truncation point. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="options">The options to use for truncating text.</param> |
| | 120 | 15 | | public sealed class FixedLengthTextTruncator(TextTruncationOptions options) : TextTruncatorBase(options) |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc/> |
| | | 18 | | protected override string ApplyCore(string input, CultureInfo culture) |
| | | 19 | | { |
| | 117 | 20 | | if (string.IsNullOrEmpty(input) || input.Length <= Options.Length) |
| | 72 | 21 | | return input; |
| | | 22 | | |
| | 45 | 23 | | if (Options.TruncationString.Length >= Options.Length) |
| | | 24 | | { |
| | 9 | 25 | | return Options.Direction == TruncateFrom.Right ? input[..Options.Length] : input[^Options.Length..]; |
| | | 26 | | } |
| | | 27 | | |
| | 36 | 28 | | var contentLength = Options.Length - Options.TruncationString.Length; |
| | | 29 | | |
| | 36 | 30 | | return Options.Direction switch |
| | 36 | 31 | | { |
| | 12 | 32 | | TruncateFrom.Left => TruncateLeft(input, input.Length - contentLength), |
| | 36 | 33 | | |
| | 24 | 34 | | _ => TruncateRight(input, contentLength) |
| | 36 | 35 | | }; |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | |