| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FixedMaskTextRedactor.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.Redaction; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Redacts text by masking the middle section. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class FixedMaskTextRedactor(TextRedactionOptions options) : ITextRedactorTransform |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc/> |
| | | 18 | | public string Apply(string input, CultureInfo culture) |
| | | 19 | | { |
| | 3 | 20 | | ArgumentNullException.ThrowIfNull(input); |
| | 3 | 21 | | ArgumentNullException.ThrowIfNull(culture); |
| | | 22 | | |
| | 3 | 23 | | if (input.Length == 0) |
| | 0 | 24 | | return input; |
| | | 25 | | |
| | 3 | 26 | | var visible = options.ShowStart + options.ShowEnd; |
| | 3 | 27 | | if (visible >= input.Length) |
| | 0 | 28 | | return input; |
| | | 29 | | |
| | 3 | 30 | | var maskedLength = input.Length - visible; |
| | 3 | 31 | | return string.Concat( |
| | 3 | 32 | | input.AsSpan(0, options.ShowStart), |
| | 3 | 33 | | new string(options.MaskCharacter, maskedLength), |
| | 3 | 34 | | input.AsSpan(input.Length - options.ShowEnd)); |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | |