Skip to content

Commit

Permalink
Handle case with only whitespace before the cut point
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekkopecky committed Dec 12, 2023
1 parent 009b99e commit 93dc830
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/vs/base/common/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,14 +713,16 @@ export function isEmojiImprecise(x: number): boolean {
* The return value can be longer than the given value of `n`. Leading whitespace is always trimmed.
*/
export function lcut(text: string, n: number, prefix = '') {
if (text.length < n) {
return text.trimStart();
const trimmed = text.trimStart();

if (trimmed.length < n) {
return trimmed;
}

const re = /\b/g;
let i = 0;
while (re.test(text)) {
if (text.length - re.lastIndex < n) {
while (re.test(trimmed)) {
if (trimmed.length - re.lastIndex < n) {
break;
}

Expand All @@ -729,10 +731,10 @@ export function lcut(text: string, n: number, prefix = '') {
}

if (i === 0) {
return text.trimStart();
return trimmed;
}

return prefix + text.substring(i).trimStart();
return prefix + trimmed.substring(i).trimStart();
}

// Escape codes, compiled from https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
Expand Down
2 changes: 2 additions & 0 deletions src/vs/base/test/common/strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ suite('Strings', () => {
assert.strictEqual(strings.lcut('a', 10), 'a');
assert.strictEqual(strings.lcut(' a', 10), 'a');
assert.strictEqual(strings.lcut(' a', 10), 'a');
assert.strictEqual(strings.lcut(' bbbb a', 10), 'bbbb a');
assert.strictEqual(strings.lcut('............a', 10), '............a');

assert.strictEqual(strings.lcut('', 10, '…'), '');
assert.strictEqual(strings.lcut('a', 10, '…'), 'a');
assert.strictEqual(strings.lcut(' a', 10, '…'), 'a');
assert.strictEqual(strings.lcut(' a', 10, '…'), 'a');
assert.strictEqual(strings.lcut(' bbbb a', 10, '…'), 'bbbb a');
assert.strictEqual(strings.lcut('............a', 10, '…'), '............a');
});

Expand Down

0 comments on commit 93dc830

Please sign in to comment.