Skip to content

Commit

Permalink
fixes dlclark#65 add support for \pX syntax for single-char code classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dlclark committed May 15, 2023
1 parent 014f217 commit 03d34d8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
33 changes: 33 additions & 0 deletions regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,3 +1296,36 @@ func TestGoodReverseOrderMessage(t *testing.T) {
t.Fatalf("expected %q got %q", expected, err.Error())
}
}

func TestParseShortSlashP(t *testing.T) {
re := MustCompile(`[!\pL\pN]{1,}`, 0)
m, err := re.FindStringMatch("this23! is a! test 1a 2b")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if m.String() != "this23!" {
t.Fatalf("Expected match")
}
}

func TestParseShortSlashNegateP(t *testing.T) {
re := MustCompile(`\PNa`, 0)
m, err := re.FindStringMatch("this is a test 1a 2b")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if m.String() != " a" {
t.Fatalf("Expected match")
}
}

func TestParseShortSlashPEnd(t *testing.T) {
re := MustCompile(`\pN`, 0)
m, err := re.FindStringMatch("this is a test 1a 2b")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if m.String() != "1" {
t.Fatalf("Expected match")
}
}
13 changes: 12 additions & 1 deletion syntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,17 @@ func (p *parser) scanBasicBackslash(scanOnly bool) (*regexNode, error) {

// Scans X for \p{X} or \P{X}
func (p *parser) parseProperty() (string, error) {
// RE2 and PCRE supports \pX syntax (no {} and only 1 letter unicode cats supported)
// since this is purely additive syntax it's not behind a flag
if p.charsRight() >= 1 && p.rightChar(0) != '{' {
ch := string(p.moveRightGetChar())
// check if it's a valid cat
if !isValidUnicodeCat(ch) {
return "", p.getErr(ErrUnknownSlashP, ch)
}
return ch, nil
}

if p.charsRight() < 3 {
return "", p.getErr(ErrIncompleteSlashP)
}
Expand Down Expand Up @@ -1427,7 +1438,7 @@ func (p *parser) scanCapname() string {
return string(p.pattern[startpos:p.textpos()])
}

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

0 comments on commit 03d34d8

Please sign in to comment.