2019-08-03 00:48:59 +03:00
|
|
|
package util
|
|
|
|
|
2020-02-09 02:31:06 +03:00
|
|
|
// 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 {
|
2020-05-21 00:00:56 +03:00
|
|
|
r, _, size := DecodeCharacterInString(str)
|
2019-08-03 00:48:59 +03:00
|
|
|
|
|
|
|
str = str[size:]
|
|
|
|
|
|
|
|
if i == runeidx {
|
|
|
|
return string(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-02-09 02:31:06 +03:00
|
|
|
// 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 {
|
2020-05-21 00:00:56 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-02-09 02:31:06 +03:00
|
|
|
// 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 {
|
2020-05-21 00:00:56 +03:00
|
|
|
r, _, _ := DecodeCharacterInString(s)
|
2019-08-03 00:48:59 +03:00
|
|
|
return IsWordChar(r)
|
|
|
|
}
|