-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-utils.ts
More file actions
257 lines (231 loc) · 6.98 KB
/
string-utils.ts
File metadata and controls
257 lines (231 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// String utility functions for CSS parsing
// Character constants (exported for use in parsers)
export const CHAR_SPACE = 0x20 // ' '
export const CHAR_TAB = 0x09 // \t
export const CHAR_NEWLINE = 0x0a // \n
export const CHAR_CARRIAGE_RETURN = 0x0d // \r
export const CHAR_FORM_FEED = 0x0c // \f
export const CHAR_FORWARD_SLASH = 0x2f // '/'
export const CHAR_ASTERISK = 0x2a // *
export const CHAR_MINUS_HYPHEN = 0x2d // -
export const CHAR_SINGLE_QUOTE = 0x27 // '
export const CHAR_DOUBLE_QUOTE = 0x22 // "
export const CHAR_PLUS = 0x2b // +
export const CHAR_PERIOD = 0x2e // .
export const CHAR_TILDE = 0x7e // ~
export const CHAR_GREATER_THAN = 0x3e // >
export const CHAR_AMPERSAND = 0x26 // &
export const CHAR_EQUALS = 0x3d // =
export const CHAR_PIPE = 0x7c // |
export const CHAR_DOLLAR = 0x24 // $
export const CHAR_CARET = 0x5e // ^
export const CHAR_COLON = 0x3a // :
export const CHAR_LESS_THAN = 0x3c // <
/**
* Check if a character code is whitespace (space, tab, newline, CR, or FF)
* @internal
*/
export function is_whitespace(ch: number): boolean {
return (
ch === CHAR_SPACE ||
ch === CHAR_TAB ||
ch === CHAR_NEWLINE ||
ch === CHAR_CARRIAGE_RETURN ||
ch === CHAR_FORM_FEED
)
}
/** @internal */
export function is_combinator(ch: number): boolean {
return ch === CHAR_GREATER_THAN || ch === CHAR_PLUS || ch === CHAR_TILDE
}
/** @internal */
export function is_digit(ch: number): boolean {
return ch >= 0x30 && ch <= 0x39 // 0-9
}
/**
* @param a Base string, MUST be lowercase!
* @param b Compare string
*/
export function str_equals(a: string, b: string): boolean {
if (a.length !== b.length) {
return false
}
for (let i = 0; i < a.length; i++) {
let ca = a.charCodeAt(i)
let cb = b.charCodeAt(i)
// normalize ASCII uppercase A-Z → a-z
cb |= 32
if (ca !== cb) {
return false
}
}
return true
}
/**
* Case-insensitive ASCII prefix check without allocations
* Returns true if string `str` starts with prefix (case-insensitive)
*
* IMPORTANT: prefix MUST be lowercase for correct comparison
*
* @param str - The string to check
* @param prefix - The lowercase prefix to match against
*/
export function str_starts_with(str: string, prefix: string): boolean {
if (str.length < prefix.length) {
return false
}
for (let i = 0; i < prefix.length; i++) {
let ca = str.charCodeAt(i)
let cb = prefix.charCodeAt(i)
// normalize only the string char (prefix is already lowercase)
if (ca >= 65 && ca <= 90) ca |= 32 // A-Z → a-z
if (ca !== cb) {
return false
}
}
return true
}
/**
* Case-insensitive character/substring search without allocations
* Returns the index of the first occurrence of searchChar (case-insensitive)
*
* IMPORTANT: searchChar MUST be lowercase for correct comparison
*
* @param str - The string to search in
* @param searchChar - The lowercase character/substring to find
* @returns The index of the first match, or -1 if not found
*/
export function str_index_of(str: string, searchChar: string): number {
if (searchChar.length === 0) {
return -1
}
// Optimize for single character search
if (searchChar.length === 1) {
const searchCode = searchChar.charCodeAt(0)
for (let i = 0; i < str.length; i++) {
let ca = str.charCodeAt(i)
// normalize only the string char (searchChar is already lowercase)
if (ca >= 65 && ca <= 90) ca |= 32 // A-Z → a-z
if (ca === searchCode) {
return i
}
}
return -1
}
// Multi-character search
for (let i = 0; i <= str.length - searchChar.length; i++) {
let match = true
for (let j = 0; j < searchChar.length; j++) {
let ca = str.charCodeAt(i + j)
let cb = searchChar.charCodeAt(j)
if (ca >= 65 && ca <= 90) ca |= 32 // A-Z → a-z
if (ca !== cb) {
match = false
break
}
}
if (match) {
return i
}
}
return -1
}
/**
* Check if a string range has a vendor prefix
*
* @param source - The source string
* @param start - Start offset in source
* @param end - End offset in source
* @returns true if the range starts with a vendor prefix (-webkit-, -moz-, -ms-, -o-)
*
* Detects vendor prefixes by checking:
* 1. Starts with a single hyphen (not --)
* 2. Contains at least 3 characters (shortest is -o-)
* 3. Has a second hyphen after the vendor name
*
* Examples:
* - `-webkit-transform` → true
* - `-moz-appearance` → true
* - `-ms-filter` → true
* - `-o-border-image` → true
* - `--custom-property` → false (CSS custom property)
* - `border-radius` → false (doesn't start with hyphen)
*/
// Overload signatures
export function is_vendor_prefixed(text: string): boolean
export function is_vendor_prefixed(source: string, start: number, end: number): boolean
// Implementation
export function is_vendor_prefixed(source: string, start?: number, end?: number): boolean {
// Handle string-only overload
if (start === undefined || end === undefined) {
start = 0
end = source.length
}
// Must start with a hyphen
if (source.charCodeAt(start) !== CHAR_MINUS_HYPHEN) {
return false
}
// Second char must not be a hyphen (to exclude CSS custom properties like --var)
if (source.charCodeAt(start + 1) === CHAR_MINUS_HYPHEN) {
return false
}
// Must be at least 3 chars (-o- is shortest vendor prefix)
let length = end - start
if (length < 3) {
return false
}
// Must have another hyphen after the vendor name
// This identifies: -webkit-, -moz-, -ms-, -o-
// Use bounded loop instead of unbounded indexOf() to only search within the range
for (let i = start + 2; i < end; i++) {
if (source.charCodeAt(i) === CHAR_MINUS_HYPHEN) {
return true
}
}
return false
}
/**
* Check if a string is a CSS custom property (starts with --)
*
* @param str - The string to check
* @returns true if the string starts with -- (custom property)
*
* Examples:
* - `--primary-color` → true
* - `--my-var` → true
* - `-webkit-transform` → false (vendor prefix, not custom)
* - `border-radius` → false (standard property)
* - `color` → false
*/
export function is_custom(str: string): boolean {
// Must start with two hyphens and have at least one character after
if (str.length < 3) return false
return str.charCodeAt(0) === CHAR_MINUS_HYPHEN && str.charCodeAt(1) === CHAR_MINUS_HYPHEN
}
/**
* Strip vendor prefix from a string
*
* @param str - The string to strip vendor prefix from
* @returns The string without vendor prefix, or original string if no prefix found
*
* Examples:
* - `-webkit-keyframes` → `keyframes`
* - `-moz-appearance` → `appearance`
* - `-ms-filter` → `filter`
* - `-o-border-image` → `border-image`
* - `keyframes` → `keyframes` (no change)
* - `--custom-property` → `--custom-property` (custom property, not vendor prefix)
*/
export function strip_vendor_prefix(str: string): string {
if (!is_vendor_prefixed(str)) {
return str
}
// Find the second hyphen (after the vendor name)
for (let i = 2; i < str.length; i++) {
if (str.charCodeAt(i) === CHAR_MINUS_HYPHEN) {
return str.substring(i + 1)
}
}
// Should never reach here if is_vendor_prefixed returned true
return str
}