| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="InitialsFormatterTransform.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; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Text.Formatting; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Builds initials by taking the first character of each whitespace-delimited token. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class InitialsFormatterTransform : ITextFormatterTransform |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="InitialsFormatterTransform"/> class. |
| | | 20 | | /// </summary> |
| | 3 | 21 | | internal InitialsFormatterTransform() { } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc/> |
| | | 24 | | public string Apply(string input, CultureInfo culture) |
| | | 25 | | { |
| | 9 | 26 | | ArgumentNullException.ThrowIfNull(input); |
| | 9 | 27 | | ArgumentNullException.ThrowIfNull(culture); |
| | | 28 | | |
| | 9 | 29 | | if (input.Length == 0) |
| | 0 | 30 | | return input; |
| | | 31 | | |
| | 9 | 32 | | var builder = new StringBuilder(); |
| | 9 | 33 | | var insideToken = false; |
| | | 34 | | |
| | 300 | 35 | | foreach (var c in input) |
| | | 36 | | { |
| | 141 | 37 | | if (char.IsWhiteSpace(c)) |
| | | 38 | | { |
| | 12 | 39 | | insideToken = false; |
| | 12 | 40 | | continue; |
| | | 41 | | } |
| | | 42 | | |
| | 129 | 43 | | if (insideToken) |
| | | 44 | | continue; |
| | | 45 | | |
| | 21 | 46 | | builder.Append(char.ToUpper(c, culture)); |
| | 21 | 47 | | insideToken = true; |
| | | 48 | | } |
| | | 49 | | |
| | 9 | 50 | | return builder.ToString(); |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |