File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -1160,12 +1160,25 @@ private void WriteDoubleQuotedScalar(string value, bool allowBreaks)
1160
1160
break ;
1161
1161
1162
1162
default :
1163
- var code = ( short ) character ;
1163
+ var code = ( ushort ) character ;
1164
1164
if ( code <= 0xFF )
1165
1165
{
1166
1166
Write ( 'x' ) ;
1167
1167
Write ( code . ToString ( "X02" , CultureInfo . InvariantCulture ) ) ;
1168
1168
}
1169
+ else if ( IsHighSurrogate ( character ) )
1170
+ {
1171
+ if ( index + 1 < value . Length && IsLowSurrogate ( value [ index + 1 ] ) )
1172
+ {
1173
+ Write ( 'U' ) ;
1174
+ Write ( char . ConvertToUtf32 ( character , value [ index + 1 ] ) . ToString ( "X08" , CultureInfo . InvariantCulture ) ) ;
1175
+ index ++ ;
1176
+ }
1177
+ else
1178
+ {
1179
+ throw new SyntaxErrorException ( "While writing a quoted scalar, found an orphaned high surrogate." ) ;
1180
+ }
1181
+ }
1169
1182
else
1170
1183
{
1171
1184
Write ( 'u' ) ;
@@ -1342,6 +1355,10 @@ private static bool IsPrintable(char character)
1342
1355
( character >= '\xE000 ' && character <= '\xFFFD ' ) ;
1343
1356
}
1344
1357
1358
+ private static bool IsHighSurrogate ( char c ) => 0xD800 <= c && c <= 0xDBFF ;
1359
+
1360
+ private static bool IsLowSurrogate ( char c ) => 0xDC00 <= c && c <= 0xDFFF ;
1361
+
1345
1362
#endregion
1346
1363
1347
1364
/// <summary>
You can’t perform that action at this time.
0 commit comments