get.pme.sh/pnats@v0.0.0-20240304004023-26bb5a137ed0/server/stree/util.go (about) 1 // Copyright 2023-2024 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 min(i, maxPrefixLen) 33 } 34 35 // Helper to copy bytes. 36 func copyBytes(src []byte) []byte { 37 if len(src) == 0 { 38 return nil 39 } 40 dst := make([]byte, len(src)) 41 copy(dst, src) 42 return dst 43 } 44 45 type position interface{ int | uint16 } 46 47 // Can return 0 if we have all the subject as prefixes. 48 func pivot[N position](subject []byte, pos N) byte { 49 if int(pos) >= len(subject) { 50 return 0 51 } 52 return subject[pos] 53 } 54 55 // TODO(dlc) - Can be removed with Go 1.21 once server is on Go 1.22. 56 func min(a, b int) int { 57 if a < b { 58 return a 59 } 60 return b 61 }