github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/pkg/fsutils/path_windows.go (about)

     1  //go:build windows
     2  
     3  package fsutils
     4  
     5  import (
     6  	"path/filepath"
     7  	"regexp"
     8  	"strings"
     9  )
    10  
    11  var separatorToReplace = regexp.QuoteMeta(string(filepath.Separator))
    12  
    13  // NormalizePathInRegex normalizes path in regular expressions.
    14  // noop on Unix.
    15  // This replacing should be safe because "/" are disallowed in Windows
    16  // https://docs.microsoft.com/windows/win32/fileio/naming-a-file
    17  func NormalizePathInRegex(path string) string {
    18  	// remove redundant character escape "\/" https://github.com/chenfeining/golangci-lint/issues/3277
    19  	clean := regexp.MustCompile(`\\+/`).
    20  		ReplaceAllStringFunc(path, func(s string) string {
    21  			if strings.Count(s, "\\")%2 == 0 {
    22  				return s
    23  			}
    24  			return s[1:]
    25  		})
    26  
    27  	return strings.ReplaceAll(clean, "/", separatorToReplace)
    28  }