Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions data/codings.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ func (*ucs2) Decode(data []byte) (string, error) {
return decode(data, tmp.NewDecoder())
}

func (*ucs2) ShouldSplit(text string, octetLimit uint) (shouldSplit bool) {
runeSlice := []rune(text)
return uint(len(runeSlice)*2) > octetLimit
func (c *ucs2) ShouldSplit(text string, octetLimit uint) (shouldSplit bool) {
totalBytes, _ := c.Encode(text)
return uint(len(totalBytes)) > octetLimit
}

func (c *ucs2) EncodeSplit(text string, octetLimit uint) (allSeg [][]byte, err error) {
Expand All @@ -331,25 +331,26 @@ func (c *ucs2) EncodeSplit(text string, octetLimit uint) (allSeg [][]byte, err e
}

allSeg = [][]byte{}
runeSlice := []rune(text)
hextetLim := int(octetLimit / 2) // round down

// hextet = 16 bits, the correct terms should be hexadectet
fr, to := 0, hextetLim
for fr < len(runeSlice) {
if to > len(runeSlice) {
to = len(runeSlice)
}
var runeBytes []byte
var seg []byte

seg, err := c.Encode(string(runeSlice[fr:to]))
for _, r := range text {
runeBytes, err = c.Encode(string(r))
if err != nil {
return nil, err
return
}
allSeg = append(allSeg, seg)

fr, to = to, to+hextetLim
if uint(len(seg)+len(runeBytes)) > octetLimit {
allSeg = append(allSeg, seg)
seg = runeBytes[:]
} else {
seg = append(seg, runeBytes...)
}
}

if len(seg) > 0 {
allSeg = append(allSeg, seg)
}
return
}

Expand Down
26 changes: 26 additions & 0 deletions data/codings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,32 @@ func TestSplit(t *testing.T) {
})
})

t.Run("testEmojiSplitUCS2_N1", func(t *testing.T) {
testEncodingSplit(t, UCS2,
140,
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA😀",
[]string{
"00410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041D83DDE00",
},
[]string{
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA😀",
})
})

t.Run("testEmojiSplitUCS2_N2", func(t *testing.T) {
testEncodingSplit(t, UCS2,
134,
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA😀BBB",
[]string{
"004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041004100410041",
"D83DDE00004200420042",
},
[]string{
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"😀BBB",
})
})

t.Run("testSplitUCS2Empty", func(t *testing.T) {
testEncodingSplit(t, UCS2,
134,
Expand Down