Class: RubyLexer::CharSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rubylexer/charset.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*charss) ⇒ CharSet

Returns a new instance of CharSet.



22
23
24
25
# File 'lib/rubylexer/charset.rb', line 22

def initialize(*charss)
   clear
   charss.each {|chars| add chars }
end

Class Method Details

.[](*charss) ⇒ Object



27
# File 'lib/rubylexer/charset.rb', line 27

def CharSet.[](*charss)        CharSet.new(*charss)       end

Instance Method Details

#===(c) ⇒ Object

c is String|Fixnum|nil



53
54
55
56
57
# File 'lib/rubylexer/charset.rb', line 53

def ===(c) #c is String|Fixnum|nil
  c.nil? and return false
  c.kind_of? String and c=c.getbyte(0)
  return ( @bitset[c] != 0 )
end

#add(chars) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/rubylexer/charset.rb', line 33

def add(chars)
   case chars
   when ::String
      chars.each_byte {|c| @bitset |= (1<<c) }
   when ::Fixnum then      @bitset |= (1<<chars)
   else chars.each    {|c| @bitset |= (1<<c) }
   end
end

#charsObject

turn bitset back into a string



78
79
80
81
82
# File 'lib/rubylexer/charset.rb', line 78

def chars #turn bitset back into a string
   result=''
   each {|c| result << c }
   return result
end

#clearObject



29
30
31
# File 'lib/rubylexer/charset.rb', line 29

def clear
   @bitset=0
end

#each(&block) ⇒ Object



74
75
76
# File 'lib/rubylexer/charset.rb', line 74

def each(&block)
   each_byte{|n| block[n.chr] }
end

#each_byte(&block) ⇒ Object

enumerate the chars in n AS INTEGERS



67
68
69
70
71
72
# File 'lib/rubylexer/charset.rb', line 67

def each_byte(&block)
   #should use ffs... not available in ruby
   (0..255).each { |n|
      @bitset[n].nonzero? and block[n]
   }
end

#remove(chars) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/rubylexer/charset.rb', line 42

def remove(chars)
   case chars
   when String
      chars.each_byte {|c| @bitset &= ~(1<<c) }
   when Fixnum then        @bitset &= ~(1<<chars)
   else chars.each    {|c| @bitset &= ~(1<<c) }
   end
   #this math works right with bignums... (i'm pretty sure)
end