yay/utils.go

115 lines
1.6 KiB
Go
Raw Normal View History

2018-03-22 16:40:33 +01:00
package main
import (
"io/ioutil"
"path/filepath"
2018-03-22 16:40:33 +01:00
"strings"
"unicode"
)
type mapStringSlice map[string][]string
type mapStringSet map[string]stringSet
2018-03-22 16:40:33 +01:00
type intRange struct {
min int
max int
}
func makeIntRange(min, max int) intRange {
return intRange{
min,
max,
}
}
func (r intRange) get(n int) bool {
return n >= r.min && n <= r.max
}
type intRanges []intRange
func (rs intRanges) get(n int) bool {
for _, r := range rs {
if r.get(n) {
return true
}
}
return false
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
func (mss mapStringSet) Add(n string, v string) {
_, ok := mss[n]
2018-03-22 16:40:33 +01:00
if !ok {
mss[n] = make(stringSet)
2018-03-22 16:40:33 +01:00
}
mss[n].set(v)
2018-03-22 16:40:33 +01:00
}
func (mss mapStringSlice) Add(n string, v string) {
_, ok := mss[n]
2018-03-22 16:40:33 +01:00
if !ok {
mss[n] = make([]string, 0, 1)
2018-03-22 16:40:33 +01:00
}
mss[n] = append(mss[n], v)
2018-03-22 16:40:33 +01:00
}
func completeFileName(dir, name string) (string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}
for _, file := range files {
if file.IsDir() {
continue
}
if strings.HasPrefix(file.Name(), name) {
return filepath.Join(dir, file.Name()), nil
2018-03-22 16:40:33 +01:00
}
}
return "", nil
}
func lessRunes(iRunes, jRunes []rune) bool {
max := len(iRunes)
if max > len(jRunes) {
max = len(jRunes)
}
for idx := 0; idx < max; idx++ {
ir := iRunes[idx]
jr := jRunes[idx]
lir := unicode.ToLower(ir)
ljr := unicode.ToLower(jr)
if lir != ljr {
return lir < ljr
}
// the lowercase runes are the same, so compare the original
if ir != jr {
return ir < jr
}
}
return len(iRunes) < len(jRunes)
}