aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rubygems/text.rb
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-14 03:30:02 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-14 03:30:02 +0000
commit4de117a61517e839f2c45eaf45d56fc243d6d5b2 (patch)
tree7cb5af7a7eb513e5dddf5e343746b1611e628387 /lib/rubygems/text.rb
parente548c09d429a5136285ea81aed418685359ed124 (diff)
downloadruby-4de117a61517e839f2c45eaf45d56fc243d6d5b2.tar.gz
* lib/rubygems: Update to RubyGems 2.4.1 master(713ab65)
Complete history at: https://github.com/rubygems/rubygems/blob/master/History.txt#L3-L216 * test/rubygems: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47582 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
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