From 0fa8dd2a3edd55274f6a7ef2a7394acb6a92879b Mon Sep 17 00:00:00 2001 From: zhing Date: Sun, 12 Jul 2015 13:22:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?add=20validate:len=3D=3F=20support=20for=20?= =?UTF-8?q?utf-8?= --- baked_in.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baked_in.go b/baked_in.go index ed69eb3..9ccfa9e 100644 --- a/baked_in.go +++ b/baked_in.go @@ -546,7 +546,7 @@ func hasLengthOf(top interface{}, current interface{}, field interface{}, param case reflect.String: p := asInt(param) - return int64(len(st.String())) == p + return int64(len([]rune(st.String()))) == p case reflect.Slice, reflect.Map, reflect.Array: p := asInt(param) From 89e9d9731d58be7eff9934ac79f417ec63b79287 Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Mon, 13 Jul 2015 14:21:06 -0400 Subject: [PATCH 2/2] update to use utf8.RuneCountInString for string length comparisons updated baked in functions: hasLengthOf isGt isGte isLt isLte to use utf8.RuneCountInString for string length comparisons, not counting multi-bye characters but runes in string length comparisons. --- baked_in.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/baked_in.go b/baked_in.go index 701757c..0e394bd 100644 --- a/baked_in.go +++ b/baked_in.go @@ -592,7 +592,7 @@ func isGte(top interface{}, current interface{}, field interface{}, param string case reflect.String: p := asInt(param) - return int64(len(st.String())) >= p + return int64(utf8.RuneCountInString(st.String())) >= p case reflect.Slice, reflect.Map, reflect.Array: p := asInt(param) @@ -637,7 +637,7 @@ func isGt(top interface{}, current interface{}, field interface{}, param string) case reflect.String: p := asInt(param) - return int64(len(st.String())) > p + return int64(utf8.RuneCountInString(st.String())) > p case reflect.Slice, reflect.Map, reflect.Array: p := asInt(param) @@ -681,7 +681,7 @@ func hasLengthOf(top interface{}, current interface{}, field interface{}, param case reflect.String: p := asInt(param) - return int64(len([]rune(st.String()))) == p + return int64(utf8.RuneCountInString(st.String())) == p case reflect.Slice, reflect.Map, reflect.Array: p := asInt(param) @@ -875,7 +875,7 @@ func isLte(top interface{}, current interface{}, field interface{}, param string case reflect.String: p := asInt(param) - return int64(len(st.String())) <= p + return int64(utf8.RuneCountInString(st.String())) <= p case reflect.Slice, reflect.Map, reflect.Array: p := asInt(param) @@ -920,7 +920,7 @@ func isLt(top interface{}, current interface{}, field interface{}, param string) case reflect.String: p := asInt(param) - return int64(len(st.String())) < p + return int64(utf8.RuneCountInString(st.String())) < p case reflect.Slice, reflect.Map, reflect.Array: p := asInt(param)