OLD | NEW |
---|---|
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 21 matching lines...) Expand all Loading... | |
32 | 32 |
33 | 33 |
34 /** | 34 /** |
35 * Creates a CSV lines parser. | 35 * Creates a CSV lines parser. |
36 */ | 36 */ |
37 devtools.profiler.CsvParser = function() { | 37 devtools.profiler.CsvParser = function() { |
38 }; | 38 }; |
39 | 39 |
40 | 40 |
41 /** | 41 /** |
42 * A regex for matching a trailing quote. | 42 * A regex for matching a CSV field. |
43 * @private | 43 * @private |
44 */ | 44 */ |
45 devtools.profiler.CsvParser.TRAILING_QUOTE_RE_ = /\"$/; | 45 devtools.profiler.CsvParser.CSV_FIELD_RE_ = /^"((?:[^"]|"")*)"|([^,]*)/; |
46 | 46 |
47 | 47 |
48 /** | 48 /** |
49 * A regex for matching a double quote. | 49 * A regex for matching a double quote. |
50 * @private | 50 * @private |
51 */ | 51 */ |
52 devtools.profiler.CsvParser.DOUBLE_QUOTE_RE_ = /\"\"/g; | 52 devtools.profiler.CsvParser.DOUBLE_QUOTE_RE_ = /""/g; |
53 | 53 |
54 | 54 |
55 /** | 55 /** |
56 * Parses a line of CSV-encoded values. Returns an array of fields. | 56 * Parses a line of CSV-encoded values. Returns an array of fields. |
57 * | 57 * |
58 * @param {string} line Input line. | 58 * @param {string} line Input line. |
59 */ | 59 */ |
60 devtools.profiler.CsvParser.prototype.parseLine = function(line) { | 60 devtools.profiler.CsvParser.prototype.parseLine = function(line) { |
61 var insideQuotes = false; | 61 var fieldRe = devtools.profiler.CsvParser.CSV_FIELD_RE_; |
62 var doubleQuoteRe = devtools.profiler.CsvParser.DOUBLE_QUOTE_RE_; | |
63 var pos = 0; | |
64 var endPos = line.length; | |
62 var fields = []; | 65 var fields = []; |
63 var prevPos = 0; | 66 if (endPos > 0) { |
64 for (var i = 0, n = line.length; i < n; ++i) { | 67 do { |
65 switch (line.charAt(i)) { | 68 var fieldMatch = fieldRe.exec(line.substr(pos)); |
66 case ',': | 69 if (typeof fieldMatch[1] === "string") { |
67 if (!insideQuotes) { | 70 var field = fieldMatch[1]; |
68 fields.push(line.substring(prevPos, i)); | 71 pos += field.length + 3; // Skip comma and quotes. |
69 prevPos = i + 1; | 72 fields.push(field.replace(doubleQuoteRe, '"')); |
Søren Thygesen Gjesse
2010/01/29 13:21:34
Is this covered by any tests?
mnaganov (inactive)
2010/01/29 13:31:10
You mean double quotes? Lines 53--63 in test/mjsun
| |
70 } | 73 } else { |
71 break; | 74 // The second field pattern will match anything, thus |
72 case '"': | 75 // in the worst case the match will be an empty string. |
73 if (!insideQuotes) { | 76 var field = fieldMatch[2]; |
74 insideQuotes = true; | 77 pos += field.length + 1; // Skip comma. |
75 // Skip the leading quote. | 78 fields.push(field); |
76 prevPos++; | 79 } |
77 } else { | 80 } while (pos <= endPos); |
78 if (i + 1 < n && line.charAt(i + 1) != '"') { | |
79 insideQuotes = false; | |
80 } else { | |
81 i++; | |
82 } | |
83 } | |
84 break; | |
85 } | |
86 } | |
87 if (n > 0) { | |
88 fields.push(line.substring(prevPos)); | |
89 } | |
90 | |
91 for (i = 0; i < fields.length; ++i) { | |
92 // Eliminate trailing quotes. | |
93 fields[i] = fields[i].replace(devtools.profiler.CsvParser.TRAILING_QUOTE_RE_ , ''); | |
94 // Convert quoted quotes into single ones. | |
95 fields[i] = fields[i].replace(devtools.profiler.CsvParser.DOUBLE_QUOTE_RE_, '"'); | |
96 } | 81 } |
97 return fields; | 82 return fields; |
98 }; | 83 }; |
OLD | NEW |