scoop:scoop.platform:0.1.0
src/fs/path.scoop
Selected production source · src/fs/path.scoop
1
package scoop.platform.fs3
import scoop.bytes.ByteArrayBuilder4
import scoop.platform.system.OsString5
import scoop.platform.system.byteContentsEqual6
import scoop.platform.system.byteContentsHash8
/**9
* An immutable native byte path with no embedded NUL. Lexical operations do not access the10
* filesystem or resolve symbolic links; equality compares stored bytes.11
*/12
public class Path private constructor(private val bytes: Array<Byte>): Equals<Path>, Hash {13
private val syntax: PathSyntax = pathSyntax(bytes)14
/**15
* Whether the path is fully qualified under the compilation target's rules.16
*/17
public val isAbsolute: Boolean get() = syntax.isAbsolute19
/**20
* The last non-separator component, or None for a root, empty path, dot, or dot-dot component.21
*/22
public val fileName: OsString? get() {23
val end = endWithoutSeparators()24
if (end == syntax.rootEnd)25
return None26
val start = componentStart(end)27
val name =28
OsString.fromByteArray(Array<Byte>(end - start) { index -> bytes[start + index] })29
if (name == OsString.fromString(".") || name == OsString.fromString(".."))30
return None31
return Some(name)32
}34
/**35
* The lexical parent when one exists; this does not query the filesystem.36
*/37
public val parent: Path? get() {38
val end = endWithoutSeparators()39
if (end == syntax.rootEnd)40
return None41
var start = componentStart(end)42
while (start > syntax.rootEnd && syntax.isSeparator(bytes[start - 1L]))43
start -= 1L44
val count =45
if (start == syntax.rootEnd && syntax.rootEnd > syntax.prefixLength)46
syntax.prefixLength + 1L47
else48
start49
return Some(Path(Array<Byte>(count) { index -> bytes[index] }))50
}52
private fun endWithoutSeparators(): Long {53
var end = bytes.size54
while (end > syntax.rootEnd && syntax.isSeparator(bytes[end - 1L]))55
end -= 1L56
return end57
}59
private fun componentStart(end: Long): Long {60
var start = end61
while (start > syntax.rootEnd && !syntax.isSeparator(bytes[start - 1L]))62
start -= 1L63
return start64
}66
/**67
* Returns nonempty components after the target's native path prefix and root.68
*/69
public fun components(): Array<OsString> {70
val parts = ArrayList<OsString>()71
var cursor = syntax.rootEnd72
while (cursor < bytes.size) {73
if (syntax.isSeparator(bytes[cursor])) {74
cursor += 1L;75
continue76
}77
val start = cursor78
while (cursor < bytes.size && !syntax.isSeparator(bytes[cursor]))79
cursor += 1L80
parts.add(81
OsString.fromByteArray(82
Array<Byte>(cursor - start) { index -> bytes[start + index] }83
)84
)85
}86
return parts.toArray()87
}89
/**90
* Joins a relative child to this path. A prefixed or absolute child replaces the base; a91
* Windows rooted child retains the base's prefix.92
*/93
public fun resolve(child: Path): Path {94
if (child.isAbsolute || child.syntax.prefixLength > 0L || bytes.size == 0L)95
return child96
if (child.bytes.size == 0L)97
return this98
val result = ByteArrayBuilder()99
if (child.syntax.hasRoot) {100
result.append(Array<Byte>(syntax.prefixLength) { index -> bytes[index] })101
appendChild(result, child)102
return Path(result.toByteArray())103
}104
result.append(bytes)105
if (106
!syntax.isSeparator(bytes[bytes.size - 1L]) &&107
!(syntax.prefixLength == bytes.size && !syntax.hasRoot)108
)109
result.append(syntax.separator)110
appendChild(result, child)111
return Path(result.toByteArray())112
}114
private fun appendChild(result: ByteArrayBuilder, child: Path) {115
if (syntax.verbatim && !child.syntax.verbatim) {116
result.append(117
Array<Byte>(child.bytes.size) {118
index ->119
val byte = child.bytes[index]120
if (child.syntax.isSeparator(byte))121
syntax.separator122
else123
byte124
}125
)126
} else127
result.append(child.bytes)128
}130
/**131
* Removes redundant separators and dot components and folds lexical dot-dot pairs. It does not132
* follow links or guarantee equivalence through symbolic links.133
*/134
public fun normalizeLexically(): Path {135
if (syntax.verbatim)136
return this137
val dot = OsString.fromString(".")138
val up = OsString.fromString("..")139
val parts = ArrayList<OsString>()140
for (part in components()) {141
if (part == dot)142
continue143
if (part == up) {144
if (parts.size > 0L && parts[parts.size - 1L] != up)145
parts.removeAt(parts.size - 1L)146
else147
if (!syntax.hasRoot)148
parts.add(part)149
} else150
parts.add(part)151
}152
val result = ByteArrayBuilder()153
for (byte in Array<Byte>(syntax.prefixLength) { index -> bytes[index] }) {154
result.append(155
if (syntax.isSeparator(byte))156
syntax.separator157
else158
byte159
)160
}161
if (syntax.hasRoot)162
result.append(syntax.separator)163
val prefix = result.size164
for (part in parts) {165
if (result.size > prefix)166
result.append(syntax.separator)167
result.append(part.toByteArray())168
}169
if (result.size == 0L)170
result.append(46.toByte())171
return Path(result.toByteArray())172
}174
/**175
* Copies the stored path bytes into an operating-system string.176
*/177
public fun toOsString(): OsString = OsString.fromByteArray(bytes)178
/**179
* Decodes the stored bytes as strict UTF-8, rejecting malformed encoding.180
*/181
public fun decodeUtf8(): String = toOsString().decodeUtf8()182
/**183
* Decodes stored bytes with replacement for malformed UTF-8.184
*/185
public fun decodeUtf8Lossy(): String = toOsString().decodeUtf8Lossy()186
/**187
* Returns a lossy UTF-8 presentation suitable for displaying native names.188
*/189
public fun display(): String = decodeUtf8Lossy()190
/**191
* Compares the exact stored byte sequences.192
*/193
public override operator fun equals(other: Path): Boolean =194
byteContentsEqual(bytes, other.bytes)195
/**196
* Hashes the stored bytes consistently with equality.197
*/198
public override fun hash(): Long = byteContentsHash(bytes)200
/**201
* Creates independent native-byte values from text or existing native bytes.202
*/203
public companion object {204
/**205
* Encodes a Unicode path as UTF-8 and rejects embedded NUL.206
*/207
public fun fromString(value: String): Path = fromOsString(OsString.fromString(value))208
/**209
* Copies a native path after rejecting embedded NUL bytes.210
*/211
public fun fromOsString(value: OsString): Path {212
val bytes = value.toByteArray()213
for (byte in bytes) {214
if (byte == 0)215
throw IllegalArgumentException(Some("path contains NUL"))216
}217
return Path(bytes)218
}219
}220
}