From 453e6f9851eb5fbc4cf8208d2966b9df2543514d Mon Sep 17 00:00:00 2001 From: douxu Date: Sat, 27 Sep 2025 15:56:46 +0800 Subject: [PATCH] add GetLongestCommonPrefixLength func --- model/object.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/model/object.go b/model/object.go index d378a59..194f16a 100644 --- a/model/object.go +++ b/model/object.go @@ -152,3 +152,19 @@ func getConstantsKeyByLength(inputLen int) string { return constants.RedisAllGridSetKey } } + +// GetLongestCommonPrefixLength define func of get longest common prefix length between two strings +func GetLongestCommonPrefixLength(s1 string, s2 string) int { + // TODO 增加对特殊字符串例如 "" 的处理 + minLen := len(s1) + if len(s2) < minLen { + minLen = len(s2) + } + + for i := 0; i < minLen; i++ { + if s1[i] != s2[i] { + return i + } + } + return minLen +}