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
 
(2 intermediate revisions by one other user not shown)
Line 5: Line 5:
|return_type=string
|return_type=string
|return_text=containing a single Unicode character whose code is '''ord'''.
|return_text=containing a single Unicode character whose code is '''ord'''.
|spec=<lsl>// Chr() function, written by Pedro Oval, 2010-05-28
|spec=<source lang="lsl2">// Chr() function, written by Pedro Oval, 2010-05-28


// Auxiliary function UrlCode, which returns the hex code of the given integer
// Auxiliary function UrlCode, which returns the hex code of the given integer
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)
                         +UrlCode(ord & 0x3F | 0x80));
                         +UrlCode(ord & 0x3F | 0x80));
}
}
</lsl>
</source>
|caveats=
|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.
* Code 0 will return an empty string instead of the NUL character. In general, LSL does not allow NUL characters to appear in strings.
Line 43: Line 43:
* 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.
* 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.
* 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>
|examples=<source lang="lsl2">
string heart = Chr(0x2665); // sets 'heart' to "♥"
string heart = Chr(0x2665); // sets 'heart' to "♥"
string space = Chr(32); // sets 'space' to the space character " "
string space = Chr(32); // sets 'space' to the space character " "
</lsl>
</source>
|also_functions=* [[Ord]]
|also_functions=* [[Ord]]
* [[Combined Library#Unicode functions]]
|cat1=User-Defined_Functions
|cat1=User-Defined_Functions
|cat2=Examples
|cat2=Examples
}}
}}

Latest revision as of 14:12, 22 January 2015

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

// 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) | 0xC0)
                            +UrlCode(ord & 0x3F | 0x80));
    if (ord < 0x10000)
        return llUnescapeURL(UrlCode((ord >> 12) | 0xE0)
                            +UrlCode((ord >> 6) & 0x3F | 0x80)
                            +UrlCode(ord & 0x3F | 0x80));
    return llUnescapeURL(UrlCode((ord >> 18) | 0xF0)
                        +UrlCode((ord >> 12) & 0x3F | 0x80)
                        +UrlCode((ord >> 6) & 0x3F | 0x80)
                        +UrlCode(ord & 0x3F | 0x80));
}

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

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