From 449510e8b5b9c32b9c68fc5832c38b47eac16ab9 Mon Sep 17 00:00:00 2001 From: jamslinger <80337165+jamslinger@users.noreply.github.com> Date: Sat, 19 Oct 2024 10:30:07 +0200 Subject: [PATCH] Count number of characters in size filter instead of number of bytes --- filters/standard_filters_test.go | 2 ++ values/arrays.go | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/filters/standard_filters_test.go b/filters/standard_filters_test.go index 6a05fa1..b10cbde 100644 --- a/filters/standard_filters_test.go +++ b/filters/standard_filters_test.go @@ -83,6 +83,8 @@ var filterTests = []struct { // sequence (array or string) filters {`"Ground control to Major Tom." | size`, 28}, {`"apples, oranges, peaches, plums" | split: ", " | size`, 4}, + // count chars, not bytes + {`"Straße" | size`, 6}, // string filters {`"Take my protein pills and put my helmet on" | replace: "my", "your"`, "Take your protein pills and put your helmet on"}, diff --git a/values/arrays.go b/values/arrays.go index 618b7e9..05b07fe 100644 --- a/values/arrays.go +++ b/values/arrays.go @@ -2,6 +2,7 @@ package values import ( "reflect" + "unicode/utf8" ) // TODO Length is now only used by the "size" filter. @@ -13,8 +14,10 @@ func Length(value any) int { value = ToLiquid(value) ref := reflect.ValueOf(value) switch ref.Kind() { - case reflect.Array, reflect.Slice, reflect.String: + case reflect.Array, reflect.Slice: return ref.Len() + case reflect.String: + return utf8.RuneCountInString(ref.String()) default: return 0 }