Table of Contents

About

The following functions retrieve all the elements of a CSV string and place them in a list.

Code

LSL

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
list wasCSVToList(string csv) {
    list l = [];
    list s = [];
    string m = "";
    do {
        string a = llGetSubString(csv, 0, 0);
        csv = llDeleteSubString(csv, 0, 0);
        if(a == "") {
            jump continue;
        }
        if(a == ",") {
            if(llList2String(s, -1) != "\"") {
                l += m;
                m = "";
                jump continue;
            }
            m += a;
            jump continue;
        }
        if(a == "\"" && llGetSubString(csv, 0, 0) == a) {
            m += a;
            csv = llDeleteSubString(csv, 0, 0);
            jump continue;
        }
        if(a == "\"") {
            if(llList2String(s, -1) != a) {
                s += a;
                jump continue;
            }
            s = llDeleteSubList(s, -1, -1);
            jump continue;
        }
        m += a;
@continue;
    } while(csv != "");
    // postcondition: length(s) = 0
    if(llStringLength(m) == 0) {
        return l;
    }
    return l + m;
}

C#

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
/// <summary>
///     Converts a comma-separated list of values to a list of strings.
/// </summary>
/// <param name="csv">a comma-separated list of values</param>
/// <returns>a list of strings</returns>
/// <remarks>compliant with RFC 4180</remarks>
public static IEnumerable<string> wasCSVToEnumerable(string csv)
{
    Stack<char> s = new Stack<char>();
    StringBuilder m = new StringBuilder();
    for (int i = 0; i < csv.Length; ++i)
    {
        switch (csv[i])
        {
            case ',':
                if (s.Count.Equals(0) || !s.Peek().Equals('"'))
                {
                    yield return m.ToString();
                    m = new StringBuilder();
                    continue;
                }
                m.Append(csv[i]);
                continue;
            case '"':
                if (i + 1 < csv.Length && csv[i].Equals(csv[i + 1]))
                {
                    m.Append(csv[i]);
                    ++i;
                    continue;
                }
                if (s.Count.Equals(0) || !s.Peek().Equals(csv[i]))
                {
                    s.Push(csv[i]);
                    continue;
                }
                s.Pop();
                continue;
        }
        m.Append(csv[i]);
    }
 
    yield return m.ToString();
}

PHP

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2015 - License: CC BY 2.0      ##
###########################################################################
function wasCSVToArray($csv) {
    $l = array();
    $s = array();
    $m = "";
    for ($i = 0; $i < strlen($csv); ++$i) {
        switch ($csv{$i}) {
            case ',':
                if (sizeof($s) == 0 || !current($s) == '"') {
                    array_push($l, $m);
                    $m = "";
                    break;
                }
                $m .= $csv{$i};
                continue;
            case '"':
                if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) {
                    $m .= $csv{$i};
                    ++$i;
                    break;
                }
                if (sizeof($s) == 0|| !current($s) == $csv[$i]) {
                    array_push($s, $csv{$i});
                    continue;
                }
                array_pop($s);
                break;
            default:
                $m .= $csv{$i};
                break;
        }
    }
    array_push($l, $m);
    return $l;
}

JavaScript

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2016 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
function wasCSVToArray(csv) {
    var l = [];
    var s = [];
    var m = "";
 
    do {
        var a = csv.charAt(0);
        csv = csv.slice(1, csv.length);
        if(a == ",") {
            if(s[s.length-1] != '"') {
                l.push(m);
                m = "";
                continue;
            }
            m += a;
            continue;
        }
        if(a == '"' && csv.charAt(0) == a) {
            m += a;
            csv = csv.slice(1, csv.length);
            continue;
        }
        if(a == '"') {
            if(s[s.length-1] != a) {
                s.push(a);
                continue;
            }
            s.pop();
            continue;
        }
        m += a;
    } while(csv != "");
 
    l.push(m);
 
    return l;
}

fuss/data_structures/comma_separated_values/list/to.txt ยท Last modified: 2022/11/16 01:40 by office

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.