Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Loathing llParseStringKeepNulls limitations...

Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
03-09-2005 15:30
Hey all, Ive just run into llParseString's 8-spacer limitation, so I wrote a function to overcome that. The problem is that my function is about 21 times slower then the ll function.

Are there any optimizations I can perform?
CODE

list parseStringKeepNulls(string src, list seperators, list spacers) {
list ret;
list delimeters = seperators + spacers;
integer len = llGetListLength(delimeters);
integer delimeterFound = FALSE; // TRUE when a delimeter exists in src.
do {
delimeterFound = FALSE;
integer closestIndex = -1;
string closestDelimeter = "";
integer i;
for (i = 0; i < len; i++) {
string delimeter = llList2String(delimeters, i);
integer index = llSubStringIndex(src, delimeter);
integer found = index != -1;
if (found) {
if (closestIndex > index || closestIndex == -1) {
closestIndex = index;
closestDelimeter = delimeter;
}
}
// If this delimeter was found,
// it marks delimeterFound as TRUE
// if not, it leaves it alone.
delimeterFound = delimeterFound || found;
}
if (closestIndex != -1) {
string element;
if (closestIndex > 0)
element = llGetSubString(src, 0, closestIndex - 1);
ret += element;
if (llListFindList(spacers, [closestDelimeter]) != -1)
ret += closestDelimeter;
src = llDeleteSubString(src, 0, closestIndex + llStringLength(closestDelimeter) - 1);
}
} while (delimeterFound);
ret += src;
return ret;
}

==Chris :confused:
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
03-09-2005 17:13
Answer sent in world... :D
_____________________
---
Dreamstalker Xevious
Registered User
Join date: 28 May 2004
Posts: 29
03-09-2005 18:12
Why not post it here so we can all enjoy the answer. ;)
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
03-09-2005 18:39
If you insist. :rolleyes:

CODE
list BetterParseStringKeepNulls(string src, list separators, list spacers)
{
integer sep_num = llGetListLength(separators);
integer spa_num = llGetListLength(spacers);

integer top;
if(sep_num >= spa_num) top = sep_num;
else top = spa_num;

integer i;
integer j;
integer k;
for(i = 0; i < top; i += 8)
{
j = i + 8;
k = j;
if(j > sep_num) j = sep_num - 1;
if(k > spa_num) k = spa_num - 1;
list test = llParseStringKeepNulls(src,llList2List(separators,i,j),llList2List(spacers,i,k));
src = llDumpList2String(test,"~!");
}
return llParseStringKeepNulls(src,["~!"],[]);
}

string stuffit = "abcdefghijklmnopqrstuvwxyz12345";
list sep = ["b","d","f","h","j","l","n","p","r","t","v","x","z"];
list space = ["a","c","e","g","i","k","m","o","q","s","u","w","y"];

default
{
state_entry()
{
list test = BetterParseStringKeepNulls(stuffit,sep,space);
integer length = llGetListLength(test);
integer i;
for(i = 0; i < length; i++)
llWhisper(0,llList2String(test,i));
}
}
_____________________
---
Francis Chung
This sentence no verb.
Join date: 22 Sep 2003
Posts: 918
03-09-2005 21:52
Good stuff Jeffrey :) I was going to suggest something similar.

Small suggestion, though:
The sample you posted wouldn't treat strings with "~!" contained in them properly.

If you chose your "magic separator" from the caller-supplied list, you could handle all cases with no loss of generality :)
_____________________
--
~If you lived here, you would be home by now~
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
03-10-2005 08:05
problem solved. Instead of replacing ever separator with a hard coded one, we replace them with the first unique seperator it finds or the last seperator.

CODE

list BetterParseStringKeepNulls(string src, list separators, list spacers)
{
integer sep_num = llGetListLength(separators);
integer spa_num;
while(1 + llSubStringIndex((string)llList2List(separators, spa_num + 1, -1), llList2String(separators,spa_num)) && spa_num + 1 < sep_num)
spa_num ++;
string sep = llList2String(separators,spa_num);
spa_num = llGetListLength(spacers);

integer top;
if(sep_num >= spa_num) top = sep_num;
else top = spa_num;

integer i;
integer j;
integer k;
for(i = 0; i < top; i += 8)
{
j = i + 8;
k = j;
if(j > sep_num) j = sep_num - 1;
if(k > spa_num) k = spa_num - 1;
list test = llParseStringKeepNulls(src,llList2List(separators, i,j),llList2List(spacers,i,k));
src = llDumpList2String(test,sep);
}
return llParseStringKeepNulls(src,[sep],[]);
}

string stuffit = "abcdefghijklmnopqrstuvwxyz12345";
list sep = ["b","d","f","h","j","l","n","p","r","t","v","x","z"];
list space = ["a","c","e","g","i","k","m","o","q","s","u","w","y"];

default
{
state_entry()
{
list test = BetterParseStringKeepNulls(stuffit,sep,space);
integer length = llGetListLength(test);
integer i;
for(i = 0; i < length; i++)
llWhisper(0,llList2String(test,i));
}
}
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
03-11-2005 10:31
Thanks Jeffrey :) Ive made a few modifications:
CODE

integer max(integer a, integer b) {
if (a > b)
return a;
return b;
}

list parseStringKeepNulls(string src, list separators, list spacers) {
integer sep_num = llGetListLength(separators);
integer spa_num = llGetListLength(spacers);
string separator;
if (sep_num > 0) {
separator = llList2String(separators, 0);
} else {
// Generate a random string:
do {
seperator = (string) llFrand(1822901);
} while (llSubStringIndex(src, seperator) != -1);
}
integer top = max(sep_num, spa_num);
integer i;
integer j;
integer k;
for(i = 0; i < top; i += 8) {
j = i + 8;
k = j;
if(j > sep_num) j = sep_num - 1;
if(k > spa_num) k = spa_num - 1;
list test = llParseStringKeepNulls(src, llList2List(separators, i, j), llList2List(spacers, i, k));
src = llDumpList2String(test, separator);
}
return llParseStringKeepNulls(src, [separator], []);
}