• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JaCraig / Archivist / 19457476719

18 Nov 2025 07:15AM UTC coverage: 77.414% (+0.06%) from 77.359%
19457476719

push

github

web-flow
Merge pull request #228 from JaCraig/dependabot/nuget/Archivist.Tests/dependencies-f4b6243359

chore: Bump the dependencies group with 1 update

2415 of 3412 branches covered (70.78%)

Branch coverage included in aggregate %.

3196 of 3836 relevant lines covered (83.32%)

5415.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.93
/Archivist/ExtensionMethods/InternalStringFormatter.cs
1
using System;
2
using System.Text;
3

4
namespace Archivist.ExtensionMethods
5
{
6
    /// <summary>
7
    /// Generic string formatter
8
    /// </summary>
9
    public static class InternalStringFormatter
10
    {
11
        /// <summary>
12
        /// Represents alpha characters (defaults to @)
13
        /// </summary>
14
        private const char _AlphaChar = '@';
15

16
        /// <summary>
17
        /// Represents digits (defaults to #)
18
        /// </summary>
19
        private const char _DigitChar = '#';
20

21
        /// <summary>
22
        /// Represents the escape character (defaults to \)
23
        /// </summary>
24
        private const char _EscapeChar = '\\';
25

26
        /// <summary>
27
        /// Formats the string based on the pattern
28
        /// </summary>
29
        /// <param name="input">Input string</param>
30
        /// <param name="formatPattern">Format pattern</param>
31
        /// <returns>The formatted string</returns>
32
        public static string Format(string? input, string? formatPattern)
33
        {
34
            if (string.IsNullOrEmpty(formatPattern) || !IsValid(formatPattern))
52✔
35
            {
36
                throw new ArgumentException("Format pattern is not valid.", nameof(formatPattern));
3✔
37
            }
38
            if (string.IsNullOrEmpty(input))
49✔
39
                return "";
1✔
40

41
            var ReturnValue = new StringBuilder();
48✔
42
            for (var X = 0; X < formatPattern.Length; ++X)
1,574✔
43
            {
44
                if (formatPattern[X] == _EscapeChar)
739!
45
                {
46
                    ++X;
×
47
                    _ = ReturnValue.Append(formatPattern[X]);
×
48
                }
49
                else
50
                {
51
                    input = GetMatchingInput(input, formatPattern[X], out var NextValue);
739✔
52
                    if (NextValue != char.MinValue)
739✔
53
                    {
54
                        _ = ReturnValue.Append(NextValue);
735✔
55
                    }
56
                }
57
            }
58
            return ReturnValue.ToString();
48✔
59
        }
60

61
        /// <summary>
62
        /// Checks if the format pattern is valid
63
        /// </summary>
64
        /// <param name="formatPattern">Format pattern</param>
65
        /// <returns>Returns true if it's valid, otherwise false</returns>
66
        internal static bool IsValid(string? formatPattern)
67
        {
68
            if (string.IsNullOrEmpty(formatPattern))
95!
69
                return false;
×
70

71
            var EscapeCharFound = false;
95✔
72
            for (var X = 0; X < formatPattern.Length; ++X)
3,132✔
73
            {
74
                if (EscapeCharFound && formatPattern[X] != _DigitChar
1,471!
75
                        && formatPattern[X] != _AlphaChar
1,471✔
76
                        && formatPattern[X] != _EscapeChar)
1,471✔
77
                {
78
                    return false;
×
79
                }
80

81
                if (EscapeCharFound)
1,471!
82
                {
83
                    EscapeCharFound = false;
×
84
                }
85
                else
86
                {
87
                    EscapeCharFound |= formatPattern[X] == _EscapeChar;
1,471✔
88
                }
89
            }
90
            return !EscapeCharFound;
95✔
91
        }
92

93
        /// <summary>
94
        /// Gets matching input
95
        /// </summary>
96
        /// <param name="input">Input string</param>
97
        /// <param name="formatChar">Current format character</param>
98
        /// <param name="matchChar">The matching character found</param>
99
        /// <returns>The remainder of the input string left</returns>
100
        private static string? GetMatchingInput(string? input, char formatChar, out char matchChar)
101
        {
102
            var Digit = formatChar == _DigitChar;
739✔
103
            var Alpha = formatChar == _AlphaChar;
739✔
104
            if (!Digit && !Alpha)
739✔
105
            {
106
                matchChar = formatChar;
180✔
107
                return input;
180✔
108
            }
109
            var Index = 0;
559✔
110
            matchChar = char.MinValue;
559✔
111
            for (var X = 0; X < input?.Length; ++X)
1,254!
112
            {
113
                if ((Digit && char.IsDigit(input[X])) || (Alpha && char.IsLetter(input[X])))
623✔
114
                {
115
                    matchChar = input[X];
555✔
116
                    Index = X + 1;
555✔
117
                    break;
555✔
118
                }
119
            }
120
            return input?[Index..];
559!
121
        }
122
    }
123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc