Skip to content

Commit 4816b53

Browse files
committed
:spark: 2020 now with Go Modules support
1 parent 6a30e5c commit 4816b53

8 files changed

+267
-228
lines changed

.travis.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
language: go
22

33
go:
4-
- 1.2
5-
- 1.3
64
- 1.4
5+
- 1.12
6+
- 1.13
77
- tip
88

99
script:
10-
- go get launchpad.net/gocheck
1110
- go test

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015-19 Carlos Cobo
3+
Copyright (c) 2015-20 Carlos Cobo
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+42-42
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Interop with [regexp](http://golang.org/pkg/regexp/) for backwards compatibility
99
The recommended way to install substring is by using `go get`:
1010

1111
```
12-
go get -t gopkg.in/toqueteos/substring.v1
12+
go get github.com/toqueteos/substring
1313
```
1414

15-
The `-t` flag is for fetching test dependencies ([gocheck](https://gopkg.in/check.v1)).
15+
Go Modules are supported!
1616

1717
## Examples
1818

@@ -22,61 +22,61 @@ A basic example with two matchers:
2222
package main
2323

2424
import (
25-
"fmt"
26-
"regexp"
25+
"fmt"
26+
"regexp"
2727

28-
"gopkg.in/toqueteos/substring.v1"
28+
"github.com/toqueteos/substring/v2"
2929
)
3030

3131
func main() {
32-
m1 := substring.After("assets/", substring.Or(
33-
substring.Has("jquery"),
34-
substring.Has("angular"),
35-
substring.Suffixes(".js", ".css", ".html"),
36-
))
37-
fmt.Println(m1.Match("assets/angular/foo/bar")) //Prints: true
38-
fmt.Println(m1.Match("assets/js/file.js")) //Prints: true
39-
fmt.Println(m1.Match("assets/style/bar.css")) //Prints: true
40-
fmt.Println(m1.Match("assets/foo/bar.html")) //Prints: false
41-
fmt.Println(m1.Match("assets/js/qux.json")) //Prints: false
42-
fmt.Println(m1.Match("core/file.html")) //Prints: false
43-
fmt.Println(m1.Match("foobar/that.jsx")) //Prints: false
44-
45-
m2 := substring.After("vendor/", substring.Suffixes(".css", ".js", ".less"))
46-
47-
fmt.Println(m2.Match("foo/vendor/bar/qux.css")) //Prints: true
48-
fmt.Println(m2.Match("foo/var/qux.less")) //Prints: false
49-
50-
re := regexp.MustCompile(`vendor\/.*\.(css|js|less)$`)
51-
fmt.Println(re.MatchString("foo/vendor/bar/qux.css")) //Prints: true
52-
fmt.Println(re.MatchString("foo/var/qux.less")) //Prints: false
32+
m1 := substring.After("assets/", substring.Or(
33+
substring.Has("jquery"),
34+
substring.Has("angular"),
35+
substring.Suffixes(".js", ".css", ".html"),
36+
))
37+
fmt.Println(m1.Match("assets/angular/foo/bar")) // Prints: true
38+
fmt.Println(m1.Match("assets/js/file.js")) // Prints: true
39+
fmt.Println(m1.Match("assets/style/bar.css")) // Prints: true
40+
fmt.Println(m1.Match("assets/foo/bar.html")) // Prints: true
41+
fmt.Println(m1.Match("assets/js/qux.json")) // Prints: false
42+
fmt.Println(m1.Match("core/file.html")) // Prints: false
43+
fmt.Println(m1.Match("foobar/that.jsx")) // Prints: false
44+
fmt.Println()
45+
46+
m2 := substring.After("vendor/", substring.Suffixes(".css", ".js", ".less"))
47+
fmt.Println(m2.Match("foo/vendor/bar/qux.css")) // Prints: true
48+
fmt.Println(m2.Match("foo/var/qux.less")) // Prints: false
49+
fmt.Println()
50+
51+
re := regexp.MustCompile(`vendor\/.*\.(css|js|less)$`)
52+
fmt.Println(re.MatchString("foo/vendor/bar/qux.css")) // Prints: true
53+
fmt.Println(re.MatchString("foo/var/qux.less")) // Prints: false
5354
}
5455
```
5556

5657
## How fast?
5758

5859
It may vary depending on your use case but 1~2 orders of magnitude faster than `regexp` is pretty common.
5960

60-
Test it out for yourself by running `go test -check.b`!
61+
Test it out for yourself by running `go test -bench .`!
6162

6263
```
63-
$ go test -check.b
64-
PASS: lib_test.go:18: LibSuite.BenchmarkExample1 10000000 221 ns/op
65-
PASS: lib_test.go:23: LibSuite.BenchmarkExample2 10000000 229 ns/op
66-
PASS: lib_test.go:28: LibSuite.BenchmarkExample3 10000000 216 ns/op
67-
PASS: lib_test.go:33: LibSuite.BenchmarkExample4 10000000 208 ns/op
68-
PASS: lib_test.go:38: LibSuite.BenchmarkExample5 20000000 82.1 ns/op
69-
PASS: lib_test.go:48: LibSuite.BenchmarkExampleRe1 500000 4136 ns/op
70-
PASS: lib_test.go:53: LibSuite.BenchmarkExampleRe2 500000 5222 ns/op
71-
PASS: lib_test.go:58: LibSuite.BenchmarkExampleRe3 500000 5116 ns/op
72-
PASS: lib_test.go:63: LibSuite.BenchmarkExampleRe4 500000 4020 ns/op
73-
PASS: lib_test.go:68: LibSuite.BenchmarkExampleRe5 10000000 226 ns/op
74-
OK: 10 passed
64+
$ go test -bench .
65+
pkg: github.com/toqueteos/substring
66+
BenchmarkExample1-16 30759529 38.4 ns/op
67+
BenchmarkExample2-16 26659675 40.0 ns/op
68+
BenchmarkExample3-16 30760317 37.7 ns/op
69+
BenchmarkExample4-16 31566652 36.8 ns/op
70+
BenchmarkExample5-16 123704845 9.70 ns/op
71+
BenchmarkExampleRe1-16 2739574 436 ns/op
72+
BenchmarkExampleRe2-16 2494791 480 ns/op
73+
BenchmarkExampleRe3-16 1681654 713 ns/op
74+
BenchmarkExampleRe4-16 2205490 540 ns/op
75+
BenchmarkExampleRe5-16 19673001 55.0 ns/op
7576
PASS
76-
ok gopkg.in/toqueteos/substring.v1 23.471s
77+
ok github.com/toqueteos/substring 15.016s
7778
```
7879

79-
License
80-
-------
80+
## License
8181

8282
MIT, see [LICENSE](LICENSE)

bytes_test.go

+95-77
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,129 @@
11
package substring
22

3-
import . "gopkg.in/check.v1"
3+
import (
4+
"testing"
5+
6+
"github.com/matryer/is"
7+
)
8+
9+
func TestBytesAny(t *testing.T) {
10+
is := is.New(t)
411

5-
func (s *LibSuite) TestBytesAny(c *C) {
612
a := BytesAny("foo") // search s in foo
7-
c.Assert(a.MatchIndex([]byte("f")), Equals, 1)
8-
c.Assert(a.MatchIndex([]byte("foo")), Equals, 3)
9-
c.Assert(a.MatchIndex([]byte("foobar")), Equals, -1)
10-
c.Assert(a.MatchIndex([]byte("p")), Equals, -1)
13+
is.Equal(a.MatchIndex([]byte("f")), 1)
14+
is.Equal(a.MatchIndex([]byte("foo")), 3)
15+
is.Equal(a.MatchIndex([]byte("foobar")), -1)
16+
is.Equal(a.MatchIndex([]byte("p")), -1)
1117
}
1218

13-
func (s *LibSuite) TestBytesHas(c *C) {
19+
func TestBytesHas(t *testing.T) {
20+
is := is.New(t)
21+
1422
h := BytesHas("foo") // search foo in s
15-
c.Assert(h.MatchIndex([]byte("foo")), Equals, 3)
16-
c.Assert(h.MatchIndex([]byte("foobar")), Equals, 3)
17-
c.Assert(h.MatchIndex([]byte("f")), Equals, -1)
23+
is.Equal(h.MatchIndex([]byte("foo")), 3)
24+
is.Equal(h.MatchIndex([]byte("foobar")), 3)
25+
is.Equal(h.MatchIndex([]byte("f")), -1)
1826
}
1927

20-
func (s *LibSuite) TestBytesPrefix(c *C) {
28+
func TestBytesPrefix(t *testing.T) {
29+
is := is.New(t)
30+
2131
p := BytesPrefix("foo")
22-
c.Assert(p.Match([]byte("foo")), Equals, true)
23-
c.Assert(p.Match([]byte("foobar")), Equals, true)
24-
c.Assert(p.Match([]byte("barfoo")), Equals, false)
25-
c.Assert(p.Match([]byte(" foo")), Equals, false)
26-
c.Assert(p.Match([]byte("bar")), Equals, false)
27-
c.Assert(p.MatchIndex([]byte("foo")), Equals, 3)
28-
c.Assert(p.MatchIndex([]byte("foobar")), Equals, 3)
29-
c.Assert(p.MatchIndex([]byte("barfoo")), Equals, -1)
30-
c.Assert(p.MatchIndex([]byte(" foo")), Equals, -1)
31-
c.Assert(p.MatchIndex([]byte("bar")), Equals, -1)
32+
is.True(p.Match([]byte("foo")))
33+
is.True(p.Match([]byte("foobar")))
34+
is.Equal(p.Match([]byte("barfoo")), false)
35+
is.Equal(p.Match([]byte(" foo")), false)
36+
is.Equal(p.Match([]byte("bar")), false)
37+
is.Equal(p.MatchIndex([]byte("foo")), 3)
38+
is.Equal(p.MatchIndex([]byte("foobar")), 3)
39+
is.Equal(p.MatchIndex([]byte("barfoo")), -1)
40+
is.Equal(p.MatchIndex([]byte(" foo")), -1)
41+
is.Equal(p.MatchIndex([]byte("bar")), -1)
3242
ps := BytesPrefixes("foo", "barfoo")
33-
c.Assert(ps.Match([]byte("foo")), Equals, true)
34-
c.Assert(ps.Match([]byte("barfoo")), Equals, true)
35-
c.Assert(ps.Match([]byte("qux")), Equals, false)
36-
c.Assert(ps.MatchIndex([]byte("foo")), Equals, 2)
37-
c.Assert(ps.MatchIndex([]byte("barfoo")), Equals, 5)
38-
c.Assert(ps.MatchIndex([]byte("qux")), Equals, -1)
43+
is.True(ps.Match([]byte("foo")))
44+
is.True(ps.Match([]byte("barfoo")))
45+
is.Equal(ps.Match([]byte("qux")), false)
46+
is.Equal(ps.MatchIndex([]byte("foo")), 2)
47+
is.Equal(ps.MatchIndex([]byte("barfoo")), 5)
48+
is.Equal(ps.MatchIndex([]byte("qux")), -1)
3949
}
4050

41-
func (s *LibSuite) TestBytesSuffix(c *C) {
51+
func TestBytesSuffix(t *testing.T) {
52+
is := is.New(t)
53+
4254
p := BytesSuffix("foo")
43-
c.Assert(p.Match([]byte("foo")), Equals, true)
44-
c.Assert(p.Match([]byte("barfoo")), Equals, true)
45-
c.Assert(p.Match([]byte("foobar")), Equals, false)
46-
c.Assert(p.Match([]byte("foo ")), Equals, false)
47-
c.Assert(p.Match([]byte("bar")), Equals, false)
48-
c.Assert(p.MatchIndex([]byte("foo")), Equals, 3)
49-
c.Assert(p.MatchIndex([]byte("barfoo")), Equals, 3)
50-
c.Assert(p.MatchIndex([]byte("foobar")), Equals, -1)
51-
c.Assert(p.MatchIndex([]byte("foo ")), Equals, -1)
52-
c.Assert(p.MatchIndex([]byte("bar")), Equals, -1)
55+
is.True(p.Match([]byte("foo")))
56+
is.True(p.Match([]byte("barfoo")))
57+
is.Equal(p.Match([]byte("foobar")), false)
58+
is.Equal(p.Match([]byte("foo ")), false)
59+
is.Equal(p.Match([]byte("bar")), false)
60+
is.Equal(p.MatchIndex([]byte("foo")), 3)
61+
is.Equal(p.MatchIndex([]byte("barfoo")), 3)
62+
is.Equal(p.MatchIndex([]byte("foobar")), -1)
63+
is.Equal(p.MatchIndex([]byte("foo ")), -1)
64+
is.Equal(p.MatchIndex([]byte("bar")), -1)
5365
ps := BytesSuffixes("foo", "foobar")
54-
c.Assert(ps.Match([]byte("foo")), Equals, true)
55-
c.Assert(ps.Match([]byte("foobar")), Equals, true)
56-
c.Assert(ps.Match([]byte("qux")), Equals, false)
57-
c.Assert(ps.MatchIndex([]byte("foo")), Equals, 2)
58-
c.Assert(ps.MatchIndex([]byte("foobar")), Equals, 5)
59-
c.Assert(ps.MatchIndex([]byte("qux")), Equals, -1)
66+
is.True(ps.Match([]byte("foo")))
67+
is.True(ps.Match([]byte("foobar")))
68+
is.Equal(ps.Match([]byte("qux")), false)
69+
is.Equal(ps.MatchIndex([]byte("foo")), 2)
70+
is.Equal(ps.MatchIndex([]byte("foobar")), 5)
71+
is.Equal(ps.MatchIndex([]byte("qux")), -1)
6072
ps2 := BytesSuffixes(".foo", ".bar", ".qux")
61-
c.Assert(ps2.Match([]byte("bar.foo")), Equals, true)
62-
c.Assert(ps2.Match([]byte("bar.js")), Equals, false)
63-
c.Assert(ps2.Match([]byte("foo/foo.bar")), Equals, true)
64-
c.Assert(ps2.Match([]byte("foo/foo.js")), Equals, false)
65-
c.Assert(ps2.Match([]byte("foo/foo/bar.qux")), Equals, true)
66-
c.Assert(ps2.Match([]byte("foo/foo/bar.css")), Equals, false)
73+
is.True(ps2.Match([]byte("bar.foo")))
74+
is.Equal(ps2.Match([]byte("bar.js")), false)
75+
is.True(ps2.Match([]byte("foo/foo.bar")))
76+
is.Equal(ps2.Match([]byte("foo/foo.js")), false)
77+
is.True(ps2.Match([]byte("foo/foo/bar.qux")))
78+
is.Equal(ps2.Match([]byte("foo/foo/bar.css")), false)
6779
}
6880

69-
func (s *LibSuite) TestBytesExact(c *C) {
81+
func TestBytesExact(t *testing.T) {
82+
is := is.New(t)
83+
7084
a := BytesExact("foo")
71-
c.Assert(a.Match([]byte("foo")), Equals, true)
72-
c.Assert(a.Match([]byte("bar")), Equals, false)
73-
c.Assert(a.Match([]byte("qux")), Equals, false)
85+
is.True(a.Match([]byte("foo")))
86+
is.Equal(a.Match([]byte("bar")), false)
87+
is.Equal(a.Match([]byte("qux")), false)
7488
}
7589

76-
func (s *LibSuite) TestBytesAfter(c *C) {
90+
func TestBytesAfter(t *testing.T) {
91+
is := is.New(t)
92+
7793
a1 := BytesAfter("foo", BytesExact("bar"))
78-
c.Assert(a1.Match([]byte("foobar")), Equals, true)
79-
c.Assert(a1.Match([]byte("foo_bar")), Equals, false)
94+
is.True(a1.Match([]byte("foobar")))
95+
is.Equal(a1.Match([]byte("foo_bar")), false)
8096
a2 := BytesAfter("foo", BytesHas("bar"))
81-
c.Assert(a2.Match([]byte("foobar")), Equals, true)
82-
c.Assert(a2.Match([]byte("foo_bar")), Equals, true)
83-
c.Assert(a2.Match([]byte("_foo_bar")), Equals, true)
84-
c.Assert(a2.Match([]byte("foo_nope")), Equals, false)
85-
c.Assert(a2.Match([]byte("qux")), Equals, false)
97+
is.True(a2.Match([]byte("foobar")))
98+
is.True(a2.Match([]byte("foo_bar")))
99+
is.True(a2.Match([]byte("_foo_bar")))
100+
is.Equal(a2.Match([]byte("foo_nope")), false)
101+
is.Equal(a2.Match([]byte("qux")), false)
86102
a3 := BytesAfter("foo", BytesPrefixes("bar", "qux"))
87-
c.Assert(a3.Match([]byte("foobar")), Equals, true)
88-
c.Assert(a3.Match([]byte("fooqux")), Equals, true)
89-
c.Assert(a3.Match([]byte("foo bar")), Equals, false)
90-
c.Assert(a3.Match([]byte("foo_qux")), Equals, false)
103+
is.True(a3.Match([]byte("foobar")))
104+
is.True(a3.Match([]byte("fooqux")))
105+
is.Equal(a3.Match([]byte("foo bar")), false)
106+
is.Equal(a3.Match([]byte("foo_qux")), false)
91107
}
92108

93-
func (s *LibSuite) TestBytesSuffixGroup(c *C) {
109+
func TestBytesSuffixGroup(t *testing.T) {
110+
is := is.New(t)
111+
94112
sg1 := BytesSuffixGroup(".foo", BytesHas("bar"))
95-
c.Assert(sg1.Match([]byte("bar.foo")), Equals, true)
96-
c.Assert(sg1.Match([]byte("barqux.foo")), Equals, true)
97-
c.Assert(sg1.Match([]byte(".foo.bar")), Equals, false)
113+
is.True(sg1.Match([]byte("bar.foo")))
114+
is.True(sg1.Match([]byte("barqux.foo")))
115+
is.Equal(sg1.Match([]byte(".foo.bar")), false)
98116
sg2 := BytesSuffixGroup(`.foo`,
99117
BytesAfter(`bar`, BytesHas("qux")),
100118
)
101-
c.Assert(sg2.Match([]byte("barqux.foo")), Equals, true)
102-
c.Assert(sg2.Match([]byte("barbarqux.foo")), Equals, true)
103-
c.Assert(sg2.Match([]byte("bar.foo")), Equals, false)
104-
c.Assert(sg2.Match([]byte("foo.foo")), Equals, false)
119+
is.True(sg2.Match([]byte("barqux.foo")))
120+
is.True(sg2.Match([]byte("barbarqux.foo")))
121+
is.Equal(sg2.Match([]byte("bar.foo")), false)
122+
is.Equal(sg2.Match([]byte("foo.foo")), false)
105123
sg3 := BytesSuffixGroup(`.foo`,
106124
BytesAfter(`bar`, BytesRegexp(`\d+`)),
107125
)
108-
c.Assert(sg3.Match([]byte("bar0.foo")), Equals, true)
109-
c.Assert(sg3.Match([]byte("bar.foo")), Equals, false)
110-
c.Assert(sg3.Match([]byte("bar0.qux")), Equals, false)
126+
is.True(sg3.Match([]byte("bar0.foo")))
127+
is.Equal(sg3.Match([]byte("bar.foo")), false)
128+
is.Equal(sg3.Match([]byte("bar0.qux")), false)
111129
}

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/toqueteos/substring/v2
2+
3+
go 1.13
4+
5+
require (
6+
github.com/matryer/is v1.2.0
7+
github.com/toqueteos/trie v1.0.0
8+
)

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
2+
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
3+
github.com/toqueteos/trie v1.0.0 h1:8i6pXxNUXNRAqP246iibb7w/pSFquNTQ+uNfriG7vlk=
4+
github.com/toqueteos/trie v1.0.0/go.mod h1:Ywk48QhEqhU1+DwhMkJ2x7eeGxDHiGkAdc9+0DYcbsM=

0 commit comments

Comments
 (0)