kopia lustrzana https://github.com/backface/turtlestitch
Revert "Fixing #1932, csv spliter, according to RFC 4180 spec"
rodzic
2ebc0d466a
commit
02b50acd50
29
threads.js
29
threads.js
|
@ -2751,27 +2751,34 @@ Process.prototype.parseCSV = function (string) {
|
|||
// parse a single row of CSV data into a one-dimensional list
|
||||
// this assumes that the whole csv data has already been split
|
||||
// by lines.
|
||||
// Following RFC 4180 specifications
|
||||
// taken from:
|
||||
// https://stackoverflow.com/questions/8493195/how-can-i-parse-a-csv-string-with-javascript-which-contains-comma-in-data
|
||||
|
||||
var re_value = /(?:^|,)(?:"((?:(?:"{2})*[^"]*)*)"|([^",]*))(?=(?:,|$))/g,
|
||||
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/,
|
||||
re_value = /(?!\s*$)\s*(?:'([^'\\]*(?:\\[\S\s][^'\\]*)*)'|"([^"\\]*(?:\\[\S\s][^"\\]*)*)"|([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*))\s*(?:,|$)/g,
|
||||
a = [];
|
||||
|
||||
var remaining = string.replace(
|
||||
if (!re_valid.test(string)) {
|
||||
return new List();
|
||||
}
|
||||
string.replace(
|
||||
re_value,
|
||||
function(m0, m1, m2) {
|
||||
function(m0, m1, m2, m3) {
|
||||
if (m1 !== undefined) {
|
||||
// load a quoted field
|
||||
a.push(m1.replace(/"{2}/g, "\""));
|
||||
// remove backslash from \' in single quoted values.
|
||||
a.push(m1.replace(/\\'/g, "'"));
|
||||
} else if (m2 !== undefined) {
|
||||
// load a non-quoted field
|
||||
a.push(m2.replace(/"{2}/g, "\""));
|
||||
// remove backslash from \" in double quoted values.
|
||||
a.push(m2.replace(/\\"/g, '"'));
|
||||
} else if (m3 !== undefined) {
|
||||
a.push(m3);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
);
|
||||
if (remaining !== '') {
|
||||
// if remaining contains something, string has a non valid format
|
||||
return new List();
|
||||
// special case: empty last value.
|
||||
if (/,\s*$/.test(string)) {
|
||||
a.push('');
|
||||
}
|
||||
return new List(a);
|
||||
};
|
||||
|
|
Ładowanie…
Reference in New Issue