Skip to content

Commit

Permalink
Combine surrogate pairs into one escape sequence when encoding. (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
tech4him1 authored and Vitaly Puzrin committed Sep 11, 2017
1 parent 0ec1037 commit b747545
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/js-yaml/dumper.js
Expand Up @@ -461,11 +461,21 @@ function foldLine(line, width) {
// Escapes a double-quoted string.
function escapeString(string) {
var result = '';
var char;
var char, nextChar;
var escapeSeq;

for (var i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
// Check for surrogate pairs (reference Unicode 3.0 section "3.7 Surrogates").
if (char >= 0xD800 && char <= 0xDBFF/* high surrogate */) {
nextChar = string.charCodeAt(i + 1);
if (nextChar >= 0xDC00 && nextChar <= 0xDFFF/* low surrogate */) {
// Combine the surrogate pair and store it escaped.
result += encodeHex((char - 0xD800) * 0x400 + nextChar - 0xDC00 + 0x10000);
// Advance index one extra since we already used that char here.
i++; continue;
}
}
escapeSeq = ESCAPE_SEQUENCES[char];
result += !escapeSeq && isPrintable(char)
? string[i]
Expand Down

0 comments on commit b747545

Please sign in to comment.