fx/utils.go
2023-09-14 21:58:39 +02:00

58 lines
964 B
Go

package main
import (
"regexp"
"strings"
)
var identifier = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
func isHexDigit(ch byte) bool {
return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')
}
func isDigit(ch byte) bool {
return ch >= '0' && ch <= '9'
}
func max(i, j int) int {
if i > j {
return i
}
return j
}
func regexCase(code string) (string, bool) {
if strings.HasSuffix(code, "/i") {
return code[:len(code)-2], true
} else if strings.HasSuffix(code, "/") {
return code[:len(code)-1], false
} else {
return code, true
}
}
func flex(width int, a, b string) string {
return a + strings.Repeat(" ", max(1, width-len(a)-len(b))) + b
}
func safeSlice(b []byte, start, end int) []byte {
length := len(b)
if start > length {
start = length
}
if end > length {
end = length
}
if start < 0 {
start = 0
}
if end < 0 {
end = 0
}
if start > end {
start = end
}
return b[start:end]
}