Skip to content

Commit

Permalink
optimized escaped sequences scan
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Jun 12, 2014
1 parent f24d461 commit 92ad0a6
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions lib/js-yaml/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,6 @@ var CHOMPING_KEEP = 3;

var SIMPLE_ESCAPE_SEQUENCES = {};

SIMPLE_ESCAPE_SEQUENCES[0x30/* 0 */] = '\x00';
SIMPLE_ESCAPE_SEQUENCES[0x61/* a */] = '\x07';
SIMPLE_ESCAPE_SEQUENCES[0x62/* b */] = '\x08';
SIMPLE_ESCAPE_SEQUENCES[0x74/* t */] = '\x09';
SIMPLE_ESCAPE_SEQUENCES[0x09/* Tab */] = '\x09';
SIMPLE_ESCAPE_SEQUENCES[0x6E/* n */] = '\x0A';
SIMPLE_ESCAPE_SEQUENCES[0x76/* v */] = '\x0B';
SIMPLE_ESCAPE_SEQUENCES[0x66/* f */] = '\x0C';
SIMPLE_ESCAPE_SEQUENCES[0x72/* r */] = '\x0D';
SIMPLE_ESCAPE_SEQUENCES[0x65/* e */] = '\x1B';
SIMPLE_ESCAPE_SEQUENCES[0x20/* Space */] = ' ';
SIMPLE_ESCAPE_SEQUENCES[0x22/* " */] = '\x22';
SIMPLE_ESCAPE_SEQUENCES[0x2F/* / */] = '/';
SIMPLE_ESCAPE_SEQUENCES[0x5C/* \ */] = '\x5C';
SIMPLE_ESCAPE_SEQUENCES[0x4E/* N */] = '\x85';
SIMPLE_ESCAPE_SEQUENCES[0x5F/* _ */] = '\xA0';
SIMPLE_ESCAPE_SEQUENCES[0x4C/* L */] = '\u2028';
SIMPLE_ESCAPE_SEQUENCES[0x50/* P */] = '\u2029';


var PATTERN_NON_PRINTABLE = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uD800-\uDFFF\uFFFE\uFFFF]/;
var PATTERN_NON_ASCII_LINE_BREAKS = /[\x85\u2028\u2029]/;
Expand Down Expand Up @@ -102,6 +83,27 @@ function fromDecimalCode(c) {
return -1;
}

function simpleEscapeSequence(c) {
return (c === 0x30/* 0 */) ? '\x00' :
(c === 0x61/* a */) ? '\x07' :
(c === 0x62/* b */) ? '\x08' :
(c === 0x74/* t */) ? '\x09' :
(c === 0x09/* Tab */) ? '\x09' :
(c === 0x6E/* n */) ? '\x0A' :
(c === 0x76/* v */) ? '\x0B' :
(c === 0x66/* f */) ? '\x0C' :
(c === 0x72/* r */) ? '\x0D' :
(c === 0x65/* e */) ? '\x1B' :
(c === 0x20/* Space */) ? ' ' :
(c === 0x22/* " */) ? '\x22' :
(c === 0x2F/* / */) ? '/' :
(c === 0x5C/* \ */) ? '\x5C' :
(c === 0x4E/* N */) ? '\x85' :
(c === 0x5F/* _ */) ? '\xA0' :
(c === 0x4C/* L */) ? '\u2028' :
(c === 0x50/* P */) ? '\u2029' : '';
}


function State(input, options) {
this.input = input;
Expand Down Expand Up @@ -562,7 +564,7 @@ function readDoubleQuotedScalar(state, nodeIndent) {
hexIndex,
hexOffset,
hexResult,
tmp,
tmp, tmpEsc,
ch,
input = state.input,
len = state.len;
Expand Down Expand Up @@ -592,8 +594,8 @@ function readDoubleQuotedScalar(state, nodeIndent) {
skipSeparationSpace(state, false, nodeIndent);

//TODO: rework to inline fn with no type cast?
} else if (SIMPLE_ESCAPE_SEQUENCES[ch]) {
state.result += SIMPLE_ESCAPE_SEQUENCES[ch];
} else if ((tmpEsc = simpleEscapeSequence(ch)) !== '') {
state.result += tmpEsc;
ch = input.charCodeAt(++state.position);

} else if ((tmp = escapedHexBase(ch)) > 0) {
Expand Down

0 comments on commit 92ad0a6

Please sign in to comment.