@@ -65,7 +65,7 @@ public static class SpecialCharacterStringExtensions
65
65
66
66
// Double-quoted strings are needed for these non-printable control characters.
67
67
// 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 ( )
69
69
{
70
70
{ '\0 ' , "\\ 0" } ,
71
71
{ '\x01 ' , "\\ x01" } ,
@@ -100,33 +100,31 @@ public static class SpecialCharacterStringExtensions
100
100
{ '\x1e ' , "\\ x1e" } ,
101
101
{ '\x1f ' , "\\ x1f" } ,
102
102
} ;
103
+
104
+ private static readonly Dictionary < string , string > _yamlControlCharacterStringReplacements = _yamlControlCharacterCharReplacements
105
+ . ToDictionary ( x => x . Key . ToString ( ) , x => x . Value ) ;
103
106
104
107
/// <summary>
105
108
/// Escapes all special characters and put the string in quotes if necessary to
106
109
/// get a YAML-compatible string.
107
110
/// </summary>
108
111
internal static string GetYamlCompatibleString ( this string input )
109
112
{
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 )
112
114
{
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 "'~'" ;
126
124
}
127
125
128
126
// 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 ) ) )
130
128
{
131
129
// Replace the backslash first, so that the new backslashes created by other Replaces are not duplicated.
132
130
input = input . Replace ( "\\ " , "\\ \\ " ) ;
@@ -135,9 +133,9 @@ internal static string GetYamlCompatibleString(this string input)
135
133
input = input . Replace ( "\" " , "\\ \" " ) ;
136
134
137
135
// Escape all the control characters.
138
- foreach ( var replacement in _yamlControlCharacterReplacements )
136
+ foreach ( var replacement in _yamlControlCharacterStringReplacements )
139
137
{
140
- input = input . Replace ( replacement . Key . ToString ( ) , replacement . Value ) ;
138
+ input = input . Replace ( replacement . Key , replacement . Value ) ;
141
139
}
142
140
143
141
return $ "\" { input } \" ";
@@ -162,10 +160,10 @@ internal static string GetYamlCompatibleString(this string input)
162
160
163
161
// If string can be mistaken as a number, c-style hexadecimal notation, a boolean, or a timestamp,
164
162
// 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 _ ) ||
166
164
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 _ ) )
169
167
{
170
168
return $ "'{ input } '";
171
169
}
0 commit comments