2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/Buffer.hh>
|
2017-08-06 12:42:01 +00:00
|
|
|
#include <qpdf/BufferInputSource.hh>
|
|
|
|
#include <qpdf/QPDFTokenizer.hh>
|
2019-06-22 01:49:12 +00:00
|
|
|
#include <cstring>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <iostream>
|
2017-08-06 12:42:01 +00:00
|
|
|
|
2022-04-09 18:49:10 +00:00
|
|
|
static std::shared_ptr<Buffer>
|
2017-08-06 12:42:01 +00:00
|
|
|
get_buffer()
|
|
|
|
{
|
|
|
|
size_t size = 3172;
|
2022-04-09 18:49:10 +00:00
|
|
|
std::shared_ptr<Buffer> b(new Buffer(size));
|
2017-08-06 12:42:01 +00:00
|
|
|
unsigned char* p = b->getBuffer();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2017-08-06 12:42:01 +00:00
|
|
|
p[i] = static_cast<unsigned char>(i & 0xff);
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Finder: public InputSource::Finder
|
|
|
|
{
|
|
|
|
public:
|
2022-04-09 18:49:10 +00:00
|
|
|
Finder(std::shared_ptr<InputSource> is, std::string const& after) :
|
2017-08-06 12:42:01 +00:00
|
|
|
is(is),
|
|
|
|
after(after)
|
|
|
|
{
|
|
|
|
}
|
2023-05-20 13:25:46 +00:00
|
|
|
~Finder() override = default;
|
|
|
|
bool check() override;
|
2017-08-06 12:42:01 +00:00
|
|
|
|
|
|
|
private:
|
2022-04-09 18:49:10 +00:00
|
|
|
std::shared_ptr<InputSource> is;
|
2017-08-06 12:42:01 +00:00
|
|
|
std::string after;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
Finder::check()
|
|
|
|
{
|
|
|
|
QPDFTokenizer tokenizer;
|
|
|
|
QPDFTokenizer::Token t = tokenizer.readToken(is, "finder", true);
|
2022-09-29 13:33:11 +00:00
|
|
|
if (t.isWord("potato")) {
|
2017-08-06 12:42:01 +00:00
|
|
|
t = tokenizer.readToken(is, "finder", true);
|
2022-09-29 13:33:11 +00:00
|
|
|
return t.isWord(after);
|
2017-08-06 12:42:01 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
check(char const* description, bool expected, bool actual)
|
2017-08-06 12:42:01 +00:00
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
std::cout << description << ": " << ((actual == expected) ? "PASS" : "FAIL") << std::endl;
|
2017-08-06 12:42:01 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
int
|
|
|
|
main()
|
2017-08-06 12:42:01 +00:00
|
|
|
{
|
2022-04-09 18:49:10 +00:00
|
|
|
std::shared_ptr<Buffer> b1 = get_buffer();
|
2017-08-06 12:42:01 +00:00
|
|
|
unsigned char* b = b1->getBuffer();
|
|
|
|
// Straddle block boundaries
|
|
|
|
memcpy(b + 1022, "potato", 6);
|
|
|
|
// Overlap so that the first check() would advance past the start
|
|
|
|
// of the next match
|
|
|
|
memcpy(b + 2037, "potato potato salad ", 20);
|
2023-05-21 17:35:09 +00:00
|
|
|
auto is =
|
|
|
|
std::shared_ptr<InputSource>(new BufferInputSource("test buffer input source", b1.get()));
|
2017-08-06 12:42:01 +00:00
|
|
|
Finder f1(is, "salad");
|
2022-04-02 21:14:10 +00:00
|
|
|
check("find potato salad", true, is->findFirst("potato", 0, 0, f1));
|
2023-05-21 17:35:09 +00:00
|
|
|
check("barely find potato salad", true, is->findFirst("potato", 1100, 945, f1));
|
|
|
|
check("barely find potato salad", true, is->findFirst("potato", 2000, 45, f1));
|
|
|
|
check("potato salad is too late", false, is->findFirst("potato", 1100, 944, f1));
|
|
|
|
check("potato salad is too late", false, is->findFirst("potato", 2000, 44, f1));
|
|
|
|
check("potato salad not found", false, is->findFirst("potato", 2045, 0, f1));
|
2022-04-02 21:14:10 +00:00
|
|
|
check("potato salad not found", false, is->findFirst("potato", 0, 1, f1));
|
2017-08-06 12:42:01 +00:00
|
|
|
|
|
|
|
// Put one more right at EOF
|
|
|
|
memcpy(b + b1->getSize() - 12, "potato salad", 12);
|
2022-04-02 21:14:10 +00:00
|
|
|
check("potato salad at EOF", true, is->findFirst("potato", 3000, 0, f1));
|
2017-08-06 12:42:01 +00:00
|
|
|
|
|
|
|
is->findFirst("potato", 0, 0, f1);
|
2022-04-02 21:14:10 +00:00
|
|
|
check("findFirst found first", true, is->tell() == 2056);
|
2023-05-21 17:35:09 +00:00
|
|
|
check("findLast found potato salad", true, is->findLast("potato", 0, 0, f1));
|
2022-04-02 21:14:10 +00:00
|
|
|
check("findLast found at EOF", true, is->tell() == 3172);
|
2017-08-06 12:42:01 +00:00
|
|
|
|
|
|
|
// Make check() bump into EOF
|
|
|
|
memcpy(b + b1->getSize() - 6, "potato", 6);
|
2023-05-21 17:35:09 +00:00
|
|
|
check("potato but not salad salad at EOF", false, is->findFirst("potato", 3000, 0, f1));
|
|
|
|
check("findLast found potato salad", true, is->findLast("potato", 0, 0, f1));
|
2022-04-02 21:14:10 +00:00
|
|
|
check("findLast found first one", true, is->tell() == 2056);
|
2017-08-06 12:42:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|