Debian Codegraph @

internal/set.go

raw symbols SCIP
1 package internal
2
3 import (
4 "encoding/binary"
5 "fmt"
6 "runtime"
7 "sync/atomic"
8 )
9
10 const unknownCompileError = "unknown error compiling pattern"
11
12 type Set struct {
13 ptr wasmPtr
14 abi *libre2ABI
15 opts CompileOptions
16 exprs []string
17 released uint32
18 }
19
20 func CompileSet(exprs []string, opts CompileOptions) (*Set, error) {
21 abi := newABI()
22 setPtr := newSet(abi, opts)
23 set := &Set{
24 ptr: setPtr,
25 abi: abi,
26 opts: opts,
27 exprs: exprs,
28 }
29 var estimatedMemorySize int
30 for _, expr := range exprs {
31 estimatedMemorySize += len(expr) + 2
32 }
33
34 alloc := abi.startOperation(estimatedMemorySize)
35 defer abi.endOperation(alloc)
36
37 for _, expr := range exprs {
38 cs := alloc.newCString(expr)
39 errMsg := setAdd(set, cs)
40 if errMsg != "" {
41 return nil, fmt.Errorf("%s", errMsg)
42 }
43 }
44 setCompile(set)
45 // Use func(interface{}) form for nottinygc compatibility.
46 runtime.SetFinalizer(set, func(obj interface{}) {
47 if s, ok := obj.(*Set); ok {
48 s.release()
49 }
50 })
51 return set, nil
52 }
53
54 func (set *Set) release() {
55 if !atomic.CompareAndSwapUint32(&set.released, 0, 1) {
56 return
57 }
58 deleteSet(set.abi, set.ptr)
59 }
60
61 // FindAllString finds all matches of the regular expressions in the Set against the input string.
62 // It returns a slice of indices of the matched patterns. If n >= 0, it returns at most n matches; otherwise, it returns all of them.
63 func (set *Set) FindAllString(s string, n int) []int {
64 if n == 0 {
65 return nil
66 }
67 if n < 0 {
68 n = len(set.exprs)
69 }
70 alloc := set.abi.startOperation(len(s) + 8 + n*8)
71 defer set.abi.endOperation(alloc)
72
73 cs := alloc.newCString(s)
74
75 var matches []int
76
77 set.findAll(&alloc, cs, n, func(match int) {
78 matches = append(matches, match)
79 })
80 return matches
81 }
82
83 // FindAll executes the Set against the input bytes. It returns a slice
84 // with the indices of the matched patterns. If n >= 0, it returns at most
85 // n matches; otherwise, it returns all of them.
86 func (set *Set) FindAll(b []byte, n int) []int {
87 if n == 0 {
88 return nil
89 }
90 if n < 0 {
91 n = len(set.exprs)
92 }
93 alloc := set.abi.startOperation(len(b) + 8 + n*8)
94 defer set.abi.endOperation(alloc)
95
96 cs := alloc.newCStringFromBytes(b)
97
98 var matches []int
99
100 set.findAll(&alloc, cs, n, func(match int) {
101 matches = append(matches, match)
102 })
103
104 return matches
105 }
106
107 func (set *Set) findAll(alloc *allocation, cs cString, n int, deliver func(match int)) {
108 matchArr := alloc.newCStringArray(n)
109 defer matchArr.free()
110
111 matchedCount := setMatch(set, cs, matchArr.ptr, n)
112 matches := alloc.read(matchArr.ptr, n*4)
113 for i := 0; i < matchedCount && i < n; i++ {
114 deliver(int(binary.LittleEndian.Uint32(matches[i*4:])))
115 }
116
117 runtime.KeepAlive(matchArr)
118 runtime.KeepAlive(set) // don't allow finalizer to run during method
119 }