• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29// Initlialize namespaces.
30var devtools = devtools || {};
31devtools.profiler = devtools.profiler || {};
32
33
34/**
35 * Creates a CSV lines parser.
36 */
37devtools.profiler.CsvParser = function() {
38};
39
40
41/**
42 * A regex for matching a trailing quote.
43 * @private
44 */
45devtools.profiler.CsvParser.TRAILING_QUOTE_RE_ = /\"$/;
46
47
48/**
49 * A regex for matching a double quote.
50 * @private
51 */
52devtools.profiler.CsvParser.DOUBLE_QUOTE_RE_ = /\"\"/g;
53
54
55/**
56 * Parses a line of CSV-encoded values. Returns an array of fields.
57 *
58 * @param {string} line Input line.
59 */
60devtools.profiler.CsvParser.prototype.parseLine = function(line) {
61  var insideQuotes = false;
62  var fields = [];
63  var prevPos = 0;
64  for (var i = 0, n = line.length; i < n; ++i) {
65    switch (line.charAt(i)) {
66      case ',':
67        if (!insideQuotes) {
68          fields.push(line.substring(prevPos, i));
69          prevPos = i + 1;
70        }
71        break;
72      case '"':
73        if (!insideQuotes) {
74          insideQuotes = true;
75          // Skip the leading quote.
76          prevPos++;
77        } else {
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  }
97  return fields;
98};
99