summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-format/print.go
blob: 288e6af1034066eae4469e5d8c4fb65b3e87e748 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package format

import (
	"io"
	"os"
	"sync"
)

// pool is the global printer buffer pool.
var pool = sync.Pool{
	New: func() interface{} {
		return &Buffer{}
	},
}

// getBuf fetches a buffer from pool.
func getBuf() *Buffer {
	return pool.Get().(*Buffer)
}

// putBuf places a Buffer back in pool.
func putBuf(buf *Buffer) {
	if buf.Cap() > 64<<10 {
		return // drop large
	}
	buf.Reset()
	pool.Put(buf)
}

// Sprint will format supplied values, returning this string.
func Sprint(v ...interface{}) string {
	buf := Buffer{}
	Append(&buf, v...)
	return buf.String()
}

// Sprintf will format supplied format string and args, returning this string.
// See Formatter.Appendf() for more documentation.
func Sprintf(s string, a ...interface{}) string {
	buf := Buffer{}
	Appendf(&buf, s, a...)
	return buf.String()
}

// Print will format supplied values, print this to os.Stdout.
func Print(v ...interface{}) {
	Fprint(os.Stdout, v...) //nolint
}

// Printf will format supplied format string and args, printing this to os.Stdout.
// See Formatter.Appendf() for more documentation.
func Printf(s string, a ...interface{}) {
	Fprintf(os.Stdout, s, a...) //nolint
}

// Println will format supplied values, append a trailing newline and print this to os.Stdout.
func Println(v ...interface{}) {
	Fprintln(os.Stdout, v...) //nolint
}

// Fprint will format supplied values, writing this to an io.Writer.
func Fprint(w io.Writer, v ...interface{}) (int, error) {
	buf := getBuf()
	Append(buf, v...)
	n, err := w.Write(buf.B)
	putBuf(buf)
	return n, err
}

// Fprintf will format supplied format string and args, writing this to an io.Writer.
// See Formatter.Appendf() for more documentation.
func Fprintf(w io.Writer, s string, a ...interface{}) (int, error) {
	buf := getBuf()
	Appendf(buf, s, a...)
	n, err := w.Write(buf.B)
	putBuf(buf)
	return n, err
}

// Println will format supplied values, append a trailing newline and writer this to an io.Writer.
func Fprintln(w io.Writer, v ...interface{}) (int, error) {
	buf := getBuf()
	Append(buf, v...)
	buf.AppendByte('\n')
	n, err := w.Write(buf.B)
	putBuf(buf)
	return n, err
}