Table of Contents

About

The functions presented here encode a list to a CSV string.

Code

LSL

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
string wasListToCSV(list l) {
    list v = [];
    do {
        string a = llDumpList2String(
            llParseStringKeepNulls(
                llList2String(
                    l, 
                    0
                ), 
                ["\""], 
                []
            ),
            "\"\""
        );
        if(llParseStringKeepNulls(
            a, 
            [" ", ",", "\n", "\""], []
            ) != 
            (list) a
        ) a = "\"" + a + "\"";
        v += a;
        l = llDeleteSubList(l, 0, 0);
    } while(l != []);
    return llDumpList2String(v, ",");
}

C#

Compatible

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
/// <summary>
///     Converts a list of string to a comma-separated values string.
/// </summary>
/// <param name="l">a list of strings</param>
/// <returns>a commma-separated list of values</returns>
/// <remarks>compliant with RFC 4180</remarks>
public static string wasEnumerableToCSV(IEnumerable<string> l)
{
    List<string> csv = new List<string>();
    foreach (string s in l)
    {
        List<char> cell = new List<char>();
        foreach (char i in s)
        {
            cell.Add(i);
            switch (!i.Equals('"'))
            {
                case false:
                    cell.Add(i);
                    break;
            }
        }
        switch (!cell.Contains('"') && !cell.Contains(' ') && !cell.Contains(',') && !cell.Contains('\r') &&
            !cell.Contains('\n'))
        {
            case false:
                cell.Insert(0, '"');
                cell.Add('"');
                break;
        }
        csv.Add(new string(cell.ToArray()));
    }
    return string.Join(",", csv.ToArray());
}

Optimised

Slightly optimised to escape the individual cells in parallel.

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
/// <summary>
///     Converts a list of string to a comma-separated values string.
/// </summary>
/// <param name="l">a list of strings</param>
/// <returns>a commma-separated list of values</returns>
/// <remarks>compliant with RFC 4180</remarks>
public static string wasEnumerableToCSV(IEnumerable<string> l)
{
    string[] csv = l.Select(o => o.Clone() as string).ToArray();
    Parallel.ForEach(csv.Select((v, i) => new { i, v }), o =>
    {
        string cell = o.v.Replace("\"", "\"\"");
        switch (new [] { '"', ' ', ',', '\r', '\n'}.Any(p => cell.Contains(p)))
        {
            case true:
                csv[o.i] = "\"" + cell + "\"";
                break;
            default:
                csv[o.i] = cell;
                break;
        }
    });
    return string.Join(",", csv);
}

PHP

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2015 - License: CC BY 2.0      ##
###########################################################################
function wasArrayToCSV($a) {
    return implode(
        ',',
        array_map(
            function($o) {
                $o = str_replace('"', '""', $o);
                switch(
                    (strpos($o, ' ') !== FALSE) ||
                    (strpos($o, '"') !== FALSE) ||
                    (strpos($o, ',') !== FALSE) ||
                    (strpos($o, '\r') !== FALSE) ||
                    (strpos($o, '\n') !== FALSE)
                )
                {
                    case TRUE:
                        return '"' . $o . '"';
                    default:
                        return $o;
                }
            },
            $a
        )
    );
}

JavaScript

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2016 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
function wasArrayToCSV(a) {
    var csv = [];
    for(var i=0; i<a.length; ++i) {
        var cell = a[i].toString().replace('"', '""');
        if(/"\s,\r\n/.test(cell)) {
            csv[i] = '"' + cell + '"';
            continue;
        }
        csv[i] = cell;
    }
    return csv.join();
}