path.go 3.1 KB

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