2022-08-23 18:25:44 +00:00
|
|
|
package lib
|
|
|
|
|
2022-08-24 14:06:00 +00:00
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-08-23 18:25:44 +00:00
|
|
|
func Contains[T comparable](s []T, v T) bool {
|
|
|
|
for _, x := range s {
|
|
|
|
if x == v {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-08-24 14:06:00 +00:00
|
|
|
|
|
|
|
func JoinInt(a []int, sep string) string {
|
2022-09-07 05:54:58 +00:00
|
|
|
b := make([]string, len(a))
|
2022-08-24 14:06:00 +00:00
|
|
|
|
|
|
|
for i, v := range a {
|
|
|
|
b[i] = strconv.Itoa(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(b, sep)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SumInt(a []int) int {
|
|
|
|
var sum int
|
|
|
|
for _, v := range a {
|
|
|
|
sum += v
|
|
|
|
}
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
|
|
|
func Itob(v int) bool {
|
2022-09-07 05:54:58 +00:00
|
|
|
return v == 1
|
2022-08-24 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BuildURI(rawuri, rawpath string) string {
|
|
|
|
u, _ := url.Parse(rawuri)
|
|
|
|
u.Path = path.Join(u.Path, rawpath)
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
2022-09-05 18:03:02 +00:00
|
|
|
func HasCommand(s, prefix string) bool {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
|
|
|
if len(s) == 0 || len(prefix) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(s, prefix) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the command prefix
|
|
|
|
s = s[len(prefix):]
|
|
|
|
|
|
|
|
// multiple assignment trick
|
|
|
|
cmd, _ := func() (string, string) {
|
|
|
|
x := strings.SplitN(s, " ", 2)
|
|
|
|
if len(x) > 1 {
|
|
|
|
return x[0], x[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return x[0], ""
|
|
|
|
}()
|
|
|
|
|
|
|
|
return len(cmd) > 0
|
|
|
|
}
|
|
|
|
|
2022-09-05 17:54:56 +00:00
|
|
|
func ContainsCommand(s, prefix, cmd string) bool {
|
2022-09-01 14:25:59 +00:00
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
2022-09-04 13:51:01 +00:00
|
|
|
args := strings.Split(s, " ")
|
|
|
|
s = args[0]
|
|
|
|
|
2022-09-01 14:25:59 +00:00
|
|
|
// a command cannot be less than two characters e.g. !x
|
2022-08-24 14:06:00 +00:00
|
|
|
if len(s) < 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(s[0]) != prefix {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-08-26 22:55:51 +00:00
|
|
|
if strings.HasPrefix(s[1:], cmd) {
|
2022-08-24 14:06:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2022-09-01 14:26:10 +00:00
|
|
|
|
2022-09-06 15:43:30 +00:00
|
|
|
func SplitCommandAndArg(s, prefix string) (cmd string, arg string) {
|
2022-09-01 14:26:10 +00:00
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
2022-09-05 18:03:47 +00:00
|
|
|
if !strings.HasPrefix(s, prefix) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:43:30 +00:00
|
|
|
// remove the command prefix
|
|
|
|
s = s[len(prefix):]
|
2022-09-01 14:26:10 +00:00
|
|
|
|
2022-09-06 15:43:30 +00:00
|
|
|
// multiple assignment trick
|
|
|
|
cmd, arg = func() (string, string) {
|
|
|
|
x := strings.SplitN(s, " ", 2)
|
|
|
|
if len(x) > 1 {
|
|
|
|
return x[0], x[1]
|
|
|
|
}
|
|
|
|
return x[0], ""
|
|
|
|
}()
|
|
|
|
|
|
|
|
return cmd, arg
|
|
|
|
}
|
2022-09-01 14:26:10 +00:00
|
|
|
|
2022-09-06 15:43:30 +00:00
|
|
|
func SplitCommandAndArgs(s, prefix string, n int) (cmd string, args []string) {
|
|
|
|
cmd, arg := SplitCommandAndArg(s, prefix)
|
2022-09-01 14:26:10 +00:00
|
|
|
|
2022-09-06 15:43:30 +00:00
|
|
|
if n == 0 {
|
|
|
|
return cmd, strings.Split(arg, " ")
|
2022-09-01 14:26:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 15:43:30 +00:00
|
|
|
return cmd, strings.SplitN(arg, " ", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SplitArgs(s string, n int) (args []string) {
|
|
|
|
if n > 0 {
|
|
|
|
args = strings.SplitN(s, " ", n)
|
|
|
|
} else {
|
|
|
|
args = strings.Split(s, " ")
|
|
|
|
}
|
|
|
|
return
|
2022-09-01 14:26:10 +00:00
|
|
|
}
|