Skip to content

Commit

Permalink
use code unit without u flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Feb 14, 2018
1 parent 68fd78e commit 7a008b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,16 @@ export class RegExpValidationState {
this.parser.raise(this.start, `Invalid regular expression: /${this.source}/: ${message}`)
}

// Node.js 0.12/0.10 don't support String.prototype.codePointAt().
codePointAt(i) {
// If u flag is given, this returns the code point at the index (it combines a surrogate pair).
// Otherwise, this returns the code unit of the index (can be a part of a surrogate pair).
at(i) {
const s = this.source
const l = s.length
if (i >= l) {
return -1
}
const c = s.charCodeAt(i)
if (c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) {
if (!this.switchU || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) {
return c
}
return (c << 10) + s.charCodeAt(i + 1) - 0x35FDC00
Expand All @@ -117,18 +118,18 @@ export class RegExpValidationState {
return l
}
const c = s.charCodeAt(i)
if (c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) {
if (!this.switchU || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) {
return i + 1
}
return i + 2
}

current() {
return this.codePointAt(this.pos)
return this.at(this.pos)
}

lookahead() {
return this.codePointAt(this.nextIndex(this.pos))
return this.at(this.nextIndex(this.pos))
}

advance() {
Expand Down
5 changes: 5 additions & 0 deletions test/tests-regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,11 @@ testFail("/[🌷-🌸]/", "Invalid regular expression: /[🌷-🌸]/: Range out
testFail("/[🌷-🌸]/", "Invalid regular expression: /[🌷-🌸]/: Range out of order in character class (1:1)", { ecmaVersion: 2015 })
testFail("/[🌷-🌸]/u", "Invalid regular expression flag (1:1)", { ecmaVersion: 5 })
test("/[🌷-🌸]/u", {}, { ecmaVersion: 2015 })
testFail("/[\\u0000-🌸-\\u0000]/", "Invalid regular expression: /[\\u0000-🌸-\\u0000]/: Range out of order in character class (1:1)", { ecmaVersion: 2015 })
testFail("/[\\u0000-\\ud83c\\udf38-\\u0000]/", "Invalid regular expression: /[\\u0000-\\ud83c\\udf38-\\u0000]/: Range out of order in character class (1:1)", { ecmaVersion: 2015 })
test("/[\\u0000-🌸-\\u0000]/u", {}, { ecmaVersion: 2015 })
test("/[\\u0000-\\u{1f338}-\\u0000]/u", {}, { ecmaVersion: 2015 })
test("/[\\u0000-\\ud83c\\udf38-\\u0000]/u", {}, { ecmaVersion: 2015 })
testFail("/[🌸-🌷]/", "Invalid regular expression: /[🌸-🌷]/: Range out of order in character class (1:1)", { ecmaVersion: 5 })
testFail("/[🌸-🌷]/", "Invalid regular expression: /[🌸-🌷]/: Range out of order in character class (1:1)", { ecmaVersion: 2015 })
testFail("/[🌸-🌷]/u", "Invalid regular expression flag (1:1)", { ecmaVersion: 5 })
Expand Down

0 comments on commit 7a008b1

Please sign in to comment.