File: print.go

package info (click to toggle)
golang-github-sanity-io-litter 1.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 200 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,120 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package litter

import (
	"io"
	"math"
	"strconv"
)

func printBool(w io.Writer, value bool) {
	if value {
		w.Write([]byte("true"))
		return
	}
	w.Write([]byte("false"))
}

func printInt(w io.Writer, val int64, base int) {
	w.Write([]byte(strconv.FormatInt(val, base)))
}

func printUint(w io.Writer, val uint64, base int) {
	w.Write([]byte(strconv.FormatUint(val, base)))
}

func printFloat(w io.Writer, val float64, precision int) {
	if math.Trunc(val) == val {
		// Ensure that floats like 1.0 are always printed with a decimal point
		w.Write([]byte(strconv.FormatFloat(val, 'f', 1, precision)))
	} else {
		w.Write([]byte(strconv.FormatFloat(val, 'g', -1, precision)))
	}
}

func printComplex(w io.Writer, c complex128, floatPrecision int) {
	w.Write([]byte("complex"))
	printInt(w, int64(floatPrecision*2), 10)
	r := real(c)
	w.Write([]byte("("))
	w.Write([]byte(strconv.FormatFloat(r, 'g', -1, floatPrecision)))
	i := imag(c)
	if i >= 0 {
		w.Write([]byte("+"))
	}
	w.Write([]byte(strconv.FormatFloat(i, 'g', -1, floatPrecision)))
	w.Write([]byte("i)"))
}

func printNil(w io.Writer) {
	w.Write([]byte("nil"))
}