Skip to content

Commit

Permalink
updated function usage
Browse files Browse the repository at this point in the history
  • Loading branch information
AspieSoft committed Dec 5, 2022
1 parent b8c8cfa commit 6b62e67
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 104 deletions.
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/AspieSoft/go-regex/v3
module github.com/AspieSoft/go-regex/v4

go 1.18

Expand All @@ -9,4 +9,7 @@ require (
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
)

require github.com/alphadose/haxmap v1.1.0 // indirect
require (
github.com/alphadose/haxmap v1.2.0 // indirect
golang.org/x/exp v0.0.0-20221204150635-6dcec336b2bb // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ github.com/AspieSoft/go-ttlcache v1.2.0 h1:mhpHWs/cl3ByMxMZCm2LUGmF3r/2tKv43sGMp
github.com/AspieSoft/go-ttlcache v1.2.0/go.mod h1:XgT/phj4GBCFyijr1fGcE8eP26zhOq+PiffAp5FYDNs=
github.com/GRbit/go-pcre v1.0.0 h1:Qv/YZ/tr436mFgep3Y0WAzKOZvAKGOGD0cAMwUcsEJo=
github.com/GRbit/go-pcre v1.0.0/go.mod h1:OuMGyux1WcDrKNwDn9MyQLa2kzxQS/xFyVqXFZ/ay0I=
github.com/alphadose/haxmap v1.1.0 h1:M7Dxdr+civMQkWCgDpoktNpLofDBz7XzdS3rF3Y6r4U=
github.com/alphadose/haxmap v1.1.0/go.mod h1:Pq2IXbl9/ytYHfrIAd7rIVtZQ2ezdIhZfvdqOizDeWY=
github.com/alphadose/haxmap v1.2.0 h1:noGrAmCE+gNheZ4KpW+sYj9W5uMcO1UAjbAq9XBOAfM=
github.com/alphadose/haxmap v1.2.0/go.mod h1:rjHw1IAqbxm0S3U5tD16GoKsiAd8FWx5BJ2IYqXwgmM=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
golang.org/x/exp v0.0.0-20221204150635-6dcec336b2bb h1:QIsP/NmClBICkqnJ4rSIhnrGiGR7Yv9ZORGGnmmLTPk=
golang.org/x/exp v0.0.0-20221204150635-6dcec336b2bb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
15 changes: 9 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This package uses the [go-pcre](https://github.com/GRbit/go-pcre) package for be
```go

import (
"github.com/AspieSoft/go-regex/v3"
"github.com/AspieSoft/go-regex/v4"
)

// pre compile a regex into the cache
Expand All @@ -42,7 +42,7 @@ regex.Compile(`re %1 and %2 ... %{12}`, `param 1`, `param 2` ..., `param 12`);
regex.Escape(`(.*)? \$ \\$ \\\$ regex hack failed`)

// run a replace function (most advanced feature)
regex.RepFunc(myByteArray, regex.Compile(`(?flags)re(capture group)`), func(data func(int) []byte) []byte {
regex.Compile(`(?flags)re(capture group)`).RepFunc(myByteArray, func(data func(int) []byte) []byte {
data(0) // get the string
data(1) // get the first capture group

Expand All @@ -53,20 +53,23 @@ regex.RepFunc(myByteArray, regex.Compile(`(?flags)re(capture group)`), func(data
}, true /* optional: if true, will not process a return output */)

// run a simple light replace function
regex.RepStr(myByteArray, regex.Compile(`re`), myReplacementByteArray)
regex.Compile(`re`).RepStr(myByteArray, myReplacementByteArray)

// return a bool if a regex matches a byte array
regex.Match(myByteArray, regex.Compile(`re`))
regex.Compile(`re`).Match(myByteArray)

// split a byte array in a similar way to JavaScript
regex.Split(myByteArray, regex.Compile(`re|(keep this and split like in JavaScript)`))
regex.Compile(`re|(keep this and split like in JavaScript)`).Split(myByteArray)

// a regex string is modified before compiling, to add a few other features
`use \' in place of ` + "`" + ` to make things easier`
`(?#This is a comment in regex)`

// an alias of pcre.Regexp
regex.Regexp
regex.PCRE

// direct access to compiled pcre.Regexp
regex.Compile("re").RE


// another helpful function
Expand Down
132 changes: 50 additions & 82 deletions regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import (
"github.com/pbnjay/memory"
)

type Regexp = pcre.Regexp
type PCRE = pcre.Regexp

type Regexp struct {
RE *pcre.Regexp
}

var regReReplaceQuote pcre.Regexp = pcre.MustCompileJIT(`\\[\\']`, pcre.UTF8, pcre.CONFIG_JIT)
var regReReplaceComment pcre.Regexp = pcre.MustCompileJIT(`\(\?\#.*?\)`, pcre.UTF8, pcre.CONFIG_JIT)
Expand All @@ -29,7 +33,7 @@ var regParamIndexCache *ttlcache.Cache[string, pcre.Regexp] = ttlcache.New[strin

var varType map[string]reflect.Type

var cache *ttlcache.Cache[string, *pcre.Regexp] = ttlcache.New[string, *pcre.Regexp](2 * time.Hour, 1 * time.Hour)
var cache *ttlcache.Cache[string, *Regexp] = ttlcache.New[string, *Regexp](2 * time.Hour, 1 * time.Hour)

func init() {
/* man := getLinuxInstaller([]string{`apt-get`, `apt`, `yum`})
Expand Down Expand Up @@ -148,16 +152,16 @@ func JoinBytes(bytes ...interface{}) []byte {
return T(res)
} */

func setCache(re string, reg *pcre.Regexp) {
func setCache(re string, reg *Regexp) {
cache.Set(re, reg)
}

func getCache(re string) (*pcre.Regexp, bool) {
func getCache(re string) (*Regexp, bool) {
if val, ok := cache.Get(re); ok {
return val, true
}

return &pcre.Regexp{}, false
return &Regexp{}, false
}

// Compile compiles a regular expression and store it in the cache
Expand Down Expand Up @@ -214,24 +218,23 @@ func Compile(re string, params ...string) *Regexp {
// reg := pcre.MustCompileJIT(re, pcre.JAVASCRIPT_COMPAT, pcre.STUDY_JIT_COMPILE)
// reg := pcre.MustCompileParseJIT(re, pcre.STUDY_JIT_COMPILE)

go setCache(re, &reg)
return &reg
compRe := Regexp{RE: &reg}

go setCache(re, &compRe)
return &compRe
}
}

// RepFunc replaces a string with the result of a function
// similar to JavaScript .replace(/re/, function(data){})
func RepFunc(str []byte, reg *Regexp, rep func(data func(int) []byte) []byte, blank ...bool) []byte {
// reg := Compile(re)

// ind := reg.FindAllIndex(b, pcre.UTF8)
ind := reg.FindAllIndex(str, 0)
func (reg *Regexp) RepFunc(str []byte, rep func(data func(int) []byte) []byte, blank ...bool) []byte {
ind := reg.RE.FindAllIndex(str, 0)

res := []byte{}
trim := 0
for _, pos := range ind {
v := str[pos[0]:pos[1]]
m := reg.Matcher(v, 0)
m := reg.RE.Matcher(v, 0)

if len(blank) != 0 {
gCache := map[int][]byte{}
Expand Down Expand Up @@ -286,17 +289,14 @@ func RepFunc(str []byte, reg *Regexp, rep func(data func(int) []byte) []byte, bl
// RepFuncRef replace a string with the result of a function
// similar to JavaScript .replace(/re/, function(data){})
// Uses Pointers For Improved Performance
func RepFuncRef(str *[]byte, reg *Regexp, rep func(data func(int) []byte) []byte, blank ...bool) []byte {
// reg := Compile(re)

// ind := reg.FindAllIndex(b, pcre.UTF8)
ind := reg.FindAllIndex(*str, 0)
func (reg *Regexp) RepFuncRef(str *[]byte, rep func(data func(int) []byte) []byte, blank ...bool) []byte {
ind := reg.RE.FindAllIndex(*str, 0)

res := []byte{}
trim := 0
for _, pos := range ind {
v := (*str)[pos[0]:pos[1]]
m := reg.Matcher(v, 0)
m := reg.RE.Matcher(v, 0)

if len(blank) != 0 {
gCache := map[int][]byte{}
Expand Down Expand Up @@ -349,18 +349,14 @@ func RepFuncRef(str *[]byte, reg *Regexp, rep func(data func(int) []byte) []byte
}

// RepFuncFirst is a copy of the RepFunc method modified to only run once
func RepFuncFirst(str []byte, reg *Regexp, rep func(func(int) []byte) []byte, blank ...bool) []byte {
// reg := Compile(re)

// ind := reg.FindAllIndex(b, pcre.UTF8)
// ind := reg.FindAllIndex(b, 0)
pos := reg.FindIndex(str, 0)
func (reg *Regexp) RepFuncFirst(str []byte, rep func(func(int) []byte) []byte, blank ...bool) []byte {
pos := reg.RE.FindIndex(str, 0)

res := []byte{}
trim := 0
// for _, pos := range ind {
v := str[pos[0]:pos[1]]
m := reg.Matcher(v, 0)
m := reg.RE.Matcher(v, 0)

if len(blank) != 0 {
gCache := map[int][]byte{}
Expand Down Expand Up @@ -414,48 +410,36 @@ func RepFuncFirst(str []byte, reg *Regexp, rep func(func(int) []byte) []byte, bl

// RepStr replaces a string with another string
// note: this function is optimized for performance, and the replacement string does not accept replacements like $1
func RepStr(str []byte, reg *Regexp, rep []byte) []byte {
// reg := Compile(re)

// return reg.ReplaceAll(str, rep, pcre.UTF8)
return reg.ReplaceAll(str, rep, 0)
func (reg *Regexp) RepStr(str []byte, rep []byte) []byte {
return reg.RE.ReplaceAll(str, rep, 0)
}

// RepStrRef replaces a string with another string
// note: this function is optimized for performance, and the replacement string does not accept replacements like $1
// Uses Pointers For Improved Performance
func RepStrRef(str *[]byte, reg *Regexp, rep []byte) []byte {
// reg := Compile(re)

// return reg.ReplaceAll(str, rep, pcre.UTF8)
return reg.ReplaceAll(*str, rep, 0)
func (reg *Regexp) RepStrRef(str *[]byte, rep []byte) []byte {
return reg.RE.ReplaceAll(*str, rep, 0)
}

// RepStrRefRes replaces a string with another string
// note: this function is optimized for performance, and the replacement string does not accept replacements like $1
// Uses Pointers For Improved Performance (also on result)
func RepStrRefRes(str *[]byte, reg *Regexp, rep *[]byte) []byte {
// reg := Compile(re)

// return reg.ReplaceAll(str, rep, pcre.UTF8)
return reg.ReplaceAll(*str, *rep, 0)
func (reg *Regexp) RepStrRefRes(str *[]byte, rep *[]byte) []byte {
return reg.RE.ReplaceAll(*str, *rep, 0)
}

// RepStrComplex is a more complex version of the RepStr method
// this function will replace things in the result like $1 with your capture groups
// use $0 to use the full regex capture group
// use ${123} to use numbers with more than one digit
func RepStrComplex(str []byte, reg *Regexp, rep []byte) []byte {
// reg := Compile(re)

// ind := reg.FindAllIndex(str, pcre.UTF8)
ind := reg.FindAllIndex(str, 0)
func (reg *Regexp) RepStrComplex(str []byte, rep []byte) []byte {
ind := reg.RE.FindAllIndex(str, 0)

res := []byte{}
trim := 0
for _, pos := range ind {
v := str[pos[0]:pos[1]]
m := reg.Matcher(v, 0)
m := reg.RE.Matcher(v, 0)

if trim == 0 {
res = append(res, str[:pos[0]]...)
Expand All @@ -464,7 +448,7 @@ func RepStrComplex(str []byte, reg *Regexp, rep []byte) []byte {
}
trim = pos[1]

r := RepFunc(rep, regComplexSel, func(data func(int) []byte) []byte {
r := regComplexSel.RepFunc(rep, func(data func(int) []byte) []byte {
if len(data(1)) != 0 {
return data(0)
}
Expand Down Expand Up @@ -497,17 +481,14 @@ func RepStrComplex(str []byte, reg *Regexp, rep []byte) []byte {
// use $0 to use the full regex capture group
// use ${123} to use numbers with more than one digit
// Uses Pointers For Improved Performance
func RepStrComplexRef(str *[]byte, reg *Regexp, rep []byte) []byte {
// reg := Compile(re)

// ind := reg.FindAllIndex(str, pcre.UTF8)
ind := reg.FindAllIndex(*str, 0)
func (reg *Regexp) RepStrComplexRef(str *[]byte, rep []byte) []byte {
ind := reg.RE.FindAllIndex(*str, 0)

res := []byte{}
trim := 0
for _, pos := range ind {
v := (*str)[pos[0]:pos[1]]
m := reg.Matcher(v, 0)
m := reg.RE.Matcher(v, 0)

if trim == 0 {
res = append(res, (*str)[:pos[0]]...)
Expand All @@ -516,7 +497,7 @@ func RepStrComplexRef(str *[]byte, reg *Regexp, rep []byte) []byte {
}
trim = pos[1]

r := RepFunc(rep, regComplexSel, func(data func(int) []byte) []byte {
r := regComplexSel.RepFunc(rep, func(data func(int) []byte) []byte {
if len(data(1)) != 0 {
return data(0)
}
Expand Down Expand Up @@ -549,17 +530,14 @@ func RepStrComplexRef(str *[]byte, reg *Regexp, rep []byte) []byte {
// use $0 to use the full regex capture group
// use ${123} to use numbers with more than one digit
// Uses Pointers For Improved Performance (also on result)
func RepStrComplexRefRes(str *[]byte, reg *Regexp, rep *[]byte) []byte {
// reg := Compile(re)

// ind := reg.FindAllIndex(str, pcre.UTF8)
ind := reg.FindAllIndex(*str, 0)
func (reg *Regexp) RepStrComplexRefRes(str *[]byte, rep *[]byte) []byte {
ind := reg.RE.FindAllIndex(*str, 0)

res := []byte{}
trim := 0
for _, pos := range ind {
v := (*str)[pos[0]:pos[1]]
m := reg.Matcher(v, 0)
m := reg.RE.Matcher(v, 0)

if trim == 0 {
res = append(res, (*str)[:pos[0]]...)
Expand All @@ -568,7 +546,7 @@ func RepStrComplexRefRes(str *[]byte, reg *Regexp, rep *[]byte) []byte {
}
trim = pos[1]

r := RepFunc(*rep, regComplexSel, func(data func(int) []byte) []byte {
r := regComplexSel.RepFunc(*rep, func(data func(int) []byte) []byte {
if len(data(1)) != 0 {
return data(0)
}
Expand Down Expand Up @@ -597,34 +575,26 @@ func RepStrComplexRefRes(str *[]byte, reg *Regexp, rep *[]byte) []byte {
}

// Match returns true if a []byte matches a regex
func Match(str []byte, reg *Regexp) bool {
// reg := Compile(re)

// return reg.Match(str, pcre.UTF8)
return reg.Match(str, 0)
func (reg *Regexp) Match(str []byte) bool {
return reg.RE.Match(str, 0)
}

// MatchRef returns true if a string matches a regex
// Uses Pointers For Improved Performance
func MatchRef(str *[]byte, reg *Regexp) bool {
// reg := Compile(re)

// return reg.Match(str, pcre.UTF8)
return reg.Match(*str, 0)
func (reg *Regexp) MatchRef(str *[]byte) bool {
return reg.RE.Match(*str, 0)
}

// Split splits a string, and keeps capture groups
// Similar to JavaScript .split(/re/)
func Split(str []byte, reg *Regexp) [][]byte {
// reg := Compile(re)

ind := reg.FindAllIndex(str, 0)
func (reg *Regexp) Split(str []byte) [][]byte {
ind := reg.RE.FindAllIndex(str, 0)

res := [][]byte{}
trim := 0
for _, pos := range ind {
v := str[pos[0]:pos[1]]
m := reg.Matcher(v, 0)
m := reg.RE.Matcher(v, 0)

if trim == 0 {
res = append(res, str[:pos[0]])
Expand Down Expand Up @@ -652,16 +622,14 @@ func Split(str []byte, reg *Regexp) [][]byte {
// SplitRef splits a string, and keeps capture groups
// Similar to JavaScript .split(/re/)
// Uses Pointers For Improved Performance
func SplitRef(str *[]byte, reg *Regexp) [][]byte {
// reg := Compile(re)

ind := reg.FindAllIndex(*str, 0)
func (reg *Regexp) SplitRef(str *[]byte) [][]byte {
ind := reg.RE.FindAllIndex(*str, 0)

res := [][]byte{}
trim := 0
for _, pos := range ind {
v := (*str)[pos[0]:pos[1]]
m := reg.Matcher(v, 0)
m := reg.RE.Matcher(v, 0)

if trim == 0 {
res = append(res, (*str)[:pos[0]])
Expand Down
Loading

0 comments on commit 6b62e67

Please sign in to comment.