Move PDF text search code into its own standalone function.
The code in PdfViewWebPlugin::SearchString() has little to do with the rest of PdfViewWebPlugin. Make it a standalone function, and use it to replace the text search code used for testing in FindTextTestClient. The function can also be easily unit tested in the future. Change-Id: Ic359757c0c299be1b7f792158902c60e391bd916 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4137334 Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com> Reviewed-by: Alan Screen <awscreen@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org> Cr-Commit-Position: refs/heads/main@{#1369464}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
859def36a9
commit
68cd51a0be
@ -139,6 +139,8 @@ if (enable_pdf) {
|
||||
"pdfium/pdfium_unsupported_features.h",
|
||||
"preview_mode_client.cc",
|
||||
"preview_mode_client.h",
|
||||
"text_search.cc",
|
||||
"text_search.h",
|
||||
"ui/document_properties.cc",
|
||||
"ui/document_properties.h",
|
||||
"ui/file_name.cc",
|
||||
|
@ -29,7 +29,6 @@
|
||||
#include "base/functional/callback.h"
|
||||
#include "base/i18n/char_iterator.h"
|
||||
#include "base/i18n/rtl.h"
|
||||
#include "base/i18n/string_search.h"
|
||||
#include "base/i18n/time_formatting.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/memory/raw_ptr.h"
|
||||
@ -70,6 +69,7 @@
|
||||
#include "pdf/pdfium/pdfium_engine.h"
|
||||
#include "pdf/pdfium/pdfium_engine_client.h"
|
||||
#include "pdf/post_message_receiver.h"
|
||||
#include "pdf/text_search.h"
|
||||
#include "pdf/ui/document_properties.h"
|
||||
#include "pdf/ui/file_name.h"
|
||||
#include "pdf/ui/thumbnail.h"
|
||||
@ -1157,14 +1157,7 @@ std::vector<PDFiumEngineClient::SearchStringResult>
|
||||
PdfViewWebPlugin::SearchString(const std::u16string& needle,
|
||||
const std::u16string& haystack,
|
||||
bool case_sensitive) {
|
||||
base::i18n::RepeatingStringSearch searcher(
|
||||
/*find_this=*/needle, /*in_this=*/haystack, case_sensitive);
|
||||
std::vector<SearchStringResult> results;
|
||||
int match_index;
|
||||
int match_length;
|
||||
while (searcher.NextMatchResult(match_index, match_length))
|
||||
results.push_back({.start_index = match_index, .length = match_length});
|
||||
return results;
|
||||
return TextSearch(/*needle=*/needle, /*haystack=*/haystack, case_sensitive);
|
||||
}
|
||||
|
||||
void PdfViewWebPlugin::DocumentLoadComplete() {
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "pdf/pdfium/pdfium_engine.h"
|
||||
#include "pdf/pdfium/pdfium_test_base.h"
|
||||
#include "pdf/test/test_client.h"
|
||||
#include "pdf/text_search.h"
|
||||
#include "testing/gmock/include/gmock/gmock.h"
|
||||
|
||||
using testing::_;
|
||||
@ -37,22 +38,7 @@ class FindTextTestClient : public TestClient {
|
||||
EXPECT_FALSE(needle.empty());
|
||||
EXPECT_FALSE(haystack.empty());
|
||||
EXPECT_EQ(case_sensitive, expected_case_sensitive_);
|
||||
|
||||
std::vector<SearchStringResult> results;
|
||||
|
||||
size_t pos = 0;
|
||||
while (true) {
|
||||
pos = haystack.find(needle, pos);
|
||||
if (pos == std::u16string::npos)
|
||||
break;
|
||||
|
||||
SearchStringResult result;
|
||||
result.length = needle.size();
|
||||
result.start_index = pos;
|
||||
results.push_back(result);
|
||||
pos += needle.size();
|
||||
}
|
||||
return results;
|
||||
return TextSearch(/*needle=*/needle, /*haystack=*/haystack, case_sensitive);
|
||||
}
|
||||
|
||||
private:
|
||||
|
30
pdf/text_search.cc
Normal file
30
pdf/text_search.cc
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright 2024 The Chromium Authors
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "pdf/text_search.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/i18n/string_search.h"
|
||||
#include "pdf/pdfium/pdfium_engine_client.h"
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
std::vector<PDFiumEngineClient::SearchStringResult> TextSearch(
|
||||
const std::u16string& needle,
|
||||
const std::u16string& haystack,
|
||||
bool case_sensitive) {
|
||||
base::i18n::RepeatingStringSearch searcher(
|
||||
/*find_this=*/needle, /*in_this=*/haystack, case_sensitive);
|
||||
std::vector<PDFiumEngineClient::SearchStringResult> results;
|
||||
int match_index;
|
||||
int match_length;
|
||||
while (searcher.NextMatchResult(match_index, match_length)) {
|
||||
results.push_back({.start_index = match_index, .length = match_length});
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
} // namespace chrome_pdf
|
22
pdf/text_search.h
Normal file
22
pdf/text_search.h
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright 2024 The Chromium Authors
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef PDF_TEXT_SEARCH_H_
|
||||
#define PDF_TEXT_SEARCH_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "pdf/pdfium/pdfium_engine_client.h"
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
std::vector<PDFiumEngineClient::SearchStringResult> TextSearch(
|
||||
const std::u16string& needle,
|
||||
const std::u16string& haystack,
|
||||
bool case_sensitive);
|
||||
|
||||
} // namespace chrome_pdf
|
||||
|
||||
#endif // PDF_TEXT_SEARCH_H_
|
Reference in New Issue
Block a user