< Summary

Information
Class: MyNet.Text.Formatting.IncrementTransform
Assembly: MyNet.Text
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Formatting/IncrementTransform.cs
Tag: 323_28699572109
Line coverage
72%
Covered lines: 27
Uncovered lines: 10
Coverable lines: 37
Total lines: 101
Line coverage: 72.9%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
ToAlphabeticSuffix(...)100%44100%
.ctor(...)100%11100%
Apply(...)100%22100%
GetSuffix(...)75%4483.33%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Formatting/IncrementTransform.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="IncrementTransform.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Globalization;
 10
 11namespace MyNet.Text.Formatting;
 12
 13/// <summary>
 14/// Represents a text transformation that increments a string by appending an incrementing number until a unique value i
 15/// </summary>
 16public sealed class IncrementTransform : ITextFormatterTransform
 17{
 18    private readonly IncrementTransformOptions _options;
 19
 20    /// <summary>
 21    /// Initializes a new instance of the <see cref="IncrementTransform"/> class.
 22    /// </summary>
 23    /// <param name="existStrings">The collection of existing strings to check against.</param>
 24    /// <param name="minIncrement">The minimum increment value. Current is 1.</param>
 25    /// <param name="step">The step value for incrementing. Current is 1.</param>
 26    /// <param name="format">Optional format string for the incrementing number.</param>
 27    /// <exception cref="ArgumentNullException">Thrown when <paramref name="existStrings"/> is null.</exception>
 28    public IncrementTransform(IEnumerable<string> existStrings, int minIncrement = 1, int step = 1, string? format = nul
 029        : this(new()
 030        {
 031            ExistingStrings = existStrings,
 032            Kind = IncrementKind.Numeric,
 033            MinIncrement = minIncrement,
 034            Step = step,
 035            NumericFormat = format
 036        })
 37    {
 038    }
 39
 40    private static string ToAlphabeticSuffix(int index)
 41    {
 24042        var chars = new Stack<char>();
 24043        var current = index;
 44
 48645        while (current > 0)
 46        {
 24647            current--;
 24648            chars.Push((char)('A' + (current % 26)));
 24649            current /= 26;
 50        }
 51
 24052        return new([.. chars]);
 53    }
 54
 55    /// <summary>
 56    /// Initializes a new instance of the <see cref="IncrementTransform"/> class with options.
 57    /// </summary>
 58    /// <param name="options">The options used to configure increment behavior.</param>
 59    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or its required members are null.
 60    /// <exception cref="ArgumentOutOfRangeException">Thrown when increment values are invalid.</exception>
 61    public IncrementTransform(IncrementTransformOptions options)
 62    {
 1263        ArgumentNullException.ThrowIfNull(options);
 1264        ArgumentNullException.ThrowIfNull(options.ExistingStrings);
 1265        ArgumentOutOfRangeException.ThrowIfLessThan(options.MinIncrement, 1);
 1266        ArgumentOutOfRangeException.ThrowIfLessThan(options.Step, 1);
 67
 1268        _options = options;
 1269    }
 70
 71    /// <summary>
 72    /// Applies the increment transformation to the input string.
 73    /// </summary>
 74    /// <param name="input">The string to increment.</param>
 75    /// <param name="culture">The culture to use for formatting the incrementing number.</param>
 76    /// <returns>A new string that is an incremented version of the input string.</returns>
 77    public string Apply(string input, CultureInfo culture)
 78    {
 1279        ArgumentNullException.ThrowIfNull(input);
 1280        ArgumentNullException.ThrowIfNull(culture);
 81
 1282        var inc = _options.MinIncrement;
 1283        var hashSet = new HashSet<string>(_options.ExistingStrings, StringComparer.Ordinal);
 23784        while (true)
 85        {
 24986            var newName = input + GetSuffix(inc, culture);
 24987            if (!hashSet.Contains(newName))
 1288                return newName;
 89
 23790            inc += _options.Step;
 91        }
 92    }
 93
 24994    private string GetSuffix(int inc, CultureInfo culture) => _options.Kind switch
 24995    {
 996        IncrementKind.Numeric => inc.ToString(_options.NumericFormat, culture),
 24097        IncrementKind.Alpha => ToAlphabeticSuffix(inc),
 098        _ => throw new NotSupportedException($"Unsupported increment kind: {_options.Kind}")
 24999    };
 100}
 101