comment updates of gregex package

This commit is contained in:
John
2019-02-19 11:19:23 +08:00
parent 9797701881
commit 8925460718
2 changed files with 40 additions and 10 deletions

View File

@ -13,17 +13,25 @@ import (
"regexp"
)
// Quote quotes <s> by replacing special chars in <s>
// 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 <pattern> valid.
//
// 校验所给定的正则表达式是否符合规范
func Validate(pattern string) error {
_, err := getRegexp(pattern)
return err
}
// IsMatch checks whether given bytes <src> matches <pattern>.
//
// 正则表达式是否匹配
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 <src> matches <pattern>.
//
// 判断给定的字符串<src>是否满足正则表达式<pattern>.
func IsMatchString(pattern string, src string) bool {
return IsMatch(pattern, []byte(src))
}
// MatchString return bytes slice that matched <pattern>.
//
// 正则匹配,并返回匹配的列表(参数[]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 <pattern>.
//
// 正则匹配,并返回匹配的列表(参数[]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 <pattern>.
//
// 正则匹配,并返回所有匹配的列表(参数[]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 <pattern>.
//
// 正则匹配,并返回所有匹配的列表(参数[][]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 <pattern> in bytes <src> with bytes <replace>.
//
// 正则替换(全部替换).
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 <pattern> in string <src> with string <replace>.
//
// 正则替换(全部替换),字符串
func ReplaceString(pattern, replace, src string) (string, error) {
r, e := Replace(pattern, []byte(replace), []byte(src))
return string(r), e
}
// ReplaceFunc replace all matched <pattern> in bytes <src>
// with custom replacement function <replaceFunc>.
//
// 正则替换(全部替换),给定自定义替换方法
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 <pattern> in string <src>
// with custom replacement function <replaceFunc>.
//
// 正则替换(全部替换),给定自定义替换方法
func ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) string) (string, error) {
bytes, err := ReplaceFunc(pattern, []byte(src), func(bytes []byte) []byte {

View File

@ -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)
}
}