github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/pathtools/path.go (about) 1 /* Copyright 2018 The Bazel 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 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 // Package pathtools provides utilities for manipulating paths. Most paths 17 // within Gazelle are slash-separated paths, relative to the repository root 18 // directory. The repository root directory is represented by the empty 19 // string. Paths in this format may be used directly as package names in labels. 20 package pathtools 21 22 import ( 23 "path" 24 "path/filepath" 25 "strings" 26 ) 27 28 // HasPrefix returns whether the slash-separated path p has the given 29 // prefix. Unlike strings.HasPrefix, this function respects component 30 // boundaries, so "/home/foo" is not a prefix is "/home/foobar/baz". If the 31 // prefix is empty, this function always returns true. 32 func HasPrefix(p, prefix string) bool { 33 p = trimTrailingSlash(p) 34 prefix = trimTrailingSlash(prefix) 35 return prefix == "" || p == prefix || strings.HasPrefix(p, prefix+"/") 36 } 37 38 // TrimPrefix returns p without the provided prefix. If p doesn't start 39 // with prefix, it returns p unchanged. Unlike strings.HasPrefix, this function 40 // respects component boundaries (assuming slash-separated paths), so 41 // TrimPrefix("foo/bar", "foo") returns "baz". 42 func TrimPrefix(p, prefix string) string { 43 origPath := p 44 p = trimTrailingSlash(p) 45 prefix = trimTrailingSlash(prefix) 46 if prefix == "" { 47 return origPath 48 } 49 if prefix == p { 50 return "" 51 } 52 return strings.TrimPrefix(p, prefix+"/") 53 } 54 55 // RelBaseName returns the base name for rel, a slash-separated path relative 56 // to the repository root. If rel is empty, RelBaseName returns the base name 57 // of prefix. If prefix is empty, RelBaseName returns the base name of root, 58 // the absolute file path of the repository root directory. If that's empty 59 // to, then RelBaseName returns "root". 60 func RelBaseName(rel, prefix, root string) string { 61 base := path.Base(rel) 62 if base == "." || base == "/" { 63 base = path.Base(prefix) 64 } 65 if base == "." || base == "/" { 66 base = filepath.Base(root) 67 } 68 if base == "." || base == "/" { 69 base = "root" 70 } 71 return base 72 } 73 74 // Index returns the starting index of the string sub within the non-absolute 75 // slash-separated path p. sub must start and end at component boundaries 76 // within p. 77 func Index(p, sub string) int { 78 if sub == "" { 79 return 0 80 } 81 p = path.Clean(p) 82 sub = path.Clean(sub) 83 if path.IsAbs(sub) { 84 if HasPrefix(p, sub) { 85 return 0 86 } else { 87 return -1 88 } 89 } 90 if p == "" || p == "/" { 91 return -1 92 } 93 94 i := 0 // i is the index of the first byte of a path element 95 if len(p) > 0 && p[0] == '/' { 96 i++ 97 } 98 for { 99 suffix := p[i:] 100 if len(suffix) < len(sub) { 101 return -1 102 } 103 if suffix[:len(sub)] == sub && (len(suffix) == len(sub) || suffix[len(sub)] == '/') { 104 return i 105 } 106 j := strings.IndexByte(suffix, '/') 107 if j < 0 { 108 return -1 109 } 110 i += j + 1 111 if i >= len(p) { 112 return -1 113 } 114 } 115 } 116 117 func trimTrailingSlash(p string) string { 118 for len(p) > 1 && p[len(p)-1] == '/' { 119 p = p[:len(p)-1] 120 } 121 return p 122 }