golib/path/winpath/path_windowsspecific.go
AJ ONeal d779cd44c1
feat(path/winpath): add Windows path handling package
Derived from Go standard library internal/filepathlite and path/filepath.
Provides Windows-style path operations that work on any platform.

Exports: Clean, Join, Split, Ext, Base, Dir, IsAbs, VolumeName,
VolumeNameLen, ToSlash, FromSlash, Separator, ListSeparator,
IsPathSeparator.
2026-03-28 18:47:02 -06:00

40 lines
790 B
Go

// Copyright 2024 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 winpath
import (
"os"
"strings"
)
func Join(elem ...string) string {
var b strings.Builder
var lastChar byte
for _, e := range elem {
switch {
case b.Len() == 0:
case os.IsPathSeparator(lastChar):
for len(e) > 0 && os.IsPathSeparator(e[0]) {
e = e[1:]
}
if b.Len() == 1 && strings.HasPrefix(e, "??") && (len(e) == len("??") || os.IsPathSeparator(e[2])) {
b.WriteString(`.\`)
}
case lastChar == ':':
default:
b.WriteByte('\\')
lastChar = '\\'
}
if len(e) > 0 {
b.WriteString(e)
lastChar = e[len(e)-1]
}
}
if b.Len() == 0 {
return ""
}
return Clean(b.String())
}