| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9b483fb commit 192c038
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,36 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common.js'); | ||
| 4 | + | ||
| 5 | + const { stripVTControlCharacters } = require('node:util'); | ||
| 6 | + const assert = require('node:assert'); | ||
| 7 | + | ||
| 8 | + const bench = common.createBenchmark(main, { | ||
| 9 | + input: ['noAnsi-short', 'noAnsi-long', 'ansi-short', 'ansi-long'], | ||
| 10 | + n: [1e6], | ||
| 11 | + }); | ||
| 12 | + | ||
| 13 | + function main({ input, n }) { | ||
| 14 | + let str; | ||
| 15 | + switch (input) { | ||
| 16 | + case 'noAnsi-short': | ||
| 17 | + str = 'This is a plain text string without any ANSI codes'; | ||
| 18 | + break; | ||
| 19 | + case 'noAnsi-long': | ||
| 20 | + str = 'Long plain text without ANSI. '.repeat(333); | ||
| 21 | + break; | ||
| 22 | + case 'ansi-short': | ||
| 23 | + str = '\u001B[31mHello\u001B[39m'; | ||
| 24 | + break; | ||
| 25 | + case 'ansi-long': | ||
| 26 | + str = ('\u001B[31m' + 'colored text '.repeat(10) + '\u001B[39m').repeat(10); | ||
| 27 | + break; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + bench.start(); | ||
| 31 | + for (let i = 0; i < n; i++) { | ||
| 32 | + const result = stripVTControlCharacters(str); | ||
| 33 | + assert.ok(typeof result === 'string'); | ||
| 34 | + } | ||
| 35 | + bench.end(n); | ||
| 36 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3016,6 +3016,13 @@ if (internalBinding('config').hasIntl) { | |||
| 3016 | 3016 | function stripVTControlCharacters(str) { | |
| 3017 | 3017 | validateString(str, 'str'); | |
| 3018 | 3018 | ||
| 3019 | + // Short-circuit: all ANSI escape sequences start with either | ||
| 3020 | + // ESC (\u001B, 7-bit) or CSI (\u009B, 8-bit) introducer. | ||
| 3021 | + // If neither is present, the string has no VT control characters. | ||
| 3022 | + if (StringPrototypeIndexOf(str, '\u001B') === -1 && | ||
| 3023 | + StringPrototypeIndexOf(str, '\u009B') === -1) | ||
| 3024 | + return str; | ||
| 3025 | + | ||
| 3019 | 3026 | return RegExpPrototypeSymbolReplace(ansi, str, ''); | |
| 3020 | 3027 | } | |
| 3021 | 3028 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -87,3 +87,13 @@ assert.throws(() => { | |||
| 87 | 87 | message: 'The "str" argument must be of type string.' + | |
| 88 | 88 | common.invalidArgTypeHelper({}) | |
| 89 | 89 | }); | |
| 90 | + | ||
| 91 | + // stripVTControlCharacters: fast path returns input when no ANSI codes | ||
| 92 | + assert.strictEqual(util.stripVTControlCharacters('hello'), 'hello'); | ||
| 93 | + assert.strictEqual(util.stripVTControlCharacters(''), ''); | ||
| 94 | + | ||
| 95 | + // stripVTControlCharacters: strips 7-bit ESC sequences | ||
| 96 | + assert.strictEqual(util.stripVTControlCharacters('\u001B[31mfoo\u001B[39m'), 'foo'); | ||
| 97 | + | ||
| 98 | + // stripVTControlCharacters: strips 8-bit CSI sequences | ||
| 99 | + assert.strictEqual(util.stripVTControlCharacters('\u009B31mfoo\u009B39m'), 'foo'); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments