github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/cmd/go/internal/str/path.go (about)

     1  // Copyright 2018 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  package str
     6  
     7  import (
     8  	"path/filepath"
     9  	"strings"
    10  )
    11  
    12  // HasFilePathPrefix reports whether the filesystem path s begins with the
    13  // elements in prefix.
    14  func HasFilePathPrefix(s, prefix string) bool {
    15  	sv := strings.ToUpper(filepath.VolumeName(s))
    16  	pv := strings.ToUpper(filepath.VolumeName(prefix))
    17  	s = s[len(sv):]
    18  	prefix = prefix[len(pv):]
    19  	switch {
    20  	default:
    21  		return false
    22  	case sv != pv:
    23  		return false
    24  	case len(s) == len(prefix):
    25  		return s == prefix
    26  	case len(s) > len(prefix):
    27  		if prefix != "" && prefix[len(prefix)-1] == filepath.Separator {
    28  			return strings.HasPrefix(s, prefix)
    29  		}
    30  		return s[len(prefix)] == filepath.Separator && s[:len(prefix)] == prefix
    31  	}
    32  }