87 lines
1.4 KiB
Go
87 lines
1.4 KiB
Go
package decanter
|
|
|
|
import "strings"
|
|
|
|
type Decanter map[rune]byte
|
|
|
|
const abcs = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
func New() Decanter {
|
|
out := Decanter(map[rune]byte{})
|
|
return out
|
|
}
|
|
|
|
func (D Decanter) AddAlphabets(as []string) Decanter {
|
|
for _, a := range as {
|
|
D.AddAlphabet(a)
|
|
}
|
|
return D
|
|
}
|
|
func (D Decanter) AddAlphabet(a string) {
|
|
idx := 0
|
|
for _, r := range a {
|
|
if r != '_' && r != rune(abcs[idx]) {
|
|
D[r] = abcs[idx]
|
|
}
|
|
idx = idx + 1
|
|
if idx > 25 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
func (D Decanter) AddLetter(b byte, r rune) {
|
|
D[r] = b
|
|
}
|
|
|
|
func (D Decanter) AddLetters(b byte, rs []rune) {
|
|
for _, r := range rs {
|
|
D[r] = b
|
|
}
|
|
}
|
|
|
|
func (D Decanter) DecantTransform(s string) []string {
|
|
forms := []string{
|
|
D.DecantString(s),
|
|
D.DecantStringStrip(s, ' '),
|
|
D.DecantStringNoRepeat(s),
|
|
}
|
|
return forms
|
|
}
|
|
|
|
func (D Decanter) Decant(r rune) rune {
|
|
if _, ok := D[r]; ok {
|
|
return rune(D[r])
|
|
}
|
|
return r
|
|
}
|
|
|
|
func (D Decanter) DecantString(s string) string {
|
|
b := new(strings.Builder)
|
|
for _, v := range s {
|
|
b.WriteRune(D.Decant(v))
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func (D Decanter) DecantStringStrip(s string, strip rune) string {
|
|
b := new(strings.Builder)
|
|
for _, v := range s {
|
|
if v != strip {
|
|
b.WriteRune(D.Decant(v))
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func (D Decanter) DecantStringNoRepeat(s string) string {
|
|
b := new(strings.Builder)
|
|
last := ' '
|
|
for _, v := range s {
|
|
if v != last {
|
|
b.WriteRune(D.Decant(v))
|
|
}
|
|
v = last
|
|
}
|
|
return b.String()
|
|
}
|