]> git.lizzy.rs Git - micro.git/blob - runtime/plugins/autoclose/utf8/README.md
39105c1016755a64572a4150cd021a7d207c507a
[micro.git] / runtime / plugins / autoclose / utf8 / README.md
1 # utf8.lua
2 one-file pure-lua 5.1 regex library
3
4 This library _is_ the simple way to add utf8 support into your application.
5
6 Some examples from http://www.lua.org/manual/5.1/manual.html#5.4 :
7 ```Lua
8 local utf8 = require "utf8"
9
10 local s = "hello world from Lua"
11 for w in string.gmatch(s, "%a+") do
12     print(w)
13 end
14 --[[
15 hello
16 world
17 from
18 Lua
19 ]]--
20
21 s = "Привет, мир, от Lua"
22 for w in utf8.gmatch(s, "[^%p%d%s%c]+") do
23     print(w)
24 end
25 --[[
26 Привет
27 мир
28 от
29 Lua
30 ]]--
31
32 local t = {}
33 s = "from=world, to=Lua"
34 for k, v in string.gmatch(s, "(%w+)=(%w+)") do
35     t[k] = v
36 end
37 for k,v in pairs(t) do
38     print(k,v)
39 end
40 --[[
41 to      Lua
42 from    world
43 ]]--
44
45 t = {}
46 s = "从=世界, 到=Lua"
47 for k, v in utf8.gmatch(s, "([^%p%s%c]+)=([^%p%s%c]+)") do
48     t[k] = v
49 end
50 for k,v in pairs(t) do
51     print(k,v)
52 end
53 --[[
54 到     Lua
55 从     世界
56 ]]--
57
58 local x = string.gsub("hello world", "(%w+)", "%1 %1")
59 print(x)
60 --hello hello world world
61
62 x = utf8.gsub("Ahoj světe", "([^%p%s%c]+)", "%1 %1")
63 print(x)
64 --Ahoj Ahoj světe světe
65
66 x = string.gsub("hello world", "%w+", "%0 %0", 1)
67 print(x)
68 --hello hello world
69
70 x = utf8.gsub("Ahoj světe", "[^%p%s%c]+", "%0 %0", 1)
71 print(x)
72 --Ahoj Ahoj světe
73
74 x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
75 print(x)
76 --world hello Lua from
77
78 x = utf8.gsub("γεια κόσμο από Lua", "([^%p%s%c]+)%s*([^%p%s%c]+)", "%2 %1")
79 print(x)
80 --κόσμο γεια Lua από
81 ```
82 Notice, there are some classes that can work only with latin(ASCII) symbols,
83 for details see: https://github.com/Stepets/utf8.lua/blob/master/utf8.lua#L470
84
85 Of course you can do this trick:
86 ```Lua
87 for k,v in pairs(utf8) do
88         string[k] = v
89 end
90 ```
91 But this can lead to very strange errors. You were warned.
92
93 A little bit more interesting examples:
94 ```Lua
95 local utf8 = require 'utf8'
96 for k,v in pairs(utf8) do
97         string[k] = v
98 end
99
100 local str = "пыщпыщ ололоо я водитель нло"
101 print(str:find("(.л.+)н"))
102 -- 8    26      ололоо я водитель 
103
104 print(str:gsub("ло+", "보라"))
105 -- пыщпыщ о보라보라 я водитель н보라     3
106
107 print(str:match("^п[лопыщ ]*я"))
108 -- пыщпыщ ололоо я
109 ```