k8s.io/apiserver@v0.31.1/pkg/storage/cacher/util.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cacher 18 19 import ( 20 "strings" 21 ) 22 23 // hasPathPrefix returns true if the string matches pathPrefix exactly, or if is prefixed with pathPrefix at a path segment boundary 24 func hasPathPrefix(s, pathPrefix string) bool { 25 // Short circuit if s doesn't contain the prefix at all 26 if !strings.HasPrefix(s, pathPrefix) { 27 return false 28 } 29 30 pathPrefixLength := len(pathPrefix) 31 32 if len(s) == pathPrefixLength { 33 // Exact match 34 return true 35 } 36 if strings.HasSuffix(pathPrefix, "/") { 37 // pathPrefix already ensured a path segment boundary 38 return true 39 } 40 if s[pathPrefixLength:pathPrefixLength+1] == "/" { 41 // The next character in s is a path segment boundary 42 // Check this instead of normalizing pathPrefix to avoid allocating on every call 43 return true 44 } 45 return false 46 }