mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
0d7e509613
https://github.com/golangci/golangci-lint/releases/tag/v2.9.0 Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
32 lines
672 B
Go
32 lines
672 B
Go
// Copyright 2025 The OPA Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package levenshtein
|
|
|
|
import (
|
|
"iter"
|
|
"slices"
|
|
|
|
"github.com/agnivade/levenshtein"
|
|
)
|
|
|
|
func ClosestStrings(minDistance int, a string, candidates iter.Seq[string]) []string {
|
|
closestStrings := []string{}
|
|
for c := range candidates {
|
|
levDist := levenshtein.ComputeDistance(a, c)
|
|
switch {
|
|
case levDist < minDistance:
|
|
closestStrings = make([]string, 1, 2)
|
|
closestStrings[0] = c
|
|
minDistance = levDist
|
|
case levDist == minDistance:
|
|
closestStrings = append(closestStrings, c)
|
|
minDistance = levDist
|
|
default:
|
|
continue
|
|
}
|
|
}
|
|
slices.Sort(closestStrings)
|
|
return closestStrings
|
|
}
|