Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
145 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Config is user config for http traffics | ||
type Config struct { | ||
level string | ||
filterIP string | ||
filterPort uint16 | ||
host string | ||
uri string | ||
status *IntSet | ||
force bool | ||
pretty bool | ||
output string | ||
} | ||
|
||
// parse int set | ||
func ParseIntSet(str string) (*IntSet, error) { | ||
if str == "" { | ||
return nil, errors.New("empty str") | ||
} | ||
var intSet IntSet | ||
for _, item := range strings.Split(str, ":") { | ||
var numbers = strings.Split(item, "-") | ||
if len(numbers) > 2 { | ||
return nil, errors.New("illegal range str: " + item) | ||
} | ||
if len(numbers) == 1 { | ||
start, err := strconv.Atoi(numbers[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
intSet.ranges = append(intSet.ranges, NewIntRange(start, start)) | ||
} else if len(numbers) == 2 { | ||
start, err := strconv.Atoi(numbers[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
end, err := strconv.Atoi(numbers[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
intSet.ranges = append(intSet.ranges, NewIntRange(start, end)) | ||
} | ||
} | ||
return &intSet, nil | ||
} | ||
|
||
// A set of int values | ||
type IntSet struct { | ||
ranges []IntRange | ||
} | ||
|
||
// Create new IntSet | ||
func NewIntSet(ranges ...IntRange) *IntSet { | ||
return &IntSet{ | ||
ranges: ranges, | ||
} | ||
} | ||
|
||
// implement Stringer | ||
func (s *IntSet) String() string { | ||
var sb strings.Builder | ||
for index, r := range s.ranges { | ||
if index > 0 { | ||
sb.WriteRune(':') | ||
} | ||
if r.Start == r.End { | ||
sb.WriteString(strconv.Itoa(r.Start)) | ||
} else { | ||
sb.WriteString(strconv.Itoa(r.Start)) | ||
sb.WriteRune('-') | ||
sb.WriteString(strconv.Itoa(r.End)) | ||
} | ||
} | ||
return sb.String() | ||
} | ||
|
||
// If this set contains int value | ||
func (s *IntSet) Contains(value int) bool { | ||
for _, r := range s.ranges { | ||
if r.Contains(value) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Range of int value | ||
type IntRange struct { | ||
Start int // inclusive | ||
End int // inclusive | ||
} | ||
|
||
// Create new int range | ||
func NewIntRange(start int, end int) IntRange { | ||
return IntRange{ | ||
Start: start, | ||
End: end, | ||
} | ||
} | ||
|
||
// If this range contains the value | ||
func (r *IntRange) Contains(value int) bool { | ||
return value >= r.Start && value <= r.End | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import "testing" | ||
import "github.com/stretchr/testify/assert" | ||
|
||
func TestIntSet_String(t *testing.T) { | ||
assert.Equal(t, "1", NewIntSet(NewIntRange(1, 1)).String()) | ||
assert.Equal(t, "1:1-2", NewIntSet(NewIntRange(1, 1), NewIntRange(1, 2)).String()) | ||
} | ||
|
||
func TestParseIntSet(t *testing.T) { | ||
intRange, err := ParseIntSet("1:1-2") | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1, intRange.ranges[0].Start) | ||
assert.Equal(t, 1, intRange.ranges[0].End) | ||
assert.Equal(t, 1, intRange.ranges[1].Start) | ||
assert.Equal(t, 2, intRange.ranges[1].End) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters