-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathstatus_code_range.go
More file actions
40 lines (31 loc) · 684 Bytes
/
status_code_range.go
File metadata and controls
40 lines (31 loc) · 684 Bytes
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
package main
import (
"fmt"
"strconv"
"strings"
)
type statusCodeRange struct {
start int
end int
}
func parseStatusCodeRange(s string) (*statusCodeRange, error) {
if c, err := strconv.Atoi(s); err == nil {
return &statusCodeRange{c, c + 1}, nil
}
ss := strings.Split(s, "..")
if len(ss) != 2 {
return nil, fmt.Errorf("invalid status code range: %v", s)
}
cs := []int{0, 0}
for i, s := range ss {
c, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("invalid status code: %v", s)
}
cs[i] = c
}
return &statusCodeRange{cs[0], cs[1]}, nil
}
func (r statusCodeRange) Contains(code int) bool {
return code >= r.start && code < r.end
}