Skip to content

Commit

Permalink
Implemented utf8.char(...) for #319
Browse files Browse the repository at this point in the history
  • Loading branch information
hymkor committed Jun 10, 2018
1 parent 7b53603 commit e84b2eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions mains/utf8lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ import (
"github.com/yuin/gopher-lua"
)

func utf8char(L *lua.LState) int {
var buffer strings.Builder
for i, n := 1, L.GetTop(); i <= n; i++ {
number, ok := L.Get(i).(lua.LNumber)
if !ok {
L.Push(lua.LNil)
L.Push(lua.LString("NaN"))
return 2
}
buffer.WriteRune(rune(number))
}
L.Push(lua.LString(buffer.String()))
return 1
}

func utf8codes(L *lua.LState) int {
lstr, ok := L.Get(-1).(lua.LString)
if !ok {
Expand Down Expand Up @@ -37,5 +52,6 @@ func SetupUtf8Table(L *lua.LState) {
table := L.NewTable()
L.SetField(table, "codes", L.NewFunction(utf8codes))
L.SetField(table, "charpattern", lua.LString("[\000-\x7F\xC2-\xF4][\x80-\xBF]*"))
L.SetField(table, "char", L.NewFunction(utf8char))
L.SetGlobal("utf8", table)
}
2 changes: 1 addition & 1 deletion t/tst_utf8lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--- lua.exe tst_utf8lib.lua | nkf32

for i,c in utf8.codes("あいうえお") do
print(i,c,type(c))
print(i,c,type(c),utf8.char(c))
end

for c in string.gmatch("あいうえお",utf8.charpattern) do
Expand Down

0 comments on commit e84b2eb

Please sign in to comment.