package jsonstring
Import Path
github.com/goccy/go-json/internal/jsonstring (on go.dev)
Dependency Relation
imports 6 packages, and imported by 10 packages
Involved Source Files
decode_rune.go
escape_control.go
html.go
invalid_utf8.go
name_bit.go
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.
quote_table.go
scan_amd64.go
scan_table.go
unescape.go
scan_amd64.s
Package-Level Type Names (total 2)
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.
Has is whether a byte of src is in the class.
func NewByteClass(bytes ...byte) *ByteClass
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.
ClearInvalid clears the record of a string of invalid UTF-8.
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.
HasInvalid reports whether the escaper recorded a string of invalid UTF-8 ( see Invalid ).
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.
func EscaperOf(flags uint) *Escaper
func TextEscaper(html, js bool) *Escaper
func V1Escaper(html, normalize uint) *Escaper
func github.com/goccy/go-json/internal/encoder.StringEscaper(ctx *encoder.RuntimeContext) *Escaper
func AppendQuoted(escape *Escaper, buf []byte, s string) ([]byte, bool)
Package-Level Functions (total 14)
AppendASCIIEscape appends the escape sequence of the character c of ASCII, which every escaper escapes: '"',
'\\' or a control character, as the escapers write it.
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.
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.
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.
AppendValidUTF8 appends b to dst, with every byte of an invalid UTF-8 sequence replaced by utf8.RuneError, as
encoding/json does.
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.
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.
NewByteClass is the class of the bytes and of the control characters.
ReplaceInvalidUTF8 replaces every byte of an invalid UTF-8 sequence of b[start:] by utf8.RuneError.
TextEscaper is the escaper of encoding/json/jsontext with the options: '<', '>' and '&' escaped for HTML, and
U+2028 and U+2029 for JavaScript.
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.
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.
UnquoteValid is Unquote of lit, which a scan found a JSON string.
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.
Package-Level Variables (total 2)
HexToInt is the value of each hexadecimal digit, and 0 for any other byte.
UnescapeMap is the byte of each escape sequence of one character after its backslash.
Package-Level Constants (total 4)
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.
the bits of the options of the encoder, which EscaperOf takes.
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.
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.
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |