code.oscarkilo.com/klex-git/util/paths.go

..
commit.go
commit_test.go
committest/
files.go
files_test.go
paths.go
paths_test.go
root.go
root_test.go
run.go
run_test.go
package util

// Path-related utilities

import (
  "path"
  "strings"
)

// SplitPath returns dir and file names, ignoring strailing and multi slashes.
func SplitPath(p string) []string {
  for strings.HasSuffix(p, "/") {
    p = p[:len(p)-1]
  }
  if p == "" {
    return nil
  }
  d, f := path.Split(p)
  return append(SplitPath(d), f)
}

// StripPathPrefix returns the stripped path, and whether the prefix was found.
func StripPathPrefix(long, pref string) (string, bool) {
  if pref == "" {
    return long, true
  }
  lo := SplitPath(long)
  pr := SplitPath(pref)
  for i := 0; i < len(pr); i++ {
    if i >= len(lo) || lo[i] != pr[i] {
      return long, false
    }
  }
  return path.Join(lo[len(pr):]...), true
}