lib/setfu.rb in setfu-1.1.0 vs lib/setfu.rb in setfu-1.2.0
- old
+ new
@@ -5,96 +5,102 @@
def entropy=(ent) # entropy only gets bigger, just like the universe!
@entropy = ent unless @entropy > ent
end
- def to_set
- return self
+ def self.uppercase_chars
+ set = Set.new
+ set.set_bits!(2475880041677272402379145216)
+ set.entropy=91
+ return set
end
- def mode
- return @mode
+ def self.lowercase_chars
+ set = Set.new
+ set.set_bits!(10633823807823001954701781295154855936)
+ set.entropy=123
+ return set
end
- def initialize(data=nil)
- @bits = 0
- @entropy = 0
- add!(data) unless data.nil?
- self
+ def self.letter_chars
+ set = Set.new
+ set.set_bits!(10633823810298881996379053697534001152)
+ set.entropy=123
+ return set
end
-
- def zap!
- @bits = 0
- @entropy = 0
- self
+
+ def self.digit_chars
+ set = Set.new
+ set.set_bits!(287948901175001088)
+ set.entropy=58
+ return set
end
- def recalculate_entropy!
- @entropy = 0
- bits = @bits
- num = 1 << 8192
- while(bits > num)
- @entropy += 8192
- bits >>= 8192
- end
- num = 1 << 256
- while(bits > num)
- @entropy += 256
- bits >>= 256
- end
- num = 1 << 16
- while(bits > num)
- @entropy += 16
- bits >>= 16
- end
- while(bits > 0)
- @entropy += 1
- bits >>= 1
- end
- #@entropy += 1
- @entropy
+ def self.parse_chars
+ set = Set.new
+ set.set_bits!(159507359650170349735020301117175103487)
+ set.entropy=127
+ return set
end
-
+
+ def replace(ent)
+ ent = ent.to_set
+ @mode = ent.mode
+ @entropy = ent.entropy
+ @bits = ent.to_i
+ self
+ end
+
def add_parse_chars!
- add! [0..47, 58..64, 91..96, 123..126]
+ # add! [0..47, 58..64, 91..96, 123..126]
+ add! Set.parse_chars
self
end
def add_parse_chars
- dup.add_parse_chars!
+ return Set.parse_chars | self
end
def add_digit_chars!
- add! [48..57]
+ add! Set.digit_chars
self
end
def add_digit_chars
- dup.add_digit_chars!
+ return Set.digit_chars | self
end
def add_uppercase_chars!
- add! [65..90]
+ add! Set.uppercase_chars
self
end
def add_uppercase_chars
- dup.add_uppercase_chars!
+ return Set.uppercase_chars | self
end
def add_lowercase_chars!
- add! [97..122]
+ add! Set.lowercase_chars
self
end
def add_lowercase_chars
- dup.add_lowercase_chars!
+ return Set.lowercase_chars | self
end
+ def add_letter_chars
+ return Set.letter_chars | self
+ end
+
+ def add_letter_chars!
+ add! Set.letter_chars
+ self
+ end
+
def add_opposing_case!
- aa = Set.new.add_lowercase_chars!
- bb = Set.new.add_uppercase_chars!
+ aa = Set.lowercase_chars
+ bb = Set.uppercase_chars
ka = aa & self # subset lower case
kb = bb & self # subset upper case
@bits |= ka.to_i >> 32
@bits |= kb.to_i << 32
self.entropy = 32 + kb.recalculate_entropy! if self.entropy <= 123
@@ -102,22 +108,68 @@
end
def add_opposing_case
dup.add_opposing_case!
end
+
+ def to_set
+ return self
+ end
+
+ def mode
+ return @mode
+ end
+
+ def initialize(data=nil)
+ @bits = 0
+ @entropy = 0
+ add!(data) unless data.nil?
+ self
+ end
+ def zap!
+ @bits = 0
+ @entropy = 0
+ self
+ end
+
+ def recalculate_entropy!
+ @entropy = 0
+ bits = @bits
+ num = 1 << 8192
+ while(bits > num)
+ @entropy += 8192
+ bits >>= 8192
+ end
+ num = 1 << 256
+ while(bits > num)
+ @entropy += 256
+ bits >>= 256
+ end
+ num = 1 << 16
+ while(bits > num)
+ @entropy += 16
+ bits >>= 16
+ end
+ while(bits > 0)
+ @entropy += 1
+ bits >>= 1
+ end
+ #@entropy += 1
+ @entropy
+ end
+
# this only works on integer ... String, Array, Range does not implement: &, |, ^
def coerce(other)
#puts "TESTING ... coerce called!"
return [self, other] # does not seem to get called ...
end
def dup
rtn = Set.new
- rtn.set_bits!(@bits)
- rtn.entropy = entropy
- rtn
+ rtn.replace(self)
+ return rtn
end
def ^(item)
rtn = self.dup
if(item.class==Set)
@@ -384,36 +436,44 @@
rtn = dup
mask = (1 << @entropy) - 1
rtn.set_bits!(mask ^ @bits)
rtn
end
-
- def [](pos)
- idx = pos.ord if pos.class==String
- idx = pos.to_i
- raise "Negative indexes are illegal for Set" if idx < 0
- self.entropy = idx+1
- y = @bits & (1<<idx)
- return true if y > 0
- false
- end
- def []=(pos,value)
- idx = pos.ord if pos.class==String
- idx = pos.to_i
- raise "Negative indexes are illegal for Set" if idx < 0
- state = value ? true : false
- self.entropy = idx+1
- if state # set bit
- @bits |= 1 << idx
- else # clear bit
- mask = 1 << idx
- @bits |= mask
- @bits ^= mask
+ # new behavior ... single element returns true/false
+ # multi element ... returns subset
+ def [](*pset)
+ idx = nil
+ if pset.count==1 # check for single instance inst[5], inst['t']
+ if pset.first.kind_of? Integer
+ idx = pset.first
+ elsif pset.first.kind_of? String
+ if pset.first.length == 1
+ idx = pset.first.ord
+ end
+ end
end
- return state
+ unless idx.nil?
+ raise "Negative indexes are illegal for Set" if idx < 0
+ self.entropy = idx+1
+ y = @bits & (1<<idx)
+ return true if y > 0
+ return false
+ end
+ return pset.to_set & self
end
+
+ def []=(*pset,value) # pset goes in the box, value after '='
+ pset = pset.to_set
+ state = value ? true : false
+ if state
+ replace pset | self # add elements to set
+ else
+ replace self - pset # remove elements from set
+ end
+ return state # this gets ignored, but to be safe, do what the previous version did
+ end
def min
return nil if empty?
range = (self.entropy)..(0)
while((range.first - range.last) >= 2) do
@@ -439,9 +499,36 @@
end
#byebug
return range.first if (self[range.first])
range.last
end
+
+ # :array :array_chars :string :set
+ def rand(elm_count, format = :set)
+ raise "rand minimum count too low" if elm_count < 1
+ ary = self.to_a
+ ary.shuffle!
+ ary = ary[0..(elm_count-1)]
+ case format
+ when :array
+ return ary
+ when :array_chars
+ rtn = []
+ ary.each do |elm|
+ rtn.push elm.chr
+ end
+ return rtn
+ when :string
+ rtn = []
+ ary.each do |elm|
+ rtn.push elm.chr
+ end
+ return rtn.join ""
+ else # :set
+ return ary.to_set
+ end
+ end
+
end # end Set
module SetFuMixinBinaryAndOperator
def &(item)
a = Set.new(self)