Skip to content
Permalink
Browse files

Merge pull request #622 from kachick/implement-string-start-end_with

Implement String#start_with?, String#end_with?
  • Loading branch information
alex committed Apr 19, 2013
2 parents 6f5076c + 4fd80dc commit 6d108ef79f6f2b99dbd7b7f3ec1cadb55e80af48
Showing with 20 additions and 14 deletions.
  1. +20 −0 lib-topaz/string.rb
  2. +0 −8 spec/tags/core/string/end_with_tags.txt
  3. +0 −6 spec/tags/core/string/start_with_tags.txt
@@ -57,6 +57,26 @@ def empty?
self.length == 0
end

def start_with?(*prefixes)
prefixes.any? do |prefix|
next false unless prefix.respond_to?(:to_str)
prefix = prefix.to_str
prelen = prefix.length
next false if prelen > self.length
0.upto(prelen - 1).all? { |index| self[index] == prefix[index] }
end
end

def end_with?(*suffixes)
suffixes.any? do |suffix|
next false unless suffix.respond_to?(:to_str)
suffix = suffix.to_str
suflen = suffix.length
next false if suflen > self.length
(-suflen).upto(-1).all? { |index| self[index] == suffix[index] }
end
end

def match(pattern)
return Regexp.new(pattern).match(self)
end

This file was deleted.

This file was deleted.

0 comments on commit 6d108ef

Please sign in to comment.
You can’t perform that action at this time.