lib/glue/string.rb in og-0.9.5 vs lib/glue/string.rb in og-0.10.0
- old
+ new
@@ -1,19 +1,15 @@
-# code:
# * George Moschovitis <gm@navel.gr>
# * Anastasios Koutoumanos <ak@navel.gr>
# * Elias Karakoulakis <ekarak@ktismata.com>
-#
-# (c) 2004 Navel, all rights reserved.
-# $Id: string.rb 202 2005-01-17 10:44:13Z gmosx $
+# (c) 2004-2005 Navel, all rights reserved.
+# $Id: string.rb 259 2005-02-15 08:54:54Z gmosx $
require "uri"
module N;
-# = StringUtils
-#
# General string utilities collection.
#
# === Design:
#
# Implement as a module to avoid class polution. You can
@@ -24,56 +20,13 @@
# === 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)
@@ -82,11 +35,11 @@
# 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
@@ -118,11 +71,11 @@
def self.rewrite(string, rules)
return nil unless string
# gmosx: helps to find bugs
- raise ArgumentError.new("the rules parameter is nil") unless rules
+ 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])
@@ -150,50 +103,51 @@
# 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
- #
+=begin
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)
+ # xfilename = self.to_greeklish(xfilename)
return xfilename
end
-
+=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)
+ 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
@@ -201,24 +155,8 @@
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
-
+end