How to use Unicode in Lua scripts

Source: Internet
Author: User

HowLuaUse in scriptUnicodeIs the content to be introduced in this article, mainly to learnUnicodeInLuaFor more information about how to use the script, see the detailed description in this article. Let's talk about the recent several things:

1. project usesLua, Ui module is certainly to thoroughly use the UTF-16, but lua does not support, how to do? At last, it seems that the userdata is written, and the multibytes can be converted into a UTF-16 and saved in this userdata; then, to ensure that the same string of userdata is the same as the key, therefore, the corresponding function to avoid duplication is added, and the gc of this userdata is also ensured.

2. google: lua unicode, you will find a wide range of articles using unicode through table, in fact not practical-tools are required, strings are not readable.

3. The lua script of World of Warcraft is saved in UTF-8 format, and some of its scripts directly write text information. Therefore, this storage format is advantageous.

4. I have read the war hammer ol plug-in and accidentally saw code similar to s = L "war hammer ol". I wonder if lua supports unicode? I tried and found no, so I thought it was because their program modified lua's parser. Soon after, my head has finally become enlightened.

In fact, many of our predecessors have already stressed lua's unicode solutions: lua string is a raw buffer that can store arbitrary data. Although this statement is correct, it is too abstract because they have not explained the specific implementation methods.

The so-called support we wantUnicodeOfLuaIn the end, what does it mean? The most basic thing is to use L as the string prefix like c, and assign the string to the variable, just like the above

 
 
  1. S = L "Attach ol"

Pay attention to this code. Do you see anything? Make it clearer

 
 
  1. S = L ("war hammer ol ")

Thanks to lua's negligible writing method, lua can write beautiful content, except for string and table.

As long as L is a function that accepts the multibytes string as a parameter, converts it to a UTF-16, then gives the string of the UTF-16 to lua string, and presses the stack.

Here I will go back and talk about the project mentioned above. I don't need userdata, because it can be a lua string. Can the same string be used for key homogeneity? Gc? All connections are handed over to lua string. It's okay here.

Finally, paste the code to take advantage of it.

 
 
  1. int utf8_to_utf16(lua_State* L)  
  2. {  
  3.    size_t n = 0;  
  4.     char* str = (char*)luaL_checklstring(L, -1, &n);  
  5.     if(!str)   
  6.         return 0;  
  7.      iconv_t h = iconv_open("utf-16", "utf-8");  
  8.     if(0==h)   
  9.         return 0;  
  10.       
  11.     char wstr[4096];  
  12.     size_t wn = 4096;  
  13.     char** ppsrc = &str;  
  14.     char* dst = wstr;  
  15.     char** ppdst = &dst;  
  16.     int ret = iconv(h, ppsrc, &n, ppdst, &wn);  
  17.     if(ret==-1 || wn<0 || n>0)  
  18.     {  
  19.         iconv_close(h);  
  20.         return 0;  
  21.     }  
  22.     iconv_close(h);  
  23.     lua_pushlstring(L, (char*)wstr, (4096-wn));  
  24.     return 1;  
  25. }  
  26. //here here  
  27. lua_register(L, "L", utf8_to_utf16);  

Summary: Explain howLuaUse in scriptUnicodeI hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.