-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
118 lines (111 loc) · 3.16 KB
/
Copy pathparse.go
File metadata and controls
118 lines (111 loc) · 3.16 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"strconv"
"strings"
)
// splitDirRange splits "dir/range" into ("dir/", "range"). If there is
// no "/" the dir part is empty and the whole string is the range. If the
// full expression points to an existing file, it is returned as the dir
// with an empty range — so explicit file paths like "slides/deck.html"
// aren't mis-parsed as dir + range.
func splitDirRange(expr string) (dir, rangeExpr string) {
if expr != "" {
if info, err := appFs.Stat(expr); err == nil {
if info.IsDir() {
if !strings.HasSuffix(expr, "/") {
return expr + "/", ""
}
return expr, ""
}
return expr, ""
}
}
i := strings.LastIndex(expr, "/")
if i < 0 {
return "", expr
}
return expr[:i+1], expr[i+1:]
}
// parseSliceExpr parses a comma-separated list of 1-based slide selectors
// into 0-based indices. Each item is either a single number or a range
// (using : or -). Negative numbers count from the end.
func parseSliceExpr(expr string, length int) ([]int, error) {
var result []int
for _, part := range strings.Split(expr, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
// Try colon range first, then dash range.
start, end, isRange := "", "", false
if idx := strings.Index(part, ":"); idx >= 0 {
start, end = part[:idx], part[idx+1:]
isRange = true
} else {
// For dash: only treat as range separator when preceded by a digit.
for j := 1; j < len(part); j++ {
if part[j] == '-' && part[j-1] >= '0' && part[j-1] <= '9' {
start, end = part[:j], part[j+1:]
isRange = true
break
}
}
}
if isRange {
s, err := resolveIndex(start, length)
if err != nil {
return nil, fmt.Errorf("invalid range start %q: %w", start, err)
}
e, err := resolveIndex(end, length)
if err != nil {
return nil, fmt.Errorf("invalid range end %q: %w", end, err)
}
if s > e {
return nil, fmt.Errorf("invalid range: %d > %d", s+1, e+1)
}
for i := s; i <= e; i++ {
result = append(result, i)
}
} else {
idx, err := resolveIndex(part, length)
if err != nil {
return nil, err
}
result = append(result, idx)
}
}
if len(result) == 0 {
return nil, fmt.Errorf("empty slide expression")
}
return result, nil
}
// resolveInsertIndex converts a 1-based (possibly negative) position string
// to a 0-based insertion index. Valid positions are 1..n+1 (where n is the
// number of slides), with n+1 meaning "append". Negative values count from
// the end: -1 = append, -2 = before last slide, etc.
func resolveInsertIndex(s string, nSlides int) (int, error) {
return resolveIndex(s, nSlides+1)
}
// resolveIndex converts a 1-based (possibly negative) index string to a
// 0-based index. Negative values count from the end (-1 = last).
func resolveIndex(s string, length int) (int, error) {
s = strings.TrimSpace(s)
n, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("invalid number %q", s)
}
if n == 0 {
return 0, fmt.Errorf("index 0 is not valid (1-based)")
}
var idx int
if n > 0 {
idx = n - 1
} else {
idx = length + n
}
if idx < 0 || idx >= length {
return 0, fmt.Errorf("index %d out of range (1..%d)", n, length)
}
return idx, nil
}