-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_range.go
96 lines (84 loc) · 1.88 KB
/
line_range.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-FileCopyrightText: 2023 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package awwan
import (
"strconv"
"strings"
)
// lineRange contains the start and end of line number.
// For single line, the Start and End must be equal.
// For range that does not have an end, the End value must be zero.
type lineRange struct {
list []linePosition
}
// parseLineRange parse a line range in the following format,
//
// line-range = start ["-" [end]] *("," line-range)
//
// The start must be greater than zero.
// The end is optional, if its exist it must be equal or greater than start.
//
// The next line-range must not overlap with previous range, otherwise it will
// be skipped.
func parseLineRange(raw string) (lr lineRange) {
raw = strings.TrimSpace(raw)
if len(raw) == 0 {
return lr
}
var (
listRange = strings.Split(raw, ",")
rawPos string
start int64
end int64
err error
ok bool
)
for _, rawPos = range listRange {
rawPos = strings.TrimSpace(rawPos)
if len(rawPos) == 0 {
continue
}
var pos = strings.Split(rawPos, "-")
if len(pos) > 2 {
// Invalid range.
continue
}
start, err = strconv.ParseInt(pos[0], 10, 64)
if err != nil || start <= 0 {
continue
}
if len(pos) == 1 {
lr.add(start, start)
continue
}
if len(pos[1]) == 0 {
ok = lr.add(start, 0)
if ok {
break
}
continue
}
end, err = strconv.ParseInt(pos[1], 10, 64)
if err != nil || end < start {
continue
}
lr.add(start, end)
}
return lr
}
// add new position to line range.
// It will return true if new position is added to the list.
func (lr *lineRange) add(start, end int64) bool {
var pos linePosition
for _, pos = range lr.list {
if start <= pos.start {
return false
}
if start <= pos.end {
return false
}
continue
}
lr.list = append(lr.list, linePosition{start: start, end: end})
return true
}