Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Commit 14530d9

Browse files
committed
Refactor emojiSequenceInexes
* Rename emojiSequenceIndexes to FindEmojiIndices * move it into text util pacakge. * improve unit test
1 parent 7b06668 commit 14530d9

File tree

4 files changed

+144
-128
lines changed

4 files changed

+144
-128
lines changed

ui/window.go

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os/exec"
88
"strings"
99
"time"
10-
"unicode"
1110

1211
"github.com/mattn/go-runewidth"
1312
"github.com/mdp/qrterminal/v3"
@@ -1504,40 +1503,6 @@ func (window *Window) prepareMessage(targetChannel *discordgo.Channel, inputText
15041503
return message
15051504
}
15061505

1507-
// emojiSequenceIndexes will find all parts of a string (rune array) that
1508-
// could potentially be emoji sequences and will return them backwards.
1509-
// they'll be returned backwards in order to allow easily manipulating the
1510-
// data without invalidating the following indexes accidentally.
1511-
// Example:
1512-
// Hello :world:, what a :nice: day.
1513-
// would result in
1514-
// []int{22,,27,6,12}
1515-
func emojiSequenceIndexes(runes []rune) []int {
1516-
var sequencesBackwards []int
1517-
for i := len(runes) - 1; i >= 0; i-- {
1518-
if runes[i] == ':' {
1519-
for j := i - 1; j >= 0; j-- {
1520-
char := runes[j]
1521-
if char == ':' {
1522-
//Handle this ':' in the next iteration of the outer loop otherwise
1523-
if j != i-1 {
1524-
sequencesBackwards = append(sequencesBackwards, j, i)
1525-
i = j
1526-
break
1527-
} else {
1528-
break
1529-
}
1530-
} else if unicode.IsSpace(char) {
1531-
i = j
1532-
break
1533-
}
1534-
}
1535-
}
1536-
}
1537-
1538-
return sequencesBackwards
1539-
}
1540-
15411506
// mergeRuneSlices copies the passed rune arrays into a new rune array of the
15421507
// correct size.
15431508
func mergeRuneSlices(a, b, c []rune) *[]rune {
@@ -1556,7 +1521,7 @@ func mergeRuneSlices(a, b, c []rune) *[]rune {
15561521
// For private channels, the channelGuild may be nil.
15571522
func (window *Window) replaceEmojiSequences(channelGuild *discordgo.Guild, message string) string {
15581523
asRunes := []rune(message)
1559-
indexes := emojiSequenceIndexes(asRunes)
1524+
indexes := text.FindEmojiIndices(asRunes)
15601525
INDEX_LOOP:
15611526
for i := 0; i < len(indexes); i += 2 {
15621527
startIndex := indexes[i]

ui/window_test.go

Lines changed: 0 additions & 92 deletions
This file was deleted.

util/text/emoji.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package text
2+
3+
import "unicode"
4+
5+
// FindEmojiIndices will find all parts of a string (rune array) that
6+
// could potentially be emoji sequences and will return them backwards.
7+
// they'll be returned backwards in order to allow easily manipulating the
8+
// data without invalidating the following indexes accidentally.
9+
// Example:
10+
// Hello :world:, what a :nice: day.
11+
// would result in
12+
// []int{22,27,6,12}
13+
func FindEmojiIndices(runes []rune) []int {
14+
var sequencesBackwards []int
15+
for i := len(runes) - 1; i >= 0; i-- {
16+
if runes[i] == ':' {
17+
for j := i - 1; j >= 0; j-- {
18+
char := runes[j]
19+
if char == ':' {
20+
//Handle this ':' in the next iteration of the outer loop otherwise
21+
if j != i-1 {
22+
sequencesBackwards = append(sequencesBackwards, j, i)
23+
i = j
24+
break
25+
} else {
26+
break
27+
}
28+
} else if unicode.IsSpace(char) {
29+
i = j
30+
break
31+
}
32+
}
33+
}
34+
}
35+
36+
return sequencesBackwards
37+
}

util/text/emoji_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package text
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestFindEmojiIndices(t *testing.T) {
9+
type testcase struct {
10+
name string
11+
input string
12+
expected []int
13+
}
14+
tests := []*testcase{
15+
{
16+
name: "empty input",
17+
input: "",
18+
expected: nil,
19+
},
20+
{
21+
name: "spaces",
22+
input: " ",
23+
expected: nil,
24+
},
25+
{
26+
name: "text with outer spaces",
27+
input: " Numma Numma yay ",
28+
expected: nil,
29+
},
30+
{
31+
name: "just single emoji, but with spaces, therefore invaid",
32+
input: ":hel lo:",
33+
expected: nil,
34+
},
35+
{
36+
name: "unclosed sequence",
37+
input: ":hello",
38+
expected: nil,
39+
},
40+
{
41+
name: "unopened sequence",
42+
input: "hello:",
43+
expected: nil,
44+
},
45+
{
46+
name: "tab inbetween sequence",
47+
input: ":hel\tlo:",
48+
expected: nil,
49+
},
50+
{
51+
name: "newline inbetween sequence",
52+
input: ":hel\nlo:",
53+
expected: nil,
54+
},
55+
{
56+
name: "just single emoji",
57+
input: ":hello:",
58+
expected: []int{0, 6},
59+
},
60+
{
61+
name: "two emoji without anything inbetween",
62+
input: ":hello::world:",
63+
expected: []int{7, 13, 0, 6},
64+
},
65+
{
66+
name: "two emoji withspaces inbetween",
67+
input: ":hello: :world:",
68+
expected: []int{9, 15, 0, 6},
69+
},
70+
{
71+
name: "two emoji with text inbetween",
72+
input: ":hello: Lorem Ipsum whatever :world:",
73+
expected: []int{29, 35, 0, 6},
74+
},
75+
{
76+
name: "two valid emoji sequences with single unnecessary double colons in between",
77+
input: ":test:::lol:",
78+
expected: []int{7, 11, 0, 5},
79+
},
80+
{
81+
name: "two valid emoji sequences with even amount of double colons in between",
82+
input: "::test:::lol:",
83+
expected: []int{8, 12, 1, 6},
84+
},
85+
{
86+
name: "two valid emoji sequences with uneven amount of double colons in between",
87+
input: "::test::::lol:",
88+
expected: []int{9, 13, 1, 6},
89+
},
90+
}
91+
92+
for _, test := range tests {
93+
t.Run(test.name, func(t2 *testing.T) {
94+
result := FindEmojiIndices([]rune(test.input))
95+
96+
//We don't care whether it's empty or nil
97+
if len(result) == 0 && len(test.expected) == 0 {
98+
return
99+
}
100+
101+
if !reflect.DeepEqual(result, test.expected) {
102+
t2.Errorf("Got %v, but expected %v", result, test.expected)
103+
}
104+
})
105+
}
106+
}

0 commit comments

Comments
 (0)