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

xaviersolau / GeneratorTools / 17747607692

15 Sep 2025 09:48PM UTC coverage: 86.455% (-0.05%) from 86.508%
17747607692

push

github

xaviersolau
Bump to version 1.0.0-alpha.45

3817 of 4415 relevant lines covered (86.46%)

11990.19 hits per line

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

85.06
/src/libs/SoloX.GeneratorTools.Core.CSharp/Generator/ReplacePattern/TextPatternHelper.cs
1
// ----------------------------------------------------------------------
2
// <copyright file="TextPatternHelper.cs" company="Xavier Solau">
3
// Copyright © 2021 Xavier Solau.
4
// Licensed under the MIT license.
5
// See LICENSE file in the project root for full license information.
6
// </copyright>
7
// ----------------------------------------------------------------------
8

9
using System;
10
using System.Collections.Generic;
11

12
namespace SoloX.GeneratorTools.Core.CSharp.Generator.ReplacePattern
13
{
14
    /// <summary>
15
    /// Helper to replace text pattern.
16
    /// </summary>
17
    public class TextPatternHelper
18
    {
19
        private readonly string pattern;
20
        private readonly string declaration;
21
        private readonly string optionalPrefix;
22
        private readonly string optionalSuffix;
23

24
        private readonly Func<string, string> replaceDelegate;
25

26
        private static readonly Dictionary<string, string> ReservedPatternNameMap = new Dictionary<string, string>()
3✔
27
        {
3✔
28
            ["object"] = nameof(Object),
3✔
29
            ["string"] = nameof(String),
3✔
30
        };
3✔
31

32
        /// <summary>
33
        /// Setup the text pattern.
34
        /// </summary>
35
        /// <param name="pattern">Pattern to be replaced.</param>
36
        /// <param name="declaration">Declaration to replace the pattern with.</param>
37
        /// <param name="optionalPrefix">Optional prefix.</param>
38
        /// <param name="optionalSuffix">Optional suffix.</param>
39
        public TextPatternHelper(string pattern, string declaration, string? optionalPrefix, string? optionalSuffix)
40
        {
41
            if (string.IsNullOrEmpty(pattern))
441✔
42
            {
43
                throw new ArgumentNullException(nameof(pattern));
×
44
            }
45
            if (string.IsNullOrEmpty(declaration))
441✔
46
            {
47
                throw new ArgumentNullException(nameof(pattern));
×
48
            }
49

50
            this.pattern = pattern;
441✔
51
            this.declaration = declaration;
441✔
52
            this.optionalPrefix = optionalPrefix;
441✔
53
            this.optionalSuffix = optionalSuffix;
441✔
54

55
            var patternLength = pattern.Length;
441✔
56
            var declarationLength = declaration.Length;
441✔
57

58
            if (!string.IsNullOrEmpty(optionalPrefix) && !string.IsNullOrEmpty(optionalSuffix))
441✔
59
            {
60
                var patternWithNoSuffix = pattern.Remove(patternLength - optionalSuffix.Length);
1✔
61
                var declarationWithNoSuffix = declaration.EndsWith(optionalSuffix, StringComparison.InvariantCultureIgnoreCase)
1✔
62
                    ? declaration.Remove(declarationLength - optionalSuffix.Length)
1✔
63
                    : declaration;
1✔
64

65
                var patternWithNoPrefix = pattern.Substring(optionalPrefix.Length);
1✔
66
                var declarationWithNoPrefix = declaration.StartsWith(optionalPrefix, StringComparison.InvariantCultureIgnoreCase)
1✔
67
                    ? declaration.Substring(optionalPrefix.Length)
1✔
68
                    : declaration;
1✔
69

70
                var patternWithNoSuffixNoPrefix = patternWithNoSuffix.Substring(optionalPrefix.Length);
1✔
71
                var declarationWithNoSuffixNoPrefix = declarationWithNoSuffix.StartsWith(optionalPrefix, StringComparison.InvariantCultureIgnoreCase)
1✔
72
                    ? declarationWithNoSuffix.Substring(optionalPrefix.Length)
1✔
73
                    : declarationWithNoSuffix;
1✔
74

75
                this.replaceDelegate = text =>
1✔
76
                {
1✔
77
                    var replacedText = ReplacePattern(pattern, declaration, text);
1✔
78
                    replacedText = ReplacePattern(patternWithNoPrefix, declarationWithNoPrefix, replacedText);
1✔
79
                    replacedText = ReplacePattern(patternWithNoSuffix, declarationWithNoSuffix, replacedText);
1✔
80
                    replacedText = ReplacePattern(patternWithNoSuffixNoPrefix, declarationWithNoSuffixNoPrefix, replacedText);
1✔
81

1✔
82
                    return replacedText;
1✔
83
                };
1✔
84
            }
85
            else if (!string.IsNullOrEmpty(optionalPrefix))
440✔
86
            {
87
                var patternWithNoPrefix = pattern.Substring(optionalPrefix.Length);
7✔
88
                var declarationWithNoPrefix = declaration.StartsWith(optionalPrefix, StringComparison.InvariantCultureIgnoreCase)
7✔
89
                    ? declaration.Substring(optionalPrefix.Length)
7✔
90
                    : declaration;
7✔
91

92
                this.replaceDelegate = text =>
7✔
93
                {
7✔
94
                    var replacedText = ReplacePattern(pattern, declaration, text);
7✔
95
                    replacedText = ReplacePattern(patternWithNoPrefix, declarationWithNoPrefix, replacedText);
7✔
96

7✔
97
                    return replacedText;
7✔
98
                };
7✔
99
            }
100
            else if (!string.IsNullOrEmpty(optionalSuffix))
433✔
101
            {
102
                var patternWithNoSuffix = pattern.Remove(patternLength - optionalSuffix.Length);
×
103
                var declarationWithNoSuffix = declaration.EndsWith(optionalSuffix, System.StringComparison.InvariantCultureIgnoreCase)
×
104
                    ? declaration.Remove(declarationLength - optionalSuffix.Length)
×
105
                    : declaration;
×
106

107
                this.replaceDelegate = text =>
×
108
                {
×
109
                    var replacedText = ReplacePattern(pattern, declaration, text);
×
110
                    replacedText = ReplacePattern(patternWithNoSuffix, declarationWithNoSuffix, replacedText);
×
111

×
112
                    return replacedText;
×
113
                };
×
114
            }
115
            else
116
            {
117
                if (declaration[0] >= 'A'
433✔
118
                    && declaration[0] <= 'Z'
433✔
119
                    && ReservedPatternNameMap.TryGetValue(pattern, out var patternMapValue))
433✔
120
                {
121

122
                    this.replaceDelegate = text =>
3✔
123
                    {
3✔
124
                        if (text.Contains(pattern))
22✔
125
                        {
3✔
126
                            text = text.Replace(pattern, patternMapValue);
3✔
127
                        }
3✔
128

3✔
129
                        return ReplacePattern(patternMapValue, declaration, text);
22✔
130
                    };
3✔
131
                }
132
                else
133
                {
134
                    this.replaceDelegate = text =>
430✔
135
                    {
430✔
136
                        return ReplacePattern(pattern, declaration, text);
4,969✔
137
                    };
430✔
138
                }
139
            }
140
        }
430✔
141

142
        private static string ReplacePattern(string pattern, string declaration, string text)
143
        {
144
            var firstLowerPattern = char.ToLowerInvariant(pattern[0]) + pattern.Substring(1);
5,009✔
145
            var firstLowerDeclaration = char.ToLowerInvariant(declaration[0]) + declaration.Substring(1);
5,009✔
146

147
            var firstUpperPattern = char.ToUpperInvariant(pattern[0]) + pattern.Substring(1);
5,009✔
148
            var firstUpperDeclaration = char.ToUpperInvariant(declaration[0]) + declaration.Substring(1);
5,009✔
149

150
            return text
5,009✔
151
                .Replace(firstUpperPattern, firstUpperDeclaration)
5,009✔
152
                .Replace(firstLowerPattern, firstLowerDeclaration)
5,009✔
153
                .Replace(pattern, declaration);
5,009✔
154
        }
155

156
        /// <summary>
157
        /// Replace pattern in the given text.
158
        /// </summary>
159
        /// <param name="text">Text where to find and replace the pattern.</param>
160
        /// <returns>The replaced text.</returns>
161
        public string ReplacePattern(string text)
162
        {
163
            return this.replaceDelegate(text);
4,999✔
164
        }
165
    }
166
}
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