Documentation
¶
Overview ¶
Package jsstring preserves JavaScript UTF-16 units at Go string boundaries. Lone surrogate units use WTF-8; ordinary Unicode strings retain their normal UTF-8 encoding.
Index ¶
- Constants
- func ASCIIPrefix(text string) int
- func AppendMarshalJSON(dst []byte, value any) ([]byte, error)
- func AppendString(dst []byte, s string) []byte
- func ArrayIndex(key string) (uint32, bool)
- func Canonical(text string) string
- func DecodeRuneInString(text string) (r rune, size int)
- func FromLatin1(s string) string
- func FromUTF8(data []byte) string
- func FromUTF16(units []uint16) string
- func IndexOf(text, search string, from int) int
- func IsSpace(r rune) bool
- func LastIndexOf(text, search string, from int) int
- func Length(text string) int
- func LocaleCompare(a, b string) int
- func MarshalJSON(value any) ([]byte, error)
- func Slice(text string, start int, ends ...int) string
- func SortOwnKeys[T any](items []T, key func(T) string)
- func Splice(text string, start, end int, insert string) string
- func StreamText(text string) string
- func StringifyLength(value any) (int, error)
- func ToFixed(x float64, digits int) string
- func ToLower(s string) string
- func ToPrecision(x float64, precision int) string
- func ToUTF8(text string) []byte
- func ToUTF16(text string) []uint16
- func ToUpper(s string) string
- func Trim(s string) string
- func TrimEnd(s string) string
- func TrimStart(s string) string
- func UnescapeForStringify(encoded []byte) []byte
- func UnescapeForStringifyInPlace(encoded []byte) []byte
- func UpperRune(r rune) string
- type Collator
Constants ¶
const SpaceClass = `\t\n\v\f\r \x{a0}\x{1680}\x{2000}-\x{200a}\x{2028}\x{2029}\x{202f}\x{205f}\x{3000}\x{feff}`
SpaceClass is the body of an RE2 character class that matches what JavaScript's \s matches: the characters IsSpace reports. RE2's own \s is ASCII only. Use it inside brackets, for example `[` + SpaceClass + `,]`.
Variables ¶
This section is empty.
Functions ¶
func ASCIIPrefix ¶
ASCIIPrefix returns the length of text's leading run of ASCII bytes, one UTF-16 unit each. It tests eight bytes per step.
func AppendMarshalJSON ¶
AppendMarshalJSON appends MarshalJSON(value) to dst, encoding into dst itself. On an error it returns dst with its original length.
func AppendString ¶
AppendString appends JSON.stringify(s) (jsparity.AppendString): a WTF-8 surrogate half prints as its escape unless it is the first half of a pair whose second half follows, and ill-formed UTF-8 prints as U+FFFD, the character a decoded JavaScript string holds.
func ArrayIndex ¶
ArrayIndex reports whether key is a canonical array index (0 to 2^32-2, no sign or leading zero) and returns it. ECMAScript enumerates such keys of an object first, in ascending order.
func Canonical ¶
Canonical preserves UTF-16 units while giving paired surrogates their ordinary UTF-8 spelling.
func DecodeRuneInString ¶
DecodeRuneInString reads a JavaScript code point from UTF-8/WTF-8. Adjacent encoded surrogate halves combine just as UTF-16 codePointAt does after strings are concatenated.
func FromLatin1 ¶
FromLatin1 is the string Node's fetch (undici) returns from Headers.get for the raw header bytes s: each byte is one code point (Latin-1), so the UTF-8 bytes of U+FEFF read as "" and a lone 0xA0 byte reads as U+00A0.
func FromUTF8 ¶
FromUTF8 decodes operating-system bytes like Buffer.toString("utf8") and napi_create_string_utf8: each maximal ill-formed subpart becomes one replacement character, and a leading BOM is retained.
func IndexOf ¶
IndexOf implements String.indexOf with UTF-16 positions, including searches for a surrogate half inside a pair.
func IsSpace ¶
IsSpace reports whether r is JavaScript whitespace or a line terminator (the characters String.prototype.trim removes).
func LastIndexOf ¶
LastIndexOf implements String.lastIndexOf, whose from argument bounds the start of the match and clamps negative positions to zero.
func LocaleCompare ¶
LocaleCompare is `a.localeCompare(b)` under the root locale: punctuation and case are weighed as Node weighs them, `a` before `A`, accents after base letters, U+FFFD last. The collation tables are golang.org/x/text's (collate.CLDRVersion, collate.UnicodeVersion), older than Node's ICU. It is safe for concurrent use and returns -1, 0 or 1.
func MarshalJSON ¶
MarshalJSON encodes value as JSON.stringify(value) does (numbers too: a non-finite number is null and negative zero is 0), retaining unmatched UTF-16 units as surrogate escapes and writing <, >, & and the line and paragraph separators literally and negative zero as 0 even inside custom marshalers. Escaped backslashes remain escaped. The encoder writes that text in one pass (json.MarshalStringify); a value that implements json.StringifyAppender appends its own text.
func Slice ¶
Slice applies String.slice's unit offsets, including negative offsets, an omitted end and retained surrogate halves.
func SortOwnKeys ¶
SortOwnKeys orders items as an ECMAScript object enumerates its own string keys: array-index keys first in ascending order, then the others in their existing (insertion) order.
func Splice ¶
Splice joins the prefix before start, insert, and the suffix beginning at end, using String.slice's UTF-16 offset rules.
func StreamText ¶
StreamText is the text process.stdout.write, console.error and a "utf8" file write send for a JavaScript string: valid UTF-8 is unchanged and a lone surrogate becomes U+FFFD.
func StringifyLength ¶
StringifyLength returns the number of UTF-16 units in JSON.stringify(value) and encodes without keeping the text. Ill-formed UTF-8 is U+FFFD in a JavaScript string, one unit per maximal subpart, which the encoder writes raw as JSON.stringify does. A slice the encoder writes element by element is counted the same way, "[", the elements joined by ",", "]", each encoded on its own: the pooled buffer then holds one element rather than the whole array, so counting a long list of tools does not allocate its whole text.
func ToFixed ¶
ToFixed is Number.prototype.toFixed for 0 <= digits <= 100: the exact decimal value of x is rounded to digits places, and a tie takes the larger n, as the specification requires (Go's %.1f rounds ties to even). A magnitude of at least 1e21 formats as Number::toString, as step 7 of the specification requires.
func ToLower ¶
ToLower is String.prototype.toLowerCase: the full Unicode lowercase mapping. Besides the simple per-code-point mapping it maps U+0130 to "i" plus U+0307 and applies the Final_Sigma rule, which lowercases a capital sigma to the final form at the end of a word.
func ToPrecision ¶
ToPrecision is Number.prototype.toPrecision for finite positive x and 1 <= precision <= 100.
func ToUTF8 ¶
ToUTF8 replaces lone UTF-16 surrogates, matching a JavaScript string's UTF-8 encoding for operating-system byte APIs.
func ToUpper ¶
ToUpper is String.prototype.toUpperCase. Go's strings.ToUpper applies only the simple mapping, so it leaves ß as ß where JavaScript gives SS and maps U+1F80 to U+1F88 where JavaScript gives U+1F08 U+0399. Unlike toLowerCase, toUpperCase has no context-dependent mapping.
func UnescapeForStringify ¶
UnescapeForStringify rewrites the escapes encoding/json writes and JSON.stringify does not, \u003c, \u003e, \u0026, \u2028 and \u2029, back to the characters they stand for. An escaped backslash before such text is left alone. It returns encoded itself when it holds none of them, so the common case does not copy; otherwise encoded is left as it is and the result is a new slice.
func UnescapeForStringifyInPlace ¶
UnescapeForStringifyInPlace is UnescapeForStringify written over encoded itself: each replacement is shorter than the escape it stands for, so the output never passes the text it reads. The result is encoded, shortened, and encoded's later bytes are undefined.
Types ¶
type Collator ¶
type Collator struct {
// contains filtered or unexported fields
}
Collator compares strings as `a.localeCompare(b)` does under the root locale (see LocaleCompare). Like the collate.Collator it wraps it keeps scratch buffers, so one Collator must not be shared by goroutines.
func NewCollator ¶
func NewCollator() *Collator
NewCollator returns a Collator that orders strings as Node's root-locale localeCompare does.
func (*Collator) CompareString ¶
CompareString returns -1, 0 or 1.