Skip to content

Commit 574fbdb

Browse files
committed
Add string parser enhancement draft
1 parent 85aadab commit 574fbdb

4 files changed

Lines changed: 119 additions & 42 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
go.trace
2+
13
build/
24
tmp/

src/rx.go

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"fmt"
5+
"os"
46
"regexp"
57
"strconv"
68
"strings"
@@ -11,12 +13,26 @@ const (
1113
rxIPv4Fragment = `^(?:\d{1,3}\.)+\d{1,3}$`
1214
)
1315

14-
func cidrNotateSplit(str string) (arr []string) {
15-
if isCIDRNotation(str) {
16-
arr = strings.Split(str, "/")
17-
} else {
18-
arr = []string{str, "32"}
16+
type tNotation struct {
17+
Pre string
18+
Op string
19+
Suf string
20+
}
21+
22+
func notateSplit(str string) (nt tNotation) {
23+
arr := rxSplit(`[\+|\-|\/]`, str)
24+
if len(arr) > 0 {
25+
nt.Pre = arr[0]
1926
}
27+
if len(arr) > 1 {
28+
nt.Suf = arr[1]
29+
}
30+
nt.Op = rxFind(`[\+|\-|\/]`, str)
31+
// if isCIDRNotation(str) {
32+
// arr = strings.Split(str, "/")
33+
// } else {
34+
// arr = []string{str, "32"}
35+
// }
2036
return
2137
}
2238

@@ -32,15 +48,36 @@ func isInt(str string) bool {
3248
}
3349

3450
func isIPv4Full(str string) bool {
35-
match, _ := regexp.MatchString(
36-
rxIPv4Full, strings.Trim(str, "."),
37-
)
38-
return match
51+
return rxMatch(rxIPv4Full, strings.Trim(str, "."))
3952
}
4053

4154
func isIPv4Fragment(str string) bool {
42-
match, _ := regexp.MatchString(
43-
rxIPv4Fragment, strings.Trim(str, "."),
44-
)
45-
return match
55+
return rxMatch(rxIPv4Fragment, strings.Trim(str, "."))
56+
}
57+
58+
func rxCompile(str string) (r *regexp.Regexp) {
59+
var err error
60+
r, err = regexp.Compile(str)
61+
if err != nil {
62+
fmt.Printf("can not compile regex: %s\n", err)
63+
os.Exit(1)
64+
}
65+
return
66+
}
67+
68+
func rxSplit(rx, str string) []string {
69+
re := regexp.MustCompile(rx)
70+
return re.Split(str, -1)
71+
}
72+
73+
func rxFind(rx string, content string) (r string) {
74+
temp := rxCompile(rx)
75+
r = temp.FindString(content)
76+
return
77+
}
78+
79+
func rxMatch(rx string, str string) (b bool) {
80+
re := rxCompile(rx)
81+
b = re.MatchString(str)
82+
return
4683
}

src/rx_test.go

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,32 @@ package main
33
import "testing"
44

55
func TestIsIPFull(t *testing.T) {
6-
assertCidrNotateSplit("192.168.33.1", []string{"192.168.33.1", "32"}, t)
7-
assertCidrNotateSplit("192.168.33.1/30", []string{"192.168.33.1", "30"}, t)
8-
assertCidrNotateSplit("255.255.0.0", []string{"255.255.0.0", "32"}, t)
9-
assertCidrNotateSplit("255.255.0.0/16", []string{"255.255.0.0", "16"}, t)
10-
assertCidrNotateSplit("255.0", []string{"255.0", "32"}, t)
11-
assertCidrNotateSplit("255.0/8", []string{"255.0", "8"}, t)
12-
assertCidrNotateSplit("1", []string{"1", "32"}, t)
13-
assertCidrNotateSplit("1/30", []string{"1", "30"}, t)
6+
assertNotateSplit("192.168.33.1", newNt("192.168.33.1"), t)
7+
assertNotateSplit("192.168.33.1/30", newNt("192.168.33.1", "/", "30"), t)
8+
assertNotateSplit("255.255.0.0", newNt("255.255.0.0"), t)
9+
assertNotateSplit("255.255.0.0/16", newNt("255.255.0.0", "/", "16"), t)
10+
assertNotateSplit("255.0", newNt("255.0"), t)
11+
assertNotateSplit("255.0/8", newNt("255.0", "/", "8"), t)
12+
assertNotateSplit("1", newNt("1"), t)
13+
assertNotateSplit("1/30", newNt("1", "/", "30"), t)
14+
15+
assertNotateSplit("192.168.33.1", newNt("192.168.33.1"), t)
16+
assertNotateSplit("192.168.33.1+30", newNt("192.168.33.1", "+", "30"), t)
17+
assertNotateSplit("255.255.0.0", newNt("255.255.0.0"), t)
18+
assertNotateSplit("255.255.0.0+16", newNt("255.255.0.0", "+", "16"), t)
19+
assertNotateSplit("255.0", newNt("255.0"), t)
20+
assertNotateSplit("255.0+8", newNt("255.0", "+", "8"), t)
21+
assertNotateSplit("1", newNt("1"), t)
22+
assertNotateSplit("1+30", newNt("1", "+", "30"), t)
23+
24+
assertNotateSplit("192.168.33.1", newNt("192.168.33.1"), t)
25+
assertNotateSplit("192.168.33.1-30", newNt("192.168.33.1", "-", "30"), t)
26+
assertNotateSplit("255.255.0.0", newNt("255.255.0.0"), t)
27+
assertNotateSplit("255.255.0.0-16", newNt("255.255.0.0", "-", "16"), t)
28+
assertNotateSplit("255.0", newNt("255.0"), t)
29+
assertNotateSplit("255.0-8", newNt("255.0", "-", "8"), t)
30+
assertNotateSplit("1", newNt("1"), t)
31+
assertNotateSplit("1-30", newNt("1", "-", "30"), t)
1432

1533
assertIsCIDRNotation("192.168.33.1", false, t)
1634
assertIsCIDRNotation("192.168.33.1/30", true, t)
@@ -40,22 +58,14 @@ func TestIsIPFull(t *testing.T) {
4058
assertIsInt("1", true, t)
4159
}
4260

43-
func assertCidrNotateSplit(inp string, exp []string, t *testing.T) {
44-
res := cidrNotateSplit(inp)
45-
if len(res) != len(exp) {
61+
func assertNotateSplit(inp string, exp tNotation, t *testing.T) {
62+
res := notateSplit(inp)
63+
if res.Pre != exp.Pre || res.Op != exp.Op || res.Suf != exp.Suf {
4664
t.Errorf(
47-
"result and expecation differ in length: %s -> %s != %s",
65+
"notateSplit failed: %s -> %s != %s",
4866
inp, exp, res,
4967
)
5068
}
51-
for i := 1; i <= len(res)-1; i++ {
52-
if exp[i] != res[i] {
53-
t.Errorf(
54-
"result and expectation differ: %s -> %s != %s",
55-
inp, exp, res,
56-
)
57-
}
58-
}
5969
}
6070

6171
func assertIsCIDRNotation(inp string, exp bool, t *testing.T) {
@@ -85,3 +95,16 @@ func assertIsInt(inp string, exp bool, t *testing.T) {
8595
t.Errorf("isInt failed: %s -> %v != %v", inp, exp, res)
8696
}
8797
}
98+
99+
func newNt(params ...any) (nt tNotation) {
100+
if len(params) > 0 {
101+
nt.Pre = params[0].(string)
102+
}
103+
if len(params) > 1 {
104+
nt.Op = params[1].(string)
105+
}
106+
if len(params) > 2 {
107+
nt.Suf = params[2].(string)
108+
}
109+
return nt
110+
}

src/stringparse.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
package main
22

3+
import (
4+
"fmt"
5+
)
6+
37
func parseInput(args []string) (cidrs []string) {
48
for _, arg := range args {
5-
cidrSplit := cidrNotateSplit(arg)
6-
app := ""
9+
nt := notateSplit(arg)
710

8-
if isIPv4Full(cidrSplit[0]) {
9-
app = cidrSplit[0]
10-
} else if isIPv4Fragment(cidrSplit[0]) || isInt(cidrSplit[0]) {
11-
if !isIPv4Full(localIP) {
12-
localIP = getLocalIP().String()
11+
app := nt.Pre
12+
switch nt.Op {
13+
case "/", "":
14+
if isIPv4Full(nt.Pre) {
15+
app = nt.Pre
16+
} else if isIPv4Fragment(nt.Pre) || isInt(nt.Pre) {
17+
if !isIPv4Full(localIP) {
18+
localIP = getLocalIP().String()
19+
}
20+
app = replaceTail(localIP, nt.Pre)
21+
}
22+
if nt.Suf != "" {
23+
app += nt.Op + nt.Suf
1324
}
14-
app = replaceTail(localIP, cidrSplit[0])
25+
case "+":
26+
fmt.Println("Tomorrow.")
27+
case "-":
28+
fmt.Println("In two days.")
1529
}
30+
1631
if app != "" {
17-
cidrs = append(cidrs, app+"/"+cidrSplit[1])
32+
cidrs = append(cidrs, app)
1833
}
1934
}
2035
return cidrs

0 commit comments

Comments
 (0)