aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rubygems/text.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rubygems/text.rb')
-rw-r--r--lib/rubygems/text.rb20
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/rubygems/text.rb b/lib/rubygems/text.rb
index 793cd953cb..8b26bebec8 100644
--- a/lib/rubygems/text.rb
+++ b/lib/rubygems/text.rb
@@ -26,6 +26,16 @@ module Gem::Text
result.join("\n").gsub(/^/, " " * indent)
end
+ def min3 a, b, c # :nodoc:
+ if a < b && a < c
+ a
+ elsif b < a && b < c
+ b
+ else
+ c
+ end
+ end
+
# This code is based directly on the Text gem implementation
# Returns a value representing the "cost" of transforming str1 into str2
def levenshtein_distance str1, str2
@@ -42,16 +52,16 @@ module Gem::Text
d = (0..m).to_a
x = nil
- n.times do |i|
+ str1.each_char.each_with_index do |char1,i|
e = i+1
- m.times do |j|
- cost = (s[i] == t[j]) ? 0 : 1
- x = [
+ str2.each_char.each_with_index do |char2,j|
+ cost = (char1 == char2) ? 0 : 1
+ x = min3(
d[j+1] + 1, # insertion
e + 1, # deletion
d[j] + cost # substitution
- ].min
+ )
d[j] = e
e = x
end