github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/os/path_unix.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris 6 // +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris 7 8 package os 9 10 const ( 11 PathSeparator = '/' // OS-specific path separator 12 PathListSeparator = ':' // OS-specific path list separator 13 ) 14 15 // IsPathSeparator reports whether c is a directory separator character. 16 func IsPathSeparator(c uint8) bool { 17 return PathSeparator == c 18 } 19 20 // basename removes trailing slashes and the leading directory name from path name. 21 func basename(name string) string { 22 i := len(name) - 1 23 // Remove trailing slashes 24 for ; i > 0 && name[i] == '/'; i-- { 25 name = name[:i] 26 } 27 // Remove leading directory name 28 for i--; i >= 0; i-- { 29 if name[i] == '/' { 30 name = name[i+1:] 31 break 32 } 33 } 34 35 return name 36 } 37 38 // splitPath returns the base name and parent directory. 39 func splitPath(path string) (string, string) { 40 // if no better parent is found, the path is relative from "here" 41 dirname := "." 42 43 // Remove all but one leading slash. 44 for len(path) > 1 && path[0] == '/' && path[1] == '/' { 45 path = path[1:] 46 } 47 48 i := len(path) - 1 49 50 // Remove trailing slashes. 51 for ; i > 0 && path[i] == '/'; i-- { 52 path = path[:i] 53 } 54 55 // if no slashes in path, base is path 56 basename := path 57 58 // Remove leading directory path 59 for i--; i >= 0; i-- { 60 if path[i] == '/' { 61 if i == 0 { 62 dirname = path[:1] 63 } else { 64 dirname = path[:i] 65 } 66 basename = path[i+1:] 67 break 68 } 69 } 70 71 return dirname, basename 72 } 73 74 func fixRootDirectory(p string) string { 75 return p 76 }