-
Notifications
You must be signed in to change notification settings - Fork 1
/
prolongedsoundmarkinputtextplugin.go
80 lines (72 loc) · 2.25 KB
/
prolongedsoundmarkinputtextplugin.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package gosudachi
import (
"fmt"
)
type ProlongedSoundMarkInputTextPluginConfig struct {
ProlongedSoundMarks *[]string
ReplacementSymbol *string
}
type ProlongedSoundMarkInputTextPlugin struct {
config *ProlongedSoundMarkInputTextPluginConfig
prolongedSoundMarkMap map[rune]bool
replacementSymbol []rune
}
func NewProlongedSoundMarkInputTextPlugin(config *ProlongedSoundMarkInputTextPluginConfig) *ProlongedSoundMarkInputTextPlugin {
if config == nil {
config = &ProlongedSoundMarkInputTextPluginConfig{}
}
return &ProlongedSoundMarkInputTextPlugin{
config: config,
prolongedSoundMarkMap: map[rune]bool{},
}
}
func (p *ProlongedSoundMarkInputTextPlugin) GetConfigStruct() interface{} {
if p.config == nil {
p.config = &ProlongedSoundMarkInputTextPluginConfig{}
}
return p.config
}
func (p *ProlongedSoundMarkInputTextPlugin) SetUp() error {
if p.config.ProlongedSoundMarks == nil || len(*p.config.ProlongedSoundMarks) == 0 {
return fmt.Errorf("ProlongedSoundMarkInputTextPlugin: prolongedSoundMarkStrings is not specified")
}
if p.config.ReplacementSymbol == nil {
return fmt.Errorf("ProlongedSoundMarkInputTextPlugin: replacementSymbol is not specified")
}
if p.prolongedSoundMarkMap == nil {
p.prolongedSoundMarkMap = map[rune]bool{}
}
for _, s := range *p.config.ProlongedSoundMarks {
runes := []rune(s)
if len(runes) > 0 {
p.prolongedSoundMarkMap[runes[0]] = true
}
}
p.replacementSymbol = []rune(*p.config.ReplacementSymbol)
p.config = nil
return nil
}
func (p *ProlongedSoundMarkInputTextPlugin) Rewrite(builder *InputTextBuilder) error {
runes := builder.GetText()
runelen := len(runes)
offset := 0
markStartIndex := runelen
isProlongedSoundMark := false
for i := 0; i < runelen; i++ {
_, ok := p.prolongedSoundMarkMap[runes[i]]
if !isProlongedSoundMark && ok {
isProlongedSoundMark = true
markStartIndex = i
} else if isProlongedSoundMark && !ok {
if (i - markStartIndex) > 1 {
builder.Replace(markStartIndex-offset, i-offset, p.replacementSymbol)
offset += i - markStartIndex - 1
}
isProlongedSoundMark = false
}
}
if isProlongedSoundMark && (runelen-markStartIndex) > 1 {
builder.Replace(markStartIndex-offset, runelen-offset, p.replacementSymbol)
}
return nil
}