aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rubygems/text.rb
diff options
context:
space:
mode:
authorryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-01-19 00:08:49 +0000
committerryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-01-19 00:08:49 +0000
commite798ccbacf489a3af2201ae30058ff0ae7f79045 (patch)
tree4147a7834eb88323057fd2120a2ddc96c1eb32ab /lib/rubygems/text.rb
parentd26fb035cae8d351dc238376722c980230dc5fbd (diff)
downloadruby-e798ccbacf489a3af2201ae30058ff0ae7f79045.tar.gz
Import rubygems 1.5.0 (release candidate)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30599 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rubygems/text.rb')
-rw-r--r--lib/rubygems/text.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/rubygems/text.rb b/lib/rubygems/text.rb
index c694e8a9c0..ddf1e820a7 100644
--- a/lib/rubygems/text.rb
+++ b/lib/rubygems/text.rb
@@ -1,3 +1,9 @@
+######################################################################
+# This file is imported from the rubygems project.
+# DO NOT make modifications in this repo. They _will_ be reverted!
+# File a patch instead and assign it to Ryan Davis or Eric Hodel.
+######################################################################
+
require 'rubygems'
##
@@ -26,5 +32,40 @@ module Gem::Text
result.join("\n").gsub(/^/, " " * indent)
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
+ s = str1
+ t = str2
+ n = s.length
+ m = t.length
+ max = n/2
+
+ return m if (0 == n)
+ return n if (0 == m)
+ return n if (n - m).abs > max
+
+ d = (0..m).to_a
+ x = nil
+
+ n.times do |i|
+ e = i+1
+
+ m.times do |j|
+ cost = (s[i] == t[j]) ? 0 : 1
+ x = [
+ d[j+1] + 1, # insertion
+ e + 1, # deletion
+ d[j] + cost # substitution
+ ].min
+ d[j] = e
+ e = x
+ end
+
+ d[m] = x
+ end
+
+ return x
+ end
end