MCPcopy
hub / github.com/subquery/subql / findMatchingIndices

Function findMatchingIndices

packages/cli/src/utils/utils.ts:142–171  ·  view source on GitHub ↗
(
  content: string,
  startChar: string,
  endChar: string,
  startFrom = 0
)

Source from the content-addressed store, hash-verified

140}
141
142export function findMatchingIndices(
143 content: string,
144 startChar: string,
145 endChar: string,
146 startFrom = 0
147): [number, number][] {
148 // JavaScript's regex engine does not support recursive patterns like (?1).
149 // This regex would work in engines that support recursion, such as PCRE (Perl-Compatible Regular Expressions).
150
151 let openCount = 0;
152 let startIndex: number | undefined;
153 const pairs: [number, number][] = [];
154
155 for (let i = startFrom; i < content.length; i++) {
156 if (content[i] === startChar) {
157 if (openCount === 0) startIndex = i;
158 openCount++;
159 } else if (content[i] === endChar) {
160 openCount--;
161 if (openCount === 0) {
162 assert(startIndex !== undefined, 'startIndex should be defined');
163 pairs.push([startIndex, i]);
164 break;
165 }
166 }
167 }
168 if (openCount !== 0) throw new Error(`Unbalanced ${startChar} and ${endChar}`);
169
170 return pairs;
171}
172export function findArrayIndicesTsManifest(content: string, key: string): [number, number] {
173 const start = content.indexOf(`${key}:`);
174 if (start === -1) throw new Error(`${key} not found`);

Callers 3

splitArrayStringFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected