fix(strutil): validate Hamming distance by rune count (#368)

This commit is contained in:
Yusuf Efe authored and GitHub committed 2026-10-23 18:14:41 +08:00
1 parent 99ed115de4
commit 33a7e5bf6d
2 files changed
+6 -4

No files matched your search

+3 -4
View File
@@ -632,12 +632,11 @@ func SubInBetween(str string, start string, end string) string {
// This func returns an error if the input strings are of unequal lengths.
// Play: https://go.dev/play/p/glNdQEA9HUi
func HammingDistance(a, b string) (int, error) {
if len(a) != len(b) {
return -1, errors.New("a length and b length are unequal")
}
ar := []rune(a)
br := []rune(b)
if len(ar) != len(br) {
return -1, errors.New("a length and b length are unequal")
}
var distance int
for i, codepoint := range ar {
+3
View File
@@ -818,6 +818,9 @@ func TestHammingDistance(t *testing.T) {
{"abc", "def", 3},
{"kitten", "sitting", -1},
{"ö", "ü", 1},
{"abc", "你", -1},
{"你", "abc", -1},
{"a", "你", 1},
{"日本語", "日本語", 0},
{"日本語", "語日本", 3},
}