-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-utils.ts
More file actions
122 lines (110 loc) · 2.61 KB
/
Copy pathparse-utils.ts
File metadata and controls
122 lines (110 loc) · 2.61 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
import {
CHAR_ASTERISK,
CHAR_COLON,
CHAR_FORWARD_SLASH,
CHAR_LEFT_PAREN,
CHAR_RIGHT_PAREN,
is_whitespace,
} from './string-utils'
/** @internal */
export function skip_whitespace_forward(source: string, pos: number, end: number): number {
while (pos < end && is_whitespace(source.charCodeAt(pos))) {
pos++
}
return pos
}
/** @internal */
export function skip_whitespace_and_comments_forward(
source: string,
pos: number,
end: number,
): number {
while (pos < end) {
let ch = source.charCodeAt(pos)
// Skip whitespace
if (is_whitespace(ch)) {
pos++
continue
}
// Skip comments /*...*/
if (
ch === CHAR_FORWARD_SLASH &&
pos + 1 < end &&
source.charCodeAt(pos + 1) === CHAR_ASTERISK
) {
pos += 2 // Skip /*
while (pos < end) {
if (
source.charCodeAt(pos) === CHAR_ASTERISK &&
pos + 1 < end &&
source.charCodeAt(pos + 1) === CHAR_FORWARD_SLASH
) {
pos += 2 // Skip */
break
}
pos++
}
continue
}
break // Found non-whitespace, non-comment
}
return pos
}
/** @internal */
export function skip_whitespace_and_comments_backward(
source: string,
pos: number,
start: number,
): number {
while (pos > start) {
let ch = source.charCodeAt(pos - 1)
// Skip whitespace
if (is_whitespace(ch)) {
pos--
continue
}
// Skip comments /*...*/ (work backwards from */)
if (pos >= 2 && ch === CHAR_FORWARD_SLASH && source.charCodeAt(pos - 2) === CHAR_ASTERISK) {
pos -= 2 // Skip */
while (pos > start) {
if (
pos >= 2 &&
source.charCodeAt(pos - 2) === CHAR_FORWARD_SLASH &&
source.charCodeAt(pos - 1) === CHAR_ASTERISK
) {
pos -= 2 // Skip /*
break
}
pos--
}
continue
}
break // Found non-whitespace, non-comment
}
return pos
}
/**
* Trims whitespace and CSS comments from both ends; returns null if the range is only whitespace/comments.
* @internal
*/
export function trim_boundaries(
source: string,
start: number,
end: number,
): [number, number] | null {
start = skip_whitespace_and_comments_forward(source, start, end)
end = skip_whitespace_and_comments_backward(source, end, start)
if (start >= end) return null
return [start, end]
}
/** Find the position of the first ':' at parenthesis depth 0 in [start, end). Returns -1 if not found. @internal */
export function find_colon_at_depth_zero(source: string, start: number, end: number): number {
let depth = 0
for (let i = start; i < end; i++) {
let ch = source.charCodeAt(i)
if (ch === CHAR_LEFT_PAREN) depth++
else if (ch === CHAR_RIGHT_PAREN) depth--
else if (ch === CHAR_COLON && depth === 0) return i
}
return -1
}