Difference between revisions of "Combined Library"

From Second Life Wiki
Jump to navigation Jump to search
(New page: The Combined Library is comprised of about 40 functions all of which are released under [http://creativecommons.org/licenses/by/3.0/ CC-by v3.0 license]. The library is still be worked on...)
 
Line 1: Line 1:
The Combined Library is comprised of about 40 functions all of which are released under [http://creativecommons.org/licenses/by/3.0/ CC-by v3.0 license].
<div style="float:right;">__TOC__</div>The Combined Library is comprised of about 40 functions all of which are released under [http://creativecommons.org/licenses/by/3.0/ CC-by v3.0 license].


The library is still be worked on so only some of the compiled functions will be posted at this time
The library is still be worked on so only some of the compiled functions will be posted at this time
== str_replace & list_replace ==
The logic of these two functions work in exactly the same way. The design of the logic had to overcome two hurdles. The first was keeping it from searching previous replacements (otherwise you could fall into an infinite loop or infinitely grow the memory). The second was so that it could do null replacements. Both of these hurdles were overcome but at the cost of some readability.
The way it works is it keeps an Unsearched Buffer (UB) which is a subset of the Input Buffer (IB) and it records the position of the UB in the IB. Each iteration it searches the UB and adds that resulting index to the position of the start of the UB, then it uses that as the index to replace that section in the IB, finally it recalculates the new start position of the UB and updates the UB.
Both functions are hand optimized.


=== str_replace ===
=== str_replace ===
<pre>
<pre>
string str_replace(string src, string from, string to)
string str_replace(string src, string from, string to)
{//replaces all occurances of 'from' with 'to' in 'src'.
{//replaces all occurrences of 'from' with 'to' in 'src'.
     integer len = (~-(llStringLength(from)));
     integer len = (~-(llStringLength(from)));
     if(~len)
     if(~len)
Line 32: Line 39:
<pre>
<pre>
list list_replace(list src, list from, list to)
list list_replace(list src, list from, list to)
{//replaces all occurances of 'from' with 'to' in 'src'.
{//replaces all occurrences of 'from' with 'to' in 'src'.
     integer len = ~([] != from);
     integer len = ~([] != from);
     if(~len)
     if(~len)
Line 43: Line 50:
         if(to_pos)
         if(to_pos)
         {
         {
//            b_pos-=to_pos;
//            b_pos -= to_pos;
//            src = llListReplaceList(src, to, b_pos, b_pos+len);
//            src = llListReplaceList(src, to, b_pos, b_pos + len);
//            b_pos += to_len;
//            b_pos += to_len;
//            buffer = llList2List(src, (-~(b_pos)), 0x4000);
//            buffer = llList2List(src, (-~(b_pos)), 0x4000);
             buffer = llList2List(src = llListReplaceList(src, to, b_pos-=to_pos, b_pos+len), (-~(b_pos += to_len)), 0x4000);
             buffer = llList2List(src = llListReplaceList(src, to, b_pos -= to_pos, b_pos + len), (-~(b_pos += to_len)), 0x4000);
             jump loop;
             jump loop;
         }
         }

Revision as of 05:52, 9 August 2007

The Combined Library is comprised of about 40 functions all of which are released under CC-by v3.0 license.

The library is still be worked on so only some of the compiled functions will be posted at this time

str_replace & list_replace

The logic of these two functions work in exactly the same way. The design of the logic had to overcome two hurdles. The first was keeping it from searching previous replacements (otherwise you could fall into an infinite loop or infinitely grow the memory). The second was so that it could do null replacements. Both of these hurdles were overcome but at the cost of some readability.

The way it works is it keeps an Unsearched Buffer (UB) which is a subset of the Input Buffer (IB) and it records the position of the UB in the IB. Each iteration it searches the UB and adds that resulting index to the position of the start of the UB, then it uses that as the index to replace that section in the IB, finally it recalculates the new start position of the UB and updates the UB.

Both functions are hand optimized.

str_replace

string str_replace(string src, string from, string to)
{//replaces all occurrences of 'from' with 'to' in 'src'.
    integer len = (~-(llStringLength(from)));
    if(~len)
    {
        string  buffer = src;
        integer b_pos = -1;
        integer to_len = (~-(llStringLength(to)));
        @loop;//instead of a while loop, saves 5 bytes (and run faster).
        integer to_pos = ~llSubStringIndex(buffer, from);
        if(to_pos)
        {
//            b_pos -= to_pos;
//            src = llInsertString(llDeleteSubString(src, b_pos, b_pos + len), b_pos, to);
//            b_pos += to_len;
//            buffer = llGetSubString(src, (-~(b_pos)), 0x8000);
            buffer = llGetSubString(src = llInsertString(llDeleteSubString(src, b_pos -= to_pos, b_pos + len), b_pos, to), (-~(b_pos += to_len)), 0x8000);
            jump loop;
        }
    }
    return src;
}

list_replace

list list_replace(list src, list from, list to)
{//replaces all occurrences of 'from' with 'to' in 'src'.
    integer len = ~([] != from);
    if(~len)
    {
        list  buffer = src;
        integer b_pos = -1;
        integer to_len = ~([] != to);
        @loop;//instead of a while loop, saves 5 bytes (and run faster).
        integer to_pos = ~llListFindList(buffer, from);
        if(to_pos)
        {
//            b_pos -= to_pos;
//            src = llListReplaceList(src, to, b_pos, b_pos + len);
//            b_pos += to_len;
//            buffer = llList2List(src, (-~(b_pos)), 0x4000);
            buffer = llList2List(src = llListReplaceList(src, to, b_pos -= to_pos, b_pos + len), (-~(b_pos += to_len)), 0x4000);
            jump loop;
        }
    }
    return src;
}