summaryrefslogtreecommitdiff
path: root/misc.go
blob: a567cc7573382d9be8d8f7730e6661e13a155be6 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (C) 2022  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package lowmemjson

import (
	"encoding/json"
	"io"
	"reflect"
	"unicode/utf8"
)

const Tab = "\t"

const hex = "0123456789abcdef"

func hex2int[T interface{ byte | rune }](c T) (byte, bool) {
	switch {
	case '0' <= c && c <= '9':
		return byte(c) - '0', true
	case 'a' <= c && c <= 'f':
		return byte(c) - 'a' + 10, true
	case 'A' <= c && c <= 'F':
		return byte(c) - 'A' + 10, true
	default:
		return 0, false
	}
}

var (
	numberType    = reflect.TypeOf(json.Number(""))
	byteType      = reflect.TypeOf(byte(0))
	byteSliceType = reflect.TypeOf(([]byte)(nil))
)

// generic I/O /////////////////////////////////////////////////////////////////

func decodeRune[T interface{ []byte | string }](s T) (r rune, size int) {
	iface := any(s)
	if str, ok := iface.(string); ok {
		return utf8.DecodeRuneInString(str)
	} else {
		return utf8.DecodeRune(iface.([]byte))
	}
}

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])
}

// JSON string encoding ////////////////////////////////////////////////////////

func UnicodeEscapeJSSafe(c rune, _ bool) bool {
	// JSON is notionally a JS subset, but that's not actually
	// true.
	//
	// http://timelessrepo.com/json-isnt-a-javascript-subset
	switch c {
	case '\u2028', '\u2029':
		return true
	default:
		return false
	}
}

func UnicodeEscapeHTMLSafe(c rune, wasEscaped bool) bool {
	switch c {
	case '&', '<', '>':
		return true
	default:
		return UnicodeEscapeJSSafe(c, wasEscaped)
	}
}

func UnicodeEscapeDefault(c rune, wasEscaped bool) bool {
	switch c {
	case '\b', '\f', utf8.RuneError:
		return true
	default:
		return UnicodeEscapeHTMLSafe(c, wasEscaped)
	}
}

func writeStringUnicodeEscape(w io.Writer, c rune) (int, error) {
	buf := [6]byte{
		'\\',
		'u',
		hex[(c>>12)&0xf],
		hex[(c>>8)&0xf],
		hex[(c>>4)&0xf],
		hex[(c>>0)&0xf],
	}
	return w.Write(buf[:])
}
func writeStringShortEscape(w io.Writer, c byte) (int, error) {
	buf := [2]byte{'\\', c}
	return w.Write(buf[:])
}
func writeStringChar(w io.Writer, c rune, wasEscaped bool, escaper func(rune, bool) bool) (int, error) {
	if escaper == nil {
		escaper = UnicodeEscapeDefault
	}
	switch {
	case c <= 0xFFFF && escaper(c, wasEscaped):
		return writeStringUnicodeEscape(w, c)
	case c == '"' || c == '\\':
		return writeStringShortEscape(w, byte(c))
	case c < 0x0020:
		switch c {
		case '\b':
			return writeStringShortEscape(w, 'b')
		case '\f':
			return writeStringShortEscape(w, 'f')
		case '\n':
			return writeStringShortEscape(w, 'n')
		case '\r':
			return writeStringShortEscape(w, 'r')
		case '\t':
			return writeStringShortEscape(w, 't')
		default:
			return writeStringUnicodeEscape(w, c)
		}
	default:
		return writeRune(w, c)
	}
}