jsstring

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 18 Imported by: 0

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

View Source
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

func ASCIIPrefix(text string) int

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

func AppendMarshalJSON(dst []byte, value any) ([]byte, error)

AppendMarshalJSON appends MarshalJSON(value) to dst, encoding into dst itself. On an error it returns dst with its original length.

func AppendString

func AppendString(dst []byte, s string) []byte

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

func ArrayIndex(key string) (uint32, bool)

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

func Canonical(text string) string

Canonical preserves UTF-16 units while giving paired surrogates their ordinary UTF-8 spelling.

func DecodeRuneInString

func DecodeRuneInString(text string) (r rune, size int)

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

func FromLatin1(s string) string

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

func FromUTF8(data []byte) string

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 FromUTF16

func FromUTF16(units []uint16) string

FromUTF16 converts UTF-16 without replacing unpaired surrogates.

func IndexOf

func IndexOf(text, search string, from int) int

IndexOf implements String.indexOf with UTF-16 positions, including searches for a surrogate half inside a pair.

func IsSpace

func IsSpace(r rune) bool

IsSpace reports whether r is JavaScript whitespace or a line terminator (the characters String.prototype.trim removes).

func LastIndexOf

func LastIndexOf(text, search string, from int) int

LastIndexOf implements String.lastIndexOf, whose from argument bounds the start of the match and clamps negative positions to zero.

func Length

func Length(text string) int

Length counts JavaScript UTF-16 units in a UTF-8/WTF-8 string.

func LocaleCompare

func LocaleCompare(a, b string) int

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

func MarshalJSON(value any) ([]byte, error)

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

func Slice(text string, start int, ends ...int) string

Slice applies String.slice's unit offsets, including negative offsets, an omitted end and retained surrogate halves.

func SortOwnKeys

func SortOwnKeys[T any](items []T, key func(T) string)

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

func Splice(text string, start, end int, insert string) string

Splice joins the prefix before start, insert, and the suffix beginning at end, using String.slice's UTF-16 offset rules.

func StreamText

func StreamText(text string) string

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

func StringifyLength(value any) (int, error)

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

func ToFixed(x float64, digits int) string

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

func ToLower(s string) string

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

func ToPrecision(x float64, precision int) string

ToPrecision is Number.prototype.toPrecision for finite positive x and 1 <= precision <= 100.

func ToUTF8

func ToUTF8(text string) []byte

ToUTF8 replaces lone UTF-16 surrogates, matching a JavaScript string's UTF-8 encoding for operating-system byte APIs.

func ToUTF16

func ToUTF16(text string) []uint16

ToUTF16 converts UTF-8/WTF-8 to the original JavaScript code units.

func ToUpper

func ToUpper(s string) string

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 Trim

func Trim(s string) string

Trim is String.prototype.trim.

func TrimEnd

func TrimEnd(s string) string

TrimEnd is String.prototype.trimEnd.

func TrimStart

func TrimStart(s string) string

TrimStart is String.prototype.trimStart.

func UnescapeForStringify

func UnescapeForStringify(encoded []byte) []byte

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

func UnescapeForStringifyInPlace(encoded []byte) []byte

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.

func UpperRune

func UpperRune(r rune) string

UpperRune is String.prototype.toUpperCase of the single character r: its SpecialCasing expansion, or the simple uppercase mapping.

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

func (c *Collator) CompareString(a, b string) int

CompareString returns -1, 0 or 1.

Jump to

Keyboard shortcuts

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