Sha256: df25c96bd8d51bbdb2481bc1d4e8d097c613a69b48b883f03b49248b64bf2ddd

Contents?: true

Size: 761 Bytes

Versions: 45

Compression:

Stored size: 761 Bytes

Contents

# 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

Version data entries

45 entries across 45 versions & 8 rubygems

Version Path
entreprise7pro-bootstrap-sass-3.4.7 tasks/converter/char_string_scanner.rb
entreprise7pro-bootstrap-sass-3.4.6 tasks/converter/char_string_scanner.rb
chroma-js-1.0.7 tasks/converter/char_string_scanner.rb
chroma-js-1.0.5 tasks/converter/char_string_scanner.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/bootstrap-sass-3.4.1/tasks/converter/char_string_scanner.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/bootstrap-sass-3.4.1/tasks/converter/char_string_scanner.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/bootstrap-sass-3.4.1/tasks/converter/char_string_scanner.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/bootstrap-sass-3.4.1/tasks/converter/char_string_scanner.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/bootstrap-sass-3.4.1/tasks/converter/char_string_scanner.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/bootstrap-sass-3.4.1/tasks/converter/char_string_scanner.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/bootstrap-sass-3.4.1/tasks/converter/char_string_scanner.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/bootstrap-sass-3.4.1/tasks/converter/char_string_scanner.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/bootstrap-sass-3.4.1/tasks/converter/char_string_scanner.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/bootstrap-sass-3.4.1/tasks/converter/char_string_scanner.rb
bootstrap-sass-3.2.0.4 tasks/converter/char_string_scanner.rb
bootstrap-sass-3.4.1 tasks/converter/char_string_scanner.rb
bootstrap-sass-3.4.0 tasks/converter/char_string_scanner.rb
toolbox_esten-1.0.0 tasks/converter/char_string_scanner.rb
bootstrap-sass-3.3.7 tasks/converter/char_string_scanner.rb
style-sass-1.0.0 tasks/converter/char_string_scanner.rb