Skip to content

Commit 1c4721a

Browse files
committed
Added secondary dictionary with string key for yaml control characters
1 parent fe74c3d commit 1c4721a

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/Microsoft.OpenApi/Writers/SpecialCharacterStringExtensions.cs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static class SpecialCharacterStringExtensions
6565

6666
// Double-quoted strings are needed for these non-printable control characters.
6767
// http://www.yaml.org/spec/1.2/spec.html#style/flow/double-quoted
68-
private static readonly Dictionary<char, string> _yamlControlCharacterReplacements = new()
68+
private static readonly Dictionary<char, string> _yamlControlCharacterCharReplacements = new()
6969
{
7070
{'\0', "\\0"},
7171
{'\x01', "\\x01"},
@@ -100,33 +100,31 @@ public static class SpecialCharacterStringExtensions
100100
{'\x1e', "\\x1e"},
101101
{'\x1f', "\\x1f"},
102102
};
103+
104+
private static readonly Dictionary<string, string> _yamlControlCharacterStringReplacements = _yamlControlCharacterCharReplacements
105+
.ToDictionary(x => x.Key.ToString(), x => x.Value);
103106

104107
/// <summary>
105108
/// Escapes all special characters and put the string in quotes if necessary to
106109
/// get a YAML-compatible string.
107110
/// </summary>
108111
internal static string GetYamlCompatibleString(this string input)
109112
{
110-
// If string is an empty string, wrap it in quote to ensure it is not recognized as null.
111-
if (input == "")
113+
switch (input)
112114
{
113-
return "''";
114-
}
115-
116-
// If string is the word null, wrap it in quote to ensure it is not recognized as empty scalar null.
117-
if (input == "null")
118-
{
119-
return "'null'";
120-
}
121-
122-
// If string is the letter ~, wrap it in quote to ensure it is not recognized as empty scalar null.
123-
if (input == "~")
124-
{
125-
return "'~'";
115+
// If string is an empty string, wrap it in quote to ensure it is not recognized as null.
116+
case "":
117+
return "''";
118+
// If string is the word null, wrap it in quote to ensure it is not recognized as empty scalar null.
119+
case "null":
120+
return "'null'";
121+
// If string is the letter ~, wrap it in quote to ensure it is not recognized as empty scalar null.
122+
case "~":
123+
return "'~'";
126124
}
127125

128126
// If string includes a control character, wrapping in double quote is required.
129-
if (input.Any(c => _yamlControlCharacterReplacements.ContainsKey(c)))
127+
if (input.Any(c => _yamlControlCharacterCharReplacements.ContainsKey(c)))
130128
{
131129
// Replace the backslash first, so that the new backslashes created by other Replaces are not duplicated.
132130
input = input.Replace("\\", "\\\\");
@@ -135,9 +133,9 @@ internal static string GetYamlCompatibleString(this string input)
135133
input = input.Replace("\"", "\\\"");
136134

137135
// Escape all the control characters.
138-
foreach (var replacement in _yamlControlCharacterReplacements)
136+
foreach (var replacement in _yamlControlCharacterStringReplacements)
139137
{
140-
input = input.Replace(replacement.Key.ToString(), replacement.Value);
138+
input = input.Replace(replacement.Key, replacement.Value);
141139
}
142140

143141
return $"\"{input}\"";
@@ -162,10 +160,10 @@ internal static string GetYamlCompatibleString(this string input)
162160

163161
// If string can be mistaken as a number, c-style hexadecimal notation, a boolean, or a timestamp,
164162
// wrap it in quote to indicate that this is indeed a string, not a number, c-style hexadecimal notation, a boolean, or a timestamp
165-
if (decimal.TryParse(input, NumberStyles.Float, CultureInfo.InvariantCulture, out var _) ||
163+
if (decimal.TryParse(input, NumberStyles.Float, CultureInfo.InvariantCulture, out _) ||
166164
IsHexadecimalNotation(input) ||
167-
bool.TryParse(input, out var _) ||
168-
DateTime.TryParse(input, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _))
165+
bool.TryParse(input, out _) ||
166+
DateTime.TryParse(input, CultureInfo.InvariantCulture, DateTimeStyles.None, out _))
169167
{
170168
return $"'{input}'";
171169
}

0 commit comments

Comments
 (0)