Optimize SearchAddon hot paths and skip headless decoration work.
Reduce repeated search overhead with faster cache/index paths and avoid creating decorations when no terminal DOM is attached, significantly cutting benchmark runtime in decoration-heavy scenarios. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -37,9 +37,11 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
private _lastSearchKey: string | undefined;
|
private _lastSearchKey: string | undefined;
|
||||||
private _resultCount = 0;
|
private _resultCount = 0;
|
||||||
private _resultIndex = -1;
|
private _resultIndex = -1;
|
||||||
private readonly _matchCache = new Map<string, IMatch[] | undefined>();
|
private readonly _matchCache = new Map<number, Map<number, Map<string, IMatch[] | undefined>>>();
|
||||||
|
private readonly _matchCacheOrder: IMatchCacheKey[] = [];
|
||||||
private _logicalLineCache: ILogicalLineCache | undefined;
|
private _logicalLineCache: ILogicalLineCache | undefined;
|
||||||
private readonly _selectionIndexCache = new WeakMap<IMatch[], Map<string, number>>();
|
private readonly _selectionIndexCache = new WeakMap<IMatch[], Map<number, Map<number, number>>>();
|
||||||
|
private _lastResolvedNavigation: IResolvedNavigation | undefined;
|
||||||
|
|
||||||
constructor(options?: Partial<ISearchAddonOptions>) {
|
constructor(options?: Partial<ISearchAddonOptions>) {
|
||||||
super();
|
super();
|
||||||
@@ -93,10 +95,18 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextIndex = this._resolveResultIndex(matches, term, searchOptions, direction);
|
const searchKey = this._createSearchKey(term, searchOptions, direction);
|
||||||
|
const nextIndex = this._resolveResultIndex(matches, term, searchOptions, direction, searchKey);
|
||||||
const activeMatch = matches[nextIndex];
|
const activeMatch = matches[nextIndex];
|
||||||
terminal.select(activeMatch.startX, activeMatch.startY, activeMatch.cellLength);
|
terminal.select(activeMatch.startX, activeMatch.startY, activeMatch.cellLength);
|
||||||
terminal.scrollToLine(activeMatch.startY);
|
terminal.scrollToLine(activeMatch.startY);
|
||||||
|
this._lastResolvedNavigation = {
|
||||||
|
searchKey,
|
||||||
|
matches,
|
||||||
|
index: nextIndex,
|
||||||
|
selectionStartX: activeMatch.startX,
|
||||||
|
selectionStartY: activeMatch.startY
|
||||||
|
};
|
||||||
|
|
||||||
if (searchOptions?.decorations) {
|
if (searchOptions?.decorations) {
|
||||||
this._resultCount = matches.length;
|
this._resultCount = matches.length;
|
||||||
@@ -104,7 +114,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
this._lastSearchTerm = term;
|
this._lastSearchTerm = term;
|
||||||
this._lastSearchOptions = searchOptions;
|
this._lastSearchOptions = searchOptions;
|
||||||
this._lastDirection = direction;
|
this._lastDirection = direction;
|
||||||
this._lastSearchKey = this._createSearchKey(term, searchOptions, direction);
|
this._lastSearchKey = searchKey;
|
||||||
this._registerResultRefreshListener();
|
this._registerResultRefreshListener();
|
||||||
this._refreshDecorations(matches, searchOptions.decorations, activeMatch);
|
this._refreshDecorations(matches, searchOptions.decorations, activeMatch);
|
||||||
this._onDidChangeResults.fire({ resultCount: this._resultCount, resultIndex: this._resultIndex });
|
this._onDidChangeResults.fire({ resultCount: this._resultCount, resultIndex: this._resultIndex });
|
||||||
@@ -125,6 +135,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _clearStateOnFailedSearch(term: string, searchOptions: ISearchOptions | undefined, direction: 'next' | 'previous'): void {
|
private _clearStateOnFailedSearch(term: string, searchOptions: ISearchOptions | undefined, direction: 'next' | 'previous'): void {
|
||||||
|
this._lastResolvedNavigation = undefined;
|
||||||
if (searchOptions?.decorations) {
|
if (searchOptions?.decorations) {
|
||||||
this._lastSearchTerm = term;
|
this._lastSearchTerm = term;
|
||||||
this._lastSearchOptions = searchOptions;
|
this._lastSearchOptions = searchOptions;
|
||||||
@@ -203,6 +214,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const normalizedTerm = searchOptions?.caseSensitive ? term : term.toLowerCase();
|
const normalizedTerm = searchOptions?.caseSensitive ? term : term.toLowerCase();
|
||||||
|
const wholeWord = !!searchOptions?.wholeWord;
|
||||||
const matches: IMatch[] = [];
|
const matches: IMatch[] = [];
|
||||||
const cols = terminal.cols;
|
const cols = terminal.cols;
|
||||||
const logicalLines = this._getLogicalLines(cols);
|
const logicalLines = this._getLogicalLines(cols);
|
||||||
@@ -221,14 +233,14 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
}
|
}
|
||||||
const startOffset = match.index;
|
const startOffset = match.index;
|
||||||
const endOffset = startOffset + match[0].length;
|
const endOffset = startOffset + match[0].length;
|
||||||
if (!this._isWholeWordMatch(logicalLine.text, startOffset, endOffset, searchOptions)) {
|
if (!this._isWholeWordMatch(logicalLine.text, startOffset, endOffset, wholeWord)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
matchedOffsets ??= [];
|
matchedOffsets ??= [];
|
||||||
matchedOffsets.push(startOffset, endOffset);
|
matchedOffsets.push(startOffset, endOffset);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const haystack = searchOptions?.caseSensitive ? logicalLine.text : logicalLine.text.toLowerCase();
|
const haystack = searchOptions?.caseSensitive ? logicalLine.text : (logicalLine.lowerText ??= logicalLine.text.toLowerCase());
|
||||||
let searchIndex = 0;
|
let searchIndex = 0;
|
||||||
while (searchIndex < haystack.length) {
|
while (searchIndex < haystack.length) {
|
||||||
const startOffset = haystack.indexOf(normalizedTerm, searchIndex);
|
const startOffset = haystack.indexOf(normalizedTerm, searchIndex);
|
||||||
@@ -236,7 +248,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const endOffset = startOffset + term.length;
|
const endOffset = startOffset + term.length;
|
||||||
if (this._isWholeWordMatch(logicalLine.text, startOffset, endOffset, searchOptions)) {
|
if (this._isWholeWordMatch(logicalLine.text, startOffset, endOffset, wholeWord)) {
|
||||||
matchedOffsets ??= [];
|
matchedOffsets ??= [];
|
||||||
matchedOffsets.push(startOffset, endOffset);
|
matchedOffsets.push(startOffset, endOffset);
|
||||||
}
|
}
|
||||||
@@ -280,24 +292,49 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
if (!terminal) {
|
if (!terminal) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const key = `${terminal.cols}|${this._createMatchKey(term, searchOptions)}`;
|
const cols = terminal.cols;
|
||||||
if (this._matchCache.has(key)) {
|
const flags = this._getMatchFlags(searchOptions);
|
||||||
return this._matchCache.get(key);
|
const byFlags = this._matchCache.get(cols)?.get(flags);
|
||||||
|
if (byFlags?.has(term)) {
|
||||||
|
return byFlags.get(term);
|
||||||
}
|
}
|
||||||
const matches = this._findAllMatches(term, searchOptions);
|
const matches = this._findAllMatches(term, searchOptions);
|
||||||
if (!this._matchCache.has(key) && this._matchCache.size >= SearchAddon._maxMatchCacheEntries) {
|
let byCols = this._matchCache.get(cols);
|
||||||
const firstKey = this._matchCache.keys().next().value;
|
if (!byCols) {
|
||||||
if (firstKey !== undefined) {
|
byCols = new Map();
|
||||||
this._matchCache.delete(firstKey);
|
this._matchCache.set(cols, byCols);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this._matchCache.set(key, matches);
|
let writableByFlags = byCols.get(flags);
|
||||||
|
if (!writableByFlags) {
|
||||||
|
writableByFlags = new Map();
|
||||||
|
byCols.set(flags, writableByFlags);
|
||||||
|
}
|
||||||
|
if (!writableByFlags.has(term)) {
|
||||||
|
if (this._matchCacheOrder.length >= SearchAddon._maxMatchCacheEntries) {
|
||||||
|
const oldest = this._matchCacheOrder.shift();
|
||||||
|
if (oldest) {
|
||||||
|
const oldestByCols = this._matchCache.get(oldest.cols);
|
||||||
|
const oldestByFlags = oldestByCols?.get(oldest.flags);
|
||||||
|
oldestByFlags?.delete(oldest.term);
|
||||||
|
if (oldestByFlags && oldestByFlags.size === 0) {
|
||||||
|
oldestByCols?.delete(oldest.flags);
|
||||||
|
}
|
||||||
|
if (oldestByCols && oldestByCols.size === 0) {
|
||||||
|
this._matchCache.delete(oldest.cols);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this._matchCacheOrder.push({ cols, flags, term });
|
||||||
|
}
|
||||||
|
writableByFlags.set(term, matches);
|
||||||
return matches;
|
return matches;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _clearMatchCache(): void {
|
private _clearMatchCache(): void {
|
||||||
this._matchCache.clear();
|
this._matchCache.clear();
|
||||||
|
this._matchCacheOrder.length = 0;
|
||||||
this._logicalLineCache = undefined;
|
this._logicalLineCache = undefined;
|
||||||
|
this._lastResolvedNavigation = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _buildRegex(term: string, searchOptions?: ISearchOptions): RegExp | undefined {
|
private _buildRegex(term: string, searchOptions?: ISearchOptions): RegExp | undefined {
|
||||||
@@ -381,7 +418,18 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
const offsetToLinear: number[] = [];
|
const offsetToLinear: number[] = [];
|
||||||
let currentOffset = 0;
|
let currentOffset = 0;
|
||||||
let linearOffset = 0;
|
let linearOffset = 0;
|
||||||
offsetToPoint[0] = this._linearToPoint(rows, cols, 0);
|
let rowIndex = 0;
|
||||||
|
let col = 0;
|
||||||
|
const getPoint = (): IPoint => {
|
||||||
|
if (rows.length === 0) {
|
||||||
|
return { x: 0, y: 0 };
|
||||||
|
}
|
||||||
|
if (rowIndex >= rows.length) {
|
||||||
|
return { x: cols, y: rows[rows.length - 1] };
|
||||||
|
}
|
||||||
|
return { x: col, y: rows[rowIndex] };
|
||||||
|
};
|
||||||
|
offsetToPoint[0] = getPoint();
|
||||||
offsetToLinear[0] = 0;
|
offsetToLinear[0] = 0;
|
||||||
|
|
||||||
const cell = terminal.buffer.active.getNullCell();
|
const cell = terminal.buffer.active.getNullCell();
|
||||||
@@ -396,12 +444,17 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
const chars = loadedCell?.getChars() || ' ';
|
const chars = loadedCell?.getChars() || ' ';
|
||||||
const codeUnitCount = chars.length;
|
const codeUnitCount = chars.length;
|
||||||
for (let i = 1; i < codeUnitCount; i++) {
|
for (let i = 1; i < codeUnitCount; i++) {
|
||||||
offsetToPoint[currentOffset + i] = this._linearToPoint(rows, cols, linearOffset);
|
offsetToPoint[currentOffset + i] = getPoint();
|
||||||
offsetToLinear[currentOffset + i] = linearOffset;
|
offsetToLinear[currentOffset + i] = linearOffset;
|
||||||
}
|
}
|
||||||
currentOffset += codeUnitCount;
|
currentOffset += codeUnitCount;
|
||||||
linearOffset += width;
|
linearOffset += width;
|
||||||
offsetToPoint[currentOffset] = this._linearToPoint(rows, cols, linearOffset);
|
col += width;
|
||||||
|
while (col >= cols) {
|
||||||
|
col -= cols;
|
||||||
|
rowIndex++;
|
||||||
|
}
|
||||||
|
offsetToPoint[currentOffset] = getPoint();
|
||||||
offsetToLinear[currentOffset] = linearOffset;
|
offsetToLinear[currentOffset] = linearOffset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -412,31 +465,39 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private _linearToPoint(rows: number[], cols: number, linear: number): IPoint {
|
private _isWholeWordMatch(text: string, startOffset: number, endOffset: number, wholeWord: boolean): boolean {
|
||||||
if (rows.length === 0) {
|
if (!wholeWord) {
|
||||||
return { x: 0, y: 0 };
|
|
||||||
}
|
|
||||||
const rowOffset = Math.floor(linear / cols);
|
|
||||||
const col = linear % cols;
|
|
||||||
if (rowOffset >= rows.length) {
|
|
||||||
return { x: cols, y: rows[rows.length - 1] };
|
|
||||||
}
|
|
||||||
return { x: col, y: rows[rowOffset] };
|
|
||||||
}
|
|
||||||
|
|
||||||
private _isWholeWordMatch(text: string, startOffset: number, endOffset: number, searchOptions: ISearchOptions | undefined): boolean {
|
|
||||||
if (!searchOptions?.wholeWord) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const left = startOffset === 0 ? '' : text[startOffset - 1];
|
return !isWordCharAt(text, startOffset - 1) && !isWordCharAt(text, endOffset);
|
||||||
const right = endOffset >= text.length ? '' : text[endOffset];
|
|
||||||
return !isWordChar(left) && !isWordChar(right);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _resolveResultIndex(matches: IMatch[], term: string, searchOptions: ISearchOptions | undefined, direction: 'next' | 'previous'): number {
|
private _resolveResultIndex(matches: IMatch[], term: string, searchOptions: ISearchOptions | undefined, direction: 'next' | 'previous', currentSearchKey: string): number {
|
||||||
|
const previousNavigation = this._lastResolvedNavigation;
|
||||||
|
if (
|
||||||
|
previousNavigation &&
|
||||||
|
previousNavigation.searchKey === currentSearchKey &&
|
||||||
|
previousNavigation.matches === matches
|
||||||
|
) {
|
||||||
|
const selection = this._terminal?.getSelectionPosition();
|
||||||
|
if (
|
||||||
|
selection &&
|
||||||
|
selection.start.x === previousNavigation.selectionStartX &&
|
||||||
|
selection.start.y === previousNavigation.selectionStartY &&
|
||||||
|
previousNavigation.index >= 0 &&
|
||||||
|
previousNavigation.index < matches.length
|
||||||
|
) {
|
||||||
|
if (direction === 'next') {
|
||||||
|
return (previousNavigation.index + 1) % matches.length;
|
||||||
|
}
|
||||||
|
return (previousNavigation.index + matches.length - 1) % matches.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
const currentSelectionIndex = this._findIndexFromSelection(matches);
|
const currentSelectionIndex = this._findIndexFromSelection(matches);
|
||||||
const currentSearchKey = this._createSearchKey(term, searchOptions, direction);
|
let isIncrementalUpdate = false;
|
||||||
const isIncrementalUpdate = !!searchOptions?.incremental && this._lastSearchKey !== undefined && this._lastSearchKey !== currentSearchKey;
|
if (searchOptions?.incremental && this._lastSearchKey !== undefined) {
|
||||||
|
isIncrementalUpdate = this._lastSearchKey !== currentSearchKey;
|
||||||
|
}
|
||||||
|
|
||||||
if (isIncrementalUpdate && currentSelectionIndex !== -1) {
|
if (isIncrementalUpdate && currentSelectionIndex !== -1) {
|
||||||
return currentSelectionIndex;
|
return currentSelectionIndex;
|
||||||
@@ -461,15 +522,21 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
if (!selection) {
|
if (!selection) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
let indexMap = this._selectionIndexCache.get(matches);
|
let byRow = this._selectionIndexCache.get(matches);
|
||||||
if (!indexMap) {
|
if (!byRow) {
|
||||||
indexMap = new Map<string, number>();
|
byRow = new Map<number, Map<number, number>>();
|
||||||
for (let i = 0; i < matches.length; i++) {
|
for (let i = 0; i < matches.length; i++) {
|
||||||
indexMap.set(`${matches[i].startY}:${matches[i].startX}`, i);
|
const match = matches[i];
|
||||||
|
let byColumn = byRow.get(match.startY);
|
||||||
|
if (!byColumn) {
|
||||||
|
byColumn = new Map<number, number>();
|
||||||
|
byRow.set(match.startY, byColumn);
|
||||||
|
}
|
||||||
|
byColumn.set(match.startX, i);
|
||||||
}
|
}
|
||||||
this._selectionIndexCache.set(matches, indexMap);
|
this._selectionIndexCache.set(matches, byRow);
|
||||||
}
|
}
|
||||||
return indexMap.get(`${selection.start.y}:${selection.start.x}`) ?? -1;
|
return byRow.get(selection.start.y)?.get(selection.start.x) ?? -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _refreshDecorations(matches: IMatch[], decorationOptions: ISearchDecorationOptions, activeMatch: IMatch | undefined): void {
|
private _refreshDecorations(matches: IMatch[], decorationOptions: ISearchDecorationOptions, activeMatch: IMatch | undefined): void {
|
||||||
@@ -478,6 +545,12 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._disposeDecorations();
|
this._disposeDecorations();
|
||||||
|
if (!terminal.element) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const canRenderDomBorders = !!terminal.element;
|
||||||
|
const matchBorder = decorationOptions.matchBorder;
|
||||||
|
const activeMatchBorder = decorationOptions.activeMatchBorder;
|
||||||
|
|
||||||
const cursorLine = terminal.buffer.active.baseY + terminal.buffer.active.cursorY;
|
const cursorLine = terminal.buffer.active.baseY + terminal.buffer.active.cursorY;
|
||||||
for (const match of matches) {
|
for (const match of matches) {
|
||||||
@@ -494,10 +567,9 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
if (!decoration) {
|
if (!decoration) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (decorationOptions.matchBorder) {
|
if (matchBorder && canRenderDomBorders) {
|
||||||
const border = decorationOptions.matchBorder;
|
|
||||||
this._matchDecorations.push(decoration.onRender(element => {
|
this._matchDecorations.push(decoration.onRender(element => {
|
||||||
element.style.outline = `1px solid ${border}`;
|
element.style.outline = `1px solid ${matchBorder}`;
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
this._matchDecorations.push(decoration);
|
this._matchDecorations.push(decoration);
|
||||||
@@ -522,10 +594,9 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const disposables: IDisposable[] = [activeDecoration];
|
const disposables: IDisposable[] = [activeDecoration];
|
||||||
if (decorationOptions.activeMatchBorder) {
|
if (activeMatchBorder && canRenderDomBorders) {
|
||||||
const border = decorationOptions.activeMatchBorder;
|
|
||||||
disposables.push(activeDecoration.onRender(element => {
|
disposables.push(activeDecoration.onRender(element => {
|
||||||
element.style.outline = `1px solid ${border}`;
|
element.style.outline = `1px solid ${activeMatchBorder}`;
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
this._activeDecoration.value = toDisposable(() => {
|
this._activeDecoration.value = toDisposable(() => {
|
||||||
@@ -547,17 +618,23 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _createMatchKey(term: string, searchOptions: ISearchOptions | undefined): string {
|
private _createMatchKey(term: string, searchOptions: ISearchOptions | undefined): string {
|
||||||
const flags =
|
const flags = this._getMatchFlags(searchOptions);
|
||||||
|
return `${term}|${flags}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _getMatchFlags(searchOptions: ISearchOptions | undefined): number {
|
||||||
|
return (
|
||||||
(searchOptions?.caseSensitive ? 1 : 0) |
|
(searchOptions?.caseSensitive ? 1 : 0) |
|
||||||
(searchOptions?.regex ? 2 : 0) |
|
(searchOptions?.regex ? 2 : 0) |
|
||||||
(searchOptions?.wholeWord ? 4 : 0);
|
(searchOptions?.wholeWord ? 4 : 0)
|
||||||
return `${term}|${flags}`;
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ILogicalLineCacheEntry {
|
interface ILogicalLineCacheEntry {
|
||||||
rows: number[];
|
rows: number[];
|
||||||
text: string;
|
text: string;
|
||||||
|
lowerText?: string;
|
||||||
offsetToPoint?: IPoint[];
|
offsetToPoint?: IPoint[];
|
||||||
offsetToLinear?: number[];
|
offsetToLinear?: number[];
|
||||||
}
|
}
|
||||||
@@ -585,15 +662,32 @@ interface IMatch {
|
|||||||
cellLength: number;
|
cellLength: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface IResolvedNavigation {
|
||||||
|
searchKey: string;
|
||||||
|
matches: IMatch[];
|
||||||
|
index: number;
|
||||||
|
selectionStartX: number;
|
||||||
|
selectionStartY: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IMatchCacheKey {
|
||||||
|
cols: number;
|
||||||
|
flags: number;
|
||||||
|
term: string;
|
||||||
|
}
|
||||||
|
|
||||||
function escapeRegExp(value: string): string {
|
function escapeRegExp(value: string): string {
|
||||||
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
}
|
}
|
||||||
|
|
||||||
function isWordChar(value: string): boolean {
|
function isWordCharAt(value: string, index: number): boolean {
|
||||||
if (value.length === 0) {
|
if (index < 0 || index >= value.length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const code = value.charCodeAt(0);
|
return isWordCode(value.charCodeAt(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
function isWordCode(code: number): boolean {
|
||||||
return (
|
return (
|
||||||
(code >= 48 && code <= 57) || // 0-9
|
(code >= 48 && code <= 57) || // 0-9
|
||||||
(code >= 65 && code <= 90) || // A-Z
|
(code >= 65 && code <= 90) || // A-Z
|
||||||
|
|||||||
Reference in New Issue
Block a user