Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Discussion: Some String functions

Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
10-26-2006 13:21
Wrote most of these a while ago but there seems to have been a couple of related posts just recently so here they are for the library.

CODE

//
// String Library functions for LSL.
// Nothing incredibly clever but functions I have found useful.
// free to use and abuse yto your hearts content.
//
string Characters = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
string Letters= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string Numbers = "0123456789";


integer STRINVALID = -99999;
integer STREQUAL = 0;
integer STRLESSTHAN = -1;
integer STRGREATERTHAN = 1;


integer OnlyContains(string a, string b)
{
integer len = llStringLength(a);
integer result = FALSE;
if(len != 0)
{
result = TRUE;
integer index = 0;
do
{
string chara = llGetSubString(a,index,index);
integer posa = llSubStringIndex(b,chara);

if(posa < 0)
{
result = FALSE;
index = len;
}
++index;

}
while(index < len);
}
return result;
}


integer IsNumeric(string a)
{
return OnlyContains(a,Numbers);
}

integer IsAlpha(string a)
{
return OnlyContains(a,Letters);
}

integer IsAlphanumeric(string a)
{
return OnlyContains(a,Letters + Numbers);
}


integer strncmp(string a, string b,integer len)
{
// compares two strings character by character.
// return as soon as there is a difference
integer result = STREQUAL;
if(a != b)
{
integer index = 0;
do
{
string chara = llGetSubString(a,index,index);
string charb = llGetSubString(b,index,index);

integer posa = llSubStringIndex(Characters ,chara);
integer posb = llSubStringIndex(Characters ,charb);

if((posa >= 0) && (posb >= 0))
{
result = posa - posb;
}
else if(posa >= 0)
{
result = STRGREATERTHAN;
}
else if(posb >= 0)
{
result = STRLESSTHAN;
}

if(result != STREQUAL) index = len;
++index;

}
while(index < len);
}

// limit to constant values
if(result < STRLESSTHAN)result = STRLESSTHAN;
else if(result > STRGREATERTHAN)result = STRGREATERTHAN;

return result;
}

integer strnicmp(string a, string b,integer len)
{
string lca = llToLower(a);
string lcb = llToLower(b);
return strncmp(lca,lcb,len);
}

integer strcmp(string a, string b)
{
integer lena = llStringLength(a);
integer lenb = llStringLength(b);
integer result = STRINVALID;
if(lena < lenb)
result = strncmp(a,b,lena);
else
result = strncmp(a,b,lenb);

return result;
}


integer stricmp(string a, string b)
{
integer lena = llStringLength(a);
integer lenb = llStringLength(b);
string lca = llToLower(a);
string lcb = llToLower(b);
integer result = STRINVALID;
if(lena < lenb)
result = strncmp(lca,lcb,lena);
else
result = strncmp(lca,lcb,lenb);

return result;
}

// Some test uses
default
{
state_entry()
{
string a = "ABCDEF[]";
string b = "abcdef[]";
string c = "hello";
string d = "1234";
integer result = strcmp(a,b);
integer result2 = strncmp(a,b,3);
integer result3 = stricmp(a,b);
integer result4 = strnicmp(a,b,4);
integer result5 = IsNumeric(c);
integer result6 = IsAlpha(c);
integer result7 = IsAlphanumeric(c);
integer result8 = IsNumeric(d);
integer result9 = IsAlpha(d);
integer result10 = IsAlphanumeric(d);
llOwnerSay("result = " + (string)result);
llOwnerSay("result2 = " + (string)result2);
llOwnerSay("result3 = " + (string)result3);
llOwnerSay("result4 = " + (string)result4);
llOwnerSay("result5 = " + (string)result5);
llOwnerSay("result6 = " + (string)result6);
llOwnerSay("result7 = " + (string)result7);
llOwnerSay("result8 = " + (string)result8);
llOwnerSay("result9 = " + (string)result9);
llOwnerSay("result10 = " + (string)result10);
}
}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Original Thread
10-27-2006 08:57
/15/d2/145640/1.html
_____________________
i've got nothing. ;)
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
11-02-2006 14:33
May i suggest maybe using these instead? The string != string function is handled by strcmp. Also your functions won't handle UTF8 unicode characters.

You may be interested to have a peak at the functions on LSLWiki: Unicode

CODE

integer strncmp(string a, string b,integer len)
{
return llDeleteSubString(a,len, 0x10000000) != llDeleteSubString(b,len, 0x10000000);
}

integer strnicmp(string a, string b,integer len)
{
return llToLower(llDeleteSubString(a,len, 0x10000000)) != llToLower(llDeleteSubString(b,len, 0x10000000));
}

integer strcmp(string a, string b)
{
return a != b;
}

integer stricmp(string a, string b)
{
return llToLower(a) != llToLower(b);
}
_____________________
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
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
11-02-2006 15:37
more elegant. Thank You Strife.
I think I was being blinkered, ok just being lazy, it sorted the problem I was having and thats all I was worried about at the time.