Skip to content

Commit fe74c3d

Browse files
committed
Use lookup collections
1 parent 543da26 commit fe74c3d

File tree

1 file changed

+74
-102
lines changed

1 file changed

+74
-102
lines changed

src/Microsoft.OpenApi/Writers/SpecialCharacterStringExtensions.cs

Lines changed: 74 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Globalization;
67
using System.Linq;
78

@@ -14,34 +15,34 @@ public static class SpecialCharacterStringExtensions
1415
{
1516
// Plain style strings cannot start with indicators.
1617
// http://www.yaml.org/spec/1.2/spec.html#indicator//
17-
private static readonly char[] _yamlIndicators =
18+
private static readonly HashSet<string> _yamlIndicators = new()
1819
{
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+
"\""
4041
};
4142

4243
// Plain style strings cannot contain these character combinations.
4344
// 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()
4546
{
4647
": ",
4748
" #",
@@ -57,47 +58,47 @@ public static class SpecialCharacterStringExtensions
5758

5859
// Plain style strings cannot end with these characters.
5960
// 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()
6162
{
6263
":"
6364
};
6465

6566
// Double-quoted strings are needed for these non-printable control characters.
6667
// 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()
6869
{
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"},
101102
};
102103

103104
/// <summary>
@@ -125,7 +126,7 @@ internal static string GetYamlCompatibleString(this string input)
125126
}
126127

127128
// 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)))
129130
{
130131
// Replace the backslash first, so that the new backslashes created by other Replaces are not duplicated.
131132
input = input.Replace("\\", "\\\\");
@@ -134,39 +135,11 @@ internal static string GetYamlCompatibleString(this string input)
134135
input = input.Replace("\"", "\\\"");
135136

136137
// 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+
170143
return $"\"{input}\"";
171144
}
172145

@@ -177,8 +150,8 @@ internal static string GetYamlCompatibleString(this string input)
177150
// wrap the string in single quote.
178151
// http://www.yaml.org/spec/1.2/spec.html#style/flow/plain
179152
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)) ||
182155
input.Trim() != input)
183156
{
184157
// Escape single quotes with two single quotes.
@@ -215,21 +188,20 @@ internal static string GetJsonCompatibleString(this string value)
215188
// http://json.org/
216189

217190
// 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("\"", "\\\"");
226198

227199
return $"\"{value}\"";
228200
}
229201

230202
internal static bool IsHexadecimalNotation(string input)
231203
{
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 _);
233205
}
234206
}
235207
}

0 commit comments

Comments
 (0)