X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup%2Futil.rb;h=f99e1c17c79a3add8d5da43901c6b161d5c7170d;hb=46f8e5116f38c8248fdc8553db18f8d2132a1f46;hp=f71094e4d8120bba2d649abc4e3f2465a8977d8d;hpb=57dea7ad93bb15491cd33281d933ca4ef6440e45;p=sup diff --git a/lib/sup/util.rb b/lib/sup/util.rb index f71094e..f99e1c1 100644 --- a/lib/sup/util.rb +++ b/lib/sup/util.rb @@ -2,6 +2,7 @@ require 'thread' require 'lockfile' require 'mime/types' require 'pathname' +require 'set' ## time for some monkeypatching! class Lockfile @@ -91,7 +92,7 @@ end class Range ## only valid for integer ranges (unless I guess it's exclusive) - def size + def size last - first + (exclude_end? ? 0 : 1) end end @@ -176,7 +177,7 @@ class String ## nasty multibyte hack for ruby 1.8. if it's utf-8, split into chars using ## the utf8 regex and count those. otherwise, use the byte length. def display_length - if $encoding == "UTF-8" + if $encoding == "UTF-8" || $encoding == "utf8" scan(/./u).size else size @@ -216,8 +217,8 @@ class String newpos = case state when :escaped_instring, :escaped_outstring then pos else index(/[,"\\]/, pos) - end - + end + if newpos char = self[newpos] else @@ -289,10 +290,12 @@ class String end 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 + ## takes a 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. + ## + ## split_on will be passed to String#split, so you can leave this nil for space. + def to_set_of_symbols split_on=nil; Set.new split(split_on).map { |x| x.strip.intern } end end class Numeric @@ -420,10 +423,6 @@ class Array def last= e; self[-1] = e end def nonempty?; !empty? end - - def to_set_of_symbols - map { |x| x.is_a?(Symbol) ? x : x.intern }.uniq - end end class Time @@ -497,19 +496,20 @@ class Time end end -## simple singleton module. far less complete and insane than the ruby -## standard library one, but automatically forwards methods calls and -## allows for constructors that take arguments. +## simple singleton module. far less complete and insane than the ruby standard +## library one, but it automatically forwards methods calls and allows for +## constructors that take arguments. ## -## You must have #initialize call "self.class.i_am_the_instance self" -## at some point or everything will fail horribly. +## classes that inherit this can define initialize. however, you cannot call +## .new on the class. To get the instance of the class, call .instance; +## to create the instance, call init. module Singleton module ClassMethods def instance; @instance; end def instantiated?; defined?(@instance) && !@instance.nil?; end def deinstantiate!; @instance = nil; end def method_missing meth, *a, &b - raise "no instance defined!" unless defined? @instance + raise "no #{name} instance defined in method call to #{meth}!" unless defined? @instance ## if we've been deinstantiated, just drop all calls. this is ## useful because threads that might be active during the @@ -519,13 +519,14 @@ module Singleton @instance.send meth, *a, &b end - def i_am_the_instance o + def init *args raise "there can be only one! (instance)" if defined? @instance - @instance = o + @instance = new(*args) end end def self.included klass + klass.private_class_method :allocate, :new klass.extend ClassMethods end end @@ -544,7 +545,7 @@ class Recoverable def has_errors?; !@error.nil?; end def method_missing m, *a, &b; __pass m, *a, &b end - + def id; __pass :id; end def to_s; __pass :to_s; end def to_yaml x; __pass :to_yaml, x; end @@ -653,7 +654,7 @@ class Iconv begin Iconv.iconv(target + "//IGNORE", charset, text + " ").join[0 .. -2] rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::InvalidCharacter, Iconv::IllegalSequence => e - Redwood::log "warning: error (#{e.class.name}) decoding text from #{charset} to #{target}: #{text[0 ... 20]}" + warn "couldn't transcode text from #{charset} to #{target} (\"#{text[0 ... 20]}\"...) (got #{e.message}); using original as is" text end end