LlChar: Difference between revisions

From Second Life Wiki
Jump to navigation Jump to search
Nexii Malthus (talk | contribs)
Add details on valid range and caveats on surrogates + special noncharacters
Tapple Gao (talk | contribs)
link to my llChar, llOrd, utf8.codepoint, utf8.char validation script
 
(6 intermediate revisions by 2 users not shown)
Line 7: Line 7:
|p1_hover=Unicode value for character.
|p1_hover=Unicode value for character.
|return_text=
|return_text=
|func_footnote=This function returns a single character string generated from the character at the indicated UTF-32 codepoint.<br/>If {{LSLP|val}} is negative, the codepoint has no valid single-character UTF-16 representation such as a part of a surrogate pair, or is outside defined range, the Unicode replacement character "�" (<code>0xFFFD</code>) is returned.</br>If {{LSLP|val}} is 0, an empty string is returned.
|func_footnote=This function returns a single character string generated from the character at the indicated UTF-32 codepoint.<br/>If {{LSLP|val}} is negative, the codepoint has no valid single-character UTF-16 representation such as a part of a surrogate pair, or is outside defined range, the Unicode replacement character "�" (<code>0xFFFD</code>) is returned. (Slua's equivilant <code>utf8.char</code> instead raises the error <code>"invalid argument #1 to 'char' (value out of range)"</code> in this situation).</br>If {{LSLP|val}} is 0, an empty string is returned.
|spec
|spec
|caveats=
|caveats=
Line 13: Line 13:
* Values in the surrogate range <code>0xD800</code>&ndash;<code>0xDFFF</code> will return the replacement character "�" (<code>0xFFFD</code>) instead
* Values in the surrogate range <code>0xD800</code>&ndash;<code>0xDFFF</code> will return the replacement character "�" (<code>0xFFFD</code>) instead
* Values for the special noncharacters <code>0xFFFE</code> and <code>0xFFFF</code> will return the replacement character instead
* Values for the special noncharacters <code>0xFFFE</code> and <code>0xFFFF</code> will return the replacement character instead
* SLua's equivilant <code>utf8.char</code> has none of the above caveats, and also handles <code>0x0</code> (the null character) correctly ([https://github.com/secondlife/lsl-definitions/pull/74#discussion_r2934141244 validation script, in SLua])
* Values for UTF-8 multibyte ranges are:
** 1 byte: <code>0x0</code>&ndash;<code>0x7F</code>
** 2 bytes: <code>0x80</code>&ndash;<code>0x07FF</code>
** 3 bytes: <code>0x0800</code>&ndash;<code>0xFFFF</code>
** 4 bytes: <code>0x10000</code>&ndash;<code>0x10FFFF</code>
|examples=
|examples=
<source lang="lsl2">
<source lang="lsl2">

Latest revision as of 16:42, 13 March 2026

Summary

Function: string llChar( integer val );
0.0 Forced Delay
10.0 Energy

Construct a single character string from the supplied Unicode value.
Returns a string

• integer val Unicode value for character.

This function returns a single character string generated from the character at the indicated UTF-32 codepoint.
If val is negative, the codepoint has no valid single-character UTF-16 representation such as a part of a surrogate pair, or is outside defined range, the Unicode replacement character "�" (0xFFFD) is returned. (Slua's equivilant utf8.char instead raises the error "invalid argument #1 to 'char' (value out of range)" in this situation).
If val is 0, an empty string is returned.

Caveats

  • Value range is between 0x10x10FFFF
  • Values in the surrogate range 0xD8000xDFFF will return the replacement character "�" (0xFFFD) instead
  • Values for the special noncharacters 0xFFFE and 0xFFFF will return the replacement character instead
  • SLua's equivilant utf8.char has none of the above caveats, and also handles 0x0 (the null character) correctly (validation script, in SLua)
  • Values for UTF-8 multibyte ranges are:
    • 1 byte: 0x00x7F
    • 2 bytes: 0x800x07FF
    • 3 bytes: 0x08000xFFFF
    • 4 bytes: 0x100000x10FFFF

Examples

default
{
    touch_start(integer total_number)
    {
        string test_string = "The quick brown fox jumped over the lazy dog";
        list test_list = [];
        string test_string2 = "";
        
        integer index;
        integer ord;
        for (index = 0; index < llStringLength(test_string); ++index)
        {
            ord = llOrd(test_string, index);
            test_list = test_list + [ ord ];
        }
        
        string char;
        for (index = 0; index < llGetListLength(test_list); ++index)
        {
            ord = llList2Integer(test_list, index);
            char = llChar(ord);
            test_string2 = test_string2 + char;
        }
        
        llSay(0, "\"" + test_string + "\" -> [" + 
            llDumpList2String(test_list, ", ") + "] -> \"" + test_string2 + "\"");
    }
}

See Also

Functions

• llOrd Convert a character into an ordinal
• llHash Calculate a 32bit hash for a string

Articles

•  "Wikipedia logo"UTF-32

Deep Notes

Signature

function string llChar( integer val );