| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FixedNumberOfCharactersTextTruncator.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Linq; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Text.Truncation; |
| | | 11 | | |
| | 84 | 12 | | public sealed class FixedNumberOfCharactersTextTruncator(TextTruncationOptions options) : TextTruncatorBase(options) |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Counts the number of alphanumeric characters in the given string. |
| | | 16 | | /// </summary> |
| | 72 | 17 | | private static int CountAlphaNumeric(string value) => value.Count(char.IsLetterOrDigit); |
| | | 18 | | |
| | | 19 | | /// <inheritdoc/> |
| | | 20 | | protected override string ApplyCore(string input, CultureInfo culture) |
| | | 21 | | { |
| | 84 | 22 | | if (string.IsNullOrEmpty(input)) |
| | 12 | 23 | | return input; |
| | | 24 | | |
| | 72 | 25 | | var totalAlphaNumeric = CountAlphaNumeric(input); |
| | | 26 | | |
| | 72 | 27 | | return totalAlphaNumeric <= Options.Length |
| | 72 | 28 | | ? input |
| | 72 | 29 | | : Options.TruncationString.Length >= Options.Length |
| | 72 | 30 | | ? Options.Direction == TruncateFrom.Right |
| | 72 | 31 | | ? input[..Options.Length] |
| | 72 | 32 | | : input[^Options.Length..] |
| | 72 | 33 | | : Options.Direction switch |
| | 72 | 34 | | { |
| | 15 | 35 | | TruncateFrom.Left => TruncateFromLeft(input), |
| | 15 | 36 | | _ => TruncateFromRight(input) |
| | 72 | 37 | | }; |
| | | 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 | | { |
| | 15 | 47 | | var processed = 0; |
| | | 48 | | |
| | 354 | 49 | | for (var i = 0; i < input.Length; i++) |
| | | 50 | | { |
| | 177 | 51 | | if (char.IsLetterOrDigit(input[i])) |
| | | 52 | | { |
| | 129 | 53 | | processed++; |
| | | 54 | | } |
| | | 55 | | |
| | 177 | 56 | | if (processed + Options.TruncationString.Length >= Options.Length) |
| | | 57 | | { |
| | 15 | 58 | | return TruncateRight(input, i + 1); |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | 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 | | { |
| | 15 | 72 | | var processed = 0; |
| | | 73 | | |
| | 336 | 74 | | for (var i = input.Length - 1; i >= 0; i--) |
| | | 75 | | { |
| | 168 | 76 | | if (char.IsLetterOrDigit(input[i])) |
| | | 77 | | { |
| | 108 | 78 | | processed++; |
| | | 79 | | } |
| | | 80 | | |
| | 168 | 81 | | if (processed + Options.TruncationString.Length >= Options.Length) |
| | | 82 | | { |
| | 15 | 83 | | return TruncateLeft(input, i); |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | return input; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |