scoop:scoop.core:0.1.0
src/characters.scoop
Selected production source · src/characters.scoop
1
/**2
* One Unicode scalar value. Surrogates are excluded; a Char is neither a UTF-8 byte nor a grapheme3
* cluster.4
*/5
@Intrinsic("core_char")6
public struct Char : Compare<Char>,7
Equals<Char>,8
ToString,9
Hash,10
RangeTo<Char, CharRange>,11
RangeUntil<Char, CharRange> {12
@NoGC @Intrinsic("char_code")13
private fun codeValue(): Int15
/**16
* The Unicode scalar number as an Int.17
*/18
public val code: Int19
@NoGC get() = codeValue()21
/**22
* Compares Unicode scalar numbers.23
*/24
@NoGC @Intrinsic("char_equals")25
public override operator fun equals(other: Char): Boolean27
/**28
* Orders characters by Unicode scalar number.29
*/30
@NoGC @Intrinsic("char_compare_to")31
public override operator fun compareTo(other: Char): Long33
/** Creates an ascending character range including both endpoints. */34
public override operator fun rangeTo(endpoint: Char): CharRange = CharRange(this, endpoint)36
/** Creates an ascending character range excluding the endpoint. */37
public override operator fun rangeUntil(endpoint: Char): CharRange = CharRange(38
this,39
endpoint,40
inclusive = false41
)43
/** Creates an ascending character range excluding the endpoint. */44
public infix fun until(endpoint: Char): CharRange = rangeUntil(endpoint)46
/** Creates a descending character range including both endpoints. */47
public infix fun downTo(endpoint: Char): CharRange = CharRange(48
this,49
endpoint,50
descending = true51
)53
/**54
* Encodes this one scalar as a UTF-8 string.55
*/56
public override fun toString(): String = coreCharToString(code.toUInt())57
/**58
* Returns the Unicode scalar number as a Long.59
*/60
public override fun hash(): Long = code.toLong()62
/**63
* Inspection and serialization operations for this value type.64
*/65
public companion object : ValueEncoder<Char>, ValueDecoder<Char>, ValueInspector<Char> {66
/**67
* Exposes the value as a typed scalar observation.68
*/69
public override fun inspect(value: Char): ValueView = ValueObservation(70
typeInfo<Char>().type,71
None,72
{ ValueShape.Scalar(ScalarValue.Char(value)) }73
)75
/**76
* Writes the scalar value through the corresponding single-value encoding operation.77
*/78
public override fun encode(value: Char, encoder: Encoder): Unit = encoder79
.singleValue()80
.writeString(value.toString())82
/**83
* Reads the scalar through the corresponding decoding operation. A character requires84
* exactly one Unicode scalar.85
*/86
public override fun decode(decoder: Decoder): Char {87
val text = decoder.singleValue().readString()88
if (text.charLength != 1L) {89
throw DecodingException(decoder.path, "expected one Unicode scalar")90
}91
return text.getCharAt(0L)92
}93
}94
}96
/**97
* Converts an Int Unicode scalar number to Char. Negative values, surrogate numbers, and values98
* above U+10FFFF throw [IllegalArgumentException].99
*/100
public fun Int.toChar(): Char {101
if (this < 0 || this > 1114111 || (this >= 55296 && this <= 57343)) {102
throw IllegalArgumentException()103
}104
@Unsafe {105
return this.characterUnchecked()106
}107
}