-
Notifications
You must be signed in to change notification settings - Fork 15
/
shape.go
57 lines (44 loc) · 899 Bytes
/
shape.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
package lingo
import (
"bytes"
"unicode"
)
// Shape represents the shape of a word. It's currently implemented as a string
type Shape string
func (l Lexeme) Shape() Shape {
s := l.Value
if len(s) > 50 {
return Shape("Long")
}
var buf bytes.Buffer
previousCharShape := ' '
currentCharShape := ' '
sequence := 0
for _, c := range s {
switch {
case unicode.IsLetter(c):
if unicode.IsUpper(c) {
currentCharShape = 'X'
} else {
currentCharShape = 'x'
}
case unicode.IsDigit(c):
currentCharShape = 'd'
case l.LexemeType == URI:
return Shape("URI")
default:
currentCharShape = c
}
if previousCharShape == currentCharShape {
sequence++
} else {
sequence = 0 // reset the sequence
previousCharShape = currentCharShape
}
if sequence < 4 {
buf.WriteRune(currentCharShape)
}
}
retVal := buf.String()
return Shape(retVal)
}