micro/internal/util/lua.go

42 lines
843 B
Go
Raw Normal View History

2019-08-03 00:48:59 +03:00
package util
// LuaRuneAt is a helper function for lua plugins to return the rune
// at an index within a string
2019-08-03 00:48:59 +03:00
func LuaRuneAt(str string, runeidx int) string {
i := 0
for len(str) > 0 {
r, _, size := DecodeCharacterInString(str)
2019-08-03 00:48:59 +03:00
str = str[size:]
if i == runeidx {
return string(r)
}
i++
}
return ""
}
// LuaGetLeadingWhitespace returns the leading whitespace of a string (used by lua plugins)
2019-08-03 00:48:59 +03:00
func LuaGetLeadingWhitespace(s string) string {
ws := []byte{}
for len(s) > 0 {
r, _, size := DecodeCharacterInString(s)
2019-08-03 00:48:59 +03:00
if r == ' ' || r == '\t' {
ws = append(ws, byte(r))
} else {
break
}
s = s[size:]
}
return string(ws)
}
// LuaIsWordChar returns true if the first rune in a string is a word character
2019-08-03 00:48:59 +03:00
func LuaIsWordChar(s string) bool {
r, _, _ := DecodeCharacterInString(s)
2019-08-03 00:48:59 +03:00
return IsWordChar(r)
}