chr

From Second Life Wiki
Revision as of 10:51, 14 May 2014 by Pedro Oval (talk | contribs) (Add a see also)
Jump to navigation Jump to search

Summary

Function: string Chr( integer ord );

Returns a string containing a single Unicode character whose code is ord.

• integer ord Code of the Unicode character to return

Specification

<lsl>// Chr() function, written by Pedro Oval, 2010-05-28

// Auxiliary function UrlCode, which returns the hex code of the given integer // (which must be in range 0-255) with a "%" prepended. string UrlCode(integer b) {

   string hexd = "0123456789ABCDEF";
   return "%" + llGetSubString(hexd, b>>4, b>>4)
              + llGetSubString(hexd, b&15, b&15);

}

// Chr function itself, which implements Unicode to UTF-8 conversion and uses // llUnescapeURL to do its job. string Chr(integer ord) {

   if (ord <= 0)
       return "";
   if (ord < 0x80)
       return llUnescapeURL(UrlCode(ord));
   if (ord < 0x800)
       return llUnescapeURL(UrlCode((ord >> 6) & 0x1F

Caveats

  • Code 0 will return an empty string instead of the NUL character. In general, LSL does not allow NUL characters to appear in strings.
  • Codes 0xD800-0xDFFF are not legal Unicode characters.
  • For some "ill" cases, behaviour can vary between using Mono or not.
  • At the time of writing this article, the performance of this function is very low, because the performance of the underlying llUnescapeURL function is also very low. Use it only if there's no alternative.
  • The function is not prepared to deal with codes beyond 0xFFFFFF. Anyway, Unicode codes don't go beyond 0x10FFFF at the time of writing this. If Unicode is extended, using codes beyond 0x10FFFF isn't guaranteed to work.

Examples

<lsl> string heart = Chr(0x2665); // sets 'heart' to "♥" string space = Chr(32); // sets 'space' to the space character " "

</lsl>