aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/string
Commit message (Collapse)AuthorAgeFilesLines
* 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 "".
* | Merge pull request #36180 from jonathanhefner/optimize-string-fromRafael França2019-07-251-2/+3
|\ \ | | | | | | Avoid extra allocation in String#from and #to
| * | 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
* / Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-131-1/+0
|/ | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* Frozen truncate (#36109)Jordan Thomas2019-04-261-1/+1
| | | | | | | | | | | | | | * Add test asserting truncate returns unfrozen string * Ensure strings returned from truncate are not frozen This fixes an issue where strings too short to be truncated were returned unfrozen, where as long-enough strings were returned frozen. Now retuned strings will not be frozen whether or not the string returned was shortened. * Update changelog w/ new truncate behavior description [Jordan Thomas + Rafael Mendonça França]
* Preserve html_safe? status on ActiveSupport::SafeBuffer#*r7kamura2019-04-191-0/+6
|
* Merge pull request #34405 from shugo/safe_buffer_backref_fixMatthew Draper2019-03-281-2/+37
|\ | | | | sub, sub!, gsub, and gsub! should set back references
| * Eliminate a thread local variable as suggested by nobuShugo Maeda2019-02-141-8/+1
| |
| * Remove trailing spaceShugo Maeda2018-11-081-1/+1
| |
| * Add a commented code example of what will be producedShugo Maeda2018-11-081-22/+22
| |
| * sub, sub!, gsub, and gsub! should set back referencesShugo Maeda2018-11-081-2/+44
| |
* | Update docs for 'parameterize()' [ci skip]Sharang Dashputre2019-03-161-1/+1
| |
* | support slice assignment on SafeBufferRichard Monette2019-03-131-2/+6
| |
* | Add locale option to parameterizeKaan Ozkan2019-03-111-2/+7
|/ | | | | | Parameterize is triggering I18n#transliterate. This method already accepts a locale. It would be cleaner if similar to other string inflection methods #parameterize also accepted 'locale' as a parameter.
* Merge pull request #33058 from gmcgibbon/string_first_last_negative_deprecationRafael França2018-10-021-0/+8
|\ | | | | Add deprecation warning when String#first and String#last receive neg…
| * Add deprecation warning when String#first and String#last receive negative ↵Gannon McGibbon2018-09-281-0/+8
| | | | | | | | | | | | integers [Gannon McGibbon + Eric Turner]
* | Add `Style/RedundantFreeze` to remove redudant `.freeze`Yasuo Honda2018-09-291-1/+1
|/ | | | | | | | | | | | | | | | | | | | | Since Rails 6.0 will support Ruby 2.4.1 or higher `# frozen_string_literal: true` magic comment is enough to make string object frozen. This magic comment is enabled by `Style/FrozenStringLiteralComment` cop. * Exclude these files not to auto correct false positive `Regexp#freeze` - 'actionpack/lib/action_dispatch/journey/router/utils.rb' - 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb' It has been fixed by https://github.com/rubocop-hq/rubocop/pull/6333 Once the newer version of RuboCop released and available at Code Climate these exclude entries should be removed. * Replace `String#freeze` with `String#-@` manually if explicit frozen string objects are required - 'actionpack/test/controller/test_case_test.rb' - 'activemodel/test/cases/type/string_test.rb' - 'activesupport/lib/active_support/core_ext/string/strip.rb' - 'activesupport/test/core_ext/string_ext_test.rb' - 'railties/test/generators/actions_test.rb'
* Handle more unsafe String methods (#33990)Janosch Müller2018-09-271-2/+15
| | | | | | | | | | * Handle more unsafe String methods * Fix codeclimate issue * Revert stylistic change [Janosch Müller + Rafael Mendonça França]
* SafeBuffer should maintain safety upon getting a slice via a range if ↵Yumin Wong2018-08-311-3/+1
| | | | | | original buffer was safe. Co-Authored-By: no-itsbackpack <no-itsbackpack@github.com>
* String#truncate_bytes: limit to N bytes without breaking multibyte charsJeremy Daer2018-02-181-0/+41
| | | | | This faithfully preserves grapheme clusters (characters composed of other characters and combining marks) and other multibyte characters.
* `String#strip_heredoc` preserves frozennessJeremy Daer2018-02-171-1/+3
| | | | | | | | | | | | | | | | | | ```ruby "foo".freeze.strip_heredoc.frozen? # => true ``` Fixes the case where frozen string literals would inadvertently become unfrozen: ```ruby foo = <<-MSG.strip_heredoc la la la MSG foo.frozen? # => false !?? ```
* Rails 6 requires Ruby 2.4.1+Jeremy Daer2018-02-171-3/+4
| | | | | | Skipping over 2.4.0 to sidestep the `"symbol_from_string".to_sym.dup` bug. References #32028
* Mention about Ruby 2.4 Unicode case mappings in `mb_chars` example [ci skip] ↵Ryuta Kamizono2017-11-291-1/+3
| | | | | (#31275) https://www.ruby-lang.org/en/news/2016/09/08/ruby-2-4-0-preview2-released/
* [ci skip] Correct output for UpcaseDixit Patel2017-11-291-1/+1
|
* Update incorrect backtick usage in RDoc to teletypeT.J. Schuck2017-11-222-2/+2
| | | [ci skip]
* [Active Support] require_relative => requireAkira Matsuda2017-10-216-10/+10
| | | | This basically reverts 8da30ad6be34339124ba4cb4e36aea260dda12bc
* Remove obsolete documentation [ci skip]Max Felsher2017-10-191-3/+0
| | | | | Instructions to use `h` or `html_escape` in ERB templates were added to `actionpack/lib/action_view/template_handlers/erb.rb` in a1b0349 (Rails 2.1), but ERB has automatically escaped values since Rails 3.
* Clarify intentions around method redefinitionsMatthew Draper2017-09-011-3/+3
| | | | | | | | | Don't use remove_method or remove_possible_method just before a new definition: at best the purpose is unclear, and at worst it creates a race condition. Instead, prefer redefine_method when practical, and silence_redefinition_of_method otherwise.
* Update String#camelize to provide feedback when wrong option is passedRicardo Díaz2017-08-021-0/+2
| | | | | | | | | String#camelize was returning nil without any feedback when an invalid option was passed as parameter. This update makes the method to raises an ArgumentError when the option passed is invalid, similar to what Ruby does for String#downcase (and others) in 2.4.1. https://ruby-doc.org/core-2.4.1/String.html#method-i-downcase
* [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment`Koichi ITO2017-07-1113-0/+13
|
* Use frozen-string-literal in ActiveSupportKir Shatrov2017-07-0913-0/+13
|
* [Active Support] require => require_relativeAkira Matsuda2017-07-016-9/+9
|
* Use keyword arguments instead of hashRafael Mendonça França2017-03-281-4/+4
|
* Merge pull request #28480 from ↵Rafael Mendonça França2017-03-281-9/+19
|\ | | | | | | | | | | mubashirhanif/add_keep_id_suffix_option_to_humanize_new Add keep id suffix option to humanize new
| * Added options hash to titleize method and keep_id_suffix option to humanizeMubashir Hanif2017-03-211-9/+19
| | | | | | | | | | | | | | | | | | | | | | | | some documentation remove extra whitespace. Added id in the middle test case and corrected some testcases. Some Coding standard guidelines corrections as suggested by codeclimate. Some more corrections suggested by codeclimate.
* | :golf: else + if = elsifAkira Matsuda2017-01-171-10/+8
| |
* | There's no such moduleAkira Matsuda2017-01-051-4/+4
|/
* Remove deprecated separator argument from parameterizeAndrew White2016-11-141-5/+1
|
* Merge pull request #26905 from bogdanvlviv/docsAndrew White2016-11-132-3/+3
|\ | | | | Add missing `+` around a some literals.
| * Add missing `+` around a some literals.bogdanvlviv2016-10-272-3/+3
| | | | | | | | | | | | Mainly around `nil` [ci skip]
* | Add more rubocop rules about whitespacesRafael Mendonça França2016-10-292-3/+3
|/
* Add three new rubocop rulesRafael Mendonça França2016-08-161-1/+1
| | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* code gardening: removes redundant selfsXavier Noria2016-08-082-3/+3
| | | | | | | | | 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.
* normalizes indentation and whitespace across the projectXavier Noria2016-08-062-4/+4
|
* applies new string literal convention in activesupport/libXavier Noria2016-08-0610-23/+23
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Missing require 'active_support/multibyte/unicode'Akira Matsuda2016-07-121-0/+1
|
* 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.