mirror of
https://github.com/qpdf/qpdf.git
synced 2024-11-15 17:17:08 +00:00
cb769c62e5
This comment expands all tabs using an 8-character tab-width. You should ignore this commit when using git blame or use git blame -w. In the early days, I used to use tabs where possible for indentation, since emacs did this automatically. In recent years, I have switched to only using spaces, which means qpdf source code has been a mixture of spaces and tabs. I have avoided cleaning this up because of not wanting gratuitous whitespaces change to cloud the output of git blame, but I changed my mind after discussing with users who view qpdf source code in editors/IDEs that have other tab widths by default and in light of the fact that I am planning to start applying automatic code formatting soon.
97 lines
2.3 KiB
C++
97 lines
2.3 KiB
C++
#include <qpdf/ContentNormalizer.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
ContentNormalizer::ContentNormalizer() :
|
|
any_bad_tokens(false),
|
|
last_token_was_bad(false)
|
|
{
|
|
}
|
|
|
|
ContentNormalizer::~ContentNormalizer()
|
|
{
|
|
}
|
|
|
|
void
|
|
ContentNormalizer::handleToken(QPDFTokenizer::Token const& token)
|
|
{
|
|
std::string value = token.getRawValue();
|
|
QPDFTokenizer::token_type_e token_type = token.getType();
|
|
|
|
if (token_type == QPDFTokenizer::tt_bad)
|
|
{
|
|
this->any_bad_tokens = true;
|
|
this->last_token_was_bad = true;
|
|
}
|
|
else if (token_type != QPDFTokenizer::tt_eof)
|
|
{
|
|
this->last_token_was_bad = false;
|
|
}
|
|
|
|
switch (token_type)
|
|
{
|
|
case QPDFTokenizer::tt_space:
|
|
{
|
|
size_t len = value.length();
|
|
for (size_t i = 0; i < len; ++i)
|
|
{
|
|
char ch = value.at(i);
|
|
if (ch == '\r')
|
|
{
|
|
if ((i + 1 < len) && (value.at(i + 1) == '\n'))
|
|
{
|
|
// ignore
|
|
}
|
|
else
|
|
{
|
|
write("\n");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
write(&ch, 1);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
case QPDFTokenizer::tt_string:
|
|
// Replacing string and name tokens in this way normalizes
|
|
// their representation as this will automatically handle
|
|
// quoting of unprintable characters, etc.
|
|
writeToken(QPDFTokenizer::Token(
|
|
QPDFTokenizer::tt_string, token.getValue()));
|
|
break;
|
|
|
|
case QPDFTokenizer::tt_name:
|
|
writeToken(QPDFTokenizer::Token(
|
|
QPDFTokenizer::tt_name, token.getValue()));
|
|
break;
|
|
|
|
default:
|
|
writeToken(token);
|
|
break;
|
|
}
|
|
|
|
value = token.getRawValue();
|
|
if (((token_type == QPDFTokenizer::tt_string) ||
|
|
(token_type == QPDFTokenizer::tt_name)) &&
|
|
((value.find('\r') != std::string::npos) ||
|
|
(value.find('\n') != std::string::npos)))
|
|
{
|
|
write("\n");
|
|
}
|
|
}
|
|
|
|
bool
|
|
ContentNormalizer::anyBadTokens() const
|
|
{
|
|
return this->any_bad_tokens;
|
|
}
|
|
|
|
bool
|
|
ContentNormalizer::lastTokenWasBad()const
|
|
{
|
|
return this->last_token_was_bad;
|
|
}
|