Difference between revisions of "Chr"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with '{{LSL_Function |func=Chr |mode=user |p1_type=integer|p1_name=ord|p1_desc=Code of the Unicode character to return |return_type=string |return_text=containing a single Unicode char...')
 
m (Add a see also)
Line 48: Line 48:
</lsl>
</lsl>
|also_functions=* [[Ord]]
|also_functions=* [[Ord]]
* [[Combined Library#Unicode functions]]
|cat1=User-Defined_Functions
|cat1=User-Defined_Functions
|cat2=Examples
|cat2=Examples
}}
}}

Revision as of 11:51, 14 May 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) & 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>