golang.org/x/tools/gopls@v0.15.3/internal/util/pathutil/util.go (about)

     1  // Copyright 2023 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 pathutil
     6  
     7  import (
     8  	"path/filepath"
     9  	"strings"
    10  )
    11  
    12  // InDir checks whether path is in the file tree rooted at dir.
    13  // It checks only the lexical form of the file names.
    14  // It does not consider symbolic links.
    15  //
    16  // Copied from go/src/cmd/go/internal/search/search.go.
    17  func InDir(dir, path string) bool {
    18  	pv := strings.ToUpper(filepath.VolumeName(path))
    19  	dv := strings.ToUpper(filepath.VolumeName(dir))
    20  	path = path[len(pv):]
    21  	dir = dir[len(dv):]
    22  	switch {
    23  	default:
    24  		return false
    25  	case pv != dv:
    26  		return false
    27  	case len(path) == len(dir):
    28  		if path == dir {
    29  			return true
    30  		}
    31  		return false
    32  	case dir == "":
    33  		return path != ""
    34  	case len(path) > len(dir):
    35  		if dir[len(dir)-1] == filepath.Separator {
    36  			if path[:len(dir)] == dir {
    37  				return path[len(dir):] != ""
    38  			}
    39  			return false
    40  		}
    41  		if path[len(dir)] == filepath.Separator && path[:len(dir)] == dir {
    42  			if len(path) == len(dir)+1 {
    43  				return true
    44  			}
    45  			return path[len(dir)+1:] != ""
    46  		}
    47  		return false
    48  	}
    49  }