llParseString2List

From Second Life Wiki
Revision as of 22:51, 12 September 2007 by Huney Jewell (talk | contribs) (Added description to snippet about how to use)
Jump to navigation Jump to search

Summary

Function: list llParseString2List( string src, list separators, list spacers );

Returns a list that is src broken into a list, discarding separators, keeping spacers, discards any null values generated.

• string src source string
• list separators separators to be discarded
• list spacers spacers to be kept

separators and spacers must be lists of strings, maximum of 8 each.

Caveats

  • Every element in the list will be a string, no matter what you think it should be. Cast the results of llList2String to get what you want.
All Issues ~ Search JIRA for related Bugs

Examples

default
{
    state_entry()
    {
        // This will say:
        // <A><crazy><fox><.><Saw><the><moon><.><.>
        string my_string = "A crazy fox.  Saw the moon..";
        list my_list = llParseString2List(my_string,[" "],["."]);
        llOwnerSay("<" + llDumpList2String(my_list,"><") + ">");
        
        // This will say:
        //  <A><crazy><fox><.><><><Saw><the><moon><.><><.><>
        my_list = llParseStringKeepNulls(my_string,[" "],["."]);
        llOwnerSay("<" + llDumpList2String(my_list,"><") + ">");
    }
}

Useful Snippets


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

Returns a list that is src broken into a list, discarding separators, keeping spacers, discards any null values generated if KeepNulls is set FALSE, keeps any null values generated if set TRUE.

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

Thus substitute a call to the llParseString2List and 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 = spacers != [];
    integer offset;
    integer test;
    list out;
    string p;
    while(i)
        if((p = llList2String(spacers, i = ~-i)))
        {
            if(~(offset = llListFindList(out, (list)p)))
                keys = llListReplaceList(keys, (list)(~i), offset = -~(offset << 1), offset);
            else if(~(test = llSubStringIndex(src, p)))
            {
                keys = test + ((~i) + keys);
                out = p + out;
            }
        }
    for(i = separators != [];i;)
        if((p = llList2String(separators, i = ~-i)))
        {
            if(~(offset = llListFindList(out, (list)p)))
                keys = llListReplaceList(keys, (list)i, offset = -~(offset << 1), offset);
            else if(~(test = llSubStringIndex(src, p)))
            {
                keys = test + (i + keys);
                out = p + out;
            }
        }
    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

Deep Notes

Search JIRA for related Issues

Signature

function list llParseString2List( string src, list separators, list spacers );