1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

64 lines
1.1 KiB
Go
Raw Normal View History

2017-06-30 11:21:01 +02:00
package main
// Some helper functions
import (
2017-06-30 12:05:46 +02:00
"regexp"
2017-07-10 14:19:36 +01:00
"strconv"
2017-06-30 11:21:01 +02:00
"strings"
)
/*
Case Insensitive Contains
*/
func ContainsCi(s, substr string) bool {
return strings.Contains(
strings.ToLower(s),
strings.ToLower(substr),
)
}
2017-06-30 12:05:46 +02:00
2017-06-30 15:02:12 +02:00
/*
Check array membership
*/
func MemberOf(list []string, key string) bool {
for _, v := range list {
if v == key {
return true
}
}
return false
}
2017-06-30 12:05:46 +02:00
/*
Check if something could be a prefix
*/
func MaybePrefix(s string) bool {
s = strings.ToLower(s)
2017-06-30 15:02:12 +02:00
// Rule out anything which can not be
if strings.ContainsAny(s, "ghijklmnopqrstuvwxyz][;'_") {
return false
}
2017-06-30 12:05:46 +02:00
// Test using regex
matches := regexp.MustCompile(`([a-f0-9/]+[\.:]?)+`).FindAllStringIndex(s, -1)
if len(matches) == 1 {
return true
}
return false
}
2017-07-10 14:19:36 +01:00
/*
Since havin ints as keys in json is
acutally undefined behaviour, we keep these interally
but provide a string as a key for serialization
*/
func SerializeReasons(reasons map[int]string) map[string]string {
res := make(map[string]string)
for id, reason := range reasons {
res[strconv.Itoa(id)] = reason
}
return res
}