forked from reworkcss/css
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathstringSearch.ts
More file actions
174 lines (164 loc) · 5.09 KB
/
stringSearch.ts
File metadata and controls
174 lines (164 loc) · 5.09 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
export const MAX_LOOP = 10000;
/**
* Find the first occurrence of any search string in the input string, ignoring escaped characters
* @param string - The input string to search in
* @param search - Array of strings to search for
* @param position - Optional starting position for the search
* @returns The index of the first match, or -1 if not found
* @throws {Error} If too many escape sequences are encountered (> MAX_LOOP)
* @example
* ```ts
* // Basic search
* indexOfArrayNonEscaped('a,b,c', [',']) // 1
*
* // Handles escaped characters
* indexOfArrayNonEscaped('a\\,b,c', [',']) // 4, the first comma is escaped
* ```
*/
export const indexOfArrayNonEscaped = (
string: string,
search: Array<string>,
position?: number,
): number => {
let currentPosition = position;
let maxLoop = MAX_LOOP;
do {
const all = search.map((v) => string.indexOf(v, currentPosition));
all.push(string.indexOf('\\', currentPosition));
const foundAll = all.filter((v) => v !== -1);
if (foundAll.length === 0) {
return -1;
}
const found = Math.min(...foundAll);
if (string[found] === '\\') {
currentPosition = found + 2;
maxLoop--;
} else {
return found;
}
} while (maxLoop > 0);
throw new Error('Too many escaping');
};
/**
* Find the first occurrence of any search string in the input string, respecting brackets and quotes
* @param string - The input string to search in
* @param search - Array of strings to search for
* @param position - Optional starting position for the search
* @returns The index of the first match, or -1 if not found
* @throws {Error} If too many escape sequences are encountered (> MAX_LOOP)
* @example
* ```ts
* // Basic search
* indexOfArrayWithBracketAndQuoteSupport('a,b,c', [',']) // 1
*
* // Respects brackets - won't match inside ()
* indexOfArrayWithBracketAndQuoteSupport('(a,b),c', [',']) // 4, ignores the comma inside ()
*
* // Respects quotes - won't match inside quotes
* indexOfArrayWithBracketAndQuoteSupport('"a,b",c', [',']) // 4, ignores the comma inside quotes
* indexOfArrayWithBracketAndQuoteSupport("'a,b',c", [',']) // 4, ignores the comma inside quotes
*
* // Handles escaped characters
* indexOfArrayWithBracketAndQuoteSupport('a\\,b,c', [',']) // 4, the first comma is escaped
* ```
*/
export const indexOfArrayWithBracketAndQuoteSupport = (
string: string,
search: Array<string>,
position?: number,
): number => {
let currentSearchPosition = position;
let maxLoop = MAX_LOOP;
do {
const all = search.map((v) => string.indexOf(v, currentSearchPosition));
all.push(string.indexOf('(', currentSearchPosition));
all.push(string.indexOf('"', currentSearchPosition));
all.push(string.indexOf("'", currentSearchPosition));
all.push(string.indexOf('\\', currentSearchPosition));
const foundAll = all.filter((v) => v !== -1);
if (foundAll.length === 0) {
return -1;
}
const firstMatchPos = Math.min(...foundAll);
const char = string[firstMatchPos];
switch (char) {
case '\\':
currentSearchPosition = firstMatchPos + 2;
break;
case '(':
{
const endPosition = indexOfArrayWithBracketAndQuoteSupport(
string,
[')'],
firstMatchPos + 1,
);
if (endPosition === -1) {
return -1;
}
currentSearchPosition = endPosition + 1;
}
break;
case '"':
{
const endQuotePosition = indexOfArrayNonEscaped(
string,
['"'],
firstMatchPos + 1,
);
if (endQuotePosition === -1) {
return -1;
}
currentSearchPosition = endQuotePosition + 1;
}
break;
case "'":
{
const endQuotePosition = indexOfArrayNonEscaped(
string,
["'"],
firstMatchPos + 1,
);
if (endQuotePosition === -1) {
return -1;
}
currentSearchPosition = endQuotePosition + 1;
}
break;
default:
return firstMatchPos;
}
maxLoop--;
} while (maxLoop > 0);
throw new Error('Too many escaping');
};
/**
* Split a string by search tokens, respecting brackets and quotes
* @example
* ```ts
* splitWithBracketAndQuoteSupport('a,b', [',']) // ['a', 'b']
* splitWithBracketAndQuoteSupport('a,(b,c)', [',']) // ['a', '(b,c)']
* splitWithBracketAndQuoteSupport('a,"b,c"', [',']) // ['a', '"b,c"']
* splitWithBracketAndQuoteSupport("a,'b,c'", [',']) // ['a', "'b,c'"]
* ```
*/
export const splitWithBracketAndQuoteSupport = (
string: string,
search: Array<string>,
): Array<string> => {
const result: Array<string> = [];
let currentPosition = 0;
while (currentPosition < string.length) {
const index = indexOfArrayWithBracketAndQuoteSupport(
string,
search,
currentPosition,
);
if (index === -1) {
result.push(string.substring(currentPosition));
return result;
}
result.push(string.substring(currentPosition, index));
currentPosition = index + 1;
}
return result;
};