-
Notifications
You must be signed in to change notification settings - Fork 47
/
ranges.go
50 lines (43 loc) · 1014 Bytes
/
ranges.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) 2014 The lexer Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lexer // import "github.com/skycoin/cx/goyacc/lexer"
import (
"sort"
"unicode"
)
type rangeSlice []unicode.Range32
// sort.Interface
func (r rangeSlice) Len() int {
return len(r)
}
// sort.Interface
func (pr *rangeSlice) Less(i, j int) bool {
r := *pr
return r[i].Lo < r[j].Lo
}
// sort.Interface
func (pr *rangeSlice) Swap(i, j int) {
r := *pr
r[i], r[j] = r[j], r[i]
}
// Limited normalization of ranges produced by ParseRE. Doesn't handle stride != 1.
func (pr *rangeSlice) normalize() {
sort.Sort(pr)
r := *pr
for ok := false; !ok; {
ok = true
for i := len(r) - 1; i > 0; i-- {
y, z := r[i-1], r[i]
if y.Hi >= z.Lo || y.Hi+1 == z.Lo { // overlap or join point found
ok = false
if z.Hi > y.Hi {
y.Hi = z.Hi
}
r[i-1] = y
r = append(r[:i], r[i+1:]...) // remove z
}
}
}
*pr = r
}