scoop:scoop.platform:0.1.0
src/fs/path_syntax.scoop
Selected production source · src/fs/path_syntax.scoop
1
package scoop.platform.fs3
import scoop.platform.system.OperatingSystem4
import scoop.platform.system.operatingSystem6
internal 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: Boolean13
) {14
fun isSeparator(value: Byte): Boolean = (windows && value == 92) || (!verbatim && value == 47)16
val separator: Byte get() =17
if (windows)18
9219
else20
4721
}23
internal fun pathSyntax(bytes: Array<Byte>): PathSyntax {24
val windows = operatingSystem == OperatingSystem.Windows25
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
9236
var prefix = 0L37
var implicitRoot = false38
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
9247
) {48
prefix = uncPrefixEnd(bytes, 8L, true)49
implicitRoot = prefix != 0L50
}51
if (prefix == 0L) {52
prefix =53
if (hasDrivePrefix(bytes, 4L))54
6L55
else56
windowsComponentEnd(bytes, 4L, true)57
implicitRoot = !hasDrivePrefix(bytes, 4L)58
}59
} else60
if (windows) {61
if (hasDrivePrefix(bytes, 0L)) {62
prefix = 2L63
} else64
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 != 0L72
}73
}74
var start = prefix75
while (76
start <77
bytes.size &&78
(79
if (windows)80
windowsSeparator(bytes[start], verbatim)81
else82
bytes[start] == 4783
)84
) {85
start += 1L86
}87
val rooted = implicitRoot || start > prefix88
return PathSyntax(89
prefix,90
start,91
rooted,92
rooted && (!windows || prefix != 0L),93
windows,94
verbatim95
)96
}98
private fun windowsSeparator(value: Byte, verbatim: Boolean): Boolean =99
value == 92 || (!verbatim && value == 47)101
private fun hasDrivePrefix(bytes: Array<Byte>, start: Long): Boolean {102
if (bytes.size - start < 2L || bytes[start + 1L] != 58)103
return false104
val letter = bytes[start]105
return (letter >= 65 && letter <= 90) || (letter >= 97 && letter <= 122)106
}108
private fun windowsComponentEnd(bytes: Array<Byte>, start: Long, verbatim: Boolean): Long {109
var end = start110
while (end < bytes.size && !windowsSeparator(bytes[end], verbatim))111
end += 1L112
return end113
}115
private 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 0L119
val shareStart = serverEnd + 1L120
val shareEnd = windowsComponentEnd(bytes, shareStart, verbatim)121
return if (shareEnd == shareStart)122
0L123
else124
shareEnd125
}