9d110e8b80
Camera and lens can now also be searched by name. Escaping and parsing of albums has been improved so that albums whose names start with and/or contain numbers will be found.
40 lines
717 B
Go
40 lines
717 B
Go
package sanitize
|
|
|
|
import (
|
|
"bytes"
|
|
)
|
|
|
|
// sqlSpecialBytes contains special bytes to escape in SQL search queries.
|
|
var sqlSpecialBytes = []byte{34, 39, 92, 95}
|
|
|
|
// SqlString escapes a string for use in an SQL query.
|
|
func SqlString(s string) string {
|
|
var i int
|
|
for i = 0; i < len(s); i++ {
|
|
if bytes.Contains(sqlSpecialBytes, []byte{s[i]}) {
|
|
break
|
|
}
|
|
}
|
|
|
|
// No special characters found, return original string.
|
|
if i >= len(s) {
|
|
return s
|
|
}
|
|
|
|
b := make([]byte, 2*len(s)-i)
|
|
copy(b, s[:i])
|
|
j := i
|
|
for ; i < len(s); i++ {
|
|
if s[i] < 31 {
|
|
// Ignore control chars.
|
|
continue
|
|
}
|
|
if bytes.Contains(sqlSpecialBytes, []byte{s[i]}) {
|
|
b[j] = '\\'
|
|
j++
|
|
}
|
|
b[j] = s[i]
|
|
j++
|
|
}
|
|
return string(b[:j])
|
|
}
|