Compare commits

...

6 Commits

Author SHA1 Message Date
4ce849193f Add riscv64 test
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-08-04 20:25:57 -07:00
107b5db1fb Add CI status badge to README
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-07-27 09:44:57 -07:00
e90cc6feac Add Varnish test case
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-07-27 09:41:27 -07:00
18912728e9 Add woodpecker config
All checks were successful
ci/woodpecker/manual/woodpecker Pipeline was successful
2023-07-27 09:36:28 -07:00
0a0008aef4 Fix typo 2023-07-26 15:53:58 -07:00
6d906d55a6 Fix panic when pcre2 returns PCRE2_UNSET 2023-07-25 23:35:29 -06:00
5 changed files with 66 additions and 32 deletions

13
.woodpecker.yml Normal file
View File

@ -0,0 +1,13 @@
matrix:
platform:
- linux/amd64
- linux/arm64
- linux/riscv64
steps:
test:
image: gitea.elara.ws/elara6331/golang:latest
commands:
- go test
when:
- event: push

View File

@ -1,6 +1,7 @@
# pcre # pcre
[![Go Reference](https://pkg.go.dev/badge/go.elara.ws/pcre.svg)](https://pkg.go.dev/go.elara.ws/pcre) [![Go Reference](https://pkg.go.dev/badge/go.elara.ws/pcre.svg)](https://pkg.go.dev/go.elara.ws/pcre)
[![status-badge](https://ci.elara.ws/api/badges/49/status.svg)](https://ci.elara.ws/49)
This package provides a CGo-free port of the PCRE2 regular expression library. The [lib](lib) directory contains source code automatically translated from PCRE2's C source. This package wraps that code and provides an interface as close as possible to Go's stdlib [regexp](https://pkg.go.dev/regexp) package This package provides a CGo-free port of the PCRE2 regular expression library. The [lib](lib) directory contains source code automatically translated from PCRE2's C source. This package wraps that code and provides an interface as close as possible to Go's stdlib [regexp](https://pkg.go.dev/regexp) package

11
pcre.go
View File

@ -8,6 +8,7 @@
package pcre package pcre
import ( import (
"math"
"os" "os"
"runtime" "runtime"
"strconv" "strconv"
@ -19,6 +20,8 @@ import (
"modernc.org/libc" "modernc.org/libc"
) )
const Unset = math.MaxUint
// Version returns the version of pcre2 embedded in this library. // Version returns the version of pcre2 embedded in this library.
func Version() string { return lib.DPACKAGE_VERSION } func Version() string { return lib.DPACKAGE_VERSION }
@ -208,8 +211,12 @@ func (r *Regexp) FindSubmatch(b []byte) [][]byte {
out := make([][]byte, 0, len(match)/2) out := make([][]byte, 0, len(match)/2)
for i := 0; i < len(match); i += 2 { for i := 0; i < len(match); i += 2 {
if match[i] == Unset {
out = append(out, nil)
} else {
out = append(out, b[match[i]:match[i+1]]) out = append(out, b[match[i]:match[i+1]])
} }
}
return out return out
} }
@ -253,8 +260,12 @@ func (r *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte {
outMatch := make([][]byte, 0, len(match)/2) outMatch := make([][]byte, 0, len(match)/2)
for i := 0; i < len(match); i += 2 { for i := 0; i < len(match); i += 2 {
if match[i] == Unset {
outMatch = append(outMatch, nil)
} else {
outMatch = append(outMatch, b[match[i]:match[i+1]]) outMatch = append(outMatch, b[match[i]:match[i+1]])
} }
}
out[index] = outMatch out[index] = outMatch
} }

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
"sync" "sync"
"testing" "testing"
"reflect"
"go.elara.ws/pcre" "go.elara.ws/pcre"
) )
@ -289,3 +290,11 @@ func TestCallout(t *testing.T) {
t.Error("expected regular expression to match the string") t.Error("expected regular expression to match the string")
} }
} }
func TestVarnish(t *testing.T) {
regex := pcre.MustCompile(`varnish(?: \(Varnish\/([\d.]{1,250})\))?`)
matches := regex.FindStringSubmatch("1.1 varnish")
if !reflect.DeepEqual(matches, []string{"varnish", ""}) {
t.Errorf(`Expected ["varnish" ""], got %q`, matches)
}
}

View File

@ -21,7 +21,7 @@ const (
FirstLine = CompileOption(lib.DPCRE2_FIRSTLINE) FirstLine = CompileOption(lib.DPCRE2_FIRSTLINE)
Literal = CompileOption(lib.DPCRE2_LITERAL) Literal = CompileOption(lib.DPCRE2_LITERAL)
MatchInvalidUTF = CompileOption(lib.DPCRE2_MATCH_INVALID_UTF) MatchInvalidUTF = CompileOption(lib.DPCRE2_MATCH_INVALID_UTF)
MactchUnsetBackref = CompileOption(lib.DPCRE2_MATCH_UNSET_BACKREF) MatchUnsetBackref = CompileOption(lib.DPCRE2_MATCH_UNSET_BACKREF)
Multiline = CompileOption(lib.DPCRE2_MULTILINE) Multiline = CompileOption(lib.DPCRE2_MULTILINE)
NeverBackslashC = CompileOption(lib.DPCRE2_NEVER_BACKSLASH_C) NeverBackslashC = CompileOption(lib.DPCRE2_NEVER_BACKSLASH_C)
NeverUCP = CompileOption(lib.DPCRE2_NEVER_UCP) NeverUCP = CompileOption(lib.DPCRE2_NEVER_UCP)