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



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

def ===(c) #c is String|Fixnum|nil
   c.nil? and return false
   c.kind_of? String and c=c[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



70
71
72
73
74
# File 'lib/rubylexer/charset.rb', line 70

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



66
67
68
# File 'lib/rubylexer/charset.rb', line 66

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

#each_byte(&block) ⇒ Object

enumerate the chars in n AS INTEGERS



59
60
61
62
63
64
# File 'lib/rubylexer/charset.rb', line 59

def each_byte(&block)
   #should use ffs... not available in ruby
   (0..255).each { |n|
      @bitset[n] 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