aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/string
Commit message (Collapse)AuthorAgeFilesLines
* Applying right result of examples in ActiveSupport Multibyte [ci skip]amitkumarsuroliya2015-09-211-6/+4
|
* String#strip_heredocs doesn't need Object#tryVlado Cingel2015-09-191-2/+0
| | | | Call to Object#try was removed with this pull request https://github.com/rails/rails/pull/21596
* Merge branch 'master' of github.com:rails/docrailsVijay Dev2015-09-181-1/+1
|\
| * s/JQuery/jQuery/Akira Matsuda2015-09-181-1/+1
| | | | | | | | [ci skip]
* | Improve String#strip_heredocJuanito Fatas2015-09-121-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Saves about 6 MB, about 40% faster. **strip_heredoc.rb** ```ruby require "active_support/core_ext/object/try" require "get_process_mem" class String def strip_heredoc indent = scan(/^[ \t]*(?=\S)/).min.try(:size) || 0 gsub(/^[ \t]{#{indent}}/, '') end end if ENV["MEASURE_MEMORY"] == "yes" mem = GetProcessMem.new GC.start GC.disable 10000.times do <<-MSG.strip_heredoc xhr and xml_http_request methods are deprecated in favor of `get :index, xhr: true` and `post :create, xhr: true` MSG end before = mem.mb after = mem.mb GC.enable puts "Before: #{before} MiB" puts "After: #{after} MiB" puts "Diff: #{after - before} MiB" end ``` **patched_strip_heredoc.rb** ```ruby require "active_support/core_ext/object/try" require "get_process_mem" class String def patched_strip_heredoc gsub(/^#{scan(/^[ \t]*(?=\S)/).min}/, "".freeze) end end if ENV["MEASURE_MEMORY"] == "yes" mem = GetProcessMem.new GC.start GC.disable 10000.times do <<-MSG.patched_strip_heredoc xhr and xml_http_request methods are deprecated in favor of `get :index, xhr: true` and `post :create, xhr: true` MSG end before = mem.mb after = mem.mb GC.enable puts "Before: #{before} MiB" puts "After: #{after} MiB" puts "Diff: #{after - before} MiB" end ``` **Before** ``` $ MEASURE_MEMORY=yes ruby strip_heredoc.rb Before: 44.73828125 MiB After: 44.7734375 MiB Diff: 0.03515625 MiB ``` **After** ``` $ MEASURE_MEMORY=yes ruby patched_strip_heredoc.rb Before: 37.9765625 MiB After: 38.015625 MiB Diff: 0.0390625 MiB ``` `44.7734375 - 38.015625 = 6.75` => **Saves about 6.75 MiB** **benchmark.rb** ```ruby require "benchmark/ips" require_relative "./strip_heredoc" require_relative "./patched_strip_heredoc" def original <<-MSG.strip_heredoc xhr and xml_http_request methods are deprecated in favor of `get :index, xhr: true` and `post :create, xhr: true` MSG end def patched <<-MSG.patched_strip_heredoc xhr and xml_http_request methods are deprecated in favor of `get :index, xhr: true` and `post :create, xhr: true` MSG end Benchmark.ips do |x| x.report("original") { original } x.report(" patched") { patched } x.compare! end ``` ``` $ ruby -v benchmark.rb ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14] Calculating ------------------------------------- original 5.652k i/100ms patched 6.477k i/100ms ------------------------------------------------- original 54.076k (± 5.7%) i/s - 271.296k patched 74.557k (± 6.2%) i/s - 375.666k Comparison: patched: 74557.0 i/s original: 54076.4 i/s - 1.38x slower ``` => **About 38% faster** 1. Clone rails project `git clone git@github.com:rails/rails.git` 2. Apply this patch to `activesupport/lib/active_support/core_ext/string/strip.rb` 3. `cd activesupport` 4. run `rake` 5. And tests passed: ``` ➜ activesupport $ rake /Users/Juan/.rubies/ruby-2.2.2/bin/ruby -w -I"lib:test" "/Users/Juan/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_test_loader.rb" "test/**/*_test.rb" ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ......................................................................S. SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS Finished in 15.343004s, 214.2344 runs/s, 24902.4898 assertions/s. 3287 runs, 382079 assertions, 0 failures, 0 errors, 48 skips You have skipped tests. Run with --verbose for details. ```
* | Fixed Time conversion example for UTC time zone [ci skip]Ronak Jangir2015-09-081-1/+1
|/
* minor documentation improvement [ci skip]Sam Auciello2015-08-241-2/+2
|
* Merge pull request #21217 from myrridin/myrridin-documentation-updatesZachary Scott2015-08-121-2/+2
|\ | | | | [ci skip] Documentation: Switch around a common phrase for readability
| * [ci skip] Switch around a common idiom for readabilityThomas Hart II2015-08-051-2/+2
| |
* | String#freeze optimizationsschneems2015-07-301-1/+1
|/
* Speedup String#squishojab2015-04-121-2/+1
|
* Merge branch 'master' of github.com:rails/docrailsVijay Dev2015-03-261-1/+1
|\ | | | | | | | | Conflicts: guides/source/4_0_release_notes.md
| * [ci skip] Add space after erb block.yui-knk2015-03-121-1/+1
| |
* | Doc fixes [ci skip]Islam Wazery2015-03-073-3/+3
|/
* [skip ci] Add documentation for String#is_utf8? methodAnton Davydov2015-02-281-0/+7
|
* Merge pull request #19121 from davydovanton/update-doc-for-removeYves Senn2015-02-281-2/+3
|\ | | | | | | Update documentation examples for String#remove [skip ci]
| * Update documentation examples for String#remove [skip ci]Anton Davydov2015-02-281-6/+7
|/
* Fix a backtracking problem in String#truncate_wordsHenrik Nygren2015-02-251-1/+1
| | | | Fixes #19070.
* Properly dump primitive-like AS::SafeBuffer strings as YAMLGodfrey Chan2015-02-111-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `coder.represent_scalar` means something along the lines of "Here is a quoted string, you can just add it to the output", which is not the case here. It only works for simple strings that can appear unquoted in YAML, but causes problems for e.g. primitive-like strings ("1", "true"). `coder.represent_object` on the other hand, means that "This is the Ruby-object representation for this thing suitable for use in YAML dumping", which is what we want here. Before: YAML.load ActiveSupport::SafeBuffer.new("Hello").to_yaml # => "Hello" YAML.load ActiveSupport::SafeBuffer.new("true").to_yaml # => true YAML.load ActiveSupport::SafeBuffer.new("false").to_yaml # => false YAML.load ActiveSupport::SafeBuffer.new("1").to_yaml # => 1 YAML.load ActiveSupport::SafeBuffer.new("1.1").to_yaml # => 1.1 After: YAML.load ActiveSupport::SafeBuffer.new("Hello").to_yaml # => "Hello" YAML.load ActiveSupport::SafeBuffer.new("true").to_yaml # => "true" YAML.load ActiveSupport::SafeBuffer.new("false").to_yaml # => "false" YAML.load ActiveSupport::SafeBuffer.new("1").to_yaml # => "1" YAML.load ActiveSupport::SafeBuffer.new("1.1").to_yaml # => "1.1" If we ever want Ruby to behave more like PHP or JavaScript though, this is an excellent trick to use ;)
* Merge pull request #14028 from uberllama/json_escape_commentsRafael Mendonça França2015-02-061-0/+5
|\ | | | | Amended json_escape comments
| * Amended json_escape comment to clarify that user-generated content must ↵Yuval Kordov2014-02-121-0/+5
| | | | | | | | still be html_escaped if being inserted ingot he DOM via JQuery's html() method.
* | Removed magic comments # encoding: utf-8 , since its default from ruby 2.0 ↵Vipul A M2015-02-031-1/+0
| | | | | | | | onwards.
* | Remove deprecated `ActiveSupport::SafeBuffer#prepend`Rafael Mendonça França2015-01-041-6/+0
| |
* | Just check if the buffer exists before changing itRafael Mendonça França2014-12-291-1/+3
| |
* | When trying to access a character on a string buffer object via `:[]`, if ↵Vipul A M2014-12-291-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | the object being accessed currently returns `html_safe?` as true, we used to set `@html_safe` variable as true on new object created. When doing something like x = 'Hello'.html_safe x[/a/, 1] would throw an error on ruby 2.2, since when nothign gets matched nil is returned by the code and it tries to set `@html_safe` value to true, which would error since starting 2.2 nil is frozen. This change adds a safety net to avoid setting `@html_safe = true` on frozen objects. Fixes #18235
* | Document `String#html_safe` [ci skip]Sean Griffin2014-11-241-0/+5
| | | | | | | | | | | | It should be part of the documented public API, since we have an entire section of the guides dedicated to it. Documented in a way that addresses the concerns which kept it undocumented in the past.
* | added example of squish!, remove, test case for multiple occurrence ofRishi Jain2014-11-061-0/+9
| | | | | | | | | | | | | | | | pattern removal added example for string#remove and test case for remove of multiple occurence of pattern removed extra whitespaces
* | Merge pull request #17383 from rwz/string-removeRafael Mendonça França2014-11-031-6/+10
|\ \ | | | | | | | | | | | | | | | | | | Make `String#remove` and `String#remove!` accept multiple arguments Conflicts: activesupport/CHANGELOG.md
| * | Make `String#remove` and `String#remove!` accept multiple argumentsPavel Pravosud2014-10-251-6/+10
| | |
* | | instance_eval is evilAkira Matsuda2014-10-251-1/+1
|/ /
* | [ci skip] ActiveSupport CHANGELOG fixesAkshay Vishnoi2014-09-181-0/+1
| | | | | | | | | | | | | | 1. spacing issues 2. spelling correction 3. grammar correction 4. Add missing docs
* | As of Unicode 6.3, Mongolian Vowel Separator is not whitespaceMatthew Draper2014-09-151-1/+1
| | | | | | | | | | Ruby 2.2 knows this, and no longer matches it with [[:space:]], so it's not a good candidate for testing String#squish.
* | Merge pull request #16190 from oss92/word_truncationMatthew Draper2014-07-171-0/+24
|\ \ | | | | | | | | | Word truncation
| * | Added truncate_words method to activesupport stringsroot2014-07-161-0/+21
|/ /
* | The hex escape sequence can be of any lengthGodfrey Chan2014-07-021-1/+1
| |
* | Fix escape_once double-escaping hex-encoded entitiesJohn F. Douthat2014-07-021-1/+1
| | | | | | | | (This is a manual merge of #9102)
* | drastically reduce object allocationsAaron Patterson2014-06-021-6/+13
| | | | | | | | | | | | | | | | | | | | before this change, we were allocating AS::SafeBuffer objects that were being interpolated in to a string, so the safe buffer object was being thrown away. This change only allocates a string (vs a string *and* a safebuffer) and interpolates the string. On my test application, this reduced the AS::SafeBuffer objects from 1527k per request to about 500 per request.
* | reduce AS::SafeBuffer allocationsAaron Patterson2014-06-021-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | html_escape_interpolated_argument is only used in mutation methods: https://github.com/rails/rails/blob/c07d09559ec171e1904b55c7ad7e8c7d586ca51b/activesupport/lib/active_support/core_ext/string/output_safety.rb#L174 https://github.com/rails/rails/blob/c07d09559ec171e1904b55c7ad7e8c7d586ca51b/activesupport/lib/active_support/core_ext/string/output_safety.rb#L179 The return value doesn't need to be converted to an AS::SafeBuffer since we know that the current object is an AS::SafeBuffer and will be mutated, and the return value from html_escape_interpolated_argument will be thrown away
* | concat is a hotspot (via AV#append=), so just directly define the methodsAaron Patterson2014-06-021-4/+6
| |
* | Fix inconsistent behavior from String#pluralizeKuldeep Aggarwal2014-04-191-1/+1
| | | | | | | | | | | | | | | | | | | | | | Before: When calling String#pluralize with count=1 then it returned same string, but with count other than 1, returned new string. After: String#pluralize always return a new string. => Prevent mutation of a string inadvertently.
* | 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.
* | Add more test case for #demodulize, Improve documentationAkshay Vishnoi2014-04-111-0/+2
| |
* | Move require to actual fileCarlos Antonio da Silva2014-04-021-0/+1
| | | | | | | | | | Change to require all active_support/deprecation since that's the actual entry point for the deprecation methods.
* | DRY AS::SafeBuffer a bit using existing helperPavel Pravosud2014-04-021-5/+1
| |
* | Make AS::SafeBuffer#prepend act like String#prependPavel Pravosud2014-03-311-6/+13
|/ | | | | | | Make `#prepend` method modify instance in-place and return self instead of just returning modified value. That is exactly what `#prepend!` method was doing previously, so it's deprecated from now on.
* Clarify behavior of json_escape, update examplesJon Jensen2014-01-091-12/+12
| | | | | | | | The behavior of json_escape was fixed in 2f1c5789, but the doc changes and example in that commit incorrectly indicated that the return value would be html-safe. Since quotation marks are preserved, the raw value is not safe to use in other contexts (specifically HTML attributes).
* 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
* Fixes interpolation on SafeBufferJulien Letessier2013-12-141-7/+12
| | | | | | | | | Interpolation was untested and did not work with hash arguments. Adds - support for interpolation with hash argument - tests for the above - tests for safe/unsafe interpolation
* Review json_escape docs [ci skip]Carlos Antonio da Silva2013-12-041-22/+22
|
* Also move html_esacpe regex to a constant (see 9d25af60)Godfrey Chan2013-12-041-1/+2
|