| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextRedactionExtensions.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 | | using System.Text.RegularExpressions; |
| | | 10 | | using MyNet.Primitives; |
| | | 11 | | using MyNet.Text.Redaction; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 14 | | namespace MyNet.Text; |
| | | 15 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 16 | | |
| | | 17 | | public static class TextRedactionExtensions |
| | | 18 | | { |
| | | 19 | | extension(TextPipeline pipeline) |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Applies a redaction transform to the pipeline. |
| | | 23 | | /// </summary> |
| | | 24 | | public TextPipeline Redact(ITextRedactorTransform transform) |
| | | 25 | | { |
| | 0 | 26 | | ArgumentNullException.ThrowIfNull(transform); |
| | 0 | 27 | | return pipeline.Apply(transform); |
| | | 28 | | } |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | extension(string input) |
| | | 32 | | { |
| | | 33 | | /// <summary> |
| | | 34 | | /// Applies a redaction transform. |
| | | 35 | | /// </summary> |
| | | 36 | | public string Redact(ITextRedactorTransform transform, CultureInfo? culture = null) |
| | | 37 | | { |
| | 15 | 38 | | ArgumentNullException.ThrowIfNull(transform); |
| | 15 | 39 | | return transform.Apply(input, culture.OrCurrent()); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Redacts the middle part of the text and keeps edge characters visible. |
| | | 44 | | /// </summary> |
| | | 45 | | public string Redact(int showStart, int showEnd, char maskCharacter = '*', CultureInfo? culture = null) |
| | 3 | 46 | | => input.Redact(new FixedMaskTextRedactor(new() |
| | 3 | 47 | | { |
| | 3 | 48 | | ShowStart = showStart, |
| | 3 | 49 | | ShowEnd = showEnd, |
| | 3 | 50 | | MaskCharacter = maskCharacter |
| | 3 | 51 | | }), |
| | 3 | 52 | | culture); |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Redacts parts matching a regular expression. |
| | | 56 | | /// </summary> |
| | 3 | 57 | | public string Redact(Regex pattern, string replacement = "***", CultureInfo? culture = null) => input.Redact(new |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | |