You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536
  1. // Use of this source code is governed by a BSD-style
  2. // license that can be found in the LICENSE file.
  3. // Package regexp implements a simple regular expression library.
  4. // QuoteMeta func is copied here to avoid linking the entire Regexp library.
  5. package rubex
  6. func special(c int) bool {
  7. for _, r := range `\.+*?()|[]^$` {
  8. if c == int(r) {
  9. return true
  10. }
  11. }
  12. return false
  13. }
  14. // QuoteMeta returns a string that quotes all regular expression metacharacters
  15. // inside the argument text; the returned string is a regular expression matching
  16. // the literal text. For example, QuoteMeta(`[foo]`) returns `\[foo\]`.
  17. func QuoteMeta(s string) string {
  18. b := make([]byte, 2*len(s))
  19. // A byte loop is correct because all metacharacters are ASCII.
  20. j := 0
  21. for i := 0; i < len(s); i++ {
  22. if special(int(s[i])) {
  23. b[j] = '\\'
  24. j++
  25. }
  26. b[j] = s[i]
  27. j++
  28. }
  29. return string(b[0:j])
  30. }