Path: blob/main/scripts/chat-simulation/fixtures/_chatperf_strings.ts
13383 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45// perf-benchmark-marker67/**8* Fixture for chat-simulation benchmarks.9* Simplified from src/vs/base/common/strings.ts for stable perf testing.10*/1112export function format(value: string, ...args: any[]): string {13return value.replace(/{(\d+)}/g, (match, index) => {14const i = parseInt(index, 10);15return i >= 0 && i < args.length ? `${args[i]}` : match;16});17}1819export function escape(value: string): string {20return value.replace(/[<>&"']/g, ch => {21switch (ch) {22case '<': return '<';23case '>': return '>';24case '&': return '&';25case '"': return '"';26case '\'': return ''';27default: return ch;28}29});30}3132export function trim(value: string, ch: string = ' '): string {33let start = 0;34let end = value.length;35while (start < end && value[start] === ch) { start++; }36while (end > start && value[end - 1] === ch) { end--; }37return value.substring(start, end);38}3940export function equalsIgnoreCase(a: string, b: string): boolean {41return a.length === b.length && a.toLowerCase() === b.toLowerCase();42}4344export function startsWithIgnoreCase(str: string, candidate: string): boolean {45if (str.length < candidate.length) { return false; }46return str.substring(0, candidate.length).toLowerCase() === candidate.toLowerCase();47}4849export function commonPrefixLength(a: string, b: string): number {50const len = Math.min(a.length, b.length);51for (let i = 0; i < len; i++) {52if (a.charCodeAt(i) !== b.charCodeAt(i)) { return i; }53}54return len;55}5657export function commonSuffixLength(a: string, b: string): number {58const len = Math.min(a.length, b.length);59for (let i = 0; i < len; i++) {60if (a.charCodeAt(a.length - 1 - i) !== b.charCodeAt(b.length - 1 - i)) { return i; }61}62return len;63}6465export function splitLines(str: string): string[] {66return str.split(/\r\n|\r|\n/);67}6869export function regExpLeadsToEndlessLoop(regexp: RegExp): boolean {70if (regexp.source === '^' || regexp.source === '^$' || regexp.source === '$') {71return false;72}73return !regexp.exec('')?.length;74}757677