Version 1
[yaffs-website] / vendor / twbs / bootstrap-sass / tasks / converter / char_string_scanner.rb
1 # regular string scanner works with bytes
2 # this one works with chars and provides #scan_next
3 class Converter
4   class CharStringScanner
5     extend Forwardable
6
7     def initialize(*args)
8       @s = StringScanner.new(*args)
9     end
10
11     def_delegators :@s, :scan_until, :skip_until, :string
12
13     # advance scanner to pos after the next match of pattern and return the match
14     def scan_next(pattern)
15       return unless @s.scan_until(pattern)
16       @s.matched
17     end
18
19     def pos
20       byte_to_str_pos @s.pos
21     end
22
23     def pos=(i)
24       @s.pos = str_to_byte_pos i
25       i
26     end
27
28     private
29
30     def byte_to_str_pos(pos)
31       @s.string.byteslice(0, pos).length
32     end
33
34     def str_to_byte_pos(pos)
35       @s.string.slice(0, pos).bytesize
36     end
37   end
38 end