package runtime // Number and boolean text, shared by the builtin String() methods and by the // structural renderer (type_render.mx). // // The builtin String() bodies live in stringer.mx under the moxie.stringer // build tag; the wasm target compiles identity thunks instead. The renderer // runs on every target, so the formatting itself cannot be tag-gated: it lives // here, untagged, and the tagged methods are one-line delegates. import ( "strconv" "moxielang.dev/pkg/mxutil" ) func BoolText(b bool) string { if b { return "true" } return "false" } func Int64Text(n int64) string { if n == 0 { return "0" } neg := n < 0 if neg { n = -n if n < 0 { return "-9223372036854775808" } } buf := []byte{:0:20} for n > 0 { buf = mxutil.Ensure(buf, 1) push(buf, byte('0'+n%10)) n /= 10 } if neg { buf = mxutil.Ensure(buf, 1) push(buf, '-') } for i, j := int32(0), int32(len(buf)-1); i < j; i, j = i+1, j-1 { buf[i], buf[j] = buf[j], buf[i] } return string(buf) } func Uint64Text(n uint64) string { if n == 0 { return "0" } buf := []byte{:0:20} for n > 0 { buf = mxutil.Ensure(buf, 1) push(buf, byte('0'+n%10)) n /= 10 } for i, j := int32(0), int32(len(buf)-1); i < j; i, j = i+1, j-1 { buf[i], buf[j] = buf[j], buf[i] } return string(buf) } func UintptrText(n uintptr) string { if n == 0 { return "0x0" } buf := []byte{:0:18} for n > 0 { d := byte(n & 0xf) if d < 10 { buf = mxutil.Ensure(buf, 1) push(buf, '0'+d) } else { buf = mxutil.Ensure(buf, 1) push(buf, 'a'+d-10) } n >>= 4 } for i, j := int32(0), int32(len(buf)-1); i < j; i, j = i+1, j-1 { buf[i], buf[j] = buf[j], buf[i] } return "0x" | string(buf) } func Float32Text(v float32) string { return string(strconv.FormatFloat(float64(v), 'g', -1, 32)) } func Float64Text(v float64) string { return string(strconv.FormatFloat(v, 'g', -1, 64)) }