-
Notifications
You must be signed in to change notification settings - Fork 5
/
path.go
77 lines (65 loc) · 1.26 KB
/
path.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
package path
import (
"errors"
"fmt"
"log"
"path"
"strings"
)
type Path struct {
Vhost string
FsPath string
}
type State int
const (
Initial State = iota
AfterOtaruScheme
BeforeVhost
BeforeFsPath
End
)
const OtaruScheme = "otaru:"
const VhostLocal = "[local]"
func advance(st *State, p *Path, s string) (string, error) {
switch *st {
case Initial:
if strings.HasPrefix(s, OtaruScheme) {
*st = AfterOtaruScheme
return s[len(OtaruScheme):], nil
}
fallthrough
case AfterOtaruScheme:
if strings.HasPrefix(s, "//") {
*st = BeforeVhost
return s[2:], nil
}
*st = BeforeFsPath
return s, nil
case BeforeVhost:
i := strings.Index(s, "/")
if i < 0 {
*st = End
return s, fmt.Errorf("parser: Expected vhost/path, but got \"%s\"", s)
}
*st = BeforeFsPath
p.Vhost, s = s[:i], s[i:]
return s, nil
case BeforeFsPath:
p.FsPath = path.Clean(s)
*st = End
return "", nil
case End:
return "", nil
default:
log.Panicf("Unknown state: %q", *st)
}
return "", errors.New("NOTREACHED")
}
func Parse(s string) (Path, error) {
p := Path{Vhost: "default", FsPath: "/"}
var err error
for st := Initial; st != End; s, err = advance(&st, &p, s) {
// fmt.Printf("state: %v, path: %+v, left: \"%s\"\n", st, p, s)
}
return p, err
}