Class: RubyLexer::CharSet
- Inherits:
-
Object
- Object
- RubyLexer::CharSet
- Defined in:
- lib/rubylexer/charset.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#===(c) ⇒ Object
c is String|Fixnum|nil.
- #add(chars) ⇒ Object
-
#chars ⇒ Object
turn bitset back into a string.
- #clear ⇒ Object
- #each(&block) ⇒ Object
-
#each_byte(&block) ⇒ Object
enumerate the chars in n AS INTEGERS.
-
#initialize(*charss) ⇒ CharSet
constructor
A new instance of CharSet.
- #remove(chars) ⇒ Object
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
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 |
#chars ⇒ Object
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 |
#clear ⇒ Object
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 |