2
Hockeypuck - OpenPGP key server
3
Copyright (C) 2012-2014 Casey Marshall
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Affero General Public License as published by
7
the Free Software Foundation, version 3.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU Affero General Public License for more details.
14
You should have received a copy of the GNU Affero General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
18
// Package util contains a few commonly used utility functions.
28
const MIN_KEYWORD_LEN = 3
30
func Reverse(s string) string {
32
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
33
runes[i], runes[j] = runes[j], runes[i]
38
var UserIdRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\S.*\b)?\s*(\([^(]+\))?\s*(<[^>]+>)?$`)
40
func isUserDelim(c rune) bool {
41
return !unicode.IsLetter(c) && !unicode.IsDigit(c)
44
// Split a user ID string into fulltext searchable keywords.
45
func SplitUserId(id string) (keywords []string) {
46
matches := UserIdRegex.FindStringSubmatch(id)
48
match := keywordNormalize(matches[1])
49
if len(match) >= MIN_KEYWORD_LEN {
50
keywords = append(keywords, match)
54
match := keywordNormalize(strings.Trim(matches[2], "()"))
55
if len(match) >= MIN_KEYWORD_LEN {
56
keywords = append(keywords, match)
60
match := strings.ToLower(strings.Trim(matches[3], "<>"))
61
if len(match) >= MIN_KEYWORD_LEN {
62
keywords = append(keywords, match)
68
func keywordNormalize(s string) string {
70
for _, s := range strings.FieldsFunc(s, isUserDelim) {
71
s = strings.ToLower(strings.TrimFunc(s, isUserDelim))
73
fields = append(fields, s)
76
return strings.Join(fields, " ")
79
func CleanUtf8(s string) string {
82
if r == utf8.RuneError {
85
if r < 0x20 || r == 0x7f {
88
runes = append(runes, r)