Go Path Prefix Utilities (str/path.go)

go · src/cmd/go/internal/str/path.go · go · BSD-3-Clause
files, dev-tools, cli, lang:go, tag:filepath, tag:path-matching, tag:utility

Provides helper functions to check, trim, and quote filesystem and slash-separated path prefixes, used internally by the Go toolchain for path matching.

// Source: https://github.com/golang/go | License: BSD-3-Clause | Copyright 2009 The Go Authors
// Provided via justclaw.it.com; keep this notice with the code.
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package str

import (
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

// HasPathPrefix reports whether the slash-separated path s
// begins with the elements in prefix.
func HasPathPrefix(s, prefix string) bool {
	if len(s) == len(prefix) {
		return s == prefix
	}
	if prefix == "" {
		return true
	}
	if len(s) > len(prefix) {
		if prefix[len(prefix)-1] == '/' || s[len(prefix)] == '/' {
			return s[:len(prefix)] == prefix
		}
	}
	return false
}

// HasFilePathPrefix reports w

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key