Sha256: dacc43a4aff012c4df38b88a63db735e54488218bacf878379e8ac947d3e6328

Contents?: true

Size: 675 Bytes

Versions: 26

Compression:

Stored size: 675 Bytes

Contents

class String

  # Like index but returns an array of all index locations.
  # The reuse flag allows the trailing portion of a match to be 
  # reused for subsquent matches.
  #
  #   "abcabcabc".index_all('a')  #=> [0,3,6]
  #
  def index_all(s, reuse=false)
    ia = []; i = 0
    while (i = self.index(s,i))
      ia << i
      i += (reuse ? 1 : $~.length)
    end
    ia
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_index_all
      assert_equal( [0,4,8], "a123a567a9".index_all(/a/) )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-1.8.49 lib/facets/core/string/index_all.rb
facets-1.8.0 lib/facets/core/string/index_all.rb
facets-1.8.20 lib/facets/core/string/index_all.rb
facets-1.8.51 lib/facets/core/string/index_all.rb
facets-1.8.54 lib/facets/core/string/index_all.rb
facets-1.8.8 lib/facets/core/string/index_all.rb