Sha256: b4d56fc9ad75b9edf9c4260da2b426ea1464babd64430d8d22f11a7b4b60f8db

Contents?: true

Size: 911 Bytes

Versions: 3

Compression:

Stored size: 911 Bytes

Contents

require 'strscan'

#TODO: rewrite this in C and commit this addition back to community
class StringScanner
  #      scan_before(pattern)
  #
  #
  # Scans the string until the +pattern+ is matched. As opposed to #scan_until,
  # it does not include a matching pattern into a returning value and sets
  # pointer just _before_ the pattern position.
  # If there is no match, +nil+ is returned.
  #
  #  s = StringScanner.new("Fri Dec 12 1975 14:39")
  #  s.scan_until(/1/)        # -> "Fri Dec "
  #  s.scan(/1/)              # -> "1"
  #  s.scan_until(/1/)        # -> nil
  #
  def scan_before(pattern)
    return nil unless self.exist?(pattern)

    pattern_size = self.matched_size
    result = self.scan_until(pattern)

    self.pos = self.pos - pattern_size
    (result.length == pattern_size) ?
      (result = '') :
      (result = result[0..(result.length - pattern_size - 1)])    

    result
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
usher-0.5.13 lib/usher/spinoffs/strscan_additions.rb
usher-0.5.12 lib/usher/spinoffs/strscan_additions.rb
usher-0.5.11 lib/usher/spinoffs/strscan_additions.rb