Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Is String a valid type?

Nyx Alsop
Registered User
Join date: 14 Dec 2008
Posts: 252
12-12-2009 00:02
Is there anyway to check like

If("22"==integer)

if("<22,22,22>"==vector)

thanks.
Ruthven Willenov
Darkness in your light
Join date: 16 Jan 2008
Posts: 965
12-12-2009 00:14
copied from http://lslwiki.net/lslwiki/wakka.php?wakka=vector

CODE
// Returns TRUE if the string is a vector
integer strIsVector(string str) //SL 1.17.2
{
str = llStringTrim(str, STRING_TRIM);//remove whitespace

if(llGetSubString(str, 0, 0) != "<" || llGetSubString(str, -1, -1) != ">")
return FALSE;//make sure angle brackets exist at first and last index

integer commaIndex = llSubStringIndex(str, ",");//position of the first comma

if(commaIndex == -1 || commaIndex == 1)
return FALSE;//if there is no first comma OR if the first comma is right after the first '<' (i.e.: "<,3,3>")

if( !strIsDecimal(llGetSubString(str, 1, commaIndex - 1)) || llGetSubString(str, commaIndex - 1, commaIndex - 1) == " " )
return FALSE;//if the substring inbetween the first '<' and the first comma (exclusive) isn't a decimal OR
//the last character of the substring inbetween the first '<' and the first comma (exclusive) is a space
//note 1: having a space after the substring that corresponds to one of the x, y or z attributes
//causes the (vector) typecast operator to return a zero vector. i.e.: (vector)"<3.0 ,5,9>" == ZERO_VECTOR is TRUE
//on the other hand, (vector)"< 3.0,5,9>" == ZERO_VECTOR would be FALSE. weird, huh? =]
//note 2: the (vector) typecast operator only accepts decimal values and not hexidecimal value.
//for example, (vector)"<0xFE8,4.0,3>" == ZERO_VECTOR is true.

str = llDeleteSubString(str, 1, commaIndex);//get rid of the substring containing the x attribute and the following delimiter (the first comma)

commaIndex = llSubStringIndex(str, ",");//position of the second comma

if(commaIndex == -1 || commaIndex == 1 || commaIndex == llStringLength(str) - 2 ||
//no second comma found OR the second comma is too close to the first comma (i.e.: "<3,,3>") OR the second comma is too close to the '>' OR...
!strIsDecimal(llGetSubString(str, 1, commaIndex - 1)) || llGetSubString(str, commaIndex - 1, commaIndex - 1) == " " ||
//... the substring containing the y attribute isn't a decimal OR the substring containing the y attribute ends in a space OR...
!strIsDecimal(llGetSubString(str, commaIndex + 1, -2)) || llGetSubString(str, -2, -2) == " ")
//... the substring containing the z attribute isn't a decimal OR the subtring containing the z attribute ends in a space
//testing if the substring that should hold the z attribute is a decimal covers having "too many" attributes
//i.e.: "<1,2,3,4>" would be parsed by this function as having "3,4" for the z attribute, which obviously is not a decimal
//and causes the above conditional to evaluate TRUE
return FALSE;

return TRUE;
}

//returns TRUE if the string is a decimal
integer strIsDecimal(string str)
{
str = llStringTrim(str, STRING_TRIM);

integer strLen = llStringLength(str);
if(!strLen){return FALSE;}//added by ruthven willenov 7/30/09 so it would return false when fed an empty string

integer i;
if(llGetSubString(str,0,0) == "-" && strLen > 1)
i = 1;//no need to test for "-": following code will take care of that
else
i = 0;

integer decimalPointFlag = FALSE;

for(; i < strLen; i++)
{
string currentChar = llGetSubString(str, i, i);

if(currentChar == ".")
if(decimalPointFlag)
return FALSE;
else
decimalPointFlag = TRUE;
else if(currentChar != "3" && currentChar != "6" && currentChar != "9" &&
currentChar != "2" && currentChar != "5" && currentChar != "8" && // the order dosen't matter
currentChar != "1" && currentChar != "4" && currentChar != "7" && currentChar != "0")
return FALSE;
}

return TRUE;
}
_____________________
Dark Heart Emporium

http://www.xstreetsl.com/modules.php?name=Marketplace&MerchantID=133020

want more layers for tattoos, specifically for the head? vote here
http://jira.secondlife.com/browse/VWR-1449?

llDetectedCollision* Functions similar to touch
http://jira.secondlife.com/browse/SVC-3369
Pale Spectre
Registered User
Join date: 2 Sep 2005
Posts: 586
12-12-2009 03:46
Here is something a lot more simplistic.

CODE

string chkType(string type, string value)
{
string result = "False";

if (type == "integer" && (string)((integer)value) == value) result = "True";
else if (type == "vector" && (string)((vector)value) != "<0.00000, 0.00000, 0.00000>") result = "True";

return result;
}

default
{
touch_start(integer total_number)
{
string s1;

llOwnerSay("Type Testing...");

s1 = "22";
llOwnerSay(s1 + " as type integer is " + chkType("integer", s1));
llOwnerSay(s1 + " as type vector is " + chkType("vector", s1));

s1 = "<22,22,22>";
llOwnerSay(s1 + " as type integer is " + chkType("integer", s1));
llOwnerSay(s1 + " as type vector is " + chkType("vector", s1));

s1 = "garbage";
llOwnerSay(s1 + " as type integer is " + chkType("integer", s1));
llOwnerSay(s1 + " as type vector is " + chkType("vector", s1));

s1 = "0";
llOwnerSay(s1 + " as type integer is " + chkType("integer", s1));
llOwnerSay(s1 + " as type vector is " + chkType("vector", s1));

// these examples return the wrong result

s1 = "001";
llOwnerSay(s1 + " as type integer is " + chkType("integer", s1));
llOwnerSay(s1 + " as type vector is " + chkType("vector", s1));

s1 = "<0,0,0>";
llOwnerSay(s1 + " as type integer is " + chkType("integer", s1));
llOwnerSay(s1 + " as type vector is " + chkType("vector", s1));
}
}

It does have problems with zero vectors, and integers where there are leading zeros.
_____________________
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
12-12-2009 06:22
or the trully simplistic

if ((vector)vStrTest) //-- actually the same as pale's test above only shorter, neither will accept ZERO_VECTOR as valid though

if ((string)((integer)vStrTest) == vStrTest)

can be simplified to
if ((integer)vStrTest)
but will suffer from converting some invalid strings into valid integers ("12s" becomes 12) and both versions can over/underflow on large integers. (causing either fals negatives, or false positives.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -