Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Test for valid key?

Monica Balut
Beam-Me
Join date: 18 Feb 2007
Posts: 311
08-12-2007 12:12
Is there a simple way to test if a given string is in the format of a valid key. I suppose I could write a simple function, but I'm running out of memory in my script and was hoping for a simple low memory use way to check.
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
08-12-2007 14:37
Neat trick from Strife:

http://lslwiki.net/lslwiki/wakka.php?wakka=key

CODE

string null_key = NULL_KEY;

integer isKey(key in)
{//by: Strife Onizuka
if(in) return 2;
return (in == null_key);
}



Properly formatted keys (except NULL_KEY) evaluate as TRUE, and invalid ones and NULL_KEY are FALSE.
Jillian Callahan
Rotary-winged Neko Girl
Join date: 24 Jun 2004
Posts: 3,766
08-12-2007 14:39
From: Masakazu Kojima
Neat trick from Strife:

http://lslwiki.net/lslwiki/wakka.php?wakka=key

CODE

string null_key = NULL_KEY;

integer isKey(key in)
{//by: Strife Onizuka
if(in) return 2;
return (in == null_key);
}



Properly formatted keys (except NULL_KEY) evaluate as TRUE, and invalid ones and NULL_KEY are FALSE.
Oooh. That's a cool trick.
Monica Balut
Beam-Me
Join date: 18 Feb 2007
Posts: 311
08-12-2007 16:17
A couple of things about this.

The essence of it seems to be in the line : if(in) return 2; It's not clear to me why that works. It clearly only evaluates to true if "in" is a valid key and false if anything else is passed, even if the invalid string is typecast as a key. It's as if the if statement is programmed to recognize valid keys all by itself. Guess it's one of those undocumented "features" Hats off to Strife. I would never have thought of this in a million years.

Although returning 2 does no harm, you could also return 1 to stay consistent with the 0=FALSE AND 1=TRUE, convention.

Finally I don't see the need to store null_key = NULL_KEY as a global. The functions seems to work just fine in my tests if you just put return (in == NULL_KEY);
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
08-13-2007 01:09
From: Monica Balut
Although returning 2 does no harm, you could also return 1 to stay consistent with the 0=FALSE AND 1=TRUE, convention.
Zero is false, and non-zero is true. Presumably it returns 2 because it makes the function more flexible; you can use it both as a binary function (isKey, !isKey) or a trinary function (0=invalid key, 1=null key, 2=normal key).

edit to add: This comes up lots of places you might not expect -- for example, the result of ("a" != "b";) is -1, not 1.

From: Monica Balut
Finally I don't see the need to store null_key = NULL_KEY as a global. The functions seems to work just fine in my tests if you just put return (in == NULL_KEY);
It doesn't make a difference functionally. It's there because the example on the page has two functions that use null_key, and Strife is kind of obsessed with micro-optimizations. Every occurrence of NULL_KEY is treated as the literal "00000000-0000-0000-0000-000000000000" by the compiler (ALL constants in LSL work this way), so putting it in a global variable and referencing it twice saves something like 26 bytes over using NULL_KEY twice. If you also use NULL_KEY elsewhere in your script you can save a lot of space by using the global variable instead.
Monica Balut
Beam-Me
Join date: 18 Feb 2007
Posts: 311
08-13-2007 04:59
Thanks. This has been very helpful. Your point about ("a" != "b";) is -1, not 1 is well taken. It suggests that one should not take things for granted in lsl, but should test out what the comparison actually is returning before blindly using it.