Scoop API

scoop:scoop.core:0.1.0

src/string_builder.scoop

Selected production source · src/string_builder.scoop

1/**
2 * Accumulates UTF-8 bytes and copies immutable snapshots when converted to text.
3 * The optional initial capacity reserves bytes without adding content.
4 */
5public class StringBuilder public constructor(initialCapacity: Long = 0L): ToString {
6 private var storage: MutableArray<UInt8> = MutableArray<UInt8>(initialCapacity) { _ -> 0u }
7 private var byteCount: Long = 0L
9 private fun ensureRoom(extra: Long) {
10 if (extra > storage.size - byteCount) {
11 grow(extra)
12 }
13 }
15 private fun grow(extra: Long) {
16 val required = coreStringAddLength(byteCount, extra)
17 val capacity = storage.size
18 val doubled =
19 if (capacity > 4611686018427387888L) {
20 required
21 } else {
22 capacity * 2L
23 }
24 val nextCapacity =
25 if (required > doubled) {
26 required
27 } else {
28 doubled
29 }
30 val replacement =
31 MutableArray<UInt8>(
32 if (nextCapacity < 32L) {
33 32L
34 } else {
35 nextCapacity
36 }
37 ) { _ -> 0u }
38 @Unsafe
39 {
40 coreBytesCopyPrefixUnchecked(storage, replacement, byteCount)
41 }
42 storage = replacement
43 }
45 /** Appends text and returns this builder for chaining. */
46 public fun append(value: String): StringBuilder {
47 val length = value.byteLength
48 ensureRoom(length)
49 @Unsafe
50 {
51 coreStringCopyToBytesUnchecked(value, storage, byteCount)
52 }
53 byteCount += length
54 return this
55 }
57 /** Appends a signed integer without allocating an intermediate String. */
58 public fun append(value: Long): StringBuilder {
59 var digits = (0u, 0u, 0u, 0u, 0u)
60 @Unsafe
61 {
62 val bytes = addressOf(digits).cast<Byte>()
63 val length = coreWriteSignedDecimal(value, bytes)
64 return appendBytes(bytes + (20L - length), length)
65 }
66 }
68 /** Appends an unsigned integer without allocating an intermediate String. */
69 public fun append(value: ULong): StringBuilder {
70 var digits = (0u, 0u, 0u, 0u, 0u)
71 @Unsafe
72 {
73 val bytes = addressOf(digits).cast<Byte>()
74 val length = coreWriteUnsignedDecimal(value, bytes)
75 return appendBytes(bytes + (20L - length), length)
76 }
77 }
79 /** Appends the decimal representation of a signed integer. */
80 public fun append(value: Int): StringBuilder = append(value.toLong())
81 /** Appends the decimal representation of a signed integer. */
82 public fun append(value: Int16): StringBuilder = append(value.toLong())
83 /** Appends the decimal representation of a signed integer. */
84 public fun append(value: Int8): StringBuilder = append(value.toLong())
85 /** Appends the decimal representation of an unsigned integer. */
86 public fun append(value: UInt): StringBuilder = append(value.toULong())
87 /** Appends the decimal representation of an unsigned integer. */
88 public fun append(value: UInt16): StringBuilder = append(value.toULong())
89 /** Appends the decimal representation of an unsigned integer. */
90 public fun append(value: UInt8): StringBuilder = append(value.toULong())
92 /** Appends one Unicode scalar without allocating an intermediate String. */
93 public fun append(value: Char): StringBuilder {
94 val code = value.code.toUInt()
95 val word = coreUtf8Word(code)
96 val width = coreUtf8Width(code)
97 ensureRoom(width)
98 val target = storage
99 val start = byteCount
100 target[start] = word.toUByte()
101 if (width > 1L) {
102 target[start + 1L] = word.shr(8L).toUByte()
103 }
104 if (width > 2L) {
105 target[start + 2L] = word.shr(16L).toUByte()
106 }
107 if (width > 3L) {
108 target[start + 3L] = word.shr(24L).toUByte()
109 }
110 byteCount = start + width
111 return this
112 }
114 /** Appends the fixed Boolean text without allocating an intermediate String. */
115 public fun append(value: Boolean): StringBuilder =
116 append(
117 if (value) {
118 "true"
119 } else {
120 "false"
121 }
122 )
124 /** Converts once before appending, preserving reentrant builder modifications. */
125 public fun <T: ToString> append(value: T): StringBuilder {
126 val text = value.toString()
127 return append(text)
128 }
130 @Unsafe
131 private fun appendBytes(bytes: Ptr<Byte>, length: Long): StringBuilder {
132 ensureRoom(length)
133 coreBytesCopyRawUnchecked(bytes, length, storage, byteCount)
134 byteCount += length
135 return this
136 }
138 /** Copies the current UTF-8 prefix into an immutable string. */
139 public override fun toString(): String {
140 if (byteCount == 0L) {
141 return ""
142 }
143 @Unsafe
144 {
145 return coreStringFromBytePrefixUnchecked(storage, byteCount)
146 }
147 }
149 /** Removes content in constant time, preserving capacity and existing snapshots. */
150 public fun clear(): StringBuilder {
151 byteCount = 0L
152 return this
153 }