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
- Variables
- func AppendASCIIEscape(dst []byte, c byte) []byte
- func AppendHTMLEscaped(dst, src []byte) []byte
- func AppendQuoted(escape *Escaper, buf []byte, s string) ([]byte, bool)
- func AppendUnescaped(dst, s []byte) []byte
- func AppendValidUTF8(dst, b []byte) []byte
- func NameBit(name []byte) uint64
- func ReplaceInvalidUTF8(b []byte, start int) []byte
- func UnescapeTo(out unsafe.Pointer, buf []byte, first int) int
- func Unquote(lit []byte) ([]byte, bool)
- func UnquoteValid(lit []byte) []byte
- type ByteClass
- type Escaper
Constants ¶
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 ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
AppendValidUTF8 appends b to dst, with every byte of an invalid UTF-8 sequence replaced by utf8.RuneError, as encoding/json does.
func NameBit ¶
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 ¶
ReplaceInvalidUTF8 replaces every byte of an invalid UTF-8 sequence of b[start:] by utf8.RuneError.
func UnescapeTo ¶
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 ¶
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 ¶
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 ¶
NewByteClass is the class of the bytes and of the control characters.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
HasInvalid reports whether the escaper recorded a string of invalid UTF-8 ( see Invalid ).