Sha256: df512087e668894f8d6e0049af624b267205200e335fa0d999a5c8a46c3139df

Contents?: true

Size: 816 Bytes

Versions: 6

Compression:

Stored size: 816 Bytes

Contents

module Dispel
  module Tools
    class << self
      # http://grosser.it/2011/08/28/ruby-string-naive-split-because-split-is-to-clever/
      # "    ".split(' ') == []
      # "    ".naive_split(' ') == ['','','','']
      # "".split(' ') == []
      # "".naive_split(' ') == ['']
      def naive_split(string, pattern)
        pattern = /#{Regexp.escape(pattern)}/ unless pattern.is_a?(Regexp)
        result = string.split(pattern, -1)
        result.empty? ? [''] : result
      end

      # http://grosser.it/2010/12/31/ruby-string-indexes-indices-find-all-indexes-in-a-string
      def indexes(string, needle)
        found = []
        current_index = -1
        while current_index = string.index(needle, current_index+1)
          found << current_index
        end
        found
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dispel-0.1.0 lib/dispel/tools.rb
dispel-0.0.8 lib/dispel/tools.rb
dispel-0.0.7 lib/dispel/tools.rb
dispel-0.0.6 lib/dispel/tools.rb
dispel-0.0.5 lib/dispel/tools.rb
dispel-0.0.4 lib/dispel/tools.rb