summaryrefslogtreecommitdiff
path: root/ioutil.go
blob: a53eac3aaba0eafdc43adc2661d180a299310db8 (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
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package lowmemjson

import (
	"io"
	"unicode/utf8"
)

func writeByte(w io.Writer, c byte) error {
	if br, ok := w.(interface{ WriteByte(byte) error }); ok {
		return br.WriteByte(c)
	}
	var buf [1]byte
	buf[0] = c
	if _, err := w.Write(buf[:]); err != nil {
		return err
	}
	return nil
}

func writeRune(w io.Writer, c rune) (int, error) {
	if rw, ok := w.(interface{ WriteRune(rune) (int, error) }); ok {
		return rw.WriteRune(c)
	}
	var buf [utf8.UTFMax]byte
	n := utf8.EncodeRune(buf[:], c)
	return w.Write(buf[:n])
}