Sha256: ca3c21bf02625cb509bc801753794dfaab7c2441f83468f24b82119bac2e9b5b

Contents?: true

Size: 939 Bytes

Versions: 1

Compression:

Stored size: 939 Bytes

Contents

# -*- frozen_string_literal: true -*-

require "rsplit/version"

module RSplit
  def rsplit(str, sep = nil, limit = 0)
    valid_encoding?(str)
    sep = $; if sep.nil?
    case sep
    when NilClass
      # do nothing
    when String
      valid_encoding?(sep)
      sep = sep.reverse
    else
      raise TypeError, "wrong argument type #{sep.class} (expected String)"
    end

    str.reverse.split(sep, limit).map(&:reverse).reverse
  end

  module_function :rsplit

  # Avoid Bug #11387
  # https://bugs.ruby-lang.org/issues/11387
  def self.valid_encoding?(string)
    raise ArgumentError, "invalid byte sequence in #{string.encoding.name}" unless string.valid_encoding?
  end
end


class String
  # :call-seq:
  #   rsplit(sep = $;, limit = 0)
  #
  # Divides string into substrings based on a delimiter (starting from right), returning an array of these substrings.
  #
  def rsplit(*args)
    RSplit.rsplit(self, *args)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rsplit-0.1.1 lib/rsplit.rb