Commit fe68f3f2 authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

WTF: Add String::Find() for base::RepeatingCallback

We'd like to pass an instance method to Find() in some cases.
We already have Find() for a function pointer, but it can't accept
instance methods.  So this CL introduces a Find() overload for
base::RepeatingCallback.

This CL has no behavior changes.

Change-Id: I0c127b872ad01ba81f8045026ce99e0e29b923a8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2325455
Commit-Queue: Kent Tamura <tkent@chromium.org>
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792619}
parent 1664d7d0
......@@ -27,6 +27,7 @@
#include <algorithm>
#include <memory>
#include "base/callback.h"
#include "third_party/blink/renderer/platform/wtf/allocator/partitions.h"
#include "third_party/blink/renderer/platform/wtf/dynamic_annotations.h"
#include "third_party/blink/renderer/platform/wtf/leak_annotations.h"
......@@ -764,6 +765,26 @@ wtf_size_t StringImpl::Find(CharacterMatchFunctionPtr match_function,
return WTF::Find(Characters16(), length_, match_function, start);
}
wtf_size_t StringImpl::Find(base::RepeatingCallback<bool(UChar)> match_callback,
wtf_size_t index) const {
if (Is8Bit()) {
const LChar* characters8 = Characters8();
while (index < length_) {
if (match_callback.Run(characters8[index]))
return index;
++index;
}
return kNotFound;
}
const UChar* characters16 = Characters16();
while (index < length_) {
if (match_callback.Run(characters16[index]))
return index;
++index;
}
return kNotFound;
}
template <typename SearchCharacterType, typename MatchCharacterType>
ALWAYS_INLINE static wtf_size_t FindInternal(
const SearchCharacterType* search_characters,
......
......@@ -28,6 +28,7 @@
#include <string.h>
#include <atomic>
#include "base/callback_forward.h"
#include "base/containers/span.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
......@@ -371,6 +372,8 @@ class WTF_EXPORT StringImpl {
wtf_size_t Find(char character, wtf_size_t start = 0);
wtf_size_t Find(UChar character, wtf_size_t start = 0);
wtf_size_t Find(CharacterMatchFunctionPtr, wtf_size_t index = 0);
wtf_size_t Find(base::RepeatingCallback<bool(UChar)> match_callback,
wtf_size_t index = 0) const;
// Find substrings.
wtf_size_t Find(const StringView&, wtf_size_t index = 0);
......
......@@ -25,6 +25,7 @@
#include <locale.h>
#include <stdarg.h>
#include <algorithm>
#include "base/callback.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/wtf/dtoa.h"
......@@ -77,6 +78,11 @@ int CodeUnitCompareIgnoringASCIICase(const String& a, const char* b) {
reinterpret_cast<const LChar*>(b));
}
wtf_size_t String::Find(base::RepeatingCallback<bool(UChar)> match_callback,
wtf_size_t index) const {
return impl_ ? impl_->Find(match_callback, index) : kNotFound;
}
UChar32 String::CharacterStartingAt(unsigned i) const {
if (!impl_ || i >= impl_->length())
return 0;
......
......@@ -206,6 +206,8 @@ class WTF_EXPORT String {
unsigned start = 0) const {
return impl_ ? impl_->Find(match_function, start) : kNotFound;
}
wtf_size_t Find(base::RepeatingCallback<bool(UChar)> match_callback,
wtf_size_t index = 0) const;
// Find substrings.
wtf_size_t Find(
......
......@@ -29,6 +29,7 @@
#include "base/stl_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
namespace WTF {
......@@ -329,4 +330,26 @@ TEST(StringTest, StringPrinter) {
String(kUnicodeSample, base::size(kUnicodeSample))));
}
class TestMatcher {
public:
explicit TestMatcher(UChar target) : target_(target) {}
bool IsTarget(UChar ch) { return ch == target_; }
private:
UChar target_;
};
TEST(StringTest, FindWithCallback) {
String test_string1("abc");
String test_string2("stu");
// An instance method.
TestMatcher matcher('t');
auto callback =
WTF::BindRepeating(&TestMatcher::IsTarget, WTF::Passed(&matcher));
EXPECT_EQ(WTF::kNotFound, test_string1.Find(callback));
EXPECT_EQ(1U, test_string2.Find(callback));
}
} // namespace WTF
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment