OSDN Git Service

Update Go library to last weekly.
[pf3gnuchains/gcc-fork.git] / libgo / go / path / filepath / path_windows.go
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         v := VolumeName(path)
18         if v == "" {
19                 return false
20         }
21         path = path[len(v):]
22         if path == "" {
23                 return false
24         }
25         return isSlash(path[0])
26 }
27
28 // VolumeName returns leading volume name.  
29 // Given "C:\foo\bar" it returns "C:" under windows.
30 // Given "\\host\share\foo" it returns "\\host\share".
31 // On other platforms it returns "".
32 func VolumeName(path string) (v string) {
33         if len(path) < 2 {
34                 return ""
35         }
36         // with drive letter
37         c := path[0]
38         if path[1] == ':' &&
39                 ('0' <= c && c <= '9' || 'a' <= c && c <= 'z' ||
40                         'A' <= c && c <= 'Z') {
41                 return path[:2]
42         }
43         // is it UNC
44         if l := len(path); l >= 5 && isSlash(path[0]) && isSlash(path[1]) &&
45                 !isSlash(path[2]) && path[2] != '.' {
46                 // first, leading `\\` and next shouldn't be `\`. its server name.
47                 for n := 3; n < l-1; n++ {
48                         // second, next '\' shouldn't be repeated.
49                         if isSlash(path[n]) {
50                                 n++
51                                 // third, following something characters. its share name.
52                                 if !isSlash(path[n]) {
53                                         if path[n] == '.' {
54                                                 break
55                                         }
56                                         for ; n < l; n++ {
57                                                 if isSlash(path[n]) {
58                                                         break
59                                                 }
60                                         }
61                                         return path[:n]
62                                 }
63                                 break
64                         }
65                 }
66         }
67         return ""
68 }
69
70 // HasPrefix tests whether the path p begins with prefix.
71 // It ignores case while comparing.
72 func HasPrefix(p, prefix string) bool {
73         if strings.HasPrefix(p, prefix) {
74                 return true
75         }
76         return strings.HasPrefix(strings.ToLower(p), strings.ToLower(prefix))
77 }