Version 1
[yaffs-website] / vendor / twbs / bootstrap-sass / tasks / converter / char_string_scanner.rb
diff --git a/vendor/twbs/bootstrap-sass/tasks/converter/char_string_scanner.rb b/vendor/twbs/bootstrap-sass/tasks/converter/char_string_scanner.rb
new file mode 100644 (file)
index 0000000..b2aead8
--- /dev/null
@@ -0,0 +1,38 @@
+# regular string scanner works with bytes
+# this one works with chars and provides #scan_next
+class Converter
+  class CharStringScanner
+    extend Forwardable
+
+    def initialize(*args)
+      @s = StringScanner.new(*args)
+    end
+
+    def_delegators :@s, :scan_until, :skip_until, :string
+
+    # advance scanner to pos after the next match of pattern and return the match
+    def scan_next(pattern)
+      return unless @s.scan_until(pattern)
+      @s.matched
+    end
+
+    def pos
+      byte_to_str_pos @s.pos
+    end
+
+    def pos=(i)
+      @s.pos = str_to_byte_pos i
+      i
+    end
+
+    private
+
+    def byte_to_str_pos(pos)
+      @s.string.byteslice(0, pos).length
+    end
+
+    def str_to_byte_pos(pos)
+      @s.string.slice(0, pos).bytesize
+    end
+  end
+end
\ No newline at end of file