Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

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
Discussion Thread
10-27-2006 08:57
/54/7d/145796/1.html
_____________________
i've got nothing. ;)