2018-03-22 16:40:33 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2018-04-17 02:32:31 +02:00
|
|
|
"path/filepath"
|
2018-03-22 16:40:33 +01:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
2018-05-08 00:01:39 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-08 00:01:39 +02:00
|
|
|
func (mss mapStringSet) Add(n string, v string) {
|
|
|
|
_, ok := mss[n]
|
2018-03-22 16:40:33 +01:00
|
|
|
if !ok {
|
2018-05-08 00:01:39 +02:00
|
|
|
mss[n] = make(stringSet)
|
2018-03-22 16:40:33 +01:00
|
|
|
}
|
2018-05-08 00:01:39 +02:00
|
|
|
mss[n].set(v)
|
2018-03-22 16:40:33 +01:00
|
|
|
}
|
|
|
|
|
2018-05-08 00:01:39 +02:00
|
|
|
func (mss mapStringSlice) Add(n string, v string) {
|
|
|
|
_, ok := mss[n]
|
2018-03-22 16:40:33 +01:00
|
|
|
if !ok {
|
2018-05-08 00:01:39 +02:00
|
|
|
mss[n] = make([]string, 0, 1)
|
2018-03-22 16:40:33 +01:00
|
|
|
}
|
2018-05-08 00:01:39 +02: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) {
|
2018-04-17 02:32:31 +02:00
|
|
|
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)
|
|
|
|
}
|