util: add fast path to stripVTControlCharacters · nodejs/node@192c038 · GitHub
Skip to content

Commit 192c038

Browse files
privatenumberaduh95
authored andcommitted
util: add fast path to stripVTControlCharacters
PR-URL: #61833 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 9b483fb commit 192c038

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions

lib/internal/util/inspect.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,13 @@ if (internalBinding('config').hasIntl) {
30163016
function stripVTControlCharacters(str) {
30173017
validateString(str, 'str');
30183018

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+
30193026
return RegExpPrototypeSymbolReplace(ansi, str, '');
30203027
}
30213028

test/parallel/test-util.js

Lines changed: 10 additions & 0 deletions

0 commit comments

Comments
 (0)