JS函数charCodeAt的Lua实现
charCodeAt by Lua
@(Lua JavaScript charCodeAt)
I wanted to have a function
charCodeAt
in Lua ,and it should works exactly likejavascript
but with Lua5.1 ,UTF8 and Unicode are not supported,
1: how charCodeAt works in javascript
to show Console press F12 in Chrome( MAC:CMD+alt+J)
1 | [ |
it will output [20320, 241, 110] ,it means the numeric value of Unicode , ‘你’=20320 , ‘ñ’=241, ‘n’=110.
The charCodeAt() method returns the numeric Unicode value of the character at the given index (except for unicode codepoints > 0x10000).
according to alexander-yakushev we can know how many bytes one UTF8 word takes using function utf8.charbytes
[https://github.com/alexander-yakushev/awesompd/blob/master/utf8.lua]
1 | function utf8.charbytes (s, i) |
Unicode & UTF8 convert method
Unicode code range | UTF-8 code | example |
---|---|---|
hex code | binary code | char |
0000 0000-0000 007F | 0xxxxxxx | n (alphabet) |
0000 0000-0000 007F | 110xxxxx 10xxxxxx | ñ |
0000 0080-0000 07FF | 1110xxxx 10xxxxxx 10xxxxxx | 你 (most CJK) |
0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx | other chars |
but we should pay attention to 4 bytes UTF8[emoji
], it works not that simple
special Method
javascript engine using UTF16,characters in Basic Multilingual Plane
were the same with unicode, but if the characters were in Supplementary Plane
it should use the formula below,usually we encounter Supplementary Plane
emoji like😝 (4 byte UTF8 character)
1 | -- formula 1 |
###code is here
###https://github.com/lilien1010/lua-bit
Feedback & Bug Report
- Twitter: @LeenuxLee
- Email: liling@apache.org
Thank you for reading this , if you got any better idea, share it.