| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hugio |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
) |
| 19 |
|
| 20 |
// HasBytesWriter is a writer will match against a slice of patterns. |
| 21 |
type HasBytesWriter struct { |
| 22 |
Patterns []*HasBytesPattern |
| 23 |
|
| 24 |
done bool |
| 25 |
// The tail of the bytes written so far, retained so we can detect a |
| 26 |
// pattern that straddles the boundary between two Write calls. |
| 27 |
buff []byte |
| 28 |
} |
| 29 |
|
| 30 |
type HasBytesPattern struct { |
| 31 |
Match bool |
| 32 |
Pattern []byte |
| 33 |
} |
| 34 |
|
| 35 |
// maxPatternLen returns the length of the longest pattern. |
| 36 |
func (h *HasBytesWriter) maxPatternLen() int { |
| 37 |
l := 0 |
| 38 |
for _, p := range h.Patterns { |
| 39 |
if len(p.Pattern) > l { |
| 40 |
l = len(p.Pattern) |
| 41 |
} |
| 42 |
} |
| 43 |
return l |
| 44 |
} |
| 45 |
|
| 46 |
func (h *HasBytesWriter) Write(p []byte) (n int, err error) { |
| 47 |
if h.done { |
| 48 |
return len(p), nil |
| 49 |
} |
| 50 |
|
| 51 |
keep := h.maxPatternLen() - 1 |
| 52 |
|
| 53 |
// Join the tail retained from previous Writes with the head of this chunk |
| 54 |
// so a pattern straddling the boundary is still detected. Only the |
| 55 |
// boundary window is copied; the chunk itself is scanned in place below. |
| 56 |
var boundary []byte |
| 57 |
if keep > 0 && len(h.buff) > 0 { |
| 58 |
head := p |
| 59 |
if len(head) > keep { |
| 60 |
head = head[:keep] |
| 61 |
} |
| 62 |
boundary = make([]byte, 0, len(h.buff)+len(head)) |
| 63 |
boundary = append(boundary, h.buff...) |
| 64 |
boundary = append(boundary, head...) |
| 65 |
} |
| 66 |
|
| 67 |
// Scan each not-yet-matched pattern once per Write instead of once per byte. |
| 68 |
done := true |
| 69 |
for _, pp := range h.Patterns { |
| 70 |
if pp.Match { |
| 71 |
continue |
| 72 |
} |
| 73 |
if bytes.Contains(p, pp.Pattern) || bytes.Contains(boundary, pp.Pattern) { |
| 74 |
pp.Match = true |
| 75 |
continue |
| 76 |
} |
| 77 |
done = false |
| 78 |
} |
| 79 |
|
| 80 |
if done { |
| 81 |
// All patterns found; no need to look at any more data. |
| 82 |
h.done = true |
| 83 |
h.buff = nil |
| 84 |
return len(p), nil |
| 85 |
} |
| 86 |
|
| 87 |
// Retain the last keep bytes of (previous tail + this chunk) to detect a |
| 88 |
// pattern straddling into the next Write. |
| 89 |
switch { |
| 90 |
case keep <= 0: |
| 91 |
h.buff = h.buff[:0] |
| 92 |
case len(p) >= keep: |
| 93 |
h.buff = append(h.buff[:0], p[len(p)-keep:]...) |
| 94 |
default: |
| 95 |
// Chunk shorter than keep: slide the window over the retained tail. |
| 96 |
if total := len(h.buff) + len(p); total > keep { |
| 97 |
h.buff = h.buff[total-keep:] |
| 98 |
} |
| 99 |
h.buff = append(h.buff, p...) |
| 100 |
} |
| 101 |
|
| 102 |
return len(p), nil |
| 103 |
} |