# code: # * George Moschovitis # * Anastasios Koutoumanos # * Elias Karakoulakis # # (c) 2004 Navel, all rights reserved. # $Id: string.rb 165 2004-11-18 12:04:04Z gmosx $ require "uri" module N; # = StringUtils # # General string utilities collection. # # === Design: # # Implement as a module to avoid class polution. You can # still Ruby's advanced features to include the module in your # class. Passing the object to act upon allows to check for nil, # which isn't possible if you use self. # # === TODO: # # - implement a method that returns easy to remember # pseudo-random strings # - add aliases for those methods in Kernel. # module StringUtils @@map_to_greeklish = { "á" => "a", "Á" => "A", "Ü" => "a", "¶" => "A", "â" => "b", "Â" => "B", "ã" => "g", "Ã" => "G", "ä" => "d", "Ä" => "D", "å" => "e", "Å" => "E", "Ý" => "e", "Å" => "E", "æ" => "z", "Æ" => "Z", "ç" => "h", "Ç" => "H", "Þ" => "h", "¹" => "H", "è" => "8", "È" => "8", "é" => "i", "É" => "I", "ß" => "i", "º" => "I", "ê" => "k", "Ê" => "K", "ë" => "l", "Ë" => "L", "ì" => "m", "Ì" => "M", "í" => "n", "Í" => "N", "î" => "3", "Î" => "3", "ï" => "o", "Ï" => "O", "ü" => "o", "¼" => "O", "ð" => "p", "Ð" => "P", "ñ" => "r", "Ñ" => "R", "ò" => "s", "ó" => "s", "Ó" => "S", "ô" => "t", "Ô" => "T", "õ" => "y", "Õ" => "Y", "ý" => "y", "¾" => "Y", "ö" => "f", "Ö" => "F", "÷" => "x", "×" => "X", "ø" => "ps","Ø" => "PS", "ù" => "w", "Ù" => "W", "þ" => "w", "¿"=>"W" } # Convert the input string to greeklish. #-- # gmosx, TODO: remove from public distribution #++ # def self.to_greeklish(input) return nil unless input output = "" # gmosx: also parse new lines input.scan(/./m) { |w| c = @@map_to_greeklish[w] output << (c.nil?? w: c) } return output end # Move this in String class? # # Tests a string for a valid value (non nil, not empty) # def self.valid?(string) return (not ((nil == string) or (string.empty?))) end # returns short abstract of long strings (first 'count' # characters, chopped at the nearest word, appended by '...') # force_cutoff: break forcibly at 'count' chars. Does not accept # count < 2. # def self.head(string, count = 128, force_cutoff = false, ellipsis="...") return nil unless string return nil if count < 2 if string.size > count cut_at = force_cutoff ? count : (string.index(' ', count-1) || count) xstring = string.slice(0, cut_at) return xstring.chomp(" ") + ellipsis else return string end end # Apply a set of rules (regular expression matches) to the # string # # === Requirements: # - the rules must be applied in order! So we cannot use a # hash because the ordering is not guaranteed! we use an # array instead. # # === Input: # the string to rewrite # the array containing rule-pairs (match, rewrite) # # === Output: # the rewritten string MATCH = 0 REWRITE = 1 def self.rewrite(string, rules) return nil unless string # gmosx: helps to find bugs raise ArgumentError.new("the rules parameter is nil") unless rules rewritten_string = string.dup for rule in rules rewritten_string.gsub!(rule[MATCH], rule[REWRITE]) end return (rewritten_string or string) end # Enforces a maximum width of a string inside an # html container. If the string exceeds this maximum width # the string gets wraped. # # Not really useful, better use the CSS overflow: hidden # functionality. # # === Input: # the string to be wrapped # the enforced width # the separator used for wrapping # # === Output: # the wrapped string # # === Example: # text = "1111111111111111111111111111111111111111111" # text = wrap(text, 10, " ") # p text # => "1111111111 1111111111 1111111111" # # See the test cases to better understand the behaviour! # def self.wrap(string, width = 20, separator = " ") return nil unless string re = /([^#{separator}]{1,#{width}})/ wrapped_string = string.scan(re).join(separator) return wrapped_string end # Replace dangerours chars in filenames # def self.rationalize_filename(filename) return nil unless filename # gmosx: rationalize a copy!!! (add unit test) xfilename = filename.dup() # gmosx: replace some dangerous chars! xfilename.gsub!(/ /, "-") xfilename.gsub!(/!/, "") xfilename.gsub!(/'/, "") xfilename.gsub!(/\(/, "") xfilename.gsub!(/\)/, "") xfilename = self.to_greeklish(xfilename) return xfilename end # Returns a random string. one possible use is # password initialization. # # === Input: # the maximum length of the string # # === Output: # the random string # def self.random(max_length = 8, char_re = /[\w\d]/) # gmosx: this is a nice example of input parameter checking. # this is NOT a real time called method so we can add this # check. Congrats to the author. raise ArgumentError.new("char_re must be a regular expression!") unless char_re.is_a?(Regexp) string = "" while string.length < max_length ch = rand(255).chr string << ch if ch =~ char_re end return string end # Screen an IP address #-- # gmosx: copied this method from n1, check how it works! # probably deprecate? #++ def self.screen_ip_address(address) if address return address.split(',').collect { |hostip| hostip.gsub(/\.[^\.]*$/, ".*") }.join(', ') else return "*.*.*.*" end end end end # module