Merge pull request #43106 from flavorjones/flavorjones-activesupport-number-converter-valid-float

Avoid use of exceptions to detect invalid floats
This commit is contained in:
Jean Boussier 2021-08-26 15:41:10 +02:00 committed by GitHub
commit 4d3a2c09c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 8 deletions

View File

@ -1,3 +1,8 @@
* Improves the performance of ActionView::Helpers::NumberHelper formatters by avoiding the use of
exceptions as flow control.
*Mike Dalessio*
* `preload_link_tag` properly inserts `as` attributes for files with `image` MIME types, such as JPG or SVG.
*Nate Berkopec*

View File

@ -448,9 +448,8 @@ module ActionView
end
def parse_float(number, raise_error)
Float(number)
rescue ArgumentError, TypeError
raise InvalidNumberError, number if raise_error
result = Float(number, exception: false)
raise InvalidNumberError, number if result.nil? && raise_error
end
end
end

View File

@ -1,3 +1,8 @@
* Improves the performance of ActiveSupport::NumberHelper formatters by avoiding the use of
exceptions as flow control.
*Mike Dalessio*
* Removed rescue block from `ActiveSupport::Cache::RedisCacheStore#handle_exception`
Previously, if you provided a `error_handler` to `redis_cache_store`, any errors thrown by

View File

@ -99,8 +99,6 @@ module ActiveSupport
# number_to_currency(1234567890.506, locale: :fr) # => "1 234 567 890,51 €"
# number_to_currency('123a456') # => "$123a456"
#
# number_to_currency("123a456", raise: true) # => InvalidNumberError
#
# number_to_currency(-0.456789, precision: 0)
# # => "$0"
# number_to_currency(-1234567890.50, negative_format: '(%u%n)')

View File

@ -174,9 +174,7 @@ module ActiveSupport
end
def valid_float?
Float(number)
rescue ArgumentError, TypeError
false
Float(number, exception: false)
end
end
end