~ubuntu-branches/ubuntu/utopic/hockeypuck/utopic-proposed

« back to all changes in this revision

Viewing changes to util/strings.go

  • Committer: Package Import Robot
  • Author(s): Casey Marshall
  • Date: 2014-04-13 20:06:01 UTC
  • Revision ID: package-import@ubuntu.com-20140413200601-oxdlqn1gy0x8m55u
Tags: 1.0~rel20140413+7a1892a~trusty
Hockeypuck 1.0 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Hockeypuck - OpenPGP key server
 
3
   Copyright (C) 2012-2014  Casey Marshall
 
4
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
// Package util contains a few commonly used utility functions.
 
19
package util
 
20
 
 
21
import (
 
22
        "regexp"
 
23
        "strings"
 
24
        "unicode"
 
25
        "unicode/utf8"
 
26
)
 
27
 
 
28
const MIN_KEYWORD_LEN = 3
 
29
 
 
30
func Reverse(s string) string {
 
31
        runes := []rune(s)
 
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]
 
34
        }
 
35
        return string(runes)
 
36
}
 
37
 
 
38
var UserIdRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\S.*\b)?\s*(\([^(]+\))?\s*(<[^>]+>)?$`)
 
39
 
 
40
func isUserDelim(c rune) bool {
 
41
        return !unicode.IsLetter(c) && !unicode.IsDigit(c)
 
42
}
 
43
 
 
44
// Split a user ID string into fulltext searchable keywords.
 
45
func SplitUserId(id string) (keywords []string) {
 
46
        matches := UserIdRegex.FindStringSubmatch(id)
 
47
        if len(matches) > 1 {
 
48
                match := keywordNormalize(matches[1])
 
49
                if len(match) >= MIN_KEYWORD_LEN {
 
50
                        keywords = append(keywords, match)
 
51
                }
 
52
        }
 
53
        if len(matches) > 2 {
 
54
                match := keywordNormalize(strings.Trim(matches[2], "()"))
 
55
                if len(match) >= MIN_KEYWORD_LEN {
 
56
                        keywords = append(keywords, match)
 
57
                }
 
58
        }
 
59
        if len(matches) > 3 {
 
60
                match := strings.ToLower(strings.Trim(matches[3], "<>"))
 
61
                if len(match) >= MIN_KEYWORD_LEN {
 
62
                        keywords = append(keywords, match)
 
63
                }
 
64
        }
 
65
        return keywords
 
66
}
 
67
 
 
68
func keywordNormalize(s string) string {
 
69
        var fields []string
 
70
        for _, s := range strings.FieldsFunc(s, isUserDelim) {
 
71
                s = strings.ToLower(strings.TrimFunc(s, isUserDelim))
 
72
                if len(s) > 2 {
 
73
                        fields = append(fields, s)
 
74
                }
 
75
        }
 
76
        return strings.Join(fields, " ")
 
77
}
 
78
 
 
79
func CleanUtf8(s string) string {
 
80
        var runes []rune
 
81
        for _, r := range s {
 
82
                if r == utf8.RuneError {
 
83
                        r = '?'
 
84
                }
 
85
                if r < 0x20 || r == 0x7f {
 
86
                        continue
 
87
                }
 
88
                runes = append(runes, r)
 
89
        }
 
90
        return string(runes)
 
91
}