diff --git a/TODO.MD b/TODO.MD index e9a5a215f..f12ac6b0c 100644 --- a/TODO.MD +++ b/TODO.MD @@ -49,7 +49,7 @@ 1. gconv针对struct的转换增加json tag支持,gconv.Map默认也支持json tag, 完善开发文档; 1. 增加SO_REUSEPORT的支持; 1. 权限管理模块; - +1. 从ghttp中剥离SESSION功能构成单独的模块gsession; diff --git a/g/util/gregex/gregex.go b/g/util/gregex/gregex.go index 10b2ddb1d..ccfce167c 100644 --- a/g/util/gregex/gregex.go +++ b/g/util/gregex/gregex.go @@ -53,7 +53,16 @@ func IsMatchString(pattern string, src string) bool { return IsMatch(pattern, []byte(src)) } -// 正则匹配,并返回匹配的列表 +// 正则匹配,并返回匹配的列表(参数[]byte) +func Match(pattern string, src []byte) ([][]byte, error) { + if r, err := getRegexp(pattern); err == nil { + return r.FindSubmatch(src), nil + } else { + return nil, err + } +} + +// 正则匹配,并返回匹配的列表(参数[]string) func MatchString(pattern string, src string) ([]string, error) { if r, err := getRegexp(pattern); err == nil { return r.FindStringSubmatch(src), nil @@ -62,6 +71,16 @@ func MatchString(pattern string, src string) ([]string, error) { } } +// 正则匹配,并返回所有匹配的列表(参数[]string) +func MatchAll(pattern string, src []byte) ([][][]byte, error) { + if r, err := getRegexp(pattern); err == nil { + return r.FindAllSubmatch(src, -1), nil + } else { + return nil, err + } +} + +// 正则匹配,并返回所有匹配的列表(参数[][]string) func MatchAllString(pattern string, src string) ([][]string, error) { if r, err := getRegexp(pattern); err == nil { return r.FindAllStringSubmatch(src, -1), nil @@ -86,18 +105,18 @@ func ReplaceString(pattern, replace, src string) (string, error) { } // 正则替换(全部替换),给定自定义替换方法 -func ReplaceFunc(pattern string, src []byte, repl func(b []byte) []byte) ([]byte, error) { +func ReplaceFunc(pattern string, src []byte, replaceFunc func(b []byte) []byte) ([]byte, error) { if r, err := getRegexp(pattern); err == nil { - return r.ReplaceAllFunc(src, repl), nil + return r.ReplaceAllFunc(src, replaceFunc), nil } else { return nil, err } } // 正则替换(全部替换),给定自定义替换方法 -func ReplaceStringFunc(pattern string, src string, repl func(s string) string) (string, error) { +func ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) string) (string, error) { bytes, err := ReplaceFunc(pattern, []byte(src), func(bytes []byte) []byte { - return []byte(repl(string(bytes))) + return []byte(replaceFunc(string(bytes))) }) return string(bytes), err } \ No newline at end of file diff --git a/g/util/gregex/gregex_test.go b/g/util/gregex/gregex_test.go deleted file mode 100644 index 360694dbd..000000000 --- a/g/util/gregex/gregex_test.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2017-2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://gitee.com/johng/gf. - -// go test *.go -bench=".*" - -package gregex - -import ( - "testing" -) - -var pattern = `(.+):(\d+)` -var src = "johng.cn:80" -var replace = "johng.cn" - -func BenchmarkValidate(b *testing.B) { - for i := 0; i < b.N; i++ { - Validate(pattern) - } -} - -func BenchmarkIsMatch(b *testing.B) { - for i := 0; i < b.N; i++ { - IsMatch(pattern, []byte(src)) - } -} - -func BenchmarkIsMatchString(b *testing.B) { - for i := 0; i < b.N; i++ { - IsMatchString(pattern, src) - } -} - -func BenchmarkMatchString(b *testing.B) { - for i := 0; i < b.N; i++ { - MatchString(pattern, src) - } -} - -func BenchmarkMatchAllString(b *testing.B) { - for i := 0; i < b.N; i++ { - MatchAllString(pattern, src) - } -} - -func BenchmarkReplace(b *testing.B) { - for i := 0; i < b.N; i++ { - Replace(pattern, []byte(replace), []byte(src)) - } -} - -func BenchmarkReplaceString(b *testing.B) { - for i := 0; i < b.N; i++ { - ReplaceString(pattern, replace, src) - } -} diff --git a/g/util/gregex/gregex_z_bench_test.go b/g/util/gregex/gregex_z_bench_test.go new file mode 100644 index 000000000..7cd54bf19 --- /dev/null +++ b/g/util/gregex/gregex_z_bench_test.go @@ -0,0 +1,74 @@ +// Copyright 2019 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://gitee.com/johng/gf. + +// go test *.go -bench=".*" + +package gregex_test + +import ( + "gitee.com/johng/gf/g/util/gregex" + "testing" +) + +var pattern = `(.+):(\d+)` +var src = "johng.cn:80" +var srcBytes = []byte("johng.cn:80") +var replace = "johng.cn" +var replaceBytes = []byte("johng.cn") + +func BenchmarkValidate(b *testing.B) { + for i := 0; i < b.N; i++ { + gregex.Validate(pattern) + } +} + +func BenchmarkIsMatch(b *testing.B) { + for i := 0; i < b.N; i++ { + gregex.IsMatch(pattern, srcBytes) + } +} + +func BenchmarkIsMatchString(b *testing.B) { + for i := 0; i < b.N; i++ { + gregex.IsMatchString(pattern, src) + } +} + +func BenchmarkMatch(b *testing.B) { + for i := 0; i < b.N; i++ { + gregex.Match(pattern, srcBytes) + } +} + +func BenchmarkMatchString(b *testing.B) { + for i := 0; i < b.N; i++ { + gregex.MatchString(pattern, src) + } +} + +func BenchmarkMatchAll(b *testing.B) { + for i := 0; i < b.N; i++ { + gregex.MatchAll(pattern, srcBytes) + } +} + +func BenchmarkMatchAllString(b *testing.B) { + for i := 0; i < b.N; i++ { + gregex.MatchAllString(pattern, src) + } +} + +func BenchmarkReplace(b *testing.B) { + for i := 0; i < b.N; i++ { + gregex.Replace(pattern, replaceBytes, srcBytes) + } +} + +func BenchmarkReplaceString(b *testing.B) { + for i := 0; i < b.N; i++ { + gregex.ReplaceString(pattern, replace, src) + } +}