mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(strutil): validate Hamming distance by rune count (#368)
This commit is contained in:
1 parent
99ed115de4
commit
33a7e5bf6d
2 files changed
+6
-4
No files matched your search
+3
-4
@@ -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 {
|
||||
|
||||
@@ -818,6 +818,9 @@ func TestHammingDistance(t *testing.T) {
|
||||
{"abc", "def", 3},
|
||||
{"kitten", "sitting", -1},
|
||||
{"ö", "ü", 1},
|
||||
{"abc", "你", -1},
|
||||
{"你", "abc", -1},
|
||||
{"a", "你", 1},
|
||||
{"日本語", "日本語", 0},
|
||||
{"日本語", "語日本", 3},
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user