mirror of https://github.com/go-gitea/gitea.git
50 lines
972 B
Go
50 lines
972 B
Go
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package zoekt
|
|
|
|
import "unicode/utf8"
|
|
|
|
// Bitmap used by func special to check whether a character needs to be escaped.
|
|
var specialBytes [16]byte
|
|
|
|
// special reports whether byte b needs to be escaped by QuoteMeta.
|
|
func special(b byte) bool {
|
|
return b < utf8.RuneSelf && specialBytes[b%16]&(1<<(b/16)) != 0
|
|
}
|
|
|
|
func init() {
|
|
for _, b := range []byte(`-:\.+*?()|[]{}^$`) {
|
|
specialBytes[b%16] |= 1 << (b / 16)
|
|
}
|
|
}
|
|
|
|
func QuoteMeta(s string) string {
|
|
// A byte loop is correct because all metacharacters are ASCII.
|
|
var i int
|
|
for i = 0; i < len(s); i++ {
|
|
if special(s[i]) {
|
|
break
|
|
}
|
|
}
|
|
// No meta characters found, so return original string.
|
|
if i >= len(s) {
|
|
return s
|
|
}
|
|
|
|
b := make([]byte, 3*len(s)-2*i)
|
|
copy(b, s[:i])
|
|
j := i
|
|
for ; i < len(s); i++ {
|
|
if special(s[i]) {
|
|
b[j] = '\\'
|
|
j++
|
|
b[j] = '\\'
|
|
j++
|
|
}
|
|
b[j] = s[i]
|
|
j++
|
|
}
|
|
return string(b[:j])
|
|
}
|