stringutil

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT, Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package stringutil Exports common rune utilities for parsing and emitting javascript

Index

Constants

View Source
const (
	// SurrogateLowStart is the boundary between the high and low halves of the
	// UTF-16 surrogate range. unicode/utf16 only exposes IsSurrogate for the
	// whole range, so this split point is defined here to distinguish the two.
	SurrogateLowStart = 0xDC00
)

Variables

This section is empty.

Functions

func AddUTF8ByteOrderMark

func AddUTF8ByteOrderMark(text string) string

func CodePointToSurrogatePair added in v0.22.0

func CodePointToSurrogatePair(ch rune) (high rune, low rune)

func CombineSurrogatePairs added in v0.22.0

func CombineSurrogatePairs(s string) string

CombineSurrogatePairs canonicalizes a JS-string value produced by concatenation, merging any adjacent high+low surrogate sentinel pair (as written by EncodeJSStringRune) into the single supplementary code point they represent. This mirrors how concatenating two UTF-16 code units forms a surrogate pair in a JavaScript string. It must be applied wherever separately scanned string values are joined, since each half is only a lone surrogate until it meets its partner. Strings without a lone-surrogate sentinel (the common case) are returned unchanged.

func ContainsNonASCII added in v0.22.0

func ContainsNonASCII(s string) bool

func DecodeJSStringRune added in v0.22.0

func DecodeJSStringRune(s string) (rune, int)

func EncodeJSStringRune added in v0.22.0

func EncodeJSStringRune(ch rune) string

func EquateStringCaseInsensitive

func EquateStringCaseInsensitive(a, b string) bool

func EquateStringCaseSensitive

func EquateStringCaseSensitive(a, b string) bool

func GetStringComparer

func GetStringComparer(ignoreCase bool) func(a, b string) Comparison

func GetStringEqualityComparer

func GetStringEqualityComparer(ignoreCase bool) func(a, b string) bool

func GuessIndentation

func GuessIndentation(lines []string) int

func HasPrefix

func HasPrefix(s string, prefix string, caseSensitive bool) bool

func HasPrefixAndSuffixWithoutOverlap

func HasPrefixAndSuffixWithoutOverlap(s string, prefix string, suffix string, caseSensitive bool) bool

func HasSuffix

func HasSuffix(s string, suffix string, caseSensitive bool) bool

func IsASCIILetter

func IsASCIILetter(ch rune) bool

func IsDigit

func IsDigit(ch rune) bool

func IsHexDigit

func IsHexDigit(ch rune) bool

func IsHighSurrogate added in v0.22.0

func IsHighSurrogate(ch rune) bool

func IsLineBreak

func IsLineBreak(ch rune) bool

func IsLowSurrogate added in v0.22.0

func IsLowSurrogate(ch rune) bool

func IsOctalDigit

func IsOctalDigit(ch rune) bool

func IsSurrogate added in v0.22.0

func IsSurrogate(ch rune) bool

func IsUnicodeIdentifierPart added in v0.22.0

func IsUnicodeIdentifierPart(ch rune) bool

IsUnicodeIdentifierPart reports whether ch may appear after the first character of an ECMAScript identifier, i.e. whether it has the Unicode ID_Continue (or Other_ID_Continue) property, which also includes ID_Start.

func IsUnicodeIdentifierStart added in v0.22.0

func IsUnicodeIdentifierStart(ch rune) bool

IsUnicodeIdentifierStart reports whether ch may begin an ECMAScript identifier, i.e. whether it has the Unicode ID_Start (or Other_ID_Start) property. The range table is generated; see generate-unicode-data.mts.

func IsWhiteSpaceLike

func IsWhiteSpaceLike(ch rune) bool

func IsWhiteSpaceSingleLine

func IsWhiteSpaceSingleLine(ch rune) bool

func LowerFirstChar

func LowerFirstChar(str string) string

func RemoveByteOrderMark

func RemoveByteOrderMark(text string) string

func SplitLines

func SplitLines(text string) []string

func StripQuotes

func StripQuotes(name string) string

func SurrogatePairToCodePoint added in v0.22.0

func SurrogatePairToCodePoint(high rune, low rune) rune

func ToLowerJS added in v0.22.0

func ToLowerJS(str string) string

func ToUpperJS added in v0.22.0

func ToUpperJS(str string) string

func TruncateByRunes

func TruncateByRunes(str string, maxLength int) string

func UnquoteString

func UnquoteString(str string) string

Types

type Comparison

type Comparison = int
const (
	ComparisonLessThan    Comparison = -1
	ComparisonEqual       Comparison = 0
	ComparisonGreaterThan Comparison = 1
)

func CompareStringsCaseInsensitive

func CompareStringsCaseInsensitive(a string, b string) Comparison

func CompareStringsCaseInsensitiveEslintCompatible

func CompareStringsCaseInsensitiveEslintCompatible(a, b string) Comparison

CompareStringsCaseInsensitiveEslintCompatible performs a case-insensitive comparison using toLowerCase() instead of toUpperCase() for ESLint compatibility.

`CompareStringsCaseInsensitive` transforms letters to uppercase for unicode reasons, while eslint's `sort-imports` rule transforms letters to lowercase. Which one you choose affects the relative order of letters and ASCII characters 91-96, of which `_` is a valid character in an identifier. So if we used `CompareStringsCaseInsensitive` for import sorting, TypeScript and eslint would disagree about the correct case-insensitive sort order for `__String` and `Foo`. Since eslint's whole job is to create consistency by enforcing nitpicky details like this, it makes way more sense for us to just adopt their convention so users can have auto-imports without making eslint angry.

func CompareStringsCaseInsensitiveThenSensitive

func CompareStringsCaseInsensitiveThenSensitive(a, b string) Comparison

func CompareStringsCaseSensitive

func CompareStringsCaseSensitive(a string, b string) Comparison

Jump to

Keyboard shortcuts

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