aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/string/access.rb
Commit message (Collapse)AuthorAgeFilesLines
* Call raise with parentheses like a normal method call with argumentsCarlos Antonio da Silva2019-07-291-2/+2
| | | | | | Using `(raise FooError, "error")` is like forcing a "new scope" around the `raise` call, it's simpler to just wrap the `raise` arguments with parentheses just like any other method call would.
* Performance improvement for `String#to`Ryuta Kamizono2019-07-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ```ruby class String def to1(position) position = [position + length, -1].max if position < 0 self[0, position + 1] end def to2(position) position += size if position < 0 self[0, position + 1] || +"" end end Benchmark.ips do |x| x.report("'foo'.to(1)") { 'foo'.to(1) } x.report("'foo'.to1(1)") { 'foo'.to1(1) } x.report("'foo'.to2(1)") { 'foo'.to2(1) } x.report("'foo'.to(-1)") { 'foo'.to(-1) } x.report("'foo'.to1(-1)") { 'foo'.to1(-1) } x.report("'foo'.to2(-1)") { 'foo'.to2(-1) } x.report("'foo'.to(-10)") { 'foo'.to(-10) } x.report("'foo'.to1(-10)") { 'foo'.to1(-10) } x.report("'foo'.to2(-10)") { 'foo'.to2(-10) } end ``` Result: ``` Warming up -------------------------------------- 'foo'.to(1) 199.859k i/100ms 'foo'.to1(1) 220.293k i/100ms 'foo'.to2(1) 221.522k i/100ms 'foo'.to(-1) 205.032k i/100ms 'foo'.to1(-1) 195.837k i/100ms 'foo'.to2(-1) 214.975k i/100ms 'foo'.to(-10) 214.331k i/100ms 'foo'.to1(-10) 182.666k i/100ms 'foo'.to2(-10) 224.696k i/100ms Calculating ------------------------------------- 'foo'.to(1) 4.685M (± 4.2%) i/s - 23.583M in 5.042568s 'foo'.to1(1) 5.233M (± 5.8%) i/s - 26.215M in 5.026778s 'foo'.to2(1) 5.180M (± 5.7%) i/s - 25.918M in 5.020735s 'foo'.to(-1) 4.253M (± 7.0%) i/s - 21.323M in 5.043133s 'foo'.to1(-1) 4.438M (±11.2%) i/s - 21.934M in 5.025751s 'foo'.to2(-1) 4.716M (± 9.8%) i/s - 23.432M in 5.028088s 'foo'.to(-10) 4.678M (± 9.5%) i/s - 23.148M in 5.007379s 'foo'.to1(-10) 4.428M (± 5.1%) i/s - 22.103M in 5.005155s 'foo'.to2(-10) 5.243M (± 4.6%) i/s - 26.289M in 5.024695s ```
* Merge pull request #36185 from jonathanhefner/optimize-string-first-and-lastRafael França2019-07-281-22/+2
|\ | | | | Improve String#first and #last performance
| * Improve String#first and #last performanceJonathan Hefner2019-05-051-22/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Removes unnecessary conditional and method call for significant performance improvement. As a side effect, this fixes an unexpected behavior where passing a limit of 0 would return a frozen string. This also implements the Rails 6.1 intended behavior with regards to negative limits, and removes the previous deprecation warnings. String#first Comparison: new: 3056515.0 i/s old: 1943310.2 i/s - 1.57x slower String#last Comparison: new: 2691919.0 i/s old: 1924256.6 i/s - 1.40x slower (Note: "old" benchmarks have deprecation warnings commented out, for a more fair comparison.)
* | Remove tough to grasp -1 + 1 = 0 from String#toKasper Timm Hansen2019-07-261-2/+2
| | | | | | | | | | | | | | | | | | | | | | In case a negative position is provided that exceeds the size of the string, we're relying on -1 returned from max to get 0 length by + 1 and let [] with a 0 length returning "" for us. E.g. "hello".to(-7), where -7 + 5 size = -2. That's lower than -1, so we use -1 instead and + 1 would turn it into 0. Instead allow outer bounds access and always return "".
* | Avoid extra allocation in String#from and #toJonathan Hefner2019-05-051-2/+3
|/ | | | | | | | | | | | Removes unnecessary Range object allocations for a significant speed-up. String#from Comparison: new: 3378594.0 i/s old: 2380129.8 i/s - 1.42x slower String#to Comparison: new: 2866175.7 i/s old: 2304406.4 i/s - 1.24x slower
* Add deprecation warning when String#first and String#last receive negative ↵Gannon McGibbon2018-09-281-0/+8
| | | | | | integers [Gannon McGibbon + Eric Turner]
* [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment`Koichi ITO2017-07-111-0/+1
|
* Use frozen-string-literal in ActiveSupportKir Shatrov2017-07-091-0/+1
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Add missing `+` around a some literals.bogdanvlviv2016-10-271-2/+2
| | | | | | Mainly around `nil` [ci skip]
* code gardening: removes redundant selfsXavier Noria2016-08-081-2/+2
| | | | | | | | | A few have been left for aesthetic reasons, but have made a pass and removed most of them. Note that if the method `foo` returns an array, `foo << 1` is a regular push, nothing to do with assignments, so no self required.
* applies new string literal convention in activesupport/libXavier Noria2016-08-061-2/+2
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Support for unified Integer class in Ruby 2.4+Jeremy Daer2016-05-181-1/+1
| | | | | | | | Ruby 2.4 unifies Fixnum and Bignum into Integer: https://bugs.ruby-lang.org/issues/12005 * Forward compat with new unified Integer class in Ruby 2.4+. * Backward compat with separate Fixnum/Bignum in Ruby 2.2 & 2.3. * Drops needless Fixnum distinction in docs, preferring Integer.
* Fix inconsistent behavior from String#first/#lastErnie Miller2014-04-181-4/+4
| | | | | | | While calling String#first or String#last with zero or a Fixnum < the string's length returns a new string, a Fixnum >= the string's length returns the string itself. This inconsistency can lead to inadvertent mutation of a string.
* Revert "Speedup String#to"Yves Senn2014-01-081-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit 2ef1fb2c455ca53a0c1e1768f50824926ce28bd3. As described in PR #13627 this commit broke functionality when passing a negative Fixnum to the `String#to` method: ```ruby assert_equal "hell", s.to(-2) ``` Before the revert, this failed with: ``` 1) Failure: StringAccessTest#test_#to_with_negative_Fixnum,_position_is_counted_from_the_end [test/core_ext/string_ext_test.rb:275]: Expected: "hell" Actual: nil ``` This revert is to keep the functionality on `master` working. If there is another way to get the performance benefit and keep the documented functionality we can add that. /cc @amatsuda @carlosantoniodasilva
* Change syntax format for example returned valuesPrem Sichanugrist2013-11-111-30/+30
| | | | | | | | | According to our guideline, we leave 1 space between `#` and `=>`, so we want `# =>` instead of `#=>`. Thanks to @fxn for the suggestion. [ci skip]
* Speedup String#toAkira Matsuda2013-07-101-1/+1
| | | | | | | | Benchmark: 1000000.times { str.to(30) } user system total real old 0.490000 0.110000 0.600000 ( 0.607374) new 0.390000 0.000000 0.390000 ( 0.387306)
* remove unnecessary requireSergey Nartimov2012-05-281-2/+0
| | | | | AS::Multibyte are no longer required by access and filters string core extensions.
* Revert "Merge pull request #6354 from lest/patch-1"Carlos Galdino + Rafael Mendonça França2012-05-161-0/+2
| | | | | | | This reverts commit e8feaff60b9c04d34ad234f7d17b5d2ad9cc7a24, reversing changes made to 9adf28c026070afb78b80027521a4ddddd68d697. Reason: This broke the actionmailer tests
* remove unnecessary requireSergey Nartimov2012-05-161-2/+0
| | | | | - access & filters don't use multibyte ext - transliterate requires only AS::Multibyte but not multibyte ext
* fix String#last exampleFrancesco Rodriguez2012-05-111-1/+1
|
* fix String#last exampleFrancesco Rodriguez2012-05-111-1/+1
|
* added docs to String#lastFrancesco Rodriguez2012-05-111-0/+10
|
* improve String#first docsFrancesco Rodriguez2012-05-111-4/+3
|
* fix typo in String#firstFrancesco Rodriguez2012-05-111-1/+1
|
* added docs to String#firstFrancesco Rodriguez2012-05-111-0/+11
|
* added docs to String#fromFrancesco Rodriguez2012-05-111-0/+13
|
* improve String#to docsFrancesco Rodriguez2012-05-111-2/+2
|
* added docs to String#toFrancesco Rodriguez2012-05-111-0/+13
|
* added docs to String#atFrancesco Rodriguez2012-05-111-0/+25
|
* AS core_ext refactoring pt.2Alexey Gaziev2012-04-291-1/+1
|
* remove checks for encodings availabilitySergey Nartimov2011-12-251-88/+24
|
* Deletes trailing whitespaces (over text files only find * -type f -exec sed ↵Santiago Pastorino2010-08-141-7/+7
| | | | 's/[ \t]*$//' -i {} \;)
* Merge remote branch 'mainstream/master'Pratik Naik2009-11-171-0/+2
|\ | | | | | | | | Conflicts: activesupport/lib/active_support/core_ext/hash/conversions.rb
| * Break up inflector to reduce the dependency burden on dependency-les methods ↵Yehuda Katz2009-11-071-0/+2
| | | | | | | | like constantize.
* | details how a corner case behaves in different Ruby versionsXavier Noria2009-09-221-2/+2
|/
* Convert string extension modules to class reopensJeremy Kemper2009-04-221-95/+86
|
* Clearer String#first and #last edge cases. Fix that 'foo'.first(0) == 'foo' ↵Jeremy Kemper2009-04-201-2/+14
| | | | instead of ''
* Clearer String#first and #last edge cases. Fix that foo.first(0) == instead ↵Jeremy Kemper2009-04-171-3/+15
| | | | of foo.
* Change all calls to String#chars to String#mb_chars.Manfred Stienstra2008-09-211-5/+5
|
* Feature check :force_encoding instead of RUBY_VERSIONJeremy Kemper2008-04-191-1/+1
|
* Ruby 1.9 compat: special-case String access methods to not depend on #charsJeremy Kemper2008-01-031-48/+72
| | | | git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8538 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
* make sure the String::Access methods return strings, and not multibyte Char ↵Jamis Buck2006-10-131-5/+5
| | | | | | instances git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5299 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
* Make core_ext/string/access.rb multibyte safe. Closes #6388 [Manfred Stienstra]Michael Koziarski2006-10-121-5/+5
| | | | git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5287 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
* A more aesthetically pleasing implementation and changelog entryScott Barron2006-02-081-3/+2
| | | | git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3549 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
* Make String#last behave more like Array#last, i.e.Scott Barron2006-02-081-0/+1
| | | | | | | | "f".last(3) => "f" not "f".last(3) => nil git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3548 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
* Added String#at, String#from, String#to, String#first, String#last in ↵David Heinemeier Hansson2005-09-031-0/+58
ActiveSupport::CoreExtensions::String::Access to ease access to individual characters and substrings in a string serving basically as human names for range access. Added easy extendability to the inflector through Inflector.inflections (using the Inflector::Inflections singleton class) git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2110 5ecf4fe2-1ee6-0310-87b1-e25e094e27de