Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

RLib - A Developer Library

Hitani Fugazi
Registered User
Join date: 14 Jun 2006
Posts: 7
06-21-2006 14:59
NOTE: As used here, 'Developer Library' is a collection of general purpose functions commonly used by developers

RLib 2.0 - A LSL Developer Library
Created by Hitani Fugazi


RLib is designed to make your lives easier, by allowing you to write in LSL logically, without having to stress over unimportant, yet essential details. Simply copy the RLib source to the top of any script you wish to use it in, and you will be able to use all of these functions with ease. RLib has been written carefully, taking every possible precaution to make sure that it is functional, flexible, and will not clutter your code.

Questions, comments, and concerns to:
[email]instinkt.aka.ink@gmail.com[/email]


Function List

string rl_GetFirstName ( key id )
Retrieves the first name of an avatar

Parameters:
  1. id - key of the avatar

Returns:
  1. The first name of the avatar specified, as a string




string rl_GetLastName ( key id )
Retrievs the last name of an avatar

Parameters:
  1. id - key of the avatar

Returns:
  1. The last name of the avatar specified, as a string




string rl_Replace ( string str, string pattern, string alternative )
Replaces a pattern in a string with an alternative pattern

Parameters:
  1. str - The string
  2. pattern - The pattern to find
  3. alternative - The string to replace pattern with

Returns:
  1. The string with pattern replaced by alternative

Example:
  1. rl_Replace( "the bar", "the", "foo" ) = "foo bar"
  2. rl_Replace( "Welcome, come right in!", "come", "fly" ) = "Welfly, fly right in!"




integer rl_RandomInteger ( integer min, integer max )
Retrieves a random integer between min and max, inclusively

Parameters:
  1. min - The minimum value of the integer
  2. max - The maximum value of the integer

Returns:
  1. A random integer from min to max




list rl_Int2Bin ( integer num )
Converts a signed 32-bit integer into a list of integers representing each bit

Parameters:
  1. num - An integer to convert

Returns:
  1. A list of 'bits' ( integers with a value of 1 or 0 )

Example:
  1. rl_Int2Bin( 32 ) = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  2. rl_Int2Bin( 21474836487 ) = [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Notes:
  1. Signed integers are not supported by LSL. Becuase of this, num has a maximum value of 21474836487
  2. Although the maximum value uses only 31 bits, to prevent incompatibility with future updates, the list returned by this function will always have a length of 32
  3. An integer with a value of 0 is FALSE
  4. An integer with a value of 1 is TRUE
  5. 'if ( a == TRUE )' is the same as 'if ( a )'




integer rl_Bin2Int ( list bin )
Converts a list of bits into a signed integer

Parameters:
  1. bin - A list of bits, either TRUE ( 1 ) or FALSE ( 0 )

Returns:
  1. An integer represented by the bits provided

Example:
  1. rl_Bin2Int( [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] ) = 32
  2. rl_Bin2Int( [1, 0, 0, 0, 0, 0] ) = 32

Notes:
  1. The list bin does not have to have a length of 32, any extra FALSE values proceeding the first occurrence of 1 in the list may be omitted
  2. The list bin cannot have a length higher than 31
  3. Regardless of the length of the length of bin, 'llList2Key( bin, llGetListLength( bin ) - 1 )' should represent 'llPow( 2, 0 )', or 2 to the 0 power




string rl_Trim ( string str )
Removes leading and trailing spaces from a string

Parameters:
  1. str - The string to trim

Returns:
  1. A string with no leading or trailing spaces


Source:
Place this at the top of any script to use it there
CODE
string rl_GetFirstName ( key id )
{
return llGetSubString( llKey2Name( id ), 0, llSubStringIndex( id, " " ) - 1 );
}

string rl_GetLastName ( key id )
{
return llGetSubString( llKey2Name( id ), llSubStringIndex( id, " " ) + 1, - 1 );
}

// Submitted by Christopher Omega
string rl_Replace ( string str, string pattern, string alternative )
{
return llDumpList2String( llParseStringKeepNulls( str, [pattern], [] ), alternative );
}

integer rl_RandomInteger ( integer min, integer max )
{
return llFloor( llFrand( max - min + 1 ) ) + min;
}

// Submitted by Strife Onizuka
list rl_Int2Bin ( integer num )
{
integer i = 32;
list binary = [];
do
{
binary += !!( num & ( 1 << --i ) );
} while ( i );
return binary;
}

// Submitted by Strife Onizuka
integer rl_Bin2Int ( list bin )
{
integer val = 0;
integer i = llGetListLength( bin );
while( i )
{
if(llList2Integer( bin, -( i-- ) ) ) //allows for the use of hex strings
{
val = val | ( 1 << i );
}
}
return val;
}

// Submitted by Sol Columbia
string rl_Trim ( string str )
{
if ( llGetSubString( str, 0, 0 ) == " " )
{
str = rl_Trim( llDeleteSubString( str, 0, 0 ) );
}
if ( llGetSubString( str, -1, -1 ) == " " )
{
str = rl_Trim( llDeleteSubString( str, -2, -1 ) );
}
return str;
}


Credits:
  1. Hitani Fugazi
  2. Christopher Omega
  3. Strife Onizuka
  4. Sol Columbia
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion Thread
06-21-2006 15:32
/54/be/115545/1.html
_____________________
i've got nothing. ;)