aboutsummaryrefslogtreecommitdiffhomepage
path: root/common/hugio/hasBytesWriter.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2024-06-08 11:52:22 +0200
committerBjørn Erik Pedersen <[email protected]>2024-06-23 11:25:47 +0200
commit6cd0784e447f18e009cbbf30de471e486f7cf356 (patch)
tree0684d05d7e487ebe93463636ed8b5a1bb78e4704 /common/hugio/hasBytesWriter.go
parent8731d8822216dd3c7587769e3cf5d98690717b0c (diff)
downloadhugo-6cd0784e447f18e009cbbf30de471e486f7cf356.tar.gz
hugo-6cd0784e447f18e009cbbf30de471e486f7cf356.zip
Implement defer
Closes #8086 Closes #12589
Diffstat (limited to 'common/hugio/hasBytesWriter.go')
-rw-r--r--common/hugio/hasBytesWriter.go41
1 files changed, 32 insertions, 9 deletions
diff --git a/common/hugio/hasBytesWriter.go b/common/hugio/hasBytesWriter.go
index 5148c82f9..d2bcd1bb4 100644
--- a/common/hugio/hasBytesWriter.go
+++ b/common/hugio/hasBytesWriter.go
@@ -17,24 +17,35 @@ import (
"bytes"
)
-// HasBytesWriter is a writer that will set Match to true if the given pattern
-// is found in the stream.
+// HasBytesWriter is a writer will match against a slice of patterns.
type HasBytesWriter struct {
- Match bool
- Pattern []byte
+ Patterns []*HasBytesPattern
i int
done bool
buff []byte
}
+type HasBytesPattern struct {
+ Match bool
+ Pattern []byte
+}
+
+func (h *HasBytesWriter) patternLen() int {
+ l := 0
+ for _, p := range h.Patterns {
+ l += len(p.Pattern)
+ }
+ return l
+}
+
func (h *HasBytesWriter) Write(p []byte) (n int, err error) {
if h.done {
return len(p), nil
}
if len(h.buff) == 0 {
- h.buff = make([]byte, len(h.Pattern)*2)
+ h.buff = make([]byte, h.patternLen()*2)
}
for i := range p {
@@ -46,11 +57,23 @@ func (h *HasBytesWriter) Write(p []byte) (n int, err error) {
h.i = len(h.buff) / 2
}
- if bytes.Contains(h.buff, h.Pattern) {
- h.Match = true
- h.done = true
- return len(p), nil
+ for _, pp := range h.Patterns {
+ if bytes.Contains(h.buff, pp.Pattern) {
+ pp.Match = true
+ done := true
+ for _, ppp := range h.Patterns {
+ if !ppp.Match {
+ done = false
+ break
+ }
+ }
+ if done {
+ h.done = true
+ }
+ return len(p), nil
+ }
}
+
}
return len(p), nil