-
Notifications
You must be signed in to change notification settings - Fork 1
/
word.go
50 lines (38 loc) · 1.02 KB
/
word.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
// Package Anonymize is a small and simple package for anonymizing names, e-mails and domains.
//
// Copyright 2022 Emmanuel Ay. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package anonymize
import (
"strings"
"unicode"
)
func processWord(input string, allowSingleToken bool, anontoken rune) string {
if len(input) == 0 || len(strings.TrimSpace(input)) == 0 {
return ""
}
if !allowSingleToken && len(input) == 1 {
return string(anontoken)
}
runeInput := []rune(input)
runeOutput := make([]rune, 0, len(runeInput))
var currentRune rune
var previousRune rune
for {
currentRune = runeInput[0]
if (previousRune == 0) ||
(!unicode.IsUpper(previousRune) && unicode.IsUpper(currentRune)) {
runeOutput = append(runeOutput, currentRune)
} else {
runeOutput = append(runeOutput, anontoken)
}
if len(runeInput) > 1 {
runeInput = runeInput[1:]
previousRune = currentRune
continue
}
break
}
return string(runeOutput)
}