Permalink
Browse files

Improves performance of String#[] method.

Improves performance of String#[] method when passing Fixnum.
  • Loading branch information...
1 parent 5cb7a7d commit 83341ab4ead5a00964eb34ade0e721fd6b4478b4 @pgrzegorczyk pgrzegorczyk committed Jun 18, 2011
Showing with 9 additions and 0 deletions.
  1. +9 −0 kernel/common/string.rb
View
9 kernel/common/string.rb
@@ -254,6 +254,15 @@ def [](index, other = undefined)
end
case index
+ when Fixnum
+ # The same code as in else section.
+ # Copied here to improve performance because Fixnum index is
+ # often used to iterate through String object.
+ index = Rubinius::Type.coerce_to index, Fixnum, :to_int
+ index = @num_bytes + index if index < 0
+
+ return if index < 0 || @num_bytes <= index
+ return @data[index]
when Regexp
match_data = index.search_region(self, 0, @num_bytes, true)
Regexp.last_match = match_data

0 comments on commit 83341ab

Please sign in to comment.