Skip to content

Commit

Permalink
add support for \d and \w
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Mar 17, 2024
1 parent dd2ec10 commit bf11728
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Then `repassgen` binary file will be created in current directory.
- [x] `[:ascii:]` ASCII characters
- [x] [Unicode code points](https://www.regular-expressions.info/unicode.html), like `[\u00e0-\u00ef]{5}`
- [x] Group references `\1`, `\2`, etc
- [x] `\d` Digits
- [x] `\w` Word characters: alphanumeric and underline, same as `[a-zA-Z0-9_]`



# Aditional Features (not part of regexp)
- [x] Combined multiple named/manual character classes, for example:
Expand Down
5 changes: 5 additions & 0 deletions lib/charclass_names.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package passgen

// \w == [a-zA-Z0-9_]
var wordChars = []rune(
`abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_`,
)

var charClasses = map[string][]rune{
// POSIX character classes, https://www.regular-expressions.info/posixbrackets.html
"alpha": []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
Expand Down
27 changes: 27 additions & 0 deletions lib/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,33 @@ func TestGenerate(t *testing.T) {
return true
},
})
test(&genCase{
Pattern: `\d{4}`,
PassLen: [2]int{4, 4},
Entropy: [2]float64{13.2, 13.3},
Validate: func(p string) bool {
for _, c := range p {
if c < '0' || c > '9' {
return false
}
}
return true
},
})
const wordChars = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_`
test(&genCase{
Pattern: `\w{4}`,
PassLen: [2]int{4, 4},
Entropy: [2]float64{23.9, 23.91},
Validate: func(p string) bool {
for _, c := range p {
if !strings.ContainsRune(wordChars, c) {
return false
}
}
return true
},
})
testErr(&genErrCase{
Pattern: `$base64(gh)`,
Error: ` ^ value error: invalid hex number "gh"`,
Expand Down
4 changes: 4 additions & 0 deletions lib/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func lexBackslash(s *State) (LexType, error) {
return nil, s.errorUnknown("incomplete buffer: %s", string(s.buffer))
}
return lexRootUnicodeWide, nil
case 'd':
return processRange(s, []rune("0123456789"))
case 'w':
return processRange(s, wordChars)
}
err := s.addOutputOne(backslashEscape(c))
if err != nil {
Expand Down

0 comments on commit bf11728

Please sign in to comment.