Scoop API

scoop:scoop.core:0.1.0

src/floating_text.scoop

Selected production source · src/floating_text.scoop

1// Ryu owns shortest digit generation. Core owns the decimal/scientific spelling
2// and constructs exactly one String from an initialized, stable stack buffer.
3internal fun coreFloatToString(value: Float): String {
4 var raw = (0uL, 0uL, 0uL, 0uL)
5 var text = (0uL, 0uL, 0uL, 0uL)
6 @Unsafe {
7 val input = addressOf(raw).cast<Byte>()
8 val output = addressOf(text).cast<Byte>()
9 val length = coreFloatShortest(value, input).toLong()
10 val size = coreFormatShortest(input, length, output)
11 return coreStringCopyUtf8Unchecked(output, size)
12 }
15internal fun coreDoubleToString(value: Double): String {
16 var raw = (0uL, 0uL, 0uL, 0uL)
17 var text = (0uL, 0uL, 0uL, 0uL)
18 @Unsafe {
19 val input = addressOf(raw).cast<Byte>()
20 val output = addressOf(text).cast<Byte>()
21 val length = coreDoubleShortest(value, input).toLong()
22 val size = coreFormatShortest(input, length, output)
23 return coreStringCopyUtf8Unchecked(output, size)
24 }
27@GCLeaf @Extern(name = "f2s_buffered_n")
28private fun coreFloatShortest(value: Float, bytes: Ptr<Byte>): Int
30@GCLeaf @Extern(name = "d2s_buffered_n")
31private fun coreDoubleShortest(value: Double, bytes: Ptr<Byte>): Int
33// Input is Ryu's at-most-24-byte output. All loops have a fixed small bound;
34// output has 32 bytes, with no terminator required by String construction.
35@NoGC @Unsafe
36private fun coreFormatShortest(raw: Ptr<Byte>, length: Long, out: Ptr<Byte>): Long {
37 var exponentStart = 0L
38 while (exponentStart < length && raw.load(exponentStart) != 69.toByte()) { exponentStart += 1L }
39 if (exponentStart == length) {
40 var index = 0L
41 while (index < length) { out.store(index, raw.load(index)); index += 1L }
42 return length
43 }
44 var written = 0L
45 var start = 0L
46 if (raw.load() == 45.toByte()) { out.store(45.toByte()); written = 1L; start = 1L }
47 var digitStorage = (0uL, 0uL, 0uL)
48 val digits = addressOf(digitStorage).cast<Byte>()
49 var count = 0L
50 var index = start
51 while (index < exponentStart) {
52 val byte = raw.load(index)
53 if (byte != 46.toByte()) { digits.store(count, byte); count += 1L }
54 index += 1L
55 }
56 index = exponentStart + 1L
57 var negative = false
58 if (raw.load(index) == 45.toByte()) { negative = true; index += 1L }
59 else if (raw.load(index) == 43.toByte()) { index += 1L }
60 var exponent = 0L
61 while (index < length) {
62 exponent = exponent * 10L + raw.load(index).toLong() - 48L
63 index += 1L
64 }
65 if (negative) { exponent = -exponent }
66 if (exponent >= -3L && exponent < 7L) {
67 val point = exponent + 1L
68 if (point <= 0L) {
69 out.store(written, 48.toByte()); out.store(written + 1L, 46.toByte())
70 written += 2L
71 index = point
72 while (index < 0L) { out.store(written, 48.toByte()); written += 1L; index += 1L }
73 index = 0L
74 } else {
75 index = 0L
76 while (index < point) {
77 out.store(written, if (index < count) digits.load(index) else 48.toByte())
78 written += 1L; index += 1L
79 }
80 out.store(written, 46.toByte()); written += 1L
81 if (point >= count) { out.store(written, 48.toByte()); return written + 1L }
82 }
83 while (index < count) { out.store(written, digits.load(index)); written += 1L; index += 1L }
84 return written
85 }
86 out.store(written, digits.load()); out.store(written + 1L, 46.toByte())
87 written += 2L
88 if (count == 1L) { out.store(written, 48.toByte()); written += 1L }
89 else {
90 index = 1L
91 while (index < count) { out.store(written, digits.load(index)); written += 1L; index += 1L }
92 }
93 out.store(written, 101.toByte()); written += 1L
94 if (exponent < 0L) { out.store(written, 45.toByte()); written += 1L; exponent = -exponent }
95 // Binary64's exponent magnitude is at most 324. Bounded subtraction avoids
96 // the throwing integer-division operation in this NoGC formatter.
97 var hundreds = 0L
98 while (exponent >= 100L) { exponent -= 100L; hundreds += 1L }
99 if (hundreds != 0L) { out.store(written, (48L + hundreds).toByte()); written += 1L }
100 var tens = 0L
101 while (exponent >= 10L) { exponent -= 10L; tens += 1L }
102 if (hundreds != 0L || tens != 0L) { out.store(written, (48L + tens).toByte()); written += 1L }
103 out.store(written, (48L + exponent).toByte()); written += 1L
104 return written