Help us shape the future of Go! Take the 2026 Go Developer Survey

jsonstring

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

This files's string processing codes are inspired by https://github.com/segmentio/encoding. The license notation is as follows.

MIT License

Copyright (c) 2019 Segment.io, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

View Source
const (

	// the bits of the options of the encoder, which EscaperOf takes.
	EscapeNormalize = stringEscapeNormalize
	EscapeHTML      = stringEscapeHTML
	EscapeText      = stringEscapeText
	EscapeStrict    = stringEscapeStrict
)

The bits of the index of an escaper of stringEscapes. An escaper of encoding/json normalizes UTF-8 as it does: a byte of invalid UTF-8 is replaced by invalidUTF8, and U+2028 and U+2029 are escaped. An escaper of encoding/json/jsontext always validates UTF-8, replaces a byte of invalid UTF-8 by U+FFFD, which it records with stringEscapeStrict as well ( see Escaper.Invalid ), and escapes U+2028 and U+2029 with stringEscapeJS.

Variables

View Source
var HexToInt = [256]int{
	'0': 0,
	'1': 1,
	'2': 2,
	'3': 3,
	'4': 4,
	'5': 5,
	'6': 6,
	'7': 7,
	'8': 8,
	'9': 9,
	'A': 10,
	'B': 11,
	'C': 12,
	'D': 13,
	'E': 14,
	'F': 15,
	'a': 10,
	'b': 11,
	'c': 12,
	'd': 13,
	'e': 14,
	'f': 15,
}

HexToInt is the value of each hexadecimal digit, and 0 for any other byte.

View Source
var UnescapeMap = [256]byte{
	'"':  '"',
	'\\': '\\',
	'/':  '/',
	'\'': '\'',
	'b':  '\b',
	'f':  '\f',
	'n':  '\n',
	'r':  '\r',
	't':  '\t',
}

UnescapeMap is the byte of each escape sequence of one character after its backslash.

Functions

func AppendASCIIEscape

func AppendASCIIEscape(dst []byte, c byte) []byte

AppendASCIIEscape appends the escape sequence of the character c of ASCII, which every escaper escapes: '"', '\\' or a control character, as the escapers write it.

func AppendHTMLEscaped

func AppendHTMLEscaped(dst, src []byte) []byte

AppendHTMLEscaped appends src to dst with '<', '>', '&', U+2028 and U+2029 escaped, wherever they are, as encoding/json escapes them for HTML: as \u003c, \u003e, \u0026, \u2028 and \u2029.

func AppendQuoted

func AppendQuoted(escape *Escaper, buf []byte, s string) ([]byte, bool)

AppendQuoted appends the string as a JSON string, escaped by the escaper, and reports whether it is valid UTF-8, which it is unless the escaper validates it. The result of the function of the escapes is returned as it is, without a look at it. The escaper is the first argument, as the context is of the functions of the VM of the encoder, which then keeps the buffer in the registers it has it in.

Most of the strings have nothing to escape and are short, so that case is handled here. A string of up to maxOnePassLength bytes is looked at and written to the capacity of the buffer in one pass, by bytes, by the halves of a word or by words, and nothing is called; a longer one is looked at by SIMD ( or by words ) and then copied. A string which may have a byte to escape is left to the function for the options, and what was written for it is left out of the buffer.

func AppendUnescaped

func AppendUnescaped(dst, s []byte) []byte

AppendUnescaped appends the value of the bytes s of a JSON string, between its quotes, to dst, which doesn't overlap s. The escapes are valid and complete, as a scan of the string checked them, and the closing quote follows s in its array, as UnescapeTo needs.

func AppendValidUTF8

func AppendValidUTF8(dst, b []byte) []byte

AppendValidUTF8 appends b to dst, with every byte of an invalid UTF-8 sequence replaced by utf8.RuneError, as encoding/json does.

func NameBit

func NameBit(name []byte) uint64

