From c7c590bace865e4ea5e5c82db18498779de0f172 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Mon, 28 Apr 2025 06:00:11 +0300 Subject: [PATCH] [#6778] updated the excerpt modifier to properly account for multibyte characters --- CHANGELOG.md | 2 ++ tools/picker/excerpt_modifier.go | 12 ++++++++---- tools/picker/excerpt_modifier_test.go | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47ba5cf4..101ae47e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - Updated the default `COUNT` list request query to use `rowid` when possible to minimize the need of having the `id` field in a covering index. +- Updated the excerpt modifier to properly account for multibyte characters ([#6778](https://github.com/pocketbase/pocketbase/issues/6778)). + ## v0.27.1 diff --git a/tools/picker/excerpt_modifier.go b/tools/picker/excerpt_modifier.go index c17269aa..a60c842f 100644 --- a/tools/picker/excerpt_modifier.go +++ b/tools/picker/excerpt_modifier.go @@ -135,10 +135,14 @@ func (m *excerptModifier) Modify(value any) (any, error) { result := strings.TrimSpace(builder.String()) if len(result) > m.max { - result = strings.TrimSpace(result[:m.max]) - - if m.withEllipsis { - result += "..." + // note: casted to []rune to properly account for multi-byte chars + runes := []rune(result) + if len(runes) > m.max { + result = string(runes[:m.max]) + result = strings.TrimSpace(result) + if m.withEllipsis { + result += "..." + } } } diff --git a/tools/picker/excerpt_modifier_test.go b/tools/picker/excerpt_modifier_test.go index d71927b1..7e5fe70a 100644 --- a/tools/picker/excerpt_modifier_test.go +++ b/tools/picker/excerpt_modifier_test.go @@ -134,6 +134,20 @@ func TestExcerptModifierModify(t *testing.T) { html, plainText, }, + + // multibyte chars + { + "mutibyte chars <= max", + []string{"4", "t"}, + "аб\nв ", + "аб в", + }, + { + "mutibyte chars > max", + []string{"3", "t"}, + "аб\nв ", + "аб...", + }, } for _, s := range scenarios {