]> git.cworth.org Git - sup/blobdiff - lib/sup/util.rb
bugfix: index label parsing code incorrect
[sup] / lib / sup / util.rb
index 9909022ffa9b0b1c5796f4fbdff925087dec2519..c54a2c0db2b47efdc547000cc0ed9f8974536960 100644 (file)
@@ -188,11 +188,6 @@ class String
     ret
   end
 
-  ## one of the few things i miss from perl
-  def ucfirst
-    self[0 .. 0].upcase + self[1 .. -1]
-  end
-
   ## a very complicated regex found on teh internets to split on
   ## commas, unless they occurr within double quotes.
   def split_on_commas
@@ -276,6 +271,11 @@ class String
   def normalize_whitespace
     gsub(/\t/, "    ").gsub(/\r/, "")
   end
+
+  ## takes a space-separated list of words, and returns an array of symbols.
+  ## typically used in Sup for translating Ferret's representation of a list
+  ## of labels (a string) to an array of label symbols.
+  def symbolistize; split.map { |x| x.intern } end
 end
 
 class Numeric
@@ -617,3 +617,21 @@ class FinishLine
     @m.synchronize { !@over && @over = true }
   end
 end
+
+class Iconv
+  def self.easy_decode target, charset, text
+    return text if charset =~ /^(x-unknown|unknown[-_ ]?8bit|ascii[-_ ]?7[-_ ]?bit)$/i
+    charset = case charset
+                when /UTF[-_ ]?8/i: "utf-8"
+                when /(iso[-_ ])?latin[-_ ]?1$/i: "ISO-8859-1"
+                when /iso[-_ ]?8859[-_ ]?15/i: 'ISO-8859-15'
+                when /unicode[-_ ]1[-_ ]1[-_ ]utf[-_]7/i: "utf-7"
+                else charset
+              end
+
+    # Convert:
+    #
+    # Remember - Iconv.open(to, from)!
+    Iconv.iconv(target + "//IGNORE", charset, text + " ").join[0 .. -2]
+  end
+end