Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 26 additions & 43 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
"use strict";

var hex = function (c){
switch (c){
case "0": return 0;
case "1": return 1;
case "2": return 2;
case "3": return 3;
case "4": return 4;
case "5": return 5;
case "6": return 6;
case "7": return 7;
case "8": return 8;
case "9": return 9;
case "a": case "A": return 10;
case "b": case "B": return 11;
case "c": case "C": return 12;
case "d": case "D": return 13;
case "e": case "E": return 14;
case "f": case "F": return 15;
}
return parseInt(c, 16);
};

module.exports = function (data, options, handlers, control){
Expand All @@ -40,7 +23,7 @@ module.exports = function (data, options, handlers, control){
var keySpace;
var sep;
var ignoreLine;

var line = function (){
if (key || value || sep){
handlers.line (key, value);
Expand All @@ -49,7 +32,7 @@ module.exports = function (data, options, handlers, control){
sep = false;
}
};

var escapeString = function (key, c, code){
if (escapingUnicode && unicodeRemaining){
unicode = (unicode << 4) + hex (c);
Expand All @@ -58,17 +41,17 @@ module.exports = function (data, options, handlers, control){
escapingUnicode = false;
return key + String.fromCharCode (unicode);
}

//code 117: u
if (code === 117){
unicode = 0;
escapingUnicode = true;
unicodeRemaining = 4;
return key;
}

escape = false;

//code 116: t
//code 114: r
//code 110: n
Expand All @@ -77,13 +60,13 @@ module.exports = function (data, options, handlers, control){
else if (code === 114) return key + "\r";
else if (code === 110) return key + "\n";
else if (code === 102) return key + "\f";

return key + c;
};

var isComment;
var isSeparator;

if (options._strict){
isComment = function (c, code, options){
return options._comments[c];
Expand All @@ -105,7 +88,7 @@ module.exports = function (data, options, handlers, control){
return code === 61 || code === 58 || options._separators[c];
};
}

for (var i=~~control.resume; i<data.length; i++){
if (control.abort) return;
if (control.pause){
Expand All @@ -114,13 +97,13 @@ module.exports = function (data, options, handlers, control){
control.resume = i;
return;
}

c = data[i];
code = data.charCodeAt (i);

//code 13: \r
if (code === 13) continue;

if (isCommentLine){
//code 10: \n
if (code === 10){
Expand All @@ -130,15 +113,15 @@ module.exports = function (data, options, handlers, control){
}
continue;
}

//code 93: ]
if (isSectionLine && code === 93){
handlers.section (section);
//Ignore chars after the section in the same line
ignoreLine = true;
continue;
}

if (skipSpace){
//code 32: " " (space)
//code 9: \t
Expand All @@ -158,7 +141,7 @@ module.exports = function (data, options, handlers, control){
skipSpace = false;
multiLine = false;
}

if (newLine){
newLine = false;
if (isComment (c, code, options)){
Expand All @@ -173,11 +156,11 @@ module.exports = function (data, options, handlers, control){
continue;
}
}

//code 10: \n
if (code !== 10){
if (control.skipSection || ignoreLine) continue;

if (!isSectionLine){
if (!escape && isKey && isSeparator (c, code, options)){
//sep is needed to detect empty key and empty value with a
Expand All @@ -190,18 +173,18 @@ module.exports = function (data, options, handlers, control){
continue;
}
}

//code 92: "\" (backslash)
if (code === 92){
if (escape){
if (escapingUnicode) continue;

if (keySpace){
//Line with whitespace separator
keySpace = false;
isKey = false;
}

if (isSectionLine) section += "\\";
else if (isKey) key += "\\";
else value += "\\";
Expand All @@ -213,7 +196,7 @@ module.exports = function (data, options, handlers, control){
keySpace = false;
isKey = false;
}

if (isSectionLine){
if (escape) section = escapeString (section, c, code);
else section += c;
Expand Down Expand Up @@ -258,19 +241,19 @@ module.exports = function (data, options, handlers, control){
newLine = true;
skipSpace = true;
isKey = true;

line ();
}
}
}

control.parsed = true;

if (isSectionLine && !ignoreLine){
//The section doesn't end with ], it's a key
control.error = new Error ("The section line \"" + section + "\" must end" +
"with \"]\"");
return;
}
line ();
};
};