StringIsValidKey

From Second Life Wiki
Revision as of 07:22, 3 April 2012 by Zion Tristan (talk | contribs) (Created page with "{{LSL Header}} This User Made Function can be used to identify if a given string is a valid Second Life UUID Key. It is to be used as a Boolean function which will retur…")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This User Made Function can be used to identify if a given string is a valid Second Life UUID Key. It is to be used as a Boolean function which will return TRUE if the string is a valid key, and return FALSE if it is not.
It will not check if the key given actually references an asset, agent/group, instance, or terrain). Checks with other functions and the given key can be used for this.

By definition, a key consists of the 16 hexadecimal characters: 0-9 and a-f. Unlike other Keys, the alphabetical characters are always lowercase.

The process of the function is to test the string using various methods. The first method is to compile the string into a list, using the hyphen (-) as a separator. A key that has been parsed into a list, will have 5 indices. These are of specific lengths, and the function checks these, causing it to fail if one of the lengths are not correct.

If all string segments are of the correct length, it then checks the characters in each segment, failing if a character is not one of the 16 hexadecimal characters.

<lsl> integer StringIsValidKey(string input) {

   //Split the given message into a list, using hyphens as separators.
   list keySegments = llParseString2List(input, ["-"], [""]);
   //Get the number of indices in the list.
   integer length = llGetListLength(keySegments);
   
   if(length != 5) return FALSE; //Keys are 5 segments seperated by hyphens (-)
   
   //Declare a few variables for use later.
   integer i;  integer j;  integer segLength;  string segment;
   //Start a for loop for each segment in the list. This for loop will execute 5 times.
   for(i = 0; i < length; i++) {
       //Assign the string and integer variables with values pertaining to the segment.
       segment = llList2String(keySegments, i);
       segLength = llStringLength(segment);
       
       if(i == 0 && segLength != 8) return FALSE; //Segment 1 must be 8 characters
       if((i > 0 && i < 4) && segLength != 4) return FALSE; //Segments 2-4 must be 4 characters
       if(i == 4 && segLength != 12) return FALSE; //Segment 5 must be 12 characters
       
       //Start a for loop checking each character in the segment. This for loop will execute a maximum of 12 times and minimum of 4 times.
       for(j = 0; j < segLength; j++) {
           string c = llGetSubString(segment, j, j);
           
           //Compare each character in the current segment if they match one of the characters below.
           if(c == "0" || c == "1" || c == "2" || c == "3" || c == "4" ||
              c == "5" || c == "6" || c == "7" || c == "8" || c == "9" ||
              c == "a" || c == "b" || c == "c" || c == "d" || c == "e" ||
              c == "f") { integer value = TRUE; } //Do something. Keeps the compiler happy.
              
           else return FALSE; //Does not contain a hex character
       }
   }
   
   return TRUE; //Must be a key.

}


//Code to check messages typed on all chat by yourself only, if they are a key. default {

   state_entry()
   {
       llListen(0, "", llGetOwner(), "");
   }
   
   listen(integer channel, string name, key id, string message)
   {
       if(StringIsValidKey(message)) llOwnerSay(message + " is a valid key.");
       else if(!StringIsValidKey(message)) llOwnerSay(message + " is NOT a valid key!");
   }

} </lsl>