Difference between revisions of "Chr"

From Second Life Wiki
Jump to navigation Jump to search
m (Add a see also)
(Remove unnecessary code)
Line 25: Line 25:
         return llUnescapeURL(UrlCode(ord));
         return llUnescapeURL(UrlCode(ord));
     if (ord < 0x800)
     if (ord < 0x800)
         return llUnescapeURL(UrlCode((ord >> 6) & 0x1F | 0xC0)
         return llUnescapeURL(UrlCode((ord >> 6) | 0xC0)
                             +UrlCode(ord & 0x3F | 0x80));
                             +UrlCode(ord & 0x3F | 0x80));
     if (ord < 0x10000)
     if (ord < 0x10000)
         return llUnescapeURL(UrlCode((ord >> 12) & 0x0F | 0xE0)
         return llUnescapeURL(UrlCode((ord >> 12) | 0xE0)
                             +UrlCode((ord >> 6) & 0x3F | 0x80)
                             +UrlCode((ord >> 6) & 0x3F | 0x80)
                             +UrlCode(ord & 0x3F | 0x80));
                             +UrlCode(ord & 0x3F | 0x80));
     return llUnescapeURL(UrlCode((ord >> 18) & 0x0F | 0xF0)
     return llUnescapeURL(UrlCode((ord >> 18) | 0xF0)
                         +UrlCode((ord >> 12) & 0x3F | 0x80)
                         +UrlCode((ord >> 12) & 0x3F | 0x80)
                         +UrlCode((ord >> 6) & 0x3F | 0x80)
                         +UrlCode((ord >> 6) & 0x3F | 0x80)

Revision as of 13:10, 21 June 2014

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)

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>