* perf: apply perfpsrint linter (#33967) * perf: apply perfpsrint linter * further simplifications * improved TestParseHashtags coverage * more simplifications * simplify renderBlockHTML further --------- Co-authored-by: Jesse Hallam <jesse@mattermost.com> * Fixed a bad merge --------- Co-authored-by: Catena cyber <35799796+catenacyber@users.noreply.github.com> Co-authored-by: Jesse Hallam <jesse@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
183e6c4a07
Коммит
b3d6c0c564
@@ -717,8 +717,8 @@ var (
|
||||
func ParseHashtags(text string) (string, string) {
|
||||
words := strings.Fields(text)
|
||||
|
||||
hashtagString := ""
|
||||
plainString := ""
|
||||
var hashtagStringSb strings.Builder
|
||||
var plainString strings.Builder
|
||||
for _, word := range words {
|
||||
// trim off surrounding punctuation
|
||||
word = puncStart.ReplaceAllString(word, "")
|
||||
@@ -728,11 +728,12 @@ func ParseHashtags(text string) (string, string) {
|
||||
word = hashtagStart.ReplaceAllString(word, "#")
|
||||
|
||||
if validHashtag.MatchString(word) {
|
||||
hashtagString += " " + word
|
||||
hashtagStringSb.WriteString(" " + word)
|
||||
} else {
|
||||
plainString += " " + word
|
||||
plainString.WriteString(" " + word)
|
||||
}
|
||||
}
|
||||
hashtagString := hashtagStringSb.String()
|
||||
|
||||
if len(hashtagString) > 1000 {
|
||||
hashtagString = hashtagString[:999]
|
||||
@@ -744,7 +745,7 @@ func ParseHashtags(text string) (string, string) {
|
||||
}
|
||||
}
|
||||
|
||||
return strings.TrimSpace(hashtagString), strings.TrimSpace(plainString)
|
||||
return strings.TrimSpace(hashtagString), strings.TrimSpace(plainString.String())
|
||||
}
|
||||
|
||||
func ClearMentionTags(post string) string {
|
||||
|
||||
@@ -541,10 +541,58 @@ func TestStringArray_Equal(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseHashtags(t *testing.T) {
|
||||
for input, output := range hashtags {
|
||||
o, _ := ParseHashtags(input)
|
||||
require.Equal(t, o, output, "failed to parse hashtags from input="+input+" expected="+output+" actual="+o)
|
||||
}
|
||||
t.Run("basic hashtag extraction", func(t *testing.T) {
|
||||
for input, output := range hashtags {
|
||||
o, _ := ParseHashtags(input)
|
||||
require.Equal(t, o, output, "failed to parse hashtags from input="+input+" expected="+output+" actual="+o)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("long hashtag string truncation", func(t *testing.T) {
|
||||
// Test case where hashtag string exceeds 1000 characters with a space to truncate at
|
||||
longHashtags := "#test " + strings.Repeat("#verylonghashtag ", 50)
|
||||
hashtagString, plainString := ParseHashtags(longHashtags)
|
||||
require.NotEmpty(t, hashtagString)
|
||||
require.LessOrEqual(t, len(hashtagString), 1000)
|
||||
require.Empty(t, plainString)
|
||||
// Ensure it truncated at a space
|
||||
require.NotEqual(t, "", hashtagString)
|
||||
require.True(t, hashtagString[len(hashtagString)-1] != ' ')
|
||||
})
|
||||
|
||||
t.Run("long hashtag string truncation without spaces", func(t *testing.T) {
|
||||
// Test case where hashtag string exceeds 1000 characters with no space after position 999
|
||||
// Create a single very long hashtag that will be truncated
|
||||
veryLongHashtag := "#" + strings.Repeat("a", 1010)
|
||||
hashtagString, plainString := ParseHashtags(veryLongHashtag)
|
||||
// Should be empty because no space was found to truncate at
|
||||
require.Equal(t, "", hashtagString)
|
||||
require.Empty(t, plainString)
|
||||
})
|
||||
|
||||
t.Run("plain text extraction", func(t *testing.T) {
|
||||
hashtagString, plainString := ParseHashtags("hello #world this is #test plain text")
|
||||
require.Equal(t, "#world #test", hashtagString)
|
||||
require.Equal(t, "hello this is plain text", plainString)
|
||||
})
|
||||
|
||||
t.Run("only plain text", func(t *testing.T) {
|
||||
hashtagString, plainString := ParseHashtags("no hashtags here")
|
||||
require.Empty(t, hashtagString)
|
||||
require.Equal(t, "no hashtags here", plainString)
|
||||
})
|
||||
|
||||
t.Run("only hashtags", func(t *testing.T) {
|
||||
hashtagString, plainString := ParseHashtags("#one #two #three")
|
||||
require.Equal(t, "#one #two #three", hashtagString)
|
||||
require.Empty(t, plainString)
|
||||
})
|
||||
|
||||
t.Run("empty string", func(t *testing.T) {
|
||||
hashtagString, plainString := ParseHashtags("")
|
||||
require.Empty(t, hashtagString)
|
||||
require.Empty(t, plainString)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIsValidAlphaNum(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user