File tree Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -121,6 +121,7 @@ const (
121121 Debug = 0x0080 // "d"
122122 ECMAScript = 0x0100 // "e"
123123 RE2 = 0x0200 // RE2 (regexp package) compatibility mode
124+ Unicode = 0x0400 // "u"
124125)
125126
126127func (re * Regexp ) RightToLeft () bool {
Original file line number Diff line number Diff line change @@ -858,6 +858,20 @@ func TestECMAScriptXCurlyBraceEscape(t *testing.T) {
858858 }
859859}
860860
861+ func TestEcmaScriptUnicodeRange (t * testing.T ) {
862+ r , err := Compile (`([\u{001a}-\u{ffff}]+)` , ECMAScript | Unicode )
863+ if err != nil {
864+ panic (err )
865+ }
866+ m , err := r .FindStringMatch ("qqqq" )
867+ if err != nil {
868+ panic (err )
869+ }
870+ if m == nil {
871+ t .Fatal ("Expected non-nil, got nil" )
872+ }
873+ }
874+
861875func TestNegateRange (t * testing.T ) {
862876 re := MustCompile (`[\D]` , 0 )
863877 if m , err := re .MatchString ("A" ); err != nil {
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ const (
2222 Debug = 0x0080 // "d"
2323 ECMAScript = 0x0100 // "e"
2424 RE2 = 0x0200 // RE2 compat mode
25+ Unicode = 0x0400 // "u"
2526)
2627
2728func optionFromCode (ch rune ) RegexOptions {
@@ -43,6 +44,8 @@ func optionFromCode(ch rune) RegexOptions {
4344 return Debug
4445 case 'e' , 'E' :
4546 return ECMAScript
47+ case 'u' , 'U' :
48+ return Unicode
4649 default :
4750 return 0
4851 }
@@ -1695,7 +1698,13 @@ func (p *parser) scanCharEscape() (r rune, err error) {
16951698 r , err = p .scanHex (2 )
16961699 }
16971700 case 'u' :
1698- r , err = p .scanHex (4 )
1701+ // ECMAscript suppot \u{HEX} only if `u` is also set
1702+ if p .useOptionE () && p .useOptionU () && p .charsRight () > 0 && p .rightChar (0 ) == '{' {
1703+ p .moveRight (1 )
1704+ return p .scanHexUntilBrace ()
1705+ } else {
1706+ r , err = p .scanHex (4 )
1707+ }
16991708 case 'a' :
17001709 return '\u0007' , nil
17011710 case 'b' :
@@ -1972,6 +1981,11 @@ func (p *parser) useRE2() bool {
19721981 return (p .options & RE2 ) != 0
19731982}
19741983
1984+ // True if U option enabling ECMAScript's Unicode behavior on.
1985+ func (p * parser ) useOptionU () bool {
1986+ return (p .options & Unicode ) != 0
1987+ }
1988+
19751989// True if options stack is empty.
19761990func (p * parser ) emptyOptionsStack () bool {
19771991 return len (p .optionsStack ) == 0
You can’t perform that action at this time.
0 commit comments