Scoop API

scoop:scoop.platform:0.1.0

src/fs/path_syntax.scoop

Selected production source · src/fs/path_syntax.scoop

1package scoop.platform.fs
3import scoop.platform.system.OperatingSystem
4import scoop.platform.system.operatingSystem
6internal struct PathSyntax(
7 val prefixLength: Long,
8 val rootEnd: Long,
9 val hasRoot: Boolean,
10 val isAbsolute: Boolean,
11 val windows: Boolean,
12 val verbatim: Boolean
13) {
14 fun isSeparator(value: Byte): Boolean = (windows && value == 92) || (!verbatim && value == 47)
16 val separator: Byte get() =
17 if (windows)
18 92
19 else
20 47
23internal fun pathSyntax(bytes: Array<Byte>): PathSyntax {
24 val windows = operatingSystem == OperatingSystem.Windows
25 val verbatim =
26 windows &&
27 bytes.size >=
28 4L &&
29 bytes[0L] ==
30 92 &&
31 bytes[1L] ==
32 92 &&
33 (bytes[2L] == 63 || bytes[2L] == 46) &&
34 bytes[3L] ==
35 92
36 var prefix = 0L
37 var implicitRoot = false
38 if (verbatim) {
39 if (
40 bytes.size >=
41 8L &&
42 (bytes[4L] == 85 || bytes[4L] == 117) &&
43 (bytes[5L] == 78 || bytes[5L] == 110) &&
44 (bytes[6L] == 67 || bytes[6L] == 99) &&
45 bytes[7L] ==
46 92
47 ) {
48 prefix = uncPrefixEnd(bytes, 8L, true)
49 implicitRoot = prefix != 0L
50 }
51 if (prefix == 0L) {
52 prefix =
53 if (hasDrivePrefix(bytes, 4L))
54 6L
55 else
56 windowsComponentEnd(bytes, 4L, true)
57 implicitRoot = !hasDrivePrefix(bytes, 4L)
58 }
59 } else
60 if (windows) {
61 if (hasDrivePrefix(bytes, 0L)) {
62 prefix = 2L
63 } else
64 if (
65 bytes.size >=
66 2L &&
67 windowsSeparator(bytes[0L], false) &&
68 windowsSeparator(bytes[1L], false)
69 ) {
70 prefix = uncPrefixEnd(bytes, 2L, false)
71 implicitRoot = prefix != 0L
72 }
73 }
74 var start = prefix
75 while (
76 start <
77 bytes.size &&
78 (
79 if (windows)
80 windowsSeparator(bytes[start], verbatim)
81 else
82 bytes[start] == 47
83 )
84 ) {
85 start += 1L
86 }
87 val rooted = implicitRoot || start > prefix
88 return PathSyntax(
89 prefix,
90 start,
91 rooted,
92 rooted && (!windows || prefix != 0L),
93 windows,
94 verbatim
95 )
98private fun windowsSeparator(value: Byte, verbatim: Boolean): Boolean =
99 value == 92 || (!verbatim && value == 47)
101private fun hasDrivePrefix(bytes: Array<Byte>, start: Long): Boolean {
102 if (bytes.size - start < 2L || bytes[start + 1L] != 58)
103 return false
104 val letter = bytes[start]
105 return (letter >= 65 && letter <= 90) || (letter >= 97 && letter <= 122)
108private fun windowsComponentEnd(bytes: Array<Byte>, start: Long, verbatim: Boolean): Long {
109 var end = start
110 while (end < bytes.size && !windowsSeparator(bytes[end], verbatim))
111 end += 1L
112 return end
115private fun uncPrefixEnd(bytes: Array<Byte>, start: Long, verbatim: Boolean): Long {
116 val serverEnd = windowsComponentEnd(bytes, start, verbatim)
117 if (serverEnd == start || serverEnd == bytes.size)
118 return 0L
119 val shareStart = serverEnd + 1L
120 val shareEnd = windowsComponentEnd(bytes, shareStart, verbatim)
121 return if (shareEnd == shareStart)
122 0L
123 else
124 shareEnd