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

JaCraig / Archivist / 15917742567

27 Jun 2025 03:51AM UTC coverage: 77.18% (-0.9%) from 78.063%
15917742567

push

github

JaCraig
chore: Merge branch 'main' of https://github.com/JaCraig/Archivist

2412 of 3412 branches covered (70.69%)

Branch coverage included in aggregate %.

3182 of 3836 relevant lines covered (82.95%)

7309.35 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))
61✔
35
            {
36
                throw new ArgumentException("Format pattern is not valid.", nameof(formatPattern));
4✔
37
            }
38
            if (string.IsNullOrEmpty(input))
57✔
39
                return "";
1✔
40

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

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

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