mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-25 12:47:14 +00:00
Optimize strings.lcut - fix #34665
This commit is contained in:
@@ -674,7 +674,7 @@ export function computeLineStarts(text: string): number[] {
|
||||
* Given a string and a max length returns a shorted version. Shorting
|
||||
* happens at favorable positions - such as whitespace or punctuation characters.
|
||||
*/
|
||||
export function lcut(text: string, n: number): string {
|
||||
export function lcut_orig(text: string, n: number): string {
|
||||
|
||||
if (text.length < n) {
|
||||
return text;
|
||||
@@ -695,6 +695,25 @@ export function lcut(text: string, n: number): string {
|
||||
return segments.join(empty).replace(/^\s/, empty);
|
||||
}
|
||||
|
||||
export function lcut(text: string, n: number) {
|
||||
if (text.length < n) {
|
||||
return text;
|
||||
}
|
||||
|
||||
const re = /\b/g;
|
||||
let i = 0;
|
||||
while (re.test(text)) {
|
||||
if (text.length - re.lastIndex < n) {
|
||||
break;
|
||||
}
|
||||
|
||||
i = re.lastIndex;
|
||||
re.lastIndex += 1;
|
||||
}
|
||||
|
||||
return text.substring(i).replace(/^\s/, empty);
|
||||
}
|
||||
|
||||
// Escape codes
|
||||
// http://en.wikipedia.org/wiki/ANSI_escape_code
|
||||
const EL = /\x1B\x5B[12]?K/g; // Erase in line
|
||||
|
||||
@@ -126,6 +126,17 @@ suite('Strings', () => {
|
||||
assertLineStart('far\nboo\nfar', 0, 4, 8);
|
||||
});
|
||||
|
||||
test('lcut', () => {
|
||||
assert.strictEqual(strings.lcut('foo bar', 0), '');
|
||||
assert.strictEqual(strings.lcut('foo bar', 1), 'bar');
|
||||
assert.strictEqual(strings.lcut('foo bar', 3), 'bar');
|
||||
assert.strictEqual(strings.lcut('foo bar', 4), 'bar'); // Leading whitespace trimmed
|
||||
assert.strictEqual(strings.lcut('foo bar', 5), 'foo bar');
|
||||
assert.strictEqual(strings.lcut('test string 0.1.2.3', 3), '2.3');
|
||||
|
||||
assert.strictEqual(strings.lcut('', 10), '');
|
||||
assert.strictEqual(strings.lcut('a', 10), 'a');
|
||||
});
|
||||
|
||||
test('pad', function () {
|
||||
assert.strictEqual(strings.pad(1, 0), '1');
|
||||
|
||||
Reference in New Issue
Block a user