Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 24 additions & 27 deletions src/vs/base/common/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,16 @@ export function ltrim(haystack: string, needle: string): string {
}

const needleLen = needle.length;
if (needleLen === 0 || haystack.length === 0) {
return haystack;
}

let offset = 0;

while (haystack.indexOf(needle, offset) === offset) {
offset = offset + needleLen;
if (needleLen === 1) {
const ch = needle.charCodeAt(0);
while (offset < haystack.length && haystack.charCodeAt(offset) === ch) {
offset++;
}
} else {
while (haystack.startsWith(needle, offset)) {
offset += needleLen;
}
}
return haystack.substring(offset);
}
Expand All @@ -168,22 +170,18 @@ export function rtrim(haystack: string, needle: string): string {
const needleLen = needle.length,
haystackLen = haystack.length;

if (needleLen === 0 || haystackLen === 0) {
return haystack;
if (needleLen === 1) {
let end = haystackLen;
const ch = needle.charCodeAt(0);
while (end > 0 && haystack.charCodeAt(end - 1) === ch) {
end--;
}
return haystack.substring(0, end);
}

let offset = haystackLen,
idx = -1;

while (true) {
idx = haystack.lastIndexOf(needle, offset - 1);
if (idx === -1 || idx + needleLen !== offset) {
break;
}
if (idx === 0) {
return '';
}
offset = idx;
let offset = haystackLen;
while (offset > 0 && haystack.endsWith(needle, offset)) {
offset -= needleLen;
}

return haystack.substring(0, offset);
Expand Down Expand Up @@ -1197,10 +1195,9 @@ export class AmbiguousCharacters {
);
});

private static readonly cache = new LRUCachedFunction<
string[],
AmbiguousCharacters
>({ getCacheKey: JSON.stringify }, (locales) => {
private static readonly cache = new LRUCachedFunction<string, AmbiguousCharacters>((localesStr) => {
const locales = localesStr.split(',');

function arrayToMap(arr: number[]): Map<number, number> {
const result = new Map<number, number>();
for (let i = 0; i < arr.length; i += 2) {
Expand Down Expand Up @@ -1257,8 +1254,8 @@ export class AmbiguousCharacters {
return new AmbiguousCharacters(map);
});

public static getInstance(locales: Set<string>): AmbiguousCharacters {
return AmbiguousCharacters.cache.get(Array.from(locales));
public static getInstance(locales: Iterable<string>): AmbiguousCharacters {
return AmbiguousCharacters.cache.get(Array.from(locales).join(','));
}

private static _locales = new Lazy<string[]>(() =>
Expand Down
12 changes: 12 additions & 0 deletions src/vs/base/test/common/strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ suite('Strings', () => {
assert.strictEqual(strings.ltrim('///', '/'), '');
assert.strictEqual(strings.ltrim('', ''), '');
assert.strictEqual(strings.ltrim('', '/'), '');
// Multi-character needle with consecutive repetitions
assert.strictEqual(strings.ltrim('---hello', '---'), 'hello');
assert.strictEqual(strings.ltrim('------hello', '---'), 'hello');
assert.strictEqual(strings.ltrim('---------hello', '---'), 'hello');
assert.strictEqual(strings.ltrim('hello---', '---'), 'hello---');
});

test('rtrim', () => {
Expand All @@ -228,6 +233,13 @@ suite('Strings', () => {
assert.strictEqual(strings.rtrim('///', '/'), '');
assert.strictEqual(strings.rtrim('', ''), '');
assert.strictEqual(strings.rtrim('', '/'), '');
// Multi-character needle with consecutive repetitions (bug fix)
assert.strictEqual(strings.rtrim('hello---', '---'), 'hello');
assert.strictEqual(strings.rtrim('hello------', '---'), 'hello');
assert.strictEqual(strings.rtrim('hello---------', '---'), 'hello');
assert.strictEqual(strings.rtrim('---hello', '---'), '---hello');
assert.strictEqual(strings.rtrim('hello world' + '---'.repeat(10), '---'), 'hello world');
assert.strictEqual(strings.rtrim('path/to/file///', '//'), 'path/to/file/');
});

test('trim', () => {
Expand Down
Loading