User:Cory Fimicoloud/LSLFunctions

From Second Life Wiki
< User:Cory Fimicoloud
Revision as of 09:11, 3 August 2009 by Cory Fimicoloud (talk | contribs) (Minor code format modification.)
Jump to navigation Jump to search

My Functions

Text

String

UTF8Len
Description
Returns the UTF-8 byte count of the string passed to it.
Returns
Returns the UTF-8 byte count of the string passed to it.
Parameters
• string fsString - The string to get the UTF-8 length of.
Examples
<lsl>default {

state_entry() { // 63 characters. Each of the characters uses 1 byte except for ¢ (2 bytes) and € (3 bytes). Which results in 66 bytes. string lsString = "Can I have $10 and 7¢, please? I would prefer it in €'s though."; llOwnerSay("'" + lsString + "' is " + (string)UTF8Len(lsString) + " bytes."); }

}</lsl>
Code
<lsl>integer UTF8Len(string fsString) {

// Author: Cory Fimicoloud // License: http://creativecommons.org/licenses/by-sa/3.0/us/ integer liLen = ~llStringLength(fsString); string lsChar = ""; string lsEnc = ""; integer liByteLen = 0; while (++liLen < 0) { lsEnc = llEscapeURL((lsChar = llGetSubString(fsString, liLen, liLen))); if (lsChar != lsEnc) { liByteLen += llStringLength(lsEnc) / 3; } else { ++liByteLen; } } return liByteLen;

}</lsl>
UTF8Split
Description
Returns a list containing the string passed to it, split every fiSplit bytes.
Returns
Returns a list containing the string passed to it, split every fiSplit bytes.
Parameters
• string fsString - The string to split.
• integer fiSplit - The length of each split.
Examples
<lsl>default {

state_entry() { list laSplit = UTF8Split("Can I have $10 and 7¢, please? I would prefer it in €'s though.", 33); llOwnerSay("\nUTF8Split\n==========\n\t0: " + llList2String(laSplit, 0) + "\n\t1: " + llList2String(laSplit, 1)); }

}</lsl>
Code
<lsl>list UTF8Split(string fsString, integer fiSplit) {

// Author: Cory Fimicoloud // License: http://creativecommons.org/licenses/by-sa/3.0/us/ string lsSplit = ""; list laSplit = []; integer liLen = ~llStringLength(fsString); string lsChar = ""; string lsEnc = ""; integer liByteLen = 0; while (++liLen < 0) { lsChar = llGetSubString(fsString, liLen, liLen); lsEnc = llEscapeURL(lsChar); if (lsChar != lsEnc) { liByteLen += llStringLength(lsEnc) / 3; } else { ++liByteLen; } if (liByteLen <= fiSplit) { lsSplit += lsChar; } else { laSplit += [lsSplit]; lsSplit = lsChar; liByteLen = 1; } } return laSplit + [lsSplit];

}</lsl>
str_repeat
Description
Returns fsString repeated fiRepeat times.
Returns
Returns the repeated string.
Parameters
• string fsString - The string to be repeated.
• integer fiRepeat - Number of times the fsString string should be repeated.
Examples
<lsl>default {

state_entry() { llOwnerSay(str_repeat("a"), 5)); // OwnerSay's "aaaaa". }

}</lsl>
Code
<lsl>string str_repeat(string fsString, integer fiRepeat) {

if (fiRepeat < 1) return ""; integer count = 1; while ((count = count << 1) < fiRepeat) { fsString += fsString; } return fsString + llGetSubString(fsString, count - fiRepeat, count);

}</lsl>