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

68 lines
1.5 KiB
Go
Raw Normal View History

2021-03-22 16:25:47 +01:00
package backend
2017-06-30 11:21:01 +02:00
// 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"
2018-09-24 22:29:13 +02:00
"time"
2017-06-30 11:21:01 +02:00
)
2018-12-16 16:29:57 +01:00
var REGEX_MATCH_IP_PREFIX = regexp.MustCompile(`([a-f0-9/]+[\.:]*)+`)
2021-03-22 17:35:20 +01:00
// ContainsCi is like `strings.Contains` but case insensitive
2017-06-30 11:21:01 +02:00
func ContainsCi(s, substr string) bool {
return strings.Contains(
strings.ToLower(s),
strings.ToLower(substr),
)
}
2017-06-30 12:05:46 +02:00
2021-03-22 17:35:20 +01:00
// MemberOf checks if a key is present in
// a list of strings.
2017-06-30 15:02:12 +02:00
func MemberOf(list []string, key string) bool {
for _, v := range list {
if v == key {
return true
}
}
return false
}
2021-03-22 17:35:20 +01:00
// MaybePrefix checks if something could be a prefix
2017-06-30 12:05:46 +02:00
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
2018-12-16 16:29:57 +01:00
matches := REGEX_MATCH_IP_PREFIX.FindAllStringIndex(s, -1)
2017-06-30 12:05:46 +02:00
if len(matches) == 1 {
return true
}
return false
}
2017-07-10 14:19:36 +01:00
2021-03-22 17:35:20 +01:00
// SerializeReasons asserts the bgp communitiy parts are
// actually strings, because there are no such things as
// integers as keys in json.
// Serialization of this is undefined behaviour, so we
// keep these interallybut provide a string as a key for
// serialization
2017-07-10 14:19:36 +01:00
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
}
2018-09-17 18:11:28 +02:00
2021-03-22 17:35:20 +01:00
// DurationMs converts time.Duration to milliseconds
2018-09-24 22:29:13 +02:00
func DurationMs(d time.Duration) float64 {
return float64(d) / 1000.0 / 1000.0 // nano -> micro -> milli
}