From 8925460718c575648f366bebf8167c6d09809919 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 19 Feb 2019 11:19:23 +0800 Subject: [PATCH] comment updates of gregex package --- g/text/gregex/gregex.go | 33 ++++++++++++++++++++++++++-- g/text/gregex/gregex_z_bench_test.go | 17 +++++++------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/g/text/gregex/gregex.go b/g/text/gregex/gregex.go index ea51489be..9f39c3093 100644 --- a/g/text/gregex/gregex.go +++ b/g/text/gregex/gregex.go @@ -13,17 +13,25 @@ import ( "regexp" ) +// Quote quotes by replacing special chars in +// to match the rules of regular expression pattern. +// And returns the copy. +// // 转移正则规则字符串,例如:Quote(`[foo]`) 返回 `\[foo\]` func Quote(s string) string { return regexp.QuoteMeta(s) } +// Validate checks whether given regular expression pattern valid. +// // 校验所给定的正则表达式是否符合规范 func Validate(pattern string) error { _, err := getRegexp(pattern) return err } +// IsMatch checks whether given bytes matches . +// // 正则表达式是否匹配 func IsMatch(pattern string, src []byte) bool { if r, err := getRegexp(pattern); err == nil { @@ -32,10 +40,15 @@ func IsMatch(pattern string, src []byte) bool { return false } +// IsMatchString checks whether given string matches . +// +// 判断给定的字符串是否满足正则表达式. func IsMatchString(pattern string, src string) bool { return IsMatch(pattern, []byte(src)) } +// MatchString return bytes slice that matched . +// // 正则匹配,并返回匹配的列表(参数[]byte) func Match(pattern string, src []byte) ([][]byte, error) { if r, err := getRegexp(pattern); err == nil { @@ -45,6 +58,8 @@ func Match(pattern string, src []byte) ([][]byte, error) { } } +// MatchString return strings that matched . +// // 正则匹配,并返回匹配的列表(参数[]string) func MatchString(pattern string, src string) ([]string, error) { if r, err := getRegexp(pattern); err == nil { @@ -54,6 +69,8 @@ func MatchString(pattern string, src string) ([]string, error) { } } +// MatchAll return all bytes slices that matched . +// // 正则匹配,并返回所有匹配的列表(参数[]string) func MatchAll(pattern string, src []byte) ([][][]byte, error) { if r, err := getRegexp(pattern); err == nil { @@ -63,7 +80,9 @@ func MatchAll(pattern string, src []byte) ([][][]byte, error) { } } -// 正则匹配,并返回所有匹配的列表(参数[][]string) +// MatchAllString return all strings that matched . +// +// 正则匹配,并返回所有匹配的列表(参数[][]string). func MatchAllString(pattern string, src string) ([][]string, error) { if r, err := getRegexp(pattern); err == nil { return r.FindAllStringSubmatch(src, -1), nil @@ -72,7 +91,9 @@ func MatchAllString(pattern string, src string) ([][]string, error) { } } -// 正则替换(全部替换) +// ReplaceString replace all matched in bytes with bytes . +// +// 正则替换(全部替换). func Replace(pattern string, replace, src []byte) ([]byte, error) { if r, err := getRegexp(pattern); err == nil { return r.ReplaceAll(src, replace), nil @@ -81,12 +102,17 @@ func Replace(pattern string, replace, src []byte) ([]byte, error) { } } +// ReplaceString replace all matched in string with string . +// // 正则替换(全部替换),字符串 func ReplaceString(pattern, replace, src string) (string, error) { r, e := Replace(pattern, []byte(replace), []byte(src)) return string(r), e } +// ReplaceFunc replace all matched in bytes +// with custom replacement function . +// // 正则替换(全部替换),给定自定义替换方法 func ReplaceFunc(pattern string, src []byte, replaceFunc func(b []byte) []byte) ([]byte, error) { if r, err := getRegexp(pattern); err == nil { @@ -96,6 +122,9 @@ func ReplaceFunc(pattern string, src []byte, replaceFunc func(b []byte) []byte) } } +// ReplaceStringFunc replace all matched in string +// with custom replacement function . +// // 正则替换(全部替换),给定自定义替换方法 func ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) string) (string, error) { bytes, err := ReplaceFunc(pattern, []byte(src), func(bytes []byte) []byte { diff --git a/g/text/gregex/gregex_z_bench_test.go b/g/text/gregex/gregex_z_bench_test.go index 0454c24c7..db72f9d28 100644 --- a/g/text/gregex/gregex_z_bench_test.go +++ b/g/text/gregex/gregex_z_bench_test.go @@ -6,9 +6,10 @@ // go test *.go -bench=".*" -package gregex +package gregex_test import ( + "github.com/gogf/gf/g/text/gregex" "testing" ) @@ -18,42 +19,42 @@ var replace = "johng.cn" func BenchmarkValidate(b *testing.B) { for i := 0; i < b.N; i++ { - Validate(pattern) + gregex.Validate(pattern) } } func BenchmarkIsMatch(b *testing.B) { for i := 0; i < b.N; i++ { - IsMatch(pattern, []byte(src)) + gregex.IsMatch(pattern, []byte(src)) } } func BenchmarkIsMatchString(b *testing.B) { for i := 0; i < b.N; i++ { - IsMatchString(pattern, src) + gregex.IsMatchString(pattern, src) } } func BenchmarkMatchString(b *testing.B) { for i := 0; i < b.N; i++ { - MatchString(pattern, src) + gregex.MatchString(pattern, src) } } func BenchmarkMatchAllString(b *testing.B) { for i := 0; i < b.N; i++ { - MatchAllString(pattern, src) + gregex.MatchAllString(pattern, src) } } func BenchmarkReplace(b *testing.B) { for i := 0; i < b.N; i++ { - Replace(pattern, []byte(replace), []byte(src)) + gregex.Replace(pattern, []byte(replace), []byte(src)) } } func BenchmarkReplaceString(b *testing.B) { for i := 0; i < b.N; i++ { - ReplaceString(pattern, replace, src) + gregex.ReplaceString(pattern, replace, src) } }