scoop:scoop.core:0.1.0
src/strings.scoop
Selected production source · src/strings.scoop
1
/** Immutable UTF-8 text. */2
@DocAlias("text")3
@Intrinsic("core_string")4
public class String : Compare<String>, Add<String, String>, Equals<String>, ToString, Hash, Iterable<Char> {5
/**6
* The UTF-8 byte count, available in constant time.7
*/8
public val byteLength: Long get() = coreStringByteLength(this)9
/**10
* The number of Unicode scalar values, computed in O(byteLength) time by scanning UTF-8.11
* Combining marks count separately.12
*/13
public val charLength: Long get() = coreStringLength(this)15
/**16
* Compares text exactly, without Unicode normalization or case folding.17
*/18
public override operator fun equals(other: String): Boolean {19
if (this === other) { return true }20
val length = byteLength21
if (length != other.byteLength) { return false }22
return length == 0L || compareTo(other) == 0L23
}25
/**26
* Concatenates the two immutable UTF-8 strings.27
*/28
@Intrinsic("string_concat")29
public override operator fun plus(other: String): String31
/**32
* Compares strings lexicographically using their Unicode text order.33
*/34
@Intrinsic("string_compare_to")35
public override operator fun compareTo(other: String): Long37
/**38
* Returns this immutable string.39
*/40
public override fun toString(): String = this41
/**42
* Returns a hash consistent with exact string equality.43
*/44
public override fun hash(): Long = coreStringHash(this)46
/**47
* Returns the Unicode scalar at a zero-based scalar index by scanning from the beginning,48
* taking O(byteLength) time in the worst case. An invalid index throws49
* [IndexOutOfBoundsException]. Use iteration for a complete sequential traversal.50
*/51
public fun getCharAt(idx: Long): Char {52
val value = coreStringGet(this, idx)53
if (value == None) { throw IndexOutOfBoundsException() }54
return value!!55
}57
/**58
* Returns the scalar-indexed half-open range from `start` to `endExclusive`. Invalid bounds59
* throw [IndexOutOfBoundsException]; an empty range is allowed.60
*/61
public fun slice(start: Long, endExclusive: Long): String {62
val result = coreStringSliceBounds(this, start, endExclusive)63
if (result == None) { throw IndexOutOfBoundsException() }64
val bounds = result!!65
return coreStringSliceBytes(this, bounds._1, bounds._2)66
}68
/**69
* Traverses Unicode scalars in order without repeatedly rescanning from the beginning.70
*/71
public override operator fun iterator(): Iterator<Char> = StringIterator(this)73
/**74
* Copies Unicode scalars into an immutable character array.75
*/76
public fun toCharArray(): Array<Char> {77
val source = iterator()78
return Array<Char>(charLength) { _ -> source.next()!! }79
}81
/**82
* Copies Unicode scalars into independently mutable character slots.83
*/84
public fun toMutableCharArray(): MutableArray<Char> {85
val source = iterator()86
return MutableArray<Char>(charLength) { _ -> source.next()!! }87
}89
/**90
* Copies the UTF-8 encoding into an immutable byte array without a NUL terminator.91
*/92
public fun toByteArray(): Array<Byte> = coreStringToBytes(this)94
/**95
* Copies the UTF-8 encoding into an independently mutable byte array.96
*/97
public fun toMutableByteArray(): MutableArray<Byte> = coreStringToMutableBytes(this)99
/**100
* String construction and scalar encoding, decoding, and inspection operations.101
*/102
public companion object : ValueEncoder<String>, ValueDecoder<String>, ValueInspector<String> {103
/**104
* Observes the complete string as a text scalar.105
*/106
public override fun inspect(value: String): ValueView =107
ValueObservation(typeInfo<String>().type, Some(value), { ValueShape.Scalar(ScalarValue.String(value)) })109
/**110
* Writes the string through the backend's text scalar operation.111
*/112
public override fun encode(value: String, encoder: Encoder): Unit =113
encoder.singleValue().writeString(value)115
/**116
* Reads a string through the backend's text scalar operation.117
*/118
public override fun decode(decoder: Decoder): String =119
decoder.singleValue().readString()121
/**122
* Encodes the supplied Unicode scalars as a new UTF-8 string.123
*/124
public fun fromChars(chars: List<Char>): String = coreStringFromChars(chars.toArray())126
/**127
* Copies bytes that the unsafe caller guarantees are valid UTF-8.128
*/129
@Unsafe130
public fun fromUtf8Unchecked(bytes: List<Byte>): String =131
coreStringFromBytes(bytes.toArray())133
/**134
* Validates and copies UTF-8 bytes. Invalid encoding throws [CharacterCodingException];135
* raw-pointer callers must provide readable, stable memory for the specified length.136
*/137
public fun fromUtf8(bytes: List<Byte>): String =138
requireValidUtf8(decodeUtf8(bytes))140
/**141
* Returns decoded UTF-8 text, or None for invalid encoding.142
*/143
public fun fromUtf8OrNone(bytes: List<Byte>): String? = decodeUtf8(bytes)._1145
/**146
* Decodes UTF-8 while replacing malformed input with Unicode replacement characters.147
*/148
public fun fromUtf8Lossy(bytes: List<Byte>): String = decodeUtf8Lossy(bytes)150
/**151
* Validates and copies UTF-8 bytes. Invalid encoding throws [CharacterCodingException];152
* raw-pointer callers must provide readable, stable memory for the specified length.153
*/154
@Unsafe155
public fun fromUtf8(pointer: Ptr<UInt8>, length: Long): String {156
if (length < 0L) { throw IllegalArgumentException() }157
return requireValidUtf8(coreStringDecodeUtf8(pointer, length))158
}160
/**161
* Copies and validates the UTF-8 bytes preceding the first NUL. The unsafe caller must162
* keep the entire terminated region readable and stable.163
*/164
@Unsafe165
public fun fromCString(pointer: Ptr<Int8>): String =166
fromUtf8(pointer.cast<UInt8>(), coreCStringLength(pointer).toLong())167
}168
}170
private fun coreStringHashLong(value: String): Long {171
@Unsafe {172
val length = coreStringByteLength(value)173
var hash = 1469598103934665603uL174
var index = 0L175
while (index < length) {176
hash = (hash.xor(coreStringByteAtUnchecked(value, index).toUByte().toULong())) * 1099511628211uL177
index += 1L178
}179
return hash.toLong()180
}181
}183
internal fun coreStringHash(value: String): Long {184
val length = coreStringByteLength(value)185
if (length > 32L) { return coreStringHashLong(value) }186
var hash = 1469598103934665603uL187
if (length <= 0L) { return hash.toLong() }188
hash = hash.xor(coreStringByteAt(value, 0L).toUByte().toULong()) * 1099511628211uL189
if (length <= 1L) { return hash.toLong() }190
hash = hash.xor(coreStringByteAt(value, 1L).toUByte().toULong()) * 1099511628211uL191
if (length <= 2L) { return hash.toLong() }192
hash = hash.xor(coreStringByteAt(value, 2L).toUByte().toULong()) * 1099511628211uL193
if (length <= 3L) { return hash.toLong() }194
hash = hash.xor(coreStringByteAt(value, 3L).toUByte().toULong()) * 1099511628211uL195
if (length <= 4L) { return hash.toLong() }196
hash = hash.xor(coreStringByteAt(value, 4L).toUByte().toULong()) * 1099511628211uL197
if (length <= 5L) { return hash.toLong() }198
hash = hash.xor(coreStringByteAt(value, 5L).toUByte().toULong()) * 1099511628211uL199
if (length <= 6L) { return hash.toLong() }200
hash = hash.xor(coreStringByteAt(value, 6L).toUByte().toULong()) * 1099511628211uL201
if (length <= 7L) { return hash.toLong() }202
hash = hash.xor(coreStringByteAt(value, 7L).toUByte().toULong()) * 1099511628211uL203
if (length <= 8L) { return hash.toLong() }204
hash = hash.xor(coreStringByteAt(value, 8L).toUByte().toULong()) * 1099511628211uL205
if (length <= 9L) { return hash.toLong() }206
hash = hash.xor(coreStringByteAt(value, 9L).toUByte().toULong()) * 1099511628211uL207
if (length <= 10L) { return hash.toLong() }208
hash = hash.xor(coreStringByteAt(value, 10L).toUByte().toULong()) * 1099511628211uL209
if (length <= 11L) { return hash.toLong() }210
hash = hash.xor(coreStringByteAt(value, 11L).toUByte().toULong()) * 1099511628211uL211
if (length <= 12L) { return hash.toLong() }212
hash = hash.xor(coreStringByteAt(value, 12L).toUByte().toULong()) * 1099511628211uL213
if (length <= 13L) { return hash.toLong() }214
hash = hash.xor(coreStringByteAt(value, 13L).toUByte().toULong()) * 1099511628211uL215
if (length <= 14L) { return hash.toLong() }216
hash = hash.xor(coreStringByteAt(value, 14L).toUByte().toULong()) * 1099511628211uL217
if (length <= 15L) { return hash.toLong() }218
hash = hash.xor(coreStringByteAt(value, 15L).toUByte().toULong()) * 1099511628211uL219
if (length <= 16L) { return hash.toLong() }220
hash = hash.xor(coreStringByteAt(value, 16L).toUByte().toULong()) * 1099511628211uL221
if (length <= 17L) { return hash.toLong() }222
hash = hash.xor(coreStringByteAt(value, 17L).toUByte().toULong()) * 1099511628211uL223
if (length <= 18L) { return hash.toLong() }224
hash = hash.xor(coreStringByteAt(value, 18L).toUByte().toULong()) * 1099511628211uL225
if (length <= 19L) { return hash.toLong() }226
hash = hash.xor(coreStringByteAt(value, 19L).toUByte().toULong()) * 1099511628211uL227
if (length <= 20L) { return hash.toLong() }228
hash = hash.xor(coreStringByteAt(value, 20L).toUByte().toULong()) * 1099511628211uL229
if (length <= 21L) { return hash.toLong() }230
hash = hash.xor(coreStringByteAt(value, 21L).toUByte().toULong()) * 1099511628211uL231
if (length <= 22L) { return hash.toLong() }232
hash = hash.xor(coreStringByteAt(value, 22L).toUByte().toULong()) * 1099511628211uL233
if (length <= 23L) { return hash.toLong() }234
hash = hash.xor(coreStringByteAt(value, 23L).toUByte().toULong()) * 1099511628211uL235
if (length <= 24L) { return hash.toLong() }236
hash = hash.xor(coreStringByteAt(value, 24L).toUByte().toULong()) * 1099511628211uL237
if (length <= 25L) { return hash.toLong() }238
hash = hash.xor(coreStringByteAt(value, 25L).toUByte().toULong()) * 1099511628211uL239
if (length <= 26L) { return hash.toLong() }240
hash = hash.xor(coreStringByteAt(value, 26L).toUByte().toULong()) * 1099511628211uL241
if (length <= 27L) { return hash.toLong() }242
hash = hash.xor(coreStringByteAt(value, 27L).toUByte().toULong()) * 1099511628211uL243
if (length <= 28L) { return hash.toLong() }244
hash = hash.xor(coreStringByteAt(value, 28L).toUByte().toULong()) * 1099511628211uL245
if (length <= 29L) { return hash.toLong() }246
hash = hash.xor(coreStringByteAt(value, 29L).toUByte().toULong()) * 1099511628211uL247
if (length <= 30L) { return hash.toLong() }248
hash = hash.xor(coreStringByteAt(value, 30L).toUByte().toULong()) * 1099511628211uL249
if (length <= 31L) { return hash.toLong() }250
hash = hash.xor(coreStringByteAt(value, 31L).toUByte().toULong()) * 1099511628211uL251
return hash.toLong()252
}254
@Intrinsic("string_byte_length")255
internal fun coreStringByteLength(value: String): Long257
@Intrinsic("string_character_at_byte")258
internal fun coreStringCharacterAtByte(value: String, index: Long): Char260
@Intrinsic("string_byte_at")261
internal fun coreStringByteAt(value: String, index: Long): Byte263
@Intrinsic("string_slice_bytes")264
internal fun coreStringSliceBytes(value: String, start: Long, endExclusive: Long): String266
@Intrinsic("string_from_bytes")267
internal fun coreStringFromBytes(value: Array<Byte>): String