Difference between revisions of "Ord"
Jump to navigation
Jump to search
Pedro Oval (talk | contribs) (Created page with '{{LSL_Function |func=Ord |mode=user |p1_type=string|p1_name=chr|p1_desc=String containing the Unicode character whose code is to be found |return_type=integer |return_text=with t...') |
m (<lsl> tag to <source>) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 5: | Line 5: | ||
|return_type=integer | |return_type=integer | ||
|return_text=with the Unicode code of the first character in '''chr'''. | |return_text=with the Unicode code of the first character in '''chr'''. | ||
|spec=< | |spec=<source lang="lsl2">// Ord() function, written by Pedro Oval, 2010-05-28 | ||
// This function works by using llEscapeURL to find the corresponding UTF-8 | // This function works by using llEscapeURL to find the corresponding UTF-8 | ||
// string then converts it to the Unicode code. In cases where llEscapeURL | // string then converts it to the Unicode code. In cases where llEscapeURL | ||
Line 14: | Line 14: | ||
if (chr == "") | if (chr == "") | ||
return 0; | return 0; | ||
chr = llGetSubString(chr, 0, 0); | |||
string hex = llEscapeURL(chr); | string hex = llEscapeURL(chr); | ||
if (llGetSubString(hex, 0, 0) != "%") | if (llGetSubString(hex, 0, 0) != "%") | ||
Line 19: | Line 20: | ||
// Regular character - we can't take advantage of llEscapeURL in this case, | // Regular character - we can't take advantage of llEscapeURL in this case, | ||
// so we use llStringToBase64/llBase64ToInteger instead. | // so we use llStringToBase64/llBase64ToInteger instead. | ||
return llBase64ToInteger("AAAA" + llStringToBase64 | return llBase64ToInteger("AAAA" + llStringToBase64(chr)); | ||
} | } | ||
integer b = (integer)("0x" + llGetSubString(hex, 1, 2)); | integer b = (integer)("0x" + llGetSubString(hex, 1, 2)); | ||
Line 40: | Line 41: | ||
return cp; | return cp; | ||
} | } | ||
</ | </source> | ||
|caveats= | |caveats= | ||
* An empty string will return 0. | * An empty string will return 0. | ||
* For some "ill" cases, behaviour can vary between using Mono or not. | * 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 functions used 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 functions used is also very low. Use it only if there's no alternative. | ||
* Passing more than 1 character is possible, and in that case only the first character's code will be returned | * Passing more than 1 character is possible, and in that case only the first character's code will be returned. | ||
|examples=< | |examples=<source lang="lsl2"> | ||
integer heart = Ord("♥"); // sets 'heart' to 9829 (0x2665, the Unicode code of the heart) | integer heart = Ord("♥"); // sets 'heart' to 9829 (0x2665, the Unicode code of the heart) | ||
integer space = Ord(" "); // sets 'space' to 32 (which is the ASCII/Unicode code of the space) | integer space = Ord(" "); // sets 'space' to 32 (which is the ASCII/Unicode code of the space) | ||
</ | </source> | ||
|also_functions=* [[Chr]] | |also_functions=* [[Chr]] | ||
* [[Combined Library#Unicode functions]] | |||
|cat1=User-Defined Functions | |cat1=User-Defined Functions | ||
|cat2=Examples | |cat2=Examples | ||
}} | }} |
Latest revision as of 14:31, 22 January 2015
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Summary
Function: integer Ord( string chr );Returns an integer with the Unicode code of the first character in chr.
• string | chr | – | String containing the Unicode character whose code is to be found |
Specification
// Ord() function, written by Pedro Oval, 2010-05-28
// This function works by using llEscapeURL to find the corresponding UTF-8
// string then converts it to the Unicode code. In cases where llEscapeURL
// doesn't help, a combination of llStringToBase64 and llBase64ToInteger
// does the job instead.
integer Ord(string chr)
{
if (chr == "")
return 0;
chr = llGetSubString(chr, 0, 0);
string hex = llEscapeURL(chr);
if (llGetSubString(hex, 0, 0) != "%")
{
// Regular character - we can't take advantage of llEscapeURL in this case,
// so we use llStringToBase64/llBase64ToInteger instead.
return llBase64ToInteger("AAAA" + llStringToBase64(chr));
}
integer b = (integer)("0x" + llGetSubString(hex, 1, 2));
if (b < 194 || b > 244)
return b;
if (b < 224)
return ((b & 0x1F) << 6) | (integer)("0x" + llGetSubString(hex, 4, 5)) & 0x3F;
integer cp;
if (b < 240)
{
cp = (b & 0x0F) << 12;
cp += ((integer)("0x" + llGetSubString(hex, 4, 5)) & 0x3F) << 6;
cp += (integer)("0x" + llGetSubString(hex, 7, 8)) & 0x3F;
return cp;
}
cp = (b & 0x07) << 18;
cp += ((integer)("0x" + llGetSubString(hex, 4, 5)) & 0x3F) << 12;
cp += ((integer)("0x" + llGetSubString(hex, 7, 8)) & 0x3F) << 6;
cp += (integer)("0x" + llGetSubString(hex, 10, 11)) & 0x3F;
return cp;
}
Caveats
- An empty string will return 0.
- 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 functions used is also very low. Use it only if there's no alternative.
- Passing more than 1 character is possible, and in that case only the first character's code will be returned.
Examples
integer heart = Ord("♥"); // sets 'heart' to 9829 (0x2665, the Unicode code of the heart)
integer space = Ord(" "); // sets 'space' to 32 (which is the ASCII/Unicode code of the space)