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

xaviersolau / GeneratorTools / 14023481222

23 Mar 2025 10:12PM UTC coverage: 86.508% (+0.05%) from 86.456%
14023481222

push

github

xaviersolau
Bump to version 1.0.0-alpha.44

3815 of 4410 relevant lines covered (86.51%)

10671.66 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
using static System.Net.Mime.MediaTypeNames;
12

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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