Permalink
Browse files

Merge branch 'jasondavies-csv_newlines'

  • Loading branch information...
2 parents 4fcff60 + 87a9b00 commit 39a32e17d1023e878ac39976c2a363645e91fe0e @mbostock mbostock committed Mar 18, 2011
Showing with 52 additions and 11 deletions.
  1. +1 −0 Makefile
  2. +9 −3 d3.csv.js
  3. +3 −2 d3.csv.min.js
  4. +1 −1 d3.js
  5. +1 −1 d3.min.js
  6. +1 −1 src/core/core.js
  7. +9 −3 src/csv/parse.js
  8. +18 −0 tests/test-csv-parse.js
  9. +9 −0 tests/test-csv-parse.out
View
@@ -139,6 +139,7 @@ tests: \
tests/test-append.test \
tests/test-attr.test \
tests/test-call.test \
+ tests/test-csv-parse.test \
tests/test-format.test \
tests/test-time-format.test \
tests/test-time-parse.test \
View
@@ -21,7 +21,7 @@ d3.csv.parseRows = function(text, f) {
var EOL = {}, // sentinel value for end-of-line
EOF = {}, // sentinel value for end-of-file
rows = [], // output rows
- re = /[,\n]/g, // field separator regex
+ re = /\r\n|[,\r\n]/g, // field separator regex
n = 0, // the current line number
t, // the current token
eol; // is the current token followed by EOL?
@@ -41,15 +41,21 @@ d3.csv.parseRows = function(text, f) {
i++;
}
}
- if (text.charCodeAt(i + 1) == 10) eol = true;
re.lastIndex = i + 2;
+ var c = text.charCodeAt(i + 1);
+ if (c == 13) {
+ eol = true;
+ if (text.charCodeAt(i + 2) == 10) re.lastIndex++;
+ } else if (c == 10) {
+ eol = true;
+ }
return text.substring(j + 1, i).replace(/""/g, "\"");
}
// common case
var m = re.exec(text);
if (m) {
- if (m[0] == "\n") eol = true;
+ eol = m[0].charCodeAt(0) != 44;
return text.substring(j, m.index);
}
re.lastIndex = text.length;
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
2 d3.js
@@ -1,4 +1,4 @@
-(function(){d3 = {version: "1.8.2"}; // semver
+(function(){d3 = {version: "1.8.3"}; // semver
if (!Date.now) Date.now = function() {
return +new Date();
};
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1 +1 @@
-d3 = {version: "1.8.2"}; // semver
+d3 = {version: "1.8.3"}; // semver
View
@@ -16,7 +16,7 @@ d3.csv.parseRows = function(text, f) {
var EOL = {}, // sentinel value for end-of-line
EOF = {}, // sentinel value for end-of-file
rows = [], // output rows
- re = /[,\n]/g, // field separator regex
+ re = /\r\n|[,\r\n]/g, // field separator regex
n = 0, // the current line number
t, // the current token
eol; // is the current token followed by EOL?
@@ -36,15 +36,21 @@ d3.csv.parseRows = function(text, f) {
i++;
}
}
- if (text.charCodeAt(i + 1) == 10) eol = true;
re.lastIndex = i + 2;
+ var c = text.charCodeAt(i + 1);
+ if (c == 13) {
+ eol = true;
+ if (text.charCodeAt(i + 2) == 10) re.lastIndex++;
+ } else if (c == 10) {
+ eol = true;
+ }
return text.substring(j + 1, i).replace(/""/g, "\"");
}
// common case
var m = re.exec(text);
if (m) {
- if (m[0] == "\n") eol = true;
+ eol = m[0].charCodeAt(0) != 44;
return text.substring(j, m.index);
}
re.lastIndex = text.length;
View
@@ -0,0 +1,18 @@
+require("./../lib/env-js/envjs/node");
+require("./../d3");
+require("./../d3.csv");
+
+var newlines = {
+ UNIX: "\n",
+ DOS: "\r\n",
+ Mac: "\r"
+};
+
+for (os in newlines) {
+ console.log("parse " + os + " newlines:");
+ var rows = d3.csv.parse(["a,b,c", "1,2,3", "4,5,\"6\"", "7,8,9"].join(newlines[os]));
+ console.log(" " + rows.map(function(row) {
+ return row.a + ":" + row.b + ":" + row.c;
+ }).join("|"));
+ console.log("");
+}
View
@@ -0,0 +1,9 @@
+parse UNIX newlines:
+ 1:2:3|4:5:6|7:8:9
+
+parse DOS newlines:
+ 1:2:3|4:5:6|7:8:9
+
+parse Mac newlines:
+ 1:2:3|4:5:6|7:8:9
+

0 comments on commit 39a32e1

Please sign in to comment.