Module | Gem::Text |
In: |
lib/rubygems/text.rb
|
A collection of text-wrangling methods
Remove any non-printable characters and make the text suitable for printing.
# File lib/rubygems/text.rb, line 11 11: def clean_text(text) 12: text.gsub(/[\000-\b\v-\f\016-\037\177]/, ".".freeze) 13: end
Wraps text to wrap characters and optionally indents by indent characters
# File lib/rubygems/text.rb, line 25 25: def format_text(text, wrap, indent=0) 26: result = [] 27: work = clean_text(text) 28: 29: while work.length > wrap do 30: if work =~ /^(.{0,#{wrap}})[ \n]/ then 31: result << $1.rstrip 32: work.slice!(0, $&.length) 33: else 34: result << work.slice!(0, wrap) 35: end 36: end 37: 38: result << work if work.length.nonzero? 39: result.join("\n").gsub(/^/, " " * indent) 40: end
This code is based directly on the Text gem implementation Returns a value representing the "cost" of transforming str1 into str2
# File lib/rubygems/text.rb, line 44 44: def levenshtein_distance str1, str2 45: s = str1 46: t = str2 47: n = s.length 48: m = t.length 49: max = n/2 50: 51: return m if (0 == n) 52: return n if (0 == m) 53: return n if (n - m).abs > max 54: 55: d = (0..m).to_a 56: x = nil 57: 58: n.times do |i| 59: e = i+1 60: 61: m.times do |j| 62: cost = (s[i] == t[j]) ? 0 : 1 63: x = [ 64: d[j+1] + 1, # insertion 65: e + 1, # deletion 66: d[j] + cost # substitution 67: ].min 68: d[j] = e 69: e = x 70: end 71: 72: d[m] = x 73: end 74: 75: return x 76: end
# File lib/rubygems/text.rb, line 15 15: def truncate_text(text, description, max_length = 100_000) 16: raise ArgumentError, "max_length must be positive" unless max_length > 0 17: return text if text.size <= max_length 18: "Truncating #{description} to #{max_length.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse} characters:\n" + text[0, max_length] 19: end