jenkins-bot has submitted this change and it was merged.
Change subject: Optimize 'count()' function
......................................................................
Optimize 'count()' function
substr_count() is just as fast as looped strpos() when there are no
matches, and gets faster as the number of matches increases.
Note that this introduces a small change in behavior when the needle
is composed of repeated substrings, e.g. 'asdasdasd' or 'aa', and
haystack is such that the needle can be matched in overlapping
positions, e.g. 'asdasdasdasd' or 'aaaaa'. The old implementation
counted overlapping matches, the new one doesn't. I don't think this
behavior was intentional and I don't think this change will cause any
real problems.
Change-Id: Icc905ca34bf08d63e969787a5e3c119d498bf878
---
M AbuseFilter.parser.php
M tests/parserTests/count.t
2 files changed, 5 insertions(+), 10 deletions(-)
Approvals:
Aaron Schulz: Looks good to me, approved
jenkins-bot: Verified
diff --git a/AbuseFilter.parser.php b/AbuseFilter.parser.php
index 50dea87..6a0d382 100644
--- a/AbuseFilter.parser.php
+++ b/AbuseFilter.parser.php
@@ -1525,21 +1525,17 @@
return new AFPData( AFPData::DINT, count(
$args[0]->data ) );
}
- $offset = -1;
-
if ( count( $args ) == 1 ) {
$count = count( explode( ',', $args[0]->toString() ) );
} else {
$needle = $args[0]->toString();
$haystack = $args[1]->toString();
- $count = 0;
-
// Bug #60203: Keep empty parameters from causing PHP
warnings
- if ( $needle !== '' ) {
- while ( ( $offset = strpos( $haystack, $needle,
$offset + 1 ) ) !== false ) {
- $count++;
- }
+ if ( $needle === '' ) {
+ $count = 0;
+ } else {
+ $count = substr_count( $haystack, $needle );
}
}
diff --git a/tests/parserTests/count.t b/tests/parserTests/count.t
index bad1dd0..5733b0b 100644
--- a/tests/parserTests/count.t
+++ b/tests/parserTests/count.t
@@ -3,5 +3,4 @@
count("", "abcd") = 0 &
count("a", "abab") = 2 &
count("ab", "abab") = 2 &
-/* This probably shouldn't count overlapping occurrences... */
-count("aa", "aaaaa") = 4
+count("aa", "aaaaa") = 2
--
To view, visit https://gerrit.wikimedia.org/r/283843
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Icc905ca34bf08d63e969787a5e3c119d498bf878
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/AbuseFilter
Gerrit-Branch: master
Gerrit-Owner: Bartosz DziewoĆski <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Jackmcbarn <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Se4598 <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits