validPattern checks if a pattern is valid without using regexp or unicode. Rules: - Components separated by '/' - Each component is non-empty - Only characters A-Z, a-z, 0-9, '.', '-', '_' or '*' - No leading, trailing, or double slashes - Asterisks rules: - '*' cannot be mixed with other characters
(s string)
| 32 | // - '*' cannot be mixed with other characters in the same component |
| 33 | // - there can be no more than two '*' per component |
| 34 | func validPattern(s string) bool { |
| 35 | if len(s) == 0 { |
| 36 | return false |
| 37 | } |
| 38 | |
| 39 | componentLen := 0 |
| 40 | wildcardLen := 0 |
| 41 | |
| 42 | for _, r := range s { |
| 43 | switch { |
| 44 | case r == '/': |
| 45 | if !isValidComponentMatcher(componentLen, wildcardLen) { |
| 46 | return false |
| 47 | } |
| 48 | componentLen = 0 |
| 49 | wildcardLen = 0 |
| 50 | case isValidPatternRune(r): |
| 51 | componentLen++ |
| 52 | if r == '*' { |
| 53 | wildcardLen++ |
| 54 | } |
| 55 | default: |
| 56 | return false |
| 57 | } |
| 58 | } |
| 59 | // Final component |
| 60 | return isValidComponentMatcher(componentLen, wildcardLen) |
| 61 | } |
| 62 | |
| 63 | func isValidComponentMatcher(componentLen, wildcardLen int) bool { |
| 64 | if wildcardLen > 2 { |
no test coverage detected