2
2
// Licensed under the MIT license.
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using System . Globalization ;
6
7
using System . Linq ;
7
8
@@ -14,34 +15,34 @@ public static class SpecialCharacterStringExtensions
14
15
{
15
16
// Plain style strings cannot start with indicators.
16
17
// http://www.yaml.org/spec/1.2/spec.html#indicator//
17
- private static readonly char [ ] _yamlIndicators =
18
+ private static readonly HashSet < string > _yamlIndicators = new ( )
18
19
{
19
- '-' ,
20
- '?' ,
21
- ':' ,
22
- ',' ,
23
- '{' ,
24
- '}' ,
25
- '[' ,
26
- ']' ,
27
- '&' ,
28
- '*' ,
29
- '#' ,
30
- '?' ,
31
- '|' ,
32
- '-' ,
33
- '>' ,
34
- '!' ,
35
- '%' ,
36
- '@' ,
37
- '`' ,
38
- ' \' ' ,
39
- '"' ,
20
+ "-" ,
21
+ "?" ,
22
+ ":" ,
23
+ "," ,
24
+ "{" ,
25
+ "}" ,
26
+ "[" ,
27
+ "]" ,
28
+ "&" ,
29
+ "*" ,
30
+ "#" ,
31
+ "?" ,
32
+ "|" ,
33
+ "-" ,
34
+ ">" ,
35
+ "!" ,
36
+ "%" ,
37
+ "@" ,
38
+ "`" ,
39
+ "'" ,
40
+ " \" "
40
41
} ;
41
42
42
43
// Plain style strings cannot contain these character combinations.
43
44
// http://www.yaml.org/spec/1.2/spec.html#style/flow/plain
44
- private static readonly string [ ] _yamlPlainStringForbiddenCombinations =
45
+ private static readonly HashSet < string > _yamlPlainStringForbiddenCombinations = new ( )
45
46
{
46
47
": " ,
47
48
" #" ,
@@ -57,47 +58,47 @@ public static class SpecialCharacterStringExtensions
57
58
58
59
// Plain style strings cannot end with these characters.
59
60
// http://www.yaml.org/spec/1.2/spec.html#style/flow/plain
60
- private static readonly string [ ] _yamlPlainStringForbiddenTerminals =
61
+ private static readonly HashSet < string > _yamlPlainStringForbiddenTerminals = new ( )
61
62
{
62
63
":"
63
64
} ;
64
65
65
66
// Double-quoted strings are needed for these non-printable control characters.
66
67
// http://www.yaml.org/spec/1.2/spec.html#style/flow/double-quoted
67
- private static readonly char [ ] _yamlControlCharacters =
68
+ private static readonly Dictionary < char , string > _yamlControlCharacterReplacements = new ( )
68
69
{
69
- '\0 ' ,
70
- '\x01 ' ,
71
- '\x02 ' ,
72
- '\x03 ' ,
73
- '\x04 ' ,
74
- '\x05 ' ,
75
- '\x06 ' ,
76
- '\a ' ,
77
- '\b ' ,
78
- '\t ' ,
79
- '\n ' ,
80
- '\v ' ,
81
- '\f ' ,
82
- '\r ' ,
83
- '\x0e ' ,
84
- '\x0f ' ,
85
- '\x10 ' ,
86
- '\x11 ' ,
87
- '\x12 ' ,
88
- '\x13 ' ,
89
- '\x14 ' ,
90
- '\x15 ' ,
91
- '\x16 ' ,
92
- '\x17 ' ,
93
- '\x18 ' ,
94
- '\x19 ' ,
95
- '\x1a ' ,
96
- '\x1b ' ,
97
- '\x1c ' ,
98
- '\x1d ' ,
99
- '\x1e ' ,
100
- '\x1f '
70
+ { '\0 ' , " \\ 0" } ,
71
+ { '\x01 ' , " \\ x01" } ,
72
+ { '\x02 ' , " \\ x02" } ,
73
+ { '\x03 ' , " \\ x03" } ,
74
+ { '\x04 ' , " \\ x04" } ,
75
+ { '\x05 ' , " \\ x05" } ,
76
+ { '\x06 ' , " \\ x06" } ,
77
+ { '\a ' , " \\ a" } ,
78
+ { '\b ' , " \\ b" } ,
79
+ { '\t ' , " \\ t" } ,
80
+ { '\n ' , " \\ n" } ,
81
+ { '\v ' , " \\ v" } ,
82
+ { '\f ' , " \\ f" } ,
83
+ { '\r ' , " \\ r" } ,
84
+ { '\x0e ' , " \\ x0e" } ,
85
+ { '\x0f ' , " \\ x0f" } ,
86
+ { '\x10 ' , " \\ x10" } ,
87
+ { '\x11 ' , " \\ x11" } ,
88
+ { '\x12 ' , " \\ x12" } ,
89
+ { '\x13 ' , " \\ x13" } ,
90
+ { '\x14 ' , " \\ x14" } ,
91
+ { '\x15 ' , " \\ x15" } ,
92
+ { '\x16 ' , " \\ x16" } ,
93
+ { '\x17 ' , " \\ x17" } ,
94
+ { '\x18 ' , " \\ x18" } ,
95
+ { '\x19 ' , " \\ x19" } ,
96
+ { '\x1a ' , " \\ x1a" } ,
97
+ { '\x1b ' , " \\ x1b" } ,
98
+ { '\x1c ' , " \\ x1c" } ,
99
+ { '\x1d ' , " \\ x1d" } ,
100
+ { '\x1e ' , " \\ x1e" } ,
101
+ { '\x1f ' , " \\ x1f" } ,
101
102
} ;
102
103
103
104
/// <summary>
@@ -125,7 +126,7 @@ internal static string GetYamlCompatibleString(this string input)
125
126
}
126
127
127
128
// If string includes a control character, wrapping in double quote is required.
128
- if ( input . Any ( c => _yamlControlCharacters . Contains ( c ) ) )
129
+ if ( input . Any ( c => _yamlControlCharacterReplacements . ContainsKey ( c ) ) )
129
130
{
130
131
// Replace the backslash first, so that the new backslashes created by other Replaces are not duplicated.
131
132
input = input . Replace ( "\\ " , "\\ \\ " ) ;
@@ -134,39 +135,11 @@ internal static string GetYamlCompatibleString(this string input)
134
135
input = input . Replace ( "\" " , "\\ \" " ) ;
135
136
136
137
// Escape all the control characters.
137
- input = input . Replace ( "\0 " , "\\ 0" ) ;
138
- input = input . Replace ( "\x01 " , "\\ x01" ) ;
139
- input = input . Replace ( "\x02 " , "\\ x02" ) ;
140
- input = input . Replace ( "\x03 " , "\\ x03" ) ;
141
- input = input . Replace ( "\x04 " , "\\ x04" ) ;
142
- input = input . Replace ( "\x05 " , "\\ x05" ) ;
143
- input = input . Replace ( "\x06 " , "\\ x06" ) ;
144
- input = input . Replace ( "\a " , "\\ a" ) ;
145
- input = input . Replace ( "\b " , "\\ b" ) ;
146
- input = input . Replace ( "\t " , "\\ t" ) ;
147
- input = input . Replace ( "\n " , "\\ n" ) ;
148
- input = input . Replace ( "\v " , "\\ v" ) ;
149
- input = input . Replace ( "\f " , "\\ f" ) ;
150
- input = input . Replace ( "\r " , "\\ r" ) ;
151
- input = input . Replace ( "\x0e " , "\\ x0e" ) ;
152
- input = input . Replace ( "\x0f " , "\\ x0f" ) ;
153
- input = input . Replace ( "\x10 " , "\\ x10" ) ;
154
- input = input . Replace ( "\x11 " , "\\ x11" ) ;
155
- input = input . Replace ( "\x12 " , "\\ x12" ) ;
156
- input = input . Replace ( "\x13 " , "\\ x13" ) ;
157
- input = input . Replace ( "\x14 " , "\\ x14" ) ;
158
- input = input . Replace ( "\x15 " , "\\ x15" ) ;
159
- input = input . Replace ( "\x16 " , "\\ x16" ) ;
160
- input = input . Replace ( "\x17 " , "\\ x17" ) ;
161
- input = input . Replace ( "\x18 " , "\\ x18" ) ;
162
- input = input . Replace ( "\x19 " , "\\ x19" ) ;
163
- input = input . Replace ( "\x1a " , "\\ x1a" ) ;
164
- input = input . Replace ( "\x1b " , "\\ x1b" ) ;
165
- input = input . Replace ( "\x1c " , "\\ x1c" ) ;
166
- input = input . Replace ( "\x1d " , "\\ x1d" ) ;
167
- input = input . Replace ( "\x1e " , "\\ x1e" ) ;
168
- input = input . Replace ( "\x1f " , "\\ x1f" ) ;
169
-
138
+ foreach ( var replacement in _yamlControlCharacterReplacements )
139
+ {
140
+ input = input . Replace ( replacement . Key . ToString ( ) , replacement . Value ) ;
141
+ }
142
+
170
143
return $ "\" { input } \" ";
171
144
}
172
145
@@ -177,8 +150,8 @@ internal static string GetYamlCompatibleString(this string input)
177
150
// wrap the string in single quote.
178
151
// http://www.yaml.org/spec/1.2/spec.html#style/flow/plain
179
152
if ( _yamlPlainStringForbiddenCombinations . Any ( fc => input . Contains ( fc ) ) ||
180
- _yamlIndicators . Any ( i => input . StartsWith ( i . ToString ( ) ) ) ||
181
- _yamlPlainStringForbiddenTerminals . Any ( i => input . EndsWith ( i . ToString ( ) ) ) ||
153
+ _yamlIndicators . Any ( i => input . StartsWith ( i ) ) ||
154
+ _yamlPlainStringForbiddenTerminals . Any ( i => input . EndsWith ( i ) ) ||
182
155
input . Trim ( ) != input )
183
156
{
184
157
// Escape single quotes with two single quotes.
@@ -215,21 +188,20 @@ internal static string GetJsonCompatibleString(this string value)
215
188
// http://json.org/
216
189
217
190
// Replace the backslash first, so that the new backslashes created by other Replaces are not duplicated.
218
- value = value . Replace ( "\\ " , "\\ \\ " ) ;
219
-
220
- value = value . Replace ( "\b " , "\\ b" ) ;
221
- value = value . Replace ( "\f " , "\\ f" ) ;
222
- value = value . Replace ( "\n " , "\\ n" ) ;
223
- value = value . Replace ( "\r " , "\\ r" ) ;
224
- value = value . Replace ( "\t " , "\\ t" ) ;
225
- value = value . Replace ( "\" " , "\\ \" " ) ;
191
+ value = value . Replace ( "\\ " , "\\ \\ " )
192
+ . Replace ( "\b " , "\\ b" )
193
+ . Replace ( "\f " , "\\ f" )
194
+ . Replace ( "\n " , "\\ n" )
195
+ . Replace ( "\r " , "\\ r" )
196
+ . Replace ( "\t " , "\\ t" )
197
+ . Replace ( "\" " , "\\ \" " ) ;
226
198
227
199
return $ "\" { value } \" ";
228
200
}
229
201
230
202
internal static bool IsHexadecimalNotation ( string input )
231
203
{
232
- return input . StartsWith ( "0x" ) && int . TryParse ( input . Substring ( 2 ) , NumberStyles . HexNumber , CultureInfo . InvariantCulture , out var _ ) ;
204
+ return input . StartsWith ( "0x" ) && int . TryParse ( input . Substring ( 2 ) , NumberStyles . HexNumber , CultureInfo . InvariantCulture , out _ ) ;
233
205
}
234
206
}
235
207
}
0 commit comments