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

JaCraig / Archivist / 21937439066

12 Feb 2026 07:26AM UTC coverage: 77.47% (-0.7%) from 78.159%
21937439066

push

github

web-flow
Merge pull request #270 from JaCraig/dependabot/nuget/Archivist.OCR/dependencies-76c0f5d4bc

fix: Bump the dependencies group with 3 updates

2415 of 3412 branches covered (70.78%)

Branch coverage included in aggregate %.

3200 of 3836 relevant lines covered (83.42%)

10726.39 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))
104✔
35
            {
36
                throw new ArgumentException("Format pattern is not valid.", nameof(formatPattern));
6✔
37
            }
38
            if (string.IsNullOrEmpty(input))
98✔
39
                return "";
2✔
40

41
            var ReturnValue = new StringBuilder();
96✔
42
            for (var X = 0; X < formatPattern.Length; ++X)
3,148✔
43
            {
44
                if (formatPattern[X] == _EscapeChar)
1,478!
45
                {
46
                    ++X;
×
47
                    _ = ReturnValue.Append(formatPattern[X]);
×
48
                }
49
                else
50
                {
51
                    input = GetMatchingInput(input, formatPattern[X], out var NextValue);
1,478✔
52
                    if (NextValue != char.MinValue)
1,478✔
53
                    {
54
                        _ = ReturnValue.Append(NextValue);
1,470✔
55
                    }
56
                }
57
            }
58
            return ReturnValue.ToString();
96✔
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))
190!
69
                return false;
×
70

71
            var EscapeCharFound = false;
190✔
72
            for (var X = 0; X < formatPattern.Length; ++X)
6,264✔
73
            {
74
                if (EscapeCharFound && formatPattern[X] != _DigitChar
2,942!
75
                        && formatPattern[X] != _AlphaChar
2,942✔
76
                        && formatPattern[X] != _EscapeChar)
2,942✔
77
                {
78
                    return false;
×
79
                }
80

81
                if (EscapeCharFound)
2,942!
82
                {
83
                    EscapeCharFound = false;
×
84
                }
85
                else
86
                {
87
                    EscapeCharFound |= formatPattern[X] == _EscapeChar;
2,942✔
88
                }
89
            }
90
            return !EscapeCharFound;
190✔
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;
1,478✔
103
            var Alpha = formatChar == _AlphaChar;
1,478✔
104
            if (!Digit && !Alpha)
1,478✔
105
            {
106
                matchChar = formatChar;
360✔
107
                return input;
360✔
108
            }
109
            var Index = 0;
1,118✔
110
            matchChar = char.MinValue;
1,118✔
111
            for (var X = 0; X < input?.Length; ++X)
2,508!
112
            {
113
                if ((Digit && char.IsDigit(input[X])) || (Alpha && char.IsLetter(input[X])))
1,246✔
114
                {
115
                    matchChar = input[X];
1,110✔
116
                    Index = X + 1;
1,110✔
117
                    break;
1,110✔
118
                }
119
            }
120
            return input?[Index..];
1,118!
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

© 2026 Coveralls, Inc