-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.go
177 lines (149 loc) · 3.6 KB
/
segment.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package oakmux
import (
"fmt"
"strings"
)
type SegmentType uint8
const (
SegmentTypeUnknown = iota
SegmentTypeStatic
SegmentTypeDynamic
SegmentTypeTerminal
SegmentTypeTrailingSlash
)
func (s SegmentType) String() string {
switch s {
case SegmentTypeStatic:
return "static"
case SegmentTypeDynamic:
return "dynamic"
case SegmentTypeTerminal:
return "terminal"
case SegmentTypeTrailingSlash:
return "trailing slash"
default:
return "unknown"
}
}
type Segment interface {
Name() string
Type() SegmentType
Match(path string) (value, remainder string, ok bool)
String() string
}
// NewSegment converts a string to a [Segment] definition.
func NewSegment(segmentDefinition string) (Segment, error) {
switch segmentDefinition {
case "":
return nil, fmt.Errorf("empty path segment")
case "/":
return trailingSlashSegment{}, nil
}
if segmentDefinition[0] == '/' {
segmentDefinition = segmentDefinition[1:]
}
if segmentDefinition[0] == '[' {
tail := len(segmentDefinition) - 1
if segmentDefinition[tail] != ']' {
return nil, fmt.Errorf("dynamic path segment definition %q is missing a closing curly brace", segmentDefinition)
}
segmentDefinition = segmentDefinition[1:tail] // cut off {}
if strings.HasPrefix(segmentDefinition, "...") && tail > 3 {
return terminalSegment(segmentDefinition[3:]), nil
}
return dynamicSegment(segmentDefinition), nil
}
return staticSegment(segmentDefinition), nil
}
type staticSegment []byte
func (s staticSegment) Name() string {
return string(s)
}
func (s staticSegment) Type() SegmentType {
return SegmentTypeStatic
}
func (s staticSegment) Match(path string) (string, string, bool) {
lengthSegment, lengthPath := len(s), len(path)
if lengthSegment <= lengthPath {
// presupposes that s[0] == '/'
for i, c := range s {
if path[i] != c {
break
}
}
if lengthSegment == lengthPath || path[lengthSegment] == '/' {
return string(s), path[:lengthSegment], true
}
}
return "", "", false
}
func (s staticSegment) String() string {
return "/" + string(s)
}
type dynamicSegment string
func (d dynamicSegment) Name() string {
return string(d)
}
func (d dynamicSegment) Type() SegmentType {
return SegmentTypeDynamic
}
func (d dynamicSegment) Match(path string) (string, string, bool) {
// if path == "" || path == "." {
// return "", "", true
// }
if path[0] == '/' {
path = path[1:] // ignore leading slash
}
var (
c byte
i int
)
for i, c = range []byte(path) {
if c == '/' {
break
}
}
return path[:i], path[i:], true
}
func (d dynamicSegment) String() string {
return "/[" + string(d) + "]"
}
type terminalSegment string
func (t terminalSegment) Name() string {
return string(t)
}
func (t terminalSegment) Type() SegmentType {
return SegmentTypeTerminal
}
func (t terminalSegment) Match(path string) (string, string, bool) {
if path[0] == '/' {
path = path[1:] // ignore leading slash
}
tail := len(path) - 1
if tail == -1 { // path == ""
return "", "", false // no content
}
if path[tail] == '/' {
if tail >= 1 && path[tail-1] == '/' {
return "", "", false // multi-slash tail, like "...//"
}
return path[:tail], path[tail:], true // trailing slash
}
return path, "", true
}
func (t terminalSegment) String() string {
return "/[..." + string(t) + "]"
}
type trailingSlashSegment struct{}
func (t trailingSlashSegment) Name() string {
return ""
}
func (t trailingSlashSegment) Type() SegmentType {
return SegmentTypeTrailingSlash
}
func (t trailingSlashSegment) Match(path string) (string, string, bool) {
return "", "", path == "/"
}
func (t trailingSlashSegment) String() string {
return "/"
}