handle multicharacter emojis in textstyleparser

see #413
This commit is contained in:
Bernhard B
2023-10-06 19:11:07 +02:00
parent 8271d53746
commit 07aee21672
2 changed files with 47 additions and 5 deletions

View File

@@ -25,6 +25,22 @@ const (
StrikethroughEnd = 9 StrikethroughEnd = 9
) )
func getUtf16CharacterCount(s string) int {
stringLength := len(s)
if stringLength == 1 {
return 1
}
return stringLength/2
}
func getAdditionalCharacterCount(characterCount int) int {
additionalCharacterCount := characterCount - 1
if additionalCharacterCount > 0 {
return additionalCharacterCount
}
return 0
}
func ParseMarkdownMessage(message string) (string, []string) { func ParseMarkdownMessage(message string) (string, []string) {
textFormat := Normal textFormat := Normal
textFormatBegin := 0 textFormatBegin := 0
@@ -34,6 +50,7 @@ func ParseMarkdownMessage(message string) (string, []string) {
signalCliFormatStrings := []string{} signalCliFormatStrings := []string{}
fullString := "" fullString := ""
lastChar := "" lastChar := ""
additionalCharacterCount := 0
runes := []rune(message) //turn string to slice runes := []rune(message) //turn string to slice
@@ -43,7 +60,7 @@ func ParseMarkdownMessage(message string) (string, []string) {
if lastChar == "*" { if lastChar == "*" {
state = BoldBegin state = BoldBegin
textFormat = Bold textFormat = Bold
textFormatBegin = i - numOfControlChars textFormatBegin = i - numOfControlChars + additionalCharacterCount
textFormatLength = 0 textFormatLength = 0
} else { } else {
state = ItalicEnd state = ItalicEnd
@@ -51,7 +68,7 @@ func ParseMarkdownMessage(message string) (string, []string) {
} else if state == None { } else if state == None {
state = ItalicBegin state = ItalicBegin
textFormat = Italic textFormat = Italic
textFormatBegin = i - numOfControlChars textFormatBegin = i - numOfControlChars + additionalCharacterCount
textFormatLength = 0 textFormatLength = 0
} else if state == BoldBegin { } else if state == BoldBegin {
state = BoldEnd1 state = BoldEnd1
@@ -63,7 +80,7 @@ func ParseMarkdownMessage(message string) (string, []string) {
if state == None { if state == None {
state = MonoSpaceBegin state = MonoSpaceBegin
textFormat = Monospace textFormat = Monospace
textFormatBegin = i - numOfControlChars textFormatBegin = i - numOfControlChars + additionalCharacterCount
textFormatLength = 0 textFormatLength = 0
} else if state == MonoSpaceBegin { } else if state == MonoSpaceBegin {
state = MonoSpaceEnd state = MonoSpaceEnd
@@ -73,7 +90,7 @@ func ParseMarkdownMessage(message string) (string, []string) {
if state == None { if state == None {
state = StrikethroughBegin state = StrikethroughBegin
textFormat = Strikethrough textFormat = Strikethrough
textFormatBegin = i - numOfControlChars textFormatBegin = i - numOfControlChars + additionalCharacterCount
textFormatLength = 0 textFormatLength = 0
} else if state == StrikethroughBegin { } else if state == StrikethroughBegin {
state = StrikethroughEnd state = StrikethroughEnd
@@ -82,11 +99,12 @@ func ParseMarkdownMessage(message string) (string, []string) {
} else { } else {
textFormatLength += 1 textFormatLength += 1
fullString += string(v) fullString += string(v)
additionalCharacterCount += getAdditionalCharacterCount(getUtf16CharacterCount(string(v)))
} }
lastChar = string(v) lastChar = string(v)
if state == ItalicEnd || state == BoldEnd2 || state == MonoSpaceEnd || state == StrikethroughEnd { if state == ItalicEnd || state == BoldEnd2 || state == MonoSpaceEnd || state == StrikethroughEnd {
signalCliFormatStrings = append(signalCliFormatStrings, strconv.Itoa(textFormatBegin)+":"+strconv.Itoa(textFormatLength)+":"+textFormat) signalCliFormatStrings = append(signalCliFormatStrings, strconv.Itoa(textFormatBegin)+":"+strconv.Itoa(textFormatLength + additionalCharacterCount)+":"+textFormat)
state = None state = None
textFormatBegin = 0 textFormatBegin = 0
textFormatLength = 0 textFormatLength = 0

View File

@@ -50,3 +50,27 @@ func TestMonospace(t *testing.T) {
expectMessageEqual(t, message, "This is a monospace and a bold message") expectMessageEqual(t, message, "This is a monospace and a bold message")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"10:9:MONOSPACE", "26:4:BOLD"}) expectFormatStringsEqual(t, signalCliFormatStrings, []string{"10:9:MONOSPACE", "26:4:BOLD"})
} }
func TestMulticharacterEmoji(t *testing.T) {
message, signalCliFormatStrings := ParseMarkdownMessage("👋abcdefg")
expectMessageEqual(t, message, "👋abcdefg")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{})
}
func TestMulticharacterEmojiWithBoldText(t *testing.T) {
message, signalCliFormatStrings := ParseMarkdownMessage("👋**abcdefg**")
expectMessageEqual(t, message, "👋abcdefg")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"2:8:BOLD"})
}
func TestMultipleMulticharacterEmoji(t *testing.T) {
message, signalCliFormatStrings := ParseMarkdownMessage("👋🏾abcdefg")
expectMessageEqual(t, message, "👋🏾abcdefg")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{})
}
func TestMultipleMulticharacterEmojiWithBoldText(t *testing.T) {
message, signalCliFormatStrings := ParseMarkdownMessage("👋🏾**abcdefg**")
expectMessageEqual(t, message, "👋🏾abcdefg")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"4:9:BOLD"})
}