Skip to content

Commit

Permalink
path, path/filepath: polish documentation.
Browse files Browse the repository at this point in the history
Fixes #2950.
Fixes #2951.

R=golang-dev, r
CC=golang-dev, remy
https://golang.org/cl/5672044
  • Loading branch information
remyoudompheng committed Feb 16, 2012
1 parent 8098d71 commit 3e7d804
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/pkg/path/filepath/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"unicode/utf8"
)

// ErrBadPattern indicates a globbing pattern was malformed.
var ErrBadPattern = errors.New("syntax error in pattern")

// Match returns true if name matches the shell file name pattern.
Expand All @@ -33,7 +34,8 @@ var ErrBadPattern = errors.New("syntax error in pattern")
// lo '-' hi matches character c for lo <= c <= hi
//
// Match requires pattern to match all of name, not just a substring.
// The only possible error return occurs when the pattern is malformed.
// The only possible returned error is ErrBadPattern, when pattern
// is malformed.
//
func Match(pattern, name string) (matched bool, err error) {
Pattern:
Expand Down Expand Up @@ -211,7 +213,6 @@ func getEsc(chunk string) (r rune, nchunk string, err error) {
// if there is no matching file. The syntax of patterns is the same
// as in Match. The pattern may describe hierarchical names such as
// /usr/*/bin/ed (assuming the Separator is '/').
// The only possible error return occurs when the pattern is malformed.
//
func Glob(pattern string) (matches []string, err error) {
if !hasMeta(pattern) {
Expand Down Expand Up @@ -253,7 +254,6 @@ func Glob(pattern string) (matches []string, err error) {
// and appends them to matches. If the directory cannot be
// opened, it returns the existing matches. New matches are
// added in lexicographical order.
// The only possible error return occurs when the pattern is malformed.
func glob(dir, pattern string, matches []string) (m []string, e error) {
m = matches
fi, err := os.Stat(dir)
Expand Down
19 changes: 12 additions & 7 deletions src/pkg/path/filepath/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
// returns the string ".".
//
// See also Rob Pike, ``Lexical File Names in Plan 9 or
// Getting Dot-Dot right,''
// Getting Dot-Dot Right,''
// http:https://plan9.bell-labs.com/sys/doc/lexnames.html
func Clean(path string) string {
vol := VolumeName(path)
Expand Down Expand Up @@ -118,7 +118,8 @@ func Clean(path string) string {
}

// ToSlash returns the result of replacing each separator character
// in path with a slash ('/') character.
// in path with a slash ('/') character. Multiple separators are
// replaced by multiple slashes.
func ToSlash(path string) string {
if Separator == '/' {
return path
Expand All @@ -127,15 +128,17 @@ func ToSlash(path string) string {
}

// FromSlash returns the result of replacing each slash ('/') character
// in path with a separator character.
// in path with a separator character. Multiple slashes are replaced
// by multiple separators.
func FromSlash(path string) string {
if Separator == '/' {
return path
}
return strings.Replace(path, "/", string(Separator), -1)
}

// SplitList splits a list of paths joined by the OS-specific ListSeparator.
// SplitList splits a list of paths joined by the OS-specific ListSeparator,
// usually found in PATH or GOPATH environment variables.
func SplitList(path string) []string {
if path == "" {
return []string{}
Expand All @@ -158,7 +161,8 @@ func Split(path string) (dir, file string) {
}

// Join joins any number of path elements into a single path, adding
// a Separator if necessary. All empty strings are ignored.
// a Separator if necessary. The result is Cleaned, in particular
// all empty strings are ignored.
func Join(elem ...string) string {
for i, e := range elem {
if e != "" {
Expand All @@ -183,7 +187,8 @@ func Ext(path string) string {

// EvalSymlinks returns the path name after the evaluation of any symbolic
// links.
// If path is relative it will be evaluated relative to the current directory.
// If path is relative the result will be relative to the current directory,
// unless one of the components is an absolute symbolic link.
func EvalSymlinks(path string) (string, error) {
if runtime.GOOS == "windows" {
// Symlinks are not supported under windows.
Expand Down Expand Up @@ -443,7 +448,7 @@ func Base(path string) string {
return path
}

// Dir returns the all but the last element of path, typically the path's directory.
// Dir returns all but the last element of path, typically the path's directory.
// Trailing path separators are removed before processing.
// If the path is empty, Dir returns ".".
// If the path consists entirely of separators, Dir returns a single separator.
Expand Down
5 changes: 5 additions & 0 deletions src/pkg/path/filepath/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ var EvalSymlinksTestDirs = []EvalSymlinksTest{
{"test/dir/link3", "../../"},
{"test/link1", "../test"},
{"test/link2", "dir"},
{"test/linkabs", "/tmp"},
}

var EvalSymlinksTests = []EvalSymlinksTest{
Expand All @@ -571,6 +572,7 @@ var EvalSymlinksTests = []EvalSymlinksTest{
{"test/link2/..", "test"},
{"test/dir/link3", "."},
{"test/link2/link3/test", "test"},
{"test/linkabs", "/tmp"},
}

var EvalSymlinksAbsWindowsTests = []EvalSymlinksTest{
Expand Down Expand Up @@ -629,6 +631,9 @@ func TestEvalSymlinks(t *testing.T) {
for _, d := range tests {
path := simpleJoin(tmpDir, d.path)
dest := simpleJoin(tmpDir, d.dest)
if filepath.IsAbs(d.dest) {
dest = d.dest
}
if p, err := filepath.EvalSymlinks(path); err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", d.path, err)
} else if filepath.Clean(p) != filepath.Clean(dest) {
Expand Down
4 changes: 3 additions & 1 deletion src/pkg/path/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"unicode/utf8"
)

// ErrBadPattern indicates a globbing pattern was malformed.
var ErrBadPattern = errors.New("syntax error in pattern")

// Match returns true if name matches the shell file name pattern.
Expand All @@ -31,7 +32,8 @@ var ErrBadPattern = errors.New("syntax error in pattern")
// lo '-' hi matches character c for lo <= c <= hi
//
// Match requires pattern to match all of name, not just a substring.
// The only possible error return is when pattern is malformed.
// The only possible returned error is ErrBadPattern, when pattern
// is malformed.
//
func Match(pattern, name string) (matched bool, err error) {
Pattern:
Expand Down
21 changes: 12 additions & 9 deletions src/pkg/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

// Package path implements utility routines for manipulating slash-separated
// filename paths.
// paths.
package path

import (
Expand All @@ -25,7 +25,7 @@ import (
// returns the string ".".
//
// See also Rob Pike, ``Lexical File Names in Plan 9 or
// Getting Dot-Dot right,''
// Getting Dot-Dot Right,''
// http:https://plan9.bell-labs.com/sys/doc/lexnames.html
func Clean(path string) string {
if path == "" {
Expand Down Expand Up @@ -100,17 +100,19 @@ func Clean(path string) string {
return string(buf[0:w])
}

// Split splits path immediately following the final path separator,
// Split splits path immediately following the final slash.
// separating it into a directory and file name component.
// If there is no separator in path, Split returns an empty dir and
// If there is no slash path, Split returns an empty dir and
// file set to path.
// The returned values have the property that path = dir+file.
func Split(path string) (dir, file string) {
i := strings.LastIndex(path, "/")
return path[:i+1], path[i+1:]
}

// Join joins any number of path elements into a single path, adding a
// separating slash if necessary. All empty strings are ignored.
// separating slash if necessary. The result is Cleaned; in particular,
// all empty strings are ignored.
func Join(elem ...string) string {
for i, e := range elem {
if e != "" {
Expand Down Expand Up @@ -161,11 +163,12 @@ func IsAbs(path string) bool {
return len(path) > 0 && path[0] == '/'
}

// Dir returns the all but the last element of path, typically the path's directory.
// Trailing path separators are removed before processing.
// Dir returns all but the last element of path, typically the path's directory.
// The path is Cleaned and trailing slashes are removed before processing.
// If the path is empty, Dir returns ".".
// If the path consists entirely of separators, Dir returns a single separator.
// The returned path does not end in a separator unless it is the root directory.
// If the path consists entirely of slashes followed by non-slash bytes, Dir
// returns a single slash. In any other case, the returned path does not end in a
// slash.
func Dir(path string) string {
dir, _ := Split(path)
dir = Clean(dir)
Expand Down

0 comments on commit 3e7d804

Please sign in to comment.