Revision: 3746
Author: [email protected]
Date: Fri Jan 29 05:32:32 2010
Log: Rewrite CsvParser.parseLine to make it simpler and gain some performance improvement.

Review URL: http://codereview.chromium.org/548198
http://code.google.com/p/v8/source/detail?r=3746

Modified:
 /branches/bleeding_edge/test/mjsunit/tools/csvparser.js
 /branches/bleeding_edge/tools/csvparser.js

=======================================
--- /branches/bleeding_edge/test/mjsunit/tools/csvparser.js Fri Apr 17 10:58:14 2009 +++ /branches/bleeding_edge/test/mjsunit/tools/csvparser.js Fri Jan 29 05:32:32 2010
@@ -77,3 +77,7 @@
 assertEquals(
     ['code-creation','RegExp','0xf6c21c00','826','NccyrJroXvg\\/([^,]*)'],
parser.parseLine('code-creation,RegExp,0xf6c21c00,826,"NccyrJroXvg\\/([^,]*)"'));
+
+assertEquals(
+    ['code-creation','Function','0x42f0a0','163',''],
+    parser.parseLine('code-creation,Function,0x42f0a0,163,""'));
=======================================
--- /branches/bleeding_edge/tools/csvparser.js  Fri Apr 17 10:58:14 2009
+++ /branches/bleeding_edge/tools/csvparser.js  Fri Jan 29 05:32:32 2010
@@ -39,17 +39,17 @@


 /**
- * A regex for matching a trailing quote.
+ * A regex for matching a CSV field.
  * @private
  */
-devtools.profiler.CsvParser.TRAILING_QUOTE_RE_ = /\"$/;
+devtools.profiler.CsvParser.CSV_FIELD_RE_ = /^"((?:[^"]|"")*)"|([^,]*)/;


 /**
  * A regex for matching a double quote.
  * @private
  */
-devtools.profiler.CsvParser.DOUBLE_QUOTE_RE_ = /\"\"/g;
+devtools.profiler.CsvParser.DOUBLE_QUOTE_RE_ = /""/g;


 /**
@@ -58,41 +58,26 @@
  * @param {string} line Input line.
  */
 devtools.profiler.CsvParser.prototype.parseLine = function(line) {
-  var insideQuotes = false;
+  var fieldRe = devtools.profiler.CsvParser.CSV_FIELD_RE_;
+  var doubleQuoteRe = devtools.profiler.CsvParser.DOUBLE_QUOTE_RE_;
+  var pos = 0;
+  var endPos = line.length;
   var fields = [];
-  var prevPos = 0;
-  for (var i = 0, n = line.length; i < n; ++i) {
-    switch (line.charAt(i)) {
-      case ',':
-        if (!insideQuotes) {
-          fields.push(line.substring(prevPos, i));
-          prevPos = i + 1;
-        }
-        break;
-      case '"':
-        if (!insideQuotes) {
-          insideQuotes = true;
-          // Skip the leading quote.
-          prevPos++;
-        } else {
-          if (i + 1 < n && line.charAt(i + 1) != '"') {
-            insideQuotes = false;
-          } else {
-            i++;
-          }
-        }
-        break;
-    }
-  }
-  if (n > 0) {
-    fields.push(line.substring(prevPos));
-  }
-
-  for (i = 0; i < fields.length; ++i) {
-    // Eliminate trailing quotes.
- fields[i] = fields[i].replace(devtools.profiler.CsvParser.TRAILING_QUOTE_RE_, '');
-    // Convert quoted quotes into single ones.
- fields[i] = fields[i].replace(devtools.profiler.CsvParser.DOUBLE_QUOTE_RE_, '"');
+  if (endPos > 0) {
+    do {
+      var fieldMatch = fieldRe.exec(line.substr(pos));
+      if (typeof fieldMatch[1] === "string") {
+        var field = fieldMatch[1];
+        pos += field.length + 3;  // Skip comma and quotes.
+        fields.push(field.replace(doubleQuoteRe, '"'));
+      } else {
+        // The second field pattern will match anything, thus
+        // in the worst case the match will be an empty string.
+        var field = fieldMatch[2];
+        pos += field.length + 1;  // Skip comma.
+        fields.push(field);
+      }
+    } while (pos <= endPos);
   }
   return fields;
 };

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to