| 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 unix || (js && wasm) || wasip1 |
| 6 |
|
| 7 |
package os |
| 8 |
|
| 9 |
const ( |
| 10 |
PathSeparator = '/' // OS-specific path separator |
| 11 |
PathListSeparator = ':' // OS-specific path list separator |
| 12 |
) |
| 13 |
|
| 14 |
// IsPathSeparator reports whether c is a directory separator character. |
| 15 |
func IsPathSeparator(c uint8) bool { |
| 16 |
return PathSeparator == c |
| 17 |
} |
| 18 |
|
| 19 |
// splitPath returns the base name and parent directory. |
| 20 |
func splitPath(path string) (string, string) { |
| 21 |
// if no better parent is found, the path is relative from "here" |
| 22 |
dirname := "." |
| 23 |
|
| 24 |
// Remove all but one leading slash. |
| 25 |
for len(path) > 1 && path[0] == '/' && path[1] == '/' { |
| 26 |
path = path[1:] |
| 27 |
} |
| 28 |
|
| 29 |
i := len(path) - 1 |
| 30 |
|
| 31 |
// Remove trailing slashes. |
| 32 |
for ; i > 0 && path[i] == '/'; i-- { |
| 33 |
path = path[:i] |
| 34 |
} |
| 35 |
|
| 36 |
// if no slashes in path, base is path |
| 37 |
basename := path |
| 38 |
|
| 39 |
// Remove leading directory path |
| 40 |
for i--; i >= 0; i-- { |
| 41 |
if path[i] == '/' { |
| 42 |
if i == 0 { |
| 43 |
dirname = path[:1] |
| 44 |
} else { |
| 45 |
dirname = path[:i] |
| 46 |
} |
| 47 |
basename = path[i+1:] |
| 48 |
break |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return dirname, basename |
| 53 |
} |