Skip to content

Commit 03d34d8

Browse files
committed
fixes #65 add support for \pX syntax for single-char code classes
1 parent 014f217 commit 03d34d8

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

regexp_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -1296,3 +1296,36 @@ func TestGoodReverseOrderMessage(t *testing.T) {
12961296
t.Fatalf("expected %q got %q", expected, err.Error())
12971297
}
12981298
}
1299+
1300+
func TestParseShortSlashP(t *testing.T) {
1301+
re := MustCompile(`[!\pL\pN]{1,}`, 0)
1302+
m, err := re.FindStringMatch("this23! is a! test 1a 2b")
1303+
if err != nil {
1304+
t.Fatalf("Unexpected error: %v", err)
1305+
}
1306+
if m.String() != "this23!" {
1307+
t.Fatalf("Expected match")
1308+
}
1309+
}
1310+
1311+
func TestParseShortSlashNegateP(t *testing.T) {
1312+
re := MustCompile(`\PNa`, 0)
1313+
m, err := re.FindStringMatch("this is a test 1a 2b")
1314+
if err != nil {
1315+
t.Fatalf("Unexpected error: %v", err)
1316+
}
1317+
if m.String() != " a" {
1318+
t.Fatalf("Expected match")
1319+
}
1320+
}
1321+
1322+
func TestParseShortSlashPEnd(t *testing.T) {
1323+
re := MustCompile(`\pN`, 0)
1324+
m, err := re.FindStringMatch("this is a test 1a 2b")
1325+
if err != nil {
1326+
t.Fatalf("Unexpected error: %v", err)
1327+
}
1328+
if m.String() != "1" {
1329+
t.Fatalf("Expected match")
1330+
}
1331+
}

syntax/parser.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,17 @@ func (p *parser) scanBasicBackslash(scanOnly bool) (*regexNode, error) {
13111311

13121312
// Scans X for \p{X} or \P{X}
13131313
func (p *parser) parseProperty() (string, error) {
1314+
// RE2 and PCRE supports \pX syntax (no {} and only 1 letter unicode cats supported)
1315+
// since this is purely additive syntax it's not behind a flag
1316+
if p.charsRight() >= 1 && p.rightChar(0) != '{' {
1317+
ch := string(p.moveRightGetChar())
1318+
// check if it's a valid cat
1319+
if !isValidUnicodeCat(ch) {
1320+
return "", p.getErr(ErrUnknownSlashP, ch)
1321+
}
1322+
return ch, nil
1323+
}
1324+
13141325
if p.charsRight() < 3 {
13151326
return "", p.getErr(ErrIncompleteSlashP)
13161327
}
@@ -1427,7 +1438,7 @@ func (p *parser) scanCapname() string {
14271438
return string(p.pattern[startpos:p.textpos()])
14281439
}
14291440

1430-
//Scans contents of [] (not including []'s), and converts to a set.
1441+
// Scans contents of [] (not including []'s), and converts to a set.
14311442
func (p *parser) scanCharSet(caseInsensitive, scanOnly bool) (*CharSet, error) {
14321443
ch := '\x00'
14331444
chPrev := '\x00'

0 commit comments

Comments
 (0)