mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
Reject invalidomain with wildcards (#412)
Co-authored-by: Cyril DUPONT <cyd@9bis.com>
This commit is contained in:
@@ -74,3 +74,40 @@ func MakePathPrefixer(prefix string) func(string) string {
|
||||
return prefix + path
|
||||
}
|
||||
}
|
||||
|
||||
// Test if a "s" string match a "p" pattern with wildcards (*, ?)
|
||||
func MatchWithWildcards(p string, s string) bool {
|
||||
runeInput := []rune(s)
|
||||
runePattern := []rune(p)
|
||||
lenInput := len(runeInput)
|
||||
lenPattern := len(runePattern)
|
||||
isMatchingMatrix := make([][]bool, lenInput+1)
|
||||
for i := range isMatchingMatrix {
|
||||
isMatchingMatrix[i] = make([]bool, lenPattern+1)
|
||||
}
|
||||
isMatchingMatrix[0][0] = true
|
||||
if lenPattern > 0 {
|
||||
if runePattern[0] == '*' {
|
||||
isMatchingMatrix[0][1] = true
|
||||
}
|
||||
}
|
||||
for j := 2; j <= lenPattern; j++ {
|
||||
if runePattern[j-1] == '*' {
|
||||
isMatchingMatrix[0][j] = isMatchingMatrix[0][j-1]
|
||||
}
|
||||
|
||||
}
|
||||
for i := 1; i <= lenInput; i++ {
|
||||
for j := 1; j <= lenPattern; j++ {
|
||||
|
||||
if runePattern[j-1] == '*' {
|
||||
isMatchingMatrix[i][j] = isMatchingMatrix[i-1][j] || isMatchingMatrix[i][j-1]
|
||||
}
|
||||
|
||||
if runePattern[j-1] == '?' || runeInput[i-1] == runePattern[j-1] {
|
||||
isMatchingMatrix[i][j] = isMatchingMatrix[i-1][j-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
return isMatchingMatrix[lenInput][lenPattern]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user