jsstring

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 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 Concat

func Concat(a, b string) string

Concat is a+b as JavaScript strings: a lone high surrogate at the end of a and a lone low surrogate at the start of b make one character.

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 HasSurrogateEscape

func HasSurrogateEscape(data []byte) bool

HasSurrogateEscape reports whether data holds the escape of a surrogate unit, `\ud800` to `\udfff`: the only text encoding/json reads differently from JSON.parse. A backslash that is itself escaped counts too; the caller then takes the slower decoder, which reads that text correctly as well.

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 JoinsSurrogates

func JoinsSurrogates(a, b string) bool

JoinsSurrogates reports whether a ends with a lone high surrogate and b starts with a lone low surrogate, both held in their three-byte forms (the way a Go string holds a JavaScript string that has one), so that a+b in JavaScript is one character.

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 LowerContains

func LowerContains(s, lowerNeedle string) bool

LowerContains is strings.Contains(ToLower(s), lowerNeedle) for a lowerNeedle that is already lowercase. An ASCII-only s, the common case of a model id or name, lowercases only A to Z, so it is searched without building its lowercase copy.

func LowerEquals

func LowerEquals(s, lowerText string) bool

LowerEquals is ToLower(s) == lowerText for a lowerText that is already lowercase, without the lowercase copy of an ASCII-only s.

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 MarshalKeepingSurrogates

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

MarshalKeepingSurrogates is encoding/json's Marshal, except that a string with a lone surrogate comes out as the escape JSON.stringify writes (`\ud83d`) instead of U+FFFD. A value that is not JSON (NaN) is the same error as encoding/json's.

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. A lone surrogate, kept as WTF-8, stays unchanged as in JavaScript.

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 UnmarshalKeepingSurrogates

func UnmarshalKeepingSurrogates[T any](data []byte, value *T) error

UnmarshalKeepingSurrogates is encoding/json's Unmarshal, except that the escape of a lone surrogate (`\ud83d`) is read as that unit, as JSON.parse does, instead of as U+FFFD.

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

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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