NameBit is one of 64 bits for a name of an object, chosen by the length of the name and its first and last bytes: two names of other bits differ. The bits of the names of an object tell most new names from the ones it has without comparing them, where a name which an object has twice is an error.

func ReplaceInvalidUTF8

func ReplaceInvalidUTF8(b []byte, start int) []byte

ReplaceInvalidUTF8 replaces every byte of an invalid UTF-8 sequence of b[start:] by utf8.RuneError.

func UnescapeTo

func UnescapeTo(out unsafe.Pointer, buf []byte, first int) int

UnescapeTo decodes the escapes of the bytes of an escaped string into out, which has room for as many bytes and is not the bytes themselves, and returns the length of the result, which is at most the length of the bytes.

The escapes are valid and complete, as a scan of the string checked them, and the array of buf has a byte after them, as the closing quote of the string: a pointer moves up to the end of buf. first is the offset of the first backslash.

func Unquote

func Unquote(lit []byte) ([]byte, bool)

Unquote returns the value of the JSON string lit, with its quotes, and true; or false if lit is not one. It takes lit as encoding/json takes the string of a ,string option and of an encoding.TextUnmarshaler, which isn't checked before: it takes the escape \' too. The value is lit between its quotes if it needs no change, else new bytes, with every byte of invalid UTF-8 replaced by utf8.RuneError.

func UnquoteValid

func UnquoteValid(lit []byte) []byte

UnquoteValid is Unquote of lit, which a scan found a JSON string.

Types

type ByteClass

type ByteClass struct {
	// contains filtered or unexported fields
}

ByteClass is a set of bytes, which has the control characters, as a table of the bytes and as the tables of the scan by SIMD.

func NewByteClass

func NewByteClass(bytes ...byte) *ByteClass

NewByteClass is the class of the bytes and of the control characters.

func (*ByteClass) Has

func (c *ByteClass) Has(src []byte) bool

Has is whether a byte of src is in the class.

type Escaper

type Escaper struct {
	// contains filtered or unexported fields
}

Escaper is what decides whether a string has a byte to escape, and escapes it, for a combination of the options of encoding/json or of encoding/json/jsontext.

func EscaperOf

func EscaperOf(flags uint) *Escaper

EscaperOf is the escaper of the options in flags, whose lowest bits are the ones of the index of stringEscapes: V1Escaper, or TextEscaper if EscapeText is set. It is small enough to be inlined into the VM of the encoder, which takes the escaper of the options for every string: it is not kept by the VM, which would have to restore it after each opcode.

func TextEscaper

func TextEscaper(html, js bool) *Escaper

TextEscaper is the escaper of encoding/json/jsontext with the options: '<', '>' and '&' escaped for HTML, and U+2028 and U+2029 for JavaScript.

func V1Escaper

func V1Escaper(html, normalize uint) *Escaper

V1Escaper is the escaper of encoding/json with the options: '<', '>' and '&' escaped for HTML if html is 1, and UTF-8 normalized if normalize is 1.

func (*Escaper) ClearInvalid

func (e *Escaper) ClearInvalid()

ClearInvalid clears the record of a string of invalid UTF-8.

func (*Escaper) CopyOf

func (e *Escaper) CopyOf(flags uint)

CopyOf sets e to a copy of the escaper of the options in flags ( see EscaperOf ), which records the first string of invalid UTF-8 it writes if EscapeStrict is set: an escaper of its own, as the record is.

func (*Escaper) HasInvalid

func (e *Escaper) HasInvalid() bool

HasInvalid reports whether the escaper recorded a string of invalid UTF-8 ( see Invalid ).

func (*Escaper) Invalid

func (e *Escaper) Invalid() ([]byte, bool)

Invalid returns the output before the first string of invalid UTF-8 which the escaper wrote, which it wrote with U+FFFD, and true; or false if it wrote none. Only an escaper of EscapeStrict records it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL