Difference between revisions of "Chr"
Jump to navigation
Jump to search
Pedro Oval (talk | contribs) m (Add a see also) |
m |
||
(One intermediate revision 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=< | |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) | 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) | 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) | 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)); | ||
} | } | ||
</ | </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=< | |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 " " | ||
</ | </source> | ||
|also_functions=* [[Ord]] | |also_functions=* [[Ord]] | ||
* [[Combined Library#Unicode functions]] | * [[Combined Library#Unicode functions]] |
Latest revision as of 14:12, 22 January 2015
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
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 " "