Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Efficient string trim?

Illya Sullivan
Wench
Join date: 3 Dec 2005
Posts: 61
08-07-2006 07:04
Copying strings around is not efficient and since LSL does provide any kind of

string newString = llTrimString(string oldString);

I was wondering if anyone has a memory efficient method of trimming leading and trailing spaces from a string.

TIA,
Illy
Rodrick Harrington
Registered User
Join date: 9 Jul 2005
Posts: 150
08-07-2006 07:15
not sure how efficient, but looks good ;)

/15/fd/24496/1.html
_____________________
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
08-07-2006 08:38
CODE

string trim_spaces( string Text ) {

integer prefix = 0; while( llGetSubString( Text, prefix, prefix ) == " " ) ++prefix;
if( prefix == llStringLength( Text ) ) return ""; // sanity check if string happens to be all spaces.
integer suffix = -1; while( llGetSubString( Text, suffix, suffix ) == " " ) --suffix;

return llGetSubString( Text, prefix, suffix );
}

This uses ~1/3rd less memory than the list approach, but the code itself takes some ~190 bytes more when compiled and is way *way* slower (250 iterations took some 23 seconds while the list did the same task in ~4 secs) ... so use only for handling long strings in memory-tight environment when speed isn't crucial, i guess o.O;
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
08-07-2006 12:10
Memory is a major issue; i had to replace a trim function in one of my scripts from using a list method to a string method. Fortunately I only needed to trim leading spaces.

We use 0x8000 so it can return a null string the number could be made larger without a proformance hit; just didn't feel the need with the 16k memory space.

The 'do-while' loop is faster then the 'while' because of how the bytecode is writen (i've talked to LL about changing how the 'while' is writen so it would be just as fast; they weren't keen).

CODE

string TrimLead(string a)
{//Trims leading spaces
integer b = -1;
do ++b;while(llGetSubString(a,b,b) == " ");
return llGetSubString(a, b, 0x8000);
}
_____________________
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
Angela Salome
Registered User
Join date: 6 Oct 2005
Posts: 224
08-07-2006 16:20
Would be nice if there was a string trim call in LSL which basically just passed the string to the C string trim function (which I can't recall at present).
Illya Sullivan
Wench
Join date: 3 Dec 2005
Posts: 61
08-08-2006 10:01
From: Strife Onizuka


The 'do-while' loop is faster then the 'while' because of how the bytecode is writen (i've talked to LL about changing how the 'while' is writen so it would be just as fast; they weren't keen).


Interesting Strife, thank you for that! How does a for loop compare in performance?
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
08-08-2006 13:02
a 'for' loop is basicly

CODE

for(a;b;c)
{
d;
}
//
a;
while(b)
{
d;
c;
}
_____________________
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