diff options
author | Rafael França <rafael@franca.dev> | 2019-07-25 23:53:59 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-25 23:53:59 -0400 |
commit | 4d6bbf02bae5960ffbda855746629c4f3033b162 (patch) | |
tree | 25b3ebc02ad2b0ebe5ac8fa795f4e56b8d6666e4 /activesupport | |
parent | 971cd757eaeb6da34a256b35457749d067073c03 (diff) | |
parent | 52498ccafd718975dc7ad8df2bf7f4a9614a239d (diff) | |
download | rails-4d6bbf02bae5960ffbda855746629c4f3033b162.tar.gz rails-4d6bbf02bae5960ffbda855746629c4f3033b162.tar.bz2 rails-4d6bbf02bae5960ffbda855746629c4f3033b162.zip |
Merge pull request #36180 from jonathanhefner/optimize-string-from
Avoid extra allocation in String#from and #to
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/access.rb | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb index 4ca24028b0..4894193665 100644 --- a/activesupport/lib/active_support/core_ext/string/access.rb +++ b/activesupport/lib/active_support/core_ext/string/access.rb @@ -44,7 +44,7 @@ class String # str.from(0).to(-1) # => "hello" # str.from(1).to(-2) # => "ell" def from(position) - self[position..-1] + self[position, length] end # Returns a substring from the beginning of the string to the given position. @@ -61,7 +61,8 @@ class String # str.from(0).to(-1) # => "hello" # str.from(1).to(-2) # => "ell" def to(position) - self[0..position] + position = [position + length, -1].max if position < 0 + self[0, position + 1] end # Returns the first character. If a limit is supplied, returns a substring |