| 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 |
// For subject matching. |
| 17 |
const ( |
| 18 |
pwc = '*' |
| 19 |
fwc = '>' |
| 20 |
tsep = '.' |
| 21 |
) |
| 22 |
|
| 23 |
// Determine index of common prefix. No match at all is 0, etc. |
| 24 |
func commonPrefixLen(s1, s2 []byte) int { |
| 25 |
limit := min(len(s1), len(s2)) |
| 26 |
var i int |
| 27 |
for ; i < limit; i++ { |
| 28 |
if s1[i] != s2[i] { |
| 29 |
break |
| 30 |
} |
| 31 |
} |
| 32 |
return i |
| 33 |
} |
| 34 |
|
| 35 |
type position interface{ int | uint16 } |
| 36 |
|
| 37 |
// No pivot available. |
| 38 |
const noPivot = byte(127) |
| 39 |
|
| 40 |
// Can return 127 (DEL) if we have all the subject as prefixes. |
| 41 |
// We used to use 0, but when that was in the subject would cause infinite recursion in some situations. |
| 42 |
func pivot[N position](subject []byte, pos N) byte { |
| 43 |
if int(pos) >= len(subject) { |
| 44 |
return noPivot |
| 45 |
} |
| 46 |
return subject[pos] |
| 47 |
} |