-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add regex support #13
Conversation
I'm sorry (and not to be rude...), but I'm really struggling to see the point of this functionality. Can you please explain the use-case for searching a regular expression? Let me rephrase that: Can you please give me an example (or examples) of what someone may use a regular expression for? |
Certainly. Here are some examples:
Since adding each letter to the search term increasing the time exponentially, we want to give the user maximum flexibility in finding the term, or terms, they are looking for. See also warner/wireguard-vanity-address#23 |
I changed this to draft, as I think we should add some of the above to the readme, as I'm sure if you've questioned its usefulness, others will to. Also, I think it's important to stop users from creating regex patterns that would never match. So, for example: |
As defer defers to the end of the function, not the end of the block. See https://blog.learngoprogramming.com/gotchas-of-defer-in-go-1-8d070894cb01
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely would not do any if len(..)
for either slices as this has to count both slices for every calculation, nor would I do a separate mutex for the string & regex wordmaps.
I would do something like:
// Allow only one routine at a time to avoid
// "concurrent map iteration and map write"
c.mapMutex.Lock()
defer c.mapMutex.Unlock()
for w, count := range c.WordMap {
if count == 0 {
continue
}
completed = false
if strings.HasPrefix(matchKey, w) {
c.WordMap[w] = count - 1
cb(Pair{Private: k.String(), Public: pub})
}
}
for w, count := range c.RegexpMap {
if count == 0 {
continue
}
completed = false
if w.MatchString(matchKey) {
c.RegexpMap[w] = count - 1
cb(Pair{Private: k.String(), Public: pub})
}
}
Also in if stripped != sword {
regex, err := regexp.Compile(sword)
if err != nil {
fmt.Printf("Invalid regular expression: %s\n", sword)
os.Exit(2)
}
c.RegexpMap[regex] = options.LimitResults
fmt.Printf("Cannot calculate probability for a regular expression: %s\n", sword)
} else {
c.WordMap[sword] = options.LimitResults
probability := keygen.CalculateProbability(stripped, options.CaseSensitive)
estimate64 := int64(speed) * probability
estimate := time.Duration(estimate64)
fmt.Printf("Probability for \"%s\": 1 in %s (approx %s per match)\n",
word, keygen.NumberFormat(probability), keygen.HumanizeDuration(estimate))
} |
Also: Output error messages to stderr Only add (?i) to regex if not already there Move regex logic to end of utils.go Trim spaces from search term
@axllent It's ready for review. Let me know your thoughts. Happy to make any changes you deem worthy. |
Thanks awesome, thanks @rasa - I did some testing and it works as I'd expect 👍 |
This has been merged and released in 0.0.9. I also just did a manual change to the README which resolves your other PR. Thanks again for your hard work @rasa! |
Fixes #12.
I tried to write logic that converts regexs such as
(abc|defg)
todefg
, but I gave up, as it's non-trivial. The cost/benefit is not worth it :). Instead, I just note the issue in the readme.