| 42 | } |
| 43 | |
| 44 | func matchCompile(pattern string) (match *Match, err error) { |
| 45 | regex := "" |
| 46 | escaped := false |
| 47 | arr := []byte(pattern) |
| 48 | |
| 49 | for i := 0; i < len(arr); i++ { |
| 50 | if escaped { |
| 51 | escaped = false |
| 52 | switch arr[i] { |
| 53 | case '*', '?', '\\': |
| 54 | regex += "\\" + string(arr[i]) |
| 55 | default: |
| 56 | return nil, fmt.Errorf("Invalid escaped character '%c'", arr[i]) |
| 57 | } |
| 58 | } else { |
| 59 | switch arr[i] { |
| 60 | case '\\': |
| 61 | escaped = true |
| 62 | case '*': |
| 63 | regex += ".*" |
| 64 | case '?': |
| 65 | regex += "." |
| 66 | case '.', '(', ')', '+', '|', '^', '$', '[', ']', '{', '}': |
| 67 | regex += "\\" + string(arr[i]) |
| 68 | default: |
| 69 | regex += string(arr[i]) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if escaped { |
| 75 | return nil, fmt.Errorf("Unterminated escape at end of pattern") |
| 76 | } |
| 77 | |
| 78 | var r *regexp.Regexp |
| 79 | |
| 80 | if r, err = regexp.Compile("^" + regex + "$"); err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | |
| 84 | return &Match{r}, nil |
| 85 | } |