Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/scripts/chat-simulation/fixtures/_chatperf_strings.ts
13383 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
// perf-benchmark-marker
7
8
/**
9
* Fixture for chat-simulation benchmarks.
10
* Simplified from src/vs/base/common/strings.ts for stable perf testing.
11
*/
12
13
export function format(value: string, ...args: any[]): string {
14
return value.replace(/{(\d+)}/g, (match, index) => {
15
const i = parseInt(index, 10);
16
return i >= 0 && i < args.length ? `${args[i]}` : match;
17
});
18
}
19
20
export function escape(value: string): string {
21
return value.replace(/[<>&"']/g, ch => {
22
switch (ch) {
23
case '<': return '&lt;';
24
case '>': return '&gt;';
25
case '&': return '&amp;';
26
case '"': return '&quot;';
27
case '\'': return '&#39;';
28
default: return ch;
29
}
30
});
31
}
32
33
export function trim(value: string, ch: string = ' '): string {
34
let start = 0;
35
let end = value.length;
36
while (start < end && value[start] === ch) { start++; }
37
while (end > start && value[end - 1] === ch) { end--; }
38
return value.substring(start, end);
39
}
40
41
export function equalsIgnoreCase(a: string, b: string): boolean {
42
return a.length === b.length && a.toLowerCase() === b.toLowerCase();
43
}
44
45
export function startsWithIgnoreCase(str: string, candidate: string): boolean {
46
if (str.length < candidate.length) { return false; }
47
return str.substring(0, candidate.length).toLowerCase() === candidate.toLowerCase();
48
}
49
50
export function commonPrefixLength(a: string, b: string): number {
51
const len = Math.min(a.length, b.length);
52
for (let i = 0; i < len; i++) {
53
if (a.charCodeAt(i) !== b.charCodeAt(i)) { return i; }
54
}
55
return len;
56
}
57
58
export function commonSuffixLength(a: string, b: string): number {
59
const len = Math.min(a.length, b.length);
60
for (let i = 0; i < len; i++) {
61
if (a.charCodeAt(a.length - 1 - i) !== b.charCodeAt(b.length - 1 - i)) { return i; }
62
}
63
return len;
64
}
65
66
export function splitLines(str: string): string[] {
67
return str.split(/\r\n|\r|\n/);
68
}
69
70
export function regExpLeadsToEndlessLoop(regexp: RegExp): boolean {
71
if (regexp.source === '^' || regexp.source === '^$' || regexp.source === '$') {
72
return false;
73
}
74
return !regexp.exec('')?.length;
75
}
76
77