| 1 |
// Copyright 2023-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 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 stree |
| 15 |
|
| 16 |
import ( |
| 17 |
"strings" |
| 18 |
) |
| 19 |
|
| 20 |
// genParts will break a filter subject up into parts. |
| 21 |
// We need to break this up into chunks based on wildcards, either pwc '*' or fwc '>'. |
| 22 |
// We do not care about other tokens per se, just parts that are separated by wildcards with an optional end fwc. |
| 23 |
func genParts(filter []byte, parts [][]byte) [][]byte { |
| 24 |
var start int |
| 25 |
for i, e := 0, len(filter)-1; i < len(filter); i++ { |
| 26 |
if filter[i] == tsep { |
| 27 |
// See if next token is pwc. Either internal or end pwc. |
| 28 |
if i < e && filter[i+1] == pwc && (i+2 <= e && filter[i+2] == tsep || i+1 == e) { |
| 29 |
if i > start { |
| 30 |
parts = append(parts, filter[start:i+1]) |
| 31 |
} |
| 32 |
parts = append(parts, filter[i+1:i+2]) |
| 33 |
i++ // Skip pwc |
| 34 |
if i+2 <= e { |
| 35 |
i++ // Skip next tsep from next part too. |
| 36 |
} |
| 37 |
start = i + 1 |
| 38 |
} else if i < e && filter[i+1] == fwc && i+1 == e { |
| 39 |
if i > start { |
| 40 |
parts = append(parts, filter[start:i+1]) |
| 41 |
} |
| 42 |
parts = append(parts, filter[i+1:i+2]) |
| 43 |
i++ // Skip fwc |
| 44 |
start = i + 1 |
| 45 |
} |
| 46 |
} else if filter[i] == pwc || filter[i] == fwc { |
| 47 |
// Wildcard must be at the start or preceded by tsep. |
| 48 |
if prev := i - 1; prev >= 0 && filter[prev] != tsep { |
| 49 |
continue |
| 50 |
} |
| 51 |
// Wildcard must be at the end or followed by tsep. |
| 52 |
if next := i + 1; next == e || next < e && filter[next] != tsep { |
| 53 |
continue |
| 54 |
} |
| 55 |
// Full wildcard must be terminal. |
| 56 |
if filter[i] == fwc && i < e { |
| 57 |
break |
| 58 |
} |
| 59 |
// We start with a pwc or fwc. |
| 60 |
parts = append(parts, filter[i:i+1]) |
| 61 |
if i+1 <= e { |
| 62 |
i++ // Skip next tsep from next part too. |
| 63 |
} |
| 64 |
start = i + 1 |
| 65 |
} |
| 66 |
} |
| 67 |
if start < len(filter) { |
| 68 |
// Check to see if we need to eat a leading tsep. |
| 69 |
if filter[start] == tsep { |
| 70 |
start++ |
| 71 |
} |
| 72 |
parts = append(parts, filter[start:]) |
| 73 |
} |
| 74 |
return parts |
| 75 |
} |
| 76 |
|
| 77 |
// Match our parts against a stored fragment, which could be a prefix for nodes |
| 78 |
// or a suffix for leaves. |
| 79 |
func matchParts(parts [][]byte, frag string) ([][]byte, bool) { |
| 80 |
lf := len(frag) |
| 81 |
if lf == 0 { |
| 82 |
return parts, true |
| 83 |
} |
| 84 |
|
| 85 |
var si int |
| 86 |
lpi := len(parts) - 1 |
| 87 |
|
| 88 |
for i, part := range parts { |
| 89 |
if si >= lf { |
| 90 |
return parts[i:], true |
| 91 |
} |
| 92 |
lp := len(part) |
| 93 |
// Check for pwc or fwc place holders. |
| 94 |
if lp == 1 { |
| 95 |
if part[0] == pwc { |
| 96 |
index := strings.IndexByte(frag[si:], tsep) |
| 97 |
// We are trying to match pwc and did not find our tsep. |
| 98 |
// Will need to move to next node from caller. |
| 99 |
if index < 0 { |
| 100 |
if i == lpi { |
| 101 |
return nil, true |
| 102 |
} |
| 103 |
return parts[i:], true |
| 104 |
} |
| 105 |
si += index + 1 |
| 106 |
continue |
| 107 |
} else if part[0] == fwc { |
| 108 |
// If we are here we should be good. |
| 109 |
return nil, true |
| 110 |
} |
| 111 |
} |
| 112 |
end := min(si+lp, lf) |
| 113 |
// If part is bigger then the remaining fragment, adjust to a portion on the part. |
| 114 |
if si+lp > end { |
| 115 |
// Frag is smaller then part itself. |
| 116 |
part = part[:end-si] |
| 117 |
} |
| 118 |
if string(part) != frag[si:end] { |
| 119 |
return parts, false |
| 120 |
} |
| 121 |
// If we still have a portion of the fragment left, update and continue. |
| 122 |
if end < lf { |
| 123 |
si = end |
| 124 |
continue |
| 125 |
} |
| 126 |
// If we matched a partial, do not move past current part |
| 127 |
// but update the part to what was consumed. This allows upper layers to continue. |
| 128 |
if end < si+lp { |
| 129 |
if end >= lf { |
| 130 |
// Create a copy before modifying. Reuse slice capacity available at the |
| 131 |
// end of the parts slice, since this saves us additional allocations. |
| 132 |
lp := len(parts) |
| 133 |
parts = append(parts[lp:], parts[:lp]...) |
| 134 |
parts[i] = parts[i][lf-si:] |
| 135 |
} else { |
| 136 |
i++ |
| 137 |
} |
| 138 |
return parts[i:], true |
| 139 |
} |
| 140 |
if i == lpi { |
| 141 |
return nil, true |
| 142 |
} |
| 143 |
// If we are here we are not the last part which means we have a wildcard |
| 144 |
// gap, so we need to match anything up to next tsep. |
| 145 |
si += len(part) |
| 146 |
} |
| 147 |
return parts, false |
| 148 |
} |