-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
repl: Handle unicode escape sequence #5189
Conversation
/cc @bnoordhuis |
CI: https://ci.nodejs.org/job/node-test-pull-request/1636/ |
LGTM if CI is green and @bnoordhuis is happy. |
@jasnell CI turned green ? |
@princejwesley it's green on all machines, the overall run is in 'cancelled' status, but I guess that's something about the CI, not this PR. |
@silverwind thanks 😃 |
@bnoordhuis ...ping |
Sorry for the delay. Is there a reason you're using regular expressions? They're not super-readable, an |
@bnoordhuis if we use pattern scanner instead of regular expression, we have to create two scanner pattern functions and have to maintain the If it improves readability, I'll build |
@bnoordhuis , Here is the comparison code with and without regular expression. I'll update PR if literal version looks good to you. // ------------------------------------------------------------------------
// With regex
// ------------------------------------------------------------------------
const unicodePointLikePattern = /\\u{([^}]+)}/mg;
const unicodeLikePattern = /\\u(.{0,4})/mg;
const hexLikePattern = /\\x(.{0,2})/mg;
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// with simple scanner
// ------------------------------------------------------------------------
class LiteralPattern {
constructor(prefix, suffixOrMaxLength) {
this._prefix = prefix;
this._prefixLength = prefix.length;
if (typeof suffixOrMaxLength === 'string') {
this._suffix = suffixOrMaxLength;
this._suffixLength = suffixOrMaxLength.length;
} else {
this._maxLength = suffixOrMaxLength;
}
this.lastIndex = 0;
}
exec(input) {
input = input.substring(this.lastIndex);
let idx = input.indexOf(this._prefix);
if (idx !== -1) {
let start = this._prefixLength + idx;
if (this._suffix) {
input = input.substring(start);
let end = input.indexOf(this._suffix);
if (end !== -1) {
this.lastIndex += start + end + this._suffixLength;
return [null, input.substring(0, end)];
}
} else {
this.lastIndex += start + this._maxLength;
return [null, input.substring(this._prefixLength + idx, this._maxLength)];
}
}
this.lastIndex = 0;
return null;
}
}
const unicodePointLikePattern = new LiteralPattern('\\u{', '}');
const unicodeLikePattern = new LiteralPattern('\\u', 4);
const hexLikePattern = new LiteralPattern('\\x', 2);
// ------------------------------------------------------------------------ |
@bnoordhuis shall I update PR with pattern scanner? |
880db4f
to
82271de
Compare
@bnoordhuis Updated PR with simple pattern scanner. |
@bnoordhuis ... ping |
82271de
to
9a9c021
Compare
@bnoordhuis Fixed merge conflicts! |
Ping @bnoordhuis |
7da4fd4
to
c7066fb
Compare
Working fine in ~ 🙈 ₹ node
> '\u{110000}'
SyntaxError: Unexpected token ILLEGAL
at Object.exports.createScript (vm.js:47:10)
at REPLServer.defaultEval (repl.js:255:25)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:489:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:188:7)
at REPLServer.Interface._onLine (readline.js:232:10)
at REPLServer.Interface._line (readline.js:574:8)
at REPLServer.Interface._ttyWrite (readline.js:851:14)
> process.version
'v6.3.0' |
Fix for #3971