1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-06 03:00:24 +08:00
Files
NanoBot/helper.go
2023-10-13 00:45:21 +09:00

69 lines
1.5 KiB
Go

package nano
import (
"runtime"
"strings"
)
func getFuncNameWithSkip(n int) string {
pc, _, _, ok := runtime.Caller(n)
if !ok {
return ""
}
fullname := runtime.FuncForPC(pc).Name()
i := strings.LastIndex(fullname, ".") + 1
if i <= 0 || i >= len(fullname) {
return fullname
}
return fullname[i:]
}
// getThisFuncName 获取正在执行的函数名
func getThisFuncName() string {
return getFuncNameWithSkip(1)
}
// getCallerFuncName 获取调用者函数名
func getCallerFuncName() string {
return getFuncNameWithSkip(2)
}
// MessageEscape 消息转义
//
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html
func MessageEscape(text string) string {
text = strings.ReplaceAll(text, "&", "&amp;")
text = strings.ReplaceAll(text, "<", "&lt;")
text = strings.ReplaceAll(text, ">", "&gt;")
return text
}
// MessageUnescape 消息解转义
//
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html
func MessageUnescape(text string) string {
text = strings.ReplaceAll(text, "&amp;", "&")
text = strings.ReplaceAll(text, "&lt;", "<")
text = strings.ReplaceAll(text, "&gt;", ">")
return text
}
// UnderlineToCamel convert abc_def to AbcDef
func UnderlineToCamel(s string) string {
sb := strings.Builder{}
isnextupper := true
for _, c := range []byte(strings.ToLower(s)) {
if c == '_' {
isnextupper = true
continue
}
if isnextupper {
sb.WriteString(strings.ToUpper(string(c)))
isnextupper = false
continue
}
sb.WriteByte(c)
}
return sb.String()
}