aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2016-04-21 09:18:35 +0200
committerXavier Noria <fxn@hashref.com>2016-04-21 09:25:38 +0200
commit0f8eefa6a581f1f04a1258342419c2896f9062ab (patch)
treeb0c12d1f883a88140381b6aa3773c8b1255a15da /activesupport
parentfe9e9f439095a81c5b22c35cd3cbb5cef479b426 (diff)
downloadrails-0f8eefa6a581f1f04a1258342419c2896f9062ab.tar.gz
rails-0f8eefa6a581f1f04a1258342419c2896f9062ab.tar.bz2
rails-0f8eefa6a581f1f04a1258342419c2896f9062ab.zip
restores code comments in String#blank? [ci skip]
When you come here without context, it is important to hightlight that checking the predicate is worthwhile due to the observation that blank strings are often empty. So you complicate the code (which has a cost in terms of readability and aesthetics), but statistically makes sense. Then, you also need to explain why the second operand is so convoluted. Otherwise, you wonder why this line is written precisely this way. That is what code comments are for.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/object/blank.rb8
1 files changed, 6 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb
index 1b94dee6cc..d6bad98376 100644
--- a/activesupport/lib/active_support/core_ext/object/blank.rb
+++ b/activesupport/lib/active_support/core_ext/object/blank.rb
@@ -110,8 +110,12 @@ class String
#
# @return [true, false]
def blank?
- # Regex check is slow, only check non-empty strings.
- # A string not blank if it contains a single non-space string.
+ # In practice, the majority of blank strings are empty. The predicate is
+ # about 3.5x faster than the regexp check so we first test empty?, and then
+ # fallback. Penalty for the rest of strings is marginal.
+ #
+ # Double negation in the second operand is also a performance tweak, it is
+ # faster than the positive \A[[:space:]]*\z due to lack of backtracking.
empty? || !(/[[:^space:]]/ === self)
end
end