| 439 | } |
| 440 | |
| 441 | function findInText( |
| 442 | text: string, |
| 443 | search: string | RegExp, |
| 444 | direction: Direction, |
| 445 | fromOffset?: number |
| 446 | ): number { |
| 447 | if (typeof search === 'string') { |
| 448 | if (direction === Direction.Backward) { |
| 449 | return text.lastIndexOf(search, fromOffset !== undefined ? fromOffset - 1 : text.length - 1); |
| 450 | } |
| 451 | return text.indexOf(search, fromOffset ?? 0); |
| 452 | } |
| 453 | |
| 454 | if (direction === Direction.Forward) { |
| 455 | let start = fromOffset ?? 0; |
| 456 | let index = text.slice(start).search(search); |
| 457 | return index >= 0 ? start + index : -1; |
| 458 | } |
| 459 | |
| 460 | let limit = fromOffset !== undefined ? fromOffset : text.length; |
| 461 | if (limit < 0) { |
| 462 | return -1; |
| 463 | } |
| 464 | |
| 465 | let re = search.flags.includes('g') ? search : new RegExp(search.source, search.flags + 'g'); |
| 466 | let matches = Array.from(text.slice(0, limit).matchAll(re)); |
| 467 | return matches.at(-1)?.index ?? -1; |
| 468 | } |
| 469 | |
| 470 | function appendSegments( |
| 471 | segments: TokenFieldSegment[], |