IsValidKeyFormat
De DigiWiki.
Function that will check wether a string is correctly formatted to be converted (casted) to a key.
// Check if a string is convertable to a key. // Produced by Logic Scripted Products and Script Services // Flennan Roffo // (c) 2007 - Flennan Roffo, Logic Scripted Products and Script Services integer IsValidKeyFormat(string str) { string keychars = "0123456789abcdef"; if (llStringLength(str) != 36) return FALSE; if ( (llGetSubString( str, 8, 8 ) != "-" || llGetSubString( str, 13, 13 ) != "-" || llGetSubString( str, 18, 18 ) != "-" || llGetSubString( str, 23, 23 ) != "-" ) ) return FALSE; integer i; for (i = 0; i < 8; ++i) { if (llSubStringIndex(keychars, llGetSubString(str,i,i)) == -1) return FALSE; } for (i = 9; i < 13; ++i) { if (llSubStringIndex(keychars, llGetSubString(str,i,i)) == -1) return FALSE; } for (i = 14; i < 18; ++i) { if (llSubStringIndex(keychars, llGetSubString(str,i,i)) == -1) return FALSE; } for (i = 19; i < 23; ++i) { if (llSubStringIndex(keychars, llGetSubString(str,i,i)) == -1) return FALSE; } for (i = 24; i < 36; ++i) { if (llSubStringIndex(keychars, llGetSubString(str,i,i)) == -1) return FALSE; } return TRUE; }
Actually any string is convertable to a key, even if it would result in an invalid key.
You can check if a string is in a valid key format in a faster and byte saving way like this :
integer IsValidKeyFormat(string str) { key str_key = (key)str; if(str_key) { return TRUE; } else return FALSE; //will also return FALSE when NULL_KEY is passed (even the null key is valid formatted too) -one could think of a workaround tough- }
Or this way to take in to account when NULL_KEY is passed:
string null_key = NULL_KEY; integer isKey(key in) {//by: Strife Onizuka if(in) return 2; // key is valid AND not equal NULL_KEY; the distinction is important in some cases (return value of 2 is still evaluated as unary boolean TRUE) return (in == null_key); // key is valid AND equal to NULL_KEY (return 1 or TRUE), or is not valid (return 0 or FALSE) }