github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/path/filepath/path_windows.go (about) 1 // Copyright 2010 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 filepath 6 7 import ( 8 "strings" 9 ) 10 11 func isSlash(c uint8) bool { 12 return c == '\\' || c == '/' 13 } 14 15 // IsAbs returns true if the path is absolute. 16 func IsAbs(path string) (b bool) { 17 l := volumeNameLen(path) 18 if l == 0 { 19 return false 20 } 21 path = path[l:] 22 if path == "" { 23 return false 24 } 25 return isSlash(path[0]) 26 } 27 28 // volumeNameLen returns length of the leading volume name on Windows. 29 // It returns 0 elsewhere. 30 func volumeNameLen(path string) int { 31 if len(path) < 2 { 32 return 0 33 } 34 // with drive letter 35 c := path[0] 36 if path[1] == ':' && ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') { 37 return 2 38 } 39 // is it UNC 40 if l := len(path); l >= 5 && isSlash(path[0]) && isSlash(path[1]) && 41 !isSlash(path[2]) && path[2] != '.' { 42 // first, leading `\\` and next shouldn't be `\`. its server name. 43 for n := 3; n < l-1; n++ { 44 // second, next '\' shouldn't be repeated. 45 if isSlash(path[n]) { 46 n++ 47 // third, following something characters. its share name. 48 if !isSlash(path[n]) { 49 if path[n] == '.' { 50 break 51 } 52 for ; n < l; n++ { 53 if isSlash(path[n]) { 54 break 55 } 56 } 57 return n 58 } 59 break 60 } 61 } 62 } 63 return 0 64 } 65 66 // HasPrefix exists for historical compatibility and should not be used. 67 func HasPrefix(p, prefix string) bool { 68 if strings.HasPrefix(p, prefix) { 69 return true 70 } 71 return strings.HasPrefix(strings.ToLower(p), strings.ToLower(prefix)) 72 } 73 74 func splitList(path string) []string { 75 // The same implementation is used in LookPath in os/exec; 76 // consider changing os/exec when changing this. 77 78 if path == "" { 79 return []string{} 80 } 81 82 // Split path, respecting but preserving quotes. 83 list := []string{} 84 start := 0 85 quo := false 86 for i := 0; i < len(path); i++ { 87 switch c := path[i]; { 88 case c == '"': 89 quo = !quo 90 case c == ListSeparator && !quo: 91 list = append(list, path[start:i]) 92 start = i + 1 93 } 94 } 95 list = append(list, path[start:]) 96 97 // Remove quotes. 98 for i, s := range list { 99 if strings.Contains(s, `"`) { 100 list[i] = strings.Replace(s, `"`, ``, -1) 101 } 102 } 103 104 return list 105 }