/**
 * csv.js - an experiment in parse CSV content.
 * @author R. S. Doiel, <rsdoiel@gmail.com>
 * copyright (c) 2013 all rights reserved
 * Released under the BSD 2-clause licensed.
 * See: http://opensource.org/licenses/BSD-2-Clause
 */
/*jslint browser: true, indent: 4 */
/*global console, modules */
(function () {
    "use strict";
                
    function parseLine(buf) {
        var delimiter = ',',
            escape = '\\',
            eol = '\n',
            quote = '',
            cut_from = 0,
            cut_length = 0,
            cur = '',
            i = 0,
            l = 0,
            results = [];
        
        function saveColumn(s) {
            var quoted = '', l = s.length - 2;
            s = s.trim();
            if (s[0] === '"' || s[0] === "'") {
                quoted = s[0];
                s = s.substr(1, l).replace(new RegExp(escape + quoted, 'g'), quoted);
            }
            console.log("DEBUG saveColumn(" + s + ")");
            return s;
        }

        buf = buf.trim();
        console.log("DEBUG buf", buf);
        cut_from = 0;
        cut_length = 0;
        for (i = 0, l = buf.length; i < l; i += 1) {
            cur = buf[i];
            console.log([
                "DEBUG i = [",
                i,
                "], cur = [",
                cur,
                "], cut_from = [",
                cut_from,
                "], cut_length = [",
                cut_length,
                "]"].join(""));
            if (cur === escape) {
                console.log("DEBUG escape " + cur + " -> " + buf.substr(i, 3));
                i += 2;
                cut_length += 2;
            } else if (cur === delimiter && quote === '') {
                // save the column's data
                results.push(saveColumn(buf.substr(cut_from, cut_length)));
                cut_from += 1;
                cut_length = 0;
                quote = '';
            } else if (cur === quote) {
                // Ending of a quote
                results.push(saveColumn(buf.substr(cut_from, cut_length)));
                cut_from += 1;
                cut_length = 0;
                quote = '';
            } else if ((cur === '"' || cur === "'") && quote === '') {
                // Starting of a quote
                cut_from += 1;
                cut_length += 1;
                quote = cur;
            } else {
                cut_length += 1;
            }
        }
    
        if (buf.trim().lastIndexOf(delimiter) === (buf.trim().length - 1)) {
            results.push("");
            console.log("DEBUG push trailing empty colomn");
        } else if (cut_length > 0) {
            results.push(buf.substr(cut_from).trim());
            console.log("DEBUG pushing unquoted trailing column");
        }
        return results;
    }
    // Export as module
    modules("CSV", {parseLine: parseLine});
}());

@@ -512,36 +512,8 @@
  0,%0A
-            cut_length = 0,%0A
     
@@ -640,55 +640,8 @@
 ) %7B%0A
-            var quoted = '', l = s.length - 2;%0A
     
@@ -730,22 +730,97 @@
     
-quoted =
+// We have a quoted string so remove/unescape any quotes%0A                // based on
  s%5B0%5D
-;
 %0A   
@@ -848,17 +848,28 @@
 bstr(1, 
-l
+s.length - 2
 ).replac
@@ -904,22 +904,20 @@
 , 'g'), 
-quoted
+s%5B0%5D
 );%0A     
@@ -967,16 +967,17 @@
 eColumn(
+'
 %22 + s + 
@@ -977,16 +977,17 @@
  + s + %22
+'
 )%22);%0A   
@@ -1020,34 +1020,8 @@
  %7D%0A%0A
-        buf = buf.trim();%0A
     
@@ -1067,599 +1067,104 @@
     
-cut_from = 0;%0A        cut_length = 0;%0A        for (i = 0, l = buf.length; i %3C l; i += 1) %7B%0A            cur = buf%5Bi%5D;%0A            console.log(%5B%0A                %22DEBUG i = %5B%22,%0A                i,%0A                %22%5D, cur = %5B%22,%0A                cur,%0A                %22%5D, cut_from = %5B%22,%0A                cut_from,%0A                %22%5D, cut_length = %5B%22,%0A                cut_length,%0A                %22%5D%22%5D.join(%22%22));%0A            if (cur === escape) %7B%0A                console.log(%22DEBUG escape %22 + cur + %22 -%3E %22 + buf.substr(i, 3));%0A                i += 2;%0A                cut_length += 2;%0A            %7D else
+for (i = 0, l = buf.length, cut_from = 0; i %3C l; i += 1) %7B%0A            cur = buf%5Bi%5D;%0A           
  if 
@@ -1300,34 +1300,36 @@
 r(cut_from, 
-cut_length
+i - cut_from
 )));%0A       
@@ -1350,44 +1350,15 @@
 rom 
-+
 = 
-1;%0A                cut_length = 0
+i + 1
 ;%0A  
@@ -1414,20 +1414,21 @@
 cur === 
-quot
+escap
 e) %7B%0A   
@@ -1444,194 +1444,94 @@
     
-// Ending of a quote%0A                results.push(saveColumn(buf.substr(cut_from, cut_length)));%0A                cut_from += 1;%0A                cut_length = 0;%0A                quote = ''
+console.log(%22DEBUG escape %22 + cur + %22 -%3E %22 + buf.substr(i, 3));%0A                i += 2
 ;%0A  
@@ -1658,21 +1658,19 @@
     
-cut_from += 1
+quote = cur
 ;%0A  
@@ -1683,78 +1683,88 @@
     
-    cut_length += 1;%0A                quote = cur;%0A            %7D else %7B
+%7D else if (cur === quote && quote !== %22%22) %7B%0A                // Ending of a quote
 %0A   
@@ -1780,23 +1780,18 @@
     
-cut_length += 1
+quote = ''
 ;%0A  
@@ -1820,176 +1820,30 @@
     
-%0A  
     
-  if (buf.trim().lastIndexOf(delimiter) === (buf.trim().length - 1)) %7B%0A            results.push(%22%22);%0A            console.log(%22DEBUG push trailing empty
+// get last
  col
-o
+u
 mn
-%22);
 %0A   
@@ -1851,23 +1851,16 @@
     
-%7D else 
 if (cut_
 leng
@@ -1859,18 +1859,17 @@
 cut_
-length %3E 0
+from %3C= l
 ) %7B%0A
@@ -1893,16 +1893,27 @@
 ts.push(
+saveColumn(
 buf.subs
@@ -1924,22 +1924,30 @@
 ut_from)
-.trim(
+, l - cut_from
 ));%0A    
@@ -2121,10 +2121,9 @@
 %7D);%0A%7D())
-;
 %0A

Update csv.js