Skip to content

Commit 6525bb8

Browse files
committed
Handle 32bits Unicode escaped sequences
1 parent 63c1125 commit 6525bb8

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

YamlDotNet/Core/Emitter.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,12 +1160,25 @@ private void WriteDoubleQuotedScalar(string value, bool allowBreaks)
11601160
break;
11611161

11621162
default:
1163-
var code = (short)character;
1163+
var code = (ushort)character;
11641164
if (code <= 0xFF)
11651165
{
11661166
Write('x');
11671167
Write(code.ToString("X02", CultureInfo.InvariantCulture));
11681168
}
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+
}
11691182
else
11701183
{
11711184
Write('u');
@@ -1342,6 +1355,10 @@ private static bool IsPrintable(char character)
13421355
(character >= '\xE000' && character <= '\xFFFD');
13431356
}
13441357

1358+
private static bool IsHighSurrogate(char c) => 0xD800 <= c && c <= 0xDBFF;
1359+
1360+
private static bool IsLowSurrogate(char c) => 0xDC00 <= c && c <= 0xDFFF;
1361+
13451362
#endregion
13461363

13471364
/// <summary>

0 commit comments

Comments
 (0)