Difference between revisions of "ParseString2List"

From Second Life Wiki
Jump to navigation Jump to search
(New page: <div id="box"> == Function: list ParseString2List(string {{LSL Param|src}}, list {{LSL Param|separators}}, list {{LSL Param|spacers}}, integer {{LSL Param|ParseStringKe...)
 
Line 1: Line 1:
<div id="box">
<div id="box">__NOTOC__
== Function: [[list]] ParseString2List([[string]] {{LSL Param|src}}, [[list]] {{LSL Param|separators}}, [[list]] {{LSL Param|spacers}}, [[integer]] {{LSL Param|ParseStringKeepNulls}}); ==
== Function: [[list]] ParseString2List([[string]] {{LSL Param|src}}, [[list]] {{LSL Param|separators}}, [[list]] {{LSL Param|spacers}}, [[integer]] {{LSL Param|ParseStringKeepNulls}}); ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
Line 19: Line 19:
  //the limits on spacers or separators
  //the limits on spacers or separators
     list keys;
     list keys;
     integer i = spacers != [];
     integer i = ~(separators != []);
     integer offset;
     integer offset;
     integer test;
     integer test;
     list out;
     list out = [""];
     string p;
     string p;
     while(i)
     while(i = -~i)
         if((p = llList2String(spacers, i = ~-i)))
         if(!~llListFindList(out, (list)(p = llList2String(separators, i))))
        {
             if(~(test = llSubStringIndex(src, p)))
             if(~(test = llSubStringIndex(src, p)))
             {
             {
                 if(~(offset = llListFindList(out, (list)p)))
                 keys = (keys + test) + ~i;
                {
                 out += p;
                    out = llDeleteSubList(out, offset, offset);
                    keys = llDeleteSubList(keys, -~(offset << 1), (-~offset) << 1);
                }
                keys = test + ((~i) + keys);
                 out = p + out;
             }
             }
        }
     for(i = ~(spacers != []);i = -~i;)
     for(i = separators != [];i;)
         if(!~llListFindList(out, (list)(p = llList2String(spacers, i))))
         if((p = llList2String(separators, i = ~-i)))
        {
             if(~(test = llSubStringIndex(src, p)))
             if(~(test = llSubStringIndex(src, p)))
             {
             {
                 if(~(offset = llListFindList(out, (list)p)))
                 keys = (keys + test) + i;
                {
                 out += p;
                    out = llDeleteSubList(out, offset, offset);
                    keys = llDeleteSubList(keys, -~(offset << 1), (-~offset) << 1);
                }
                keys = test + (i + keys);
                 out = p + out;
             }
             }
        }
     out = [];
     out = [];
     offset = 0;
     offset = 0;
Line 65: Line 51:
                 out += llDeleteSubString(src, i - offset, -1);
                 out += llDeleteSubString(src, i - offset, -1);
             if(0x80000000 & test = llList2Integer(t, 1))
             if(0x80000000 & test = llList2Integer(t, 1))
                 out += p = llList2String(spacers, ~test);
                 out += p = llList2String(spacers, test);
             else
             else
                 p = llList2String(separators, test);
                 p = llList2String(separators, ~test);
             src = llDeleteSubString(src, 0, ~(offset - (i += llStringLength(p))));
             src = llDeleteSubString(src, 0, ~(offset - (i += llStringLength(p))));
             offset = i;
             offset = i;
Line 80: Line 66:
     return out;
     return out;
}//Strife Onizuka
}//Strife Onizuka
</pre></div></div>
</pre>
<noinclude>
<pre>
//Use for testing the function.
string test(string src, list separators, list spacers, integer nulls)
{
    list t = [];
    if(nulls)
        t = llParseStringKeepNulls(src, separators, spacers);
    else
        t = llParseString2List(src, separators, spacers);
    string a = llList2CSV(t);
    string b = llList2CSV(ParseString2List(src, separators, spacers, nulls));
    return (string)(a==b) + " : " + a + "            " + b;
}
 
default
{
    state_entry()
    {
        llOwnerSay("----------------  " + (string)llGetFreeMemory());
        llOwnerSay(test("abcdefg", ["b"], ["b"], FALSE));
        llOwnerSay(test("abcdefg", ["b"], ["bc"], FALSE));
        llOwnerSay(test("abcdefg", ["bc"], ["b"], FALSE));
        llOwnerSay(test("abcdefg", ["b"], ["ab"], FALSE));
        llOwnerSay(test("abcdefg", ["b", "g"], ["ab"], FALSE));
        llOwnerSay(test("abcdefg", ["b"], ["ab", "g"], FALSE));
        llOwnerSay(test("abcdefg", ["b", "a"], ["a", "b"], FALSE));
        llOwnerSay(test("abcdefg", ["a", "b"], ["b", "a"], FALSE));
        llOwnerSay(test("abcdefg", ["b", "c"], ["a", "b"], FALSE));
        llOwnerSay(test("abcdefg", ["c", "b"], ["b", "a"], FALSE));
        llOwnerSay(test("abcdefg", ["b", "a"], ["c", "b"], FALSE));
        llOwnerSay(test("abcdefg", ["a", "b"], ["b", "c"], FALSE));
        llOwnerSay(test("abcdefg", ["b"], ["b"], TRUE));
        llOwnerSay(test("abcdefg", ["b"], ["bc"], TRUE));
        llOwnerSay(test("abcdefg", ["bc"], ["b"], TRUE));
        llOwnerSay(test("abcdefg", ["b"], ["ab"], TRUE));
        llOwnerSay(test("abcdefg", ["b", "g"], ["ab"], TRUE));
        llOwnerSay(test("abcdefg", ["b"], ["ab", "g"], TRUE));
        llOwnerSay("----------------  " + (string)llGetFreeMemory());
    }
}</pre>
</noinclude></div></div>

Revision as of 01:53, 18 September 2007

Function: list ParseString2List(string src, list separators, list spacers, integer ParseStringKeepNulls);

Returns a list that is src broken into a list, discarding separators, keeping spacers.

if ParseStringKeepNulls == FALSE

Same as llParseString2List, but not limited to 8 spacers or separators.

Thus substitute a call to the llParseString2List function by a call to ParseString2List whenever you have more than 8 separators or more than 8 spacers.

if ParseStringKeepNulls != FALSE

Same as llParseStringKeepNulls, but not limited to 8 spacers or separators.

Thus substitute a call to the llParseStringKeepNulls function by a call to ParseString2List whenever you have more than 8 separators or more than 8 spacers.

list ParseString2List(string src, list separators, list spacers, integer ParseStringKeepNulls)
{//works just like llParseString2List and llParseStringKeepNulls but without 
 //the limits on spacers or separators
    list keys;
    integer i = ~(separators != []);
    integer offset;
    integer test;
    list out = [""];
    string p;
    while(i = -~i)
        if(!~llListFindList(out, (list)(p = llList2String(separators, i))))
            if(~(test = llSubStringIndex(src, p)))
            {
                keys = (keys + test) + ~i;
                out += p;
            }
    for(i = ~(spacers != []);i = -~i;)
        if(!~llListFindList(out, (list)(p = llList2String(spacers, i))))
            if(~(test = llSubStringIndex(src, p)))
            {
                keys = (keys + test) + i;
                out += p;
            }
    out = [];
    offset = 0;
    while(keys)
    {
        list t = llList2List(llListSort(keys, 2, TRUE), 0, 1);
        integer r = llListFindList(keys, t);
        if((i = llList2Integer(t, 0)) < offset)
            keys = llDeleteSubList(keys, r, -~r);
        else
        {
            if(offset != i || ParseStringKeepNulls)
                out += llDeleteSubString(src, i - offset, -1);
            if(0x80000000 & test = llList2Integer(t, 1))
                out += p = llList2String(spacers, test);
            else
                p = llList2String(separators, ~test);
            src = llDeleteSubString(src, 0, ~(offset - (i += llStringLength(p))));
            offset = i;
            if(~(test = llSubStringIndex(src, p)))
                keys = llListReplaceList(keys, (list)(test + offset), r, r);
            else
                keys = llDeleteSubList(keys, r, -~r);
        }
    }
    if(src != "" || ParseStringKeepNulls)
        return out + src;
    return out;
}//Strife Onizuka
//Use for testing the function.
string test(string src, list separators, list spacers, integer nulls)
{
    list t = [];
    if(nulls)
        t = llParseStringKeepNulls(src, separators, spacers);
    else
        t = llParseString2List(src, separators, spacers);
    string a = llList2CSV(t);
    string b = llList2CSV(ParseString2List(src, separators, spacers, nulls));
    return (string)(a==b) + " : " + a + "            " + b;
}

default
{
    state_entry()
    {
        llOwnerSay("----------------  " + (string)llGetFreeMemory());
        llOwnerSay(test("abcdefg", ["b"], ["b"], FALSE));
        llOwnerSay(test("abcdefg", ["b"], ["bc"], FALSE));
        llOwnerSay(test("abcdefg", ["bc"], ["b"], FALSE));
        llOwnerSay(test("abcdefg", ["b"], ["ab"], FALSE));
        llOwnerSay(test("abcdefg", ["b", "g"], ["ab"], FALSE));
        llOwnerSay(test("abcdefg", ["b"], ["ab", "g"], FALSE));
        llOwnerSay(test("abcdefg", ["b", "a"], ["a", "b"], FALSE));
        llOwnerSay(test("abcdefg", ["a", "b"], ["b", "a"], FALSE));
        llOwnerSay(test("abcdefg", ["b", "c"], ["a", "b"], FALSE));
        llOwnerSay(test("abcdefg", ["c", "b"], ["b", "a"], FALSE));
        llOwnerSay(test("abcdefg", ["b", "a"], ["c", "b"], FALSE));
        llOwnerSay(test("abcdefg", ["a", "b"], ["b", "c"], FALSE));
        llOwnerSay(test("abcdefg", ["b"], ["b"], TRUE));
        llOwnerSay(test("abcdefg", ["b"], ["bc"], TRUE));
        llOwnerSay(test("abcdefg", ["bc"], ["b"], TRUE));
        llOwnerSay(test("abcdefg", ["b"], ["ab"], TRUE));
        llOwnerSay(test("abcdefg", ["b", "g"], ["ab"], TRUE));
        llOwnerSay(test("abcdefg", ["b"], ["ab", "g"], TRUE));
        llOwnerSay("----------------  " + (string)llGetFreeMemory());
    }
}