Difference between revisions of "LlStringLength"

From Second Life Wiki
Jump to navigation Jump to search
(getStringBytes is excelent, i don't think you can prove the other works.)
Line 35: Line 35:
*Both UTF8 and UTF16 use multibyte characters
*Both UTF8 and UTF16 use multibyte characters
*Some communication functions (i.e. llHTTPResponse) are limited by number of Bytes, and work with UTF8 strings
*Some communication functions (i.e. llHTTPResponse) are limited by number of Bytes, and work with UTF8 strings
<lsl>
 
integer UTF8Length(string msg)
{
//Returns the number of BYTES a string will have when converted to UTF8 by communication functions
//Simple and efficient!
//Released to the public domain by kimmie Loveless
    integer rNum = llStringLength(msg);
    return rNum + ((llStringLength(llEscapeURL(msg)) - rNum)/4);
}
</lsl>
----
<lsl>
<lsl>
integer getStringBytes(string msg) {
integer getStringBytes(string msg) {
     string s = llEscapeURL(msg);
    //Definitions:
    //msg == unescapable_chars + escapable_chars
    //bytes == unescapable_bytes + escapable_bytes
    //unescapable_bytes == unescapable_chars
 
    //s == unescapable_chars + (escapable_bytes * 3)
     string s = llEscapeURL(msg);//uses 3 characters per byte escaped.
   
    //remove 1 char from each escapable_byte's triplet.
    //t == unescapable_chars + (escapable_bytes * 2)
     string t = (string)llParseString2List(s,["%"],[]);
     string t = (string)llParseString2List(s,["%"],[]);
   
    //return == (unescapable_chars * 2 + escapable_bytes * 4) - (unescapable_chars + (escapable_bytes * 3))
    //return == unescapable_chars + escapable_bytes == unescapable_bytes + escapable_bytes
    //return == bytes
     return llStringLength(t) * 2 - llStringLength(s);
     return llStringLength(t) * 2 - llStringLength(s);
}
}

Revision as of 13:30, 17 September 2012

Summary

Function: integer llStringLength( string str );
0.0 Forced Delay
10.0 Energy

Returns an integer that is the number of characters in str (not counting the null).

• string str

Caveats

  • The index of the last character is not equal to the string length.
    • Character indexs start at zero (the index of the first character is zero).

Examples

<lsl> // assumptions: // object name: LSLWiki // script name: _lslwiki

default {

   state_entry()
   {
       string HowLongAmI = "123";
       integer strlen = llStringLength(HowLongAmI);
       llOwnerSay( "'" + HowLongAmI + "' has " +(string) strlen + " characters.");
       // The owner of object LSLWiki will hear 
       // LSLWiki: '123' has 3 characters.
   }

}

</lsl>


  • LSL-2 sees all strings as UTF8
  • LSL-Mono sees all string as UTF16
  • llStringLength gets the number of characters
  • Both UTF8 and UTF16 use multibyte characters
  • Some communication functions (i.e. llHTTPResponse) are limited by number of Bytes, and work with UTF8 strings

<lsl> integer getStringBytes(string msg) {

   //Definitions:
   //msg == unescapable_chars + escapable_chars
   //bytes == unescapable_bytes + escapable_bytes
   //unescapable_bytes == unescapable_chars
   //s == unescapable_chars + (escapable_bytes * 3)
   string s = llEscapeURL(msg);//uses 3 characters per byte escaped.
   
   //remove 1 char from each escapable_byte's triplet.
   //t == unescapable_chars + (escapable_bytes * 2)
   string t = (string)llParseString2List(s,["%"],[]);
   
   //return == (unescapable_chars * 2 + escapable_bytes * 4) - (unescapable_chars + (escapable_bytes * 3))
   //return == unescapable_chars + escapable_bytes == unescapable_bytes + escapable_bytes
   //return == bytes
   return llStringLength(t) * 2 - llStringLength(s);

} </lsl>

  • Getting string bytes including multibyte characters
If we write bytes of msg and s as:
mb(msg bytes) = nb(no escaped bytes) + bb(before escaped bytes)
sb(s bytes) = nb + ab(after escaped bytes)
And,
ab = bb * 3 (each 1 byte like '#' becomes escaped to 3 bytes "%23"):
Then,
mb = sb - bb * 2
All escaped in s are now "%xx"(x:0-9 or A-F) forms, so, bb = number of '%'s in s.
To count '%'s, we make string t removing '%'s from s, and:
bb = sb - tb(t bytes)
(We separate s to a list removing '%'s by llParseString2List(), and again join to one string t by typecast.)
Now we got answer:
mb = sb - (sb - tb) * 2 = tb * 2 - sb
  • Refer Blog(Japanese)
http://snumaw.blogspot.jp/2009/01/lsl-lldialog.html
http://jinko.slmame.com/e602732.html

See Also

Functions

•  llGetListLength

Deep Notes

Signature

function integer llStringLength( string str );