-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslug.go
65 lines (57 loc) · 1.73 KB
/
slug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package slug provides utility functions for normalizing strings into
// "slugs". Slugs are lower-case, sanitized strings that are safe for use in
// sensitive environments like URLs.
package slug
import (
"code.google.com/p/go.text/unicode/norm"
"unicode"
)
var (
// Replace non-alphanumeric characters with this byte.
Replacement = '-'
// The "safe" set of characters.
alphanum = &unicode.RangeTable{
R16: []unicode.Range16{
{0x0030, 0x0039, 1}, // 0-9
{0x0041, 0x005A, 1}, // A-Z
{0x0061, 0x007A, 1}, // a-z
},
}
// Characters in these ranges will be ignored.
nop = []*unicode.RangeTable{
unicode.Mark,
unicode.Sk, // Symbol - modifier
unicode.Lm, // Letter - modifier
unicode.Cc, // Other - control
unicode.Cf, // Other - format
}
)
// Slug replaces each run of characters which are not ASCII letters or numbers
// with the Replacement character, except for leading or trailing runs. Letters
// will be stripped of diacritical marks and lowercased. Letter or number
// codepoints that do not have combining marks or a lower-cased variant will be
// passed through unaltered.
func Clean(s string) string {
buf := make([]rune, 0, len(s))
replacement := false
for _, r := range norm.NFKD.String(s) {
switch {
case unicode.In(r, alphanum):
buf = append(buf, unicode.ToLower(r))
replacement = true
case unicode.IsOneOf(nop, r):
// skip
case replacement:
buf = append(buf, Replacement)
replacement = false
}
}
// Strip trailing Replacement byte
if i := len(buf) - 1; i >= 0 && buf[i] == Replacement {
buf = buf[:i]
}
return string(buf)
}