2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-05-28 16:00:53 +00:00

Move lexer helper functions to QUtil

This commit is contained in:
Jay Berkenbilt 2017-07-22 19:23:52 -04:00
parent 0a745021e7
commit dd8dad74f4
3 changed files with 80 additions and 60 deletions

View File

@ -157,6 +157,21 @@ namespace QUtil
// exception will be thrown.
QPDF_DLL
RandomDataProvider* getRandomDataProvider();
// These routines help the tokenizer recognize certain character
// classes without using ctype, which we avoid because of locale
// considerations.
QPDF_DLL
bool is_hex_digit(char);
QPDF_DLL
bool is_space(char);
QPDF_DLL
bool is_digit(char);
QPDF_DLL
bool is_number(char const*);
};
#endif // __QUTIL_HH__

View File

@ -6,66 +6,11 @@
#include <qpdf/QTC.hh>
#include <qpdf/QPDFExc.hh>
#include <qpdf/QUtil.hh>
#include <stdexcept>
#include <string.h>
// See note above about ctype.
static bool is_hex_digit(char ch)
{
return (strchr("0123456789abcdefABCDEF", ch) != 0);
}
static bool is_space(char ch)
{
return (strchr(" \f\n\r\t\v", ch) != 0);
}
static bool is_digit(char ch)
{
return ((ch >= '0') && (ch <= '9'));
}
static bool
is_number(std::string const& str)
{
// ^[\+\-]?(\.\d+|\d+(\.\d+)?)$
char const* p = str.c_str();
if (! *p)
{
return false;
}
if ((*p == '-') || (*p == '+'))
{
++p;
}
bool found_dot = false;
bool found_digit = false;
for (; *p; ++p)
{
if (*p == '.')
{
if (found_dot)
{
// only one dot
return false;
}
if (! *(p+1))
{
// dot can't be last
return false;
}
found_dot = true;
}
else if (is_digit(*p))
{
found_digit = true;
}
else
{
return false;
}
}
return found_digit;
}
QPDFTokenizer::QPDFTokenizer() :
pound_special_in_name(true),
allow_eof(false)
@ -117,7 +62,7 @@ QPDFTokenizer::resolveLiteral()
if ((*p == '#') && this->pound_special_in_name)
{
if (p[1] && p[2] &&
is_hex_digit(p[1]) && is_hex_digit(p[2]))
QUtil::is_hex_digit(p[1]) && QUtil::is_hex_digit(p[2]))
{
char num[3];
num[0] = p[1];
@ -153,7 +98,7 @@ QPDFTokenizer::resolveLiteral()
}
val = nval;
}
else if (is_number(val))
else if (QUtil::is_number(val.c_str()))
{
if (val.find('.') != std::string::npos)
{
@ -447,7 +392,7 @@ QPDFTokenizer::presentCharacter(char ch)
}
val = nval;
}
else if (is_hex_digit(ch))
else if (QUtil::is_hex_digit(ch))
{
val += ch;
}
@ -554,7 +499,7 @@ QPDFTokenizer::readToken(PointerHolder<InputSource> input,
}
else
{
if (is_space(static_cast<unsigned char>(ch)) &&
if (QUtil::is_space(static_cast<unsigned char>(ch)) &&
(input->getLastOffset() == offset))
{
++offset;

View File

@ -456,3 +456,63 @@ QUtil::srandom(unsigned int seed)
srand(seed);
#endif
}
bool
QUtil::is_hex_digit(char ch)
{
return (strchr("0123456789abcdefABCDEF", ch) != 0);
}
bool
QUtil::is_space(char ch)
{
return (strchr(" \f\n\r\t\v", ch) != 0);
}
bool
QUtil::is_digit(char ch)
{
return ((ch >= '0') && (ch <= '9'));
}
bool
QUtil::is_number(char const* p)
{
// ^[\+\-]?(\.\d+|\d+(\.\d+)?)$
if (! *p)
{
return false;
}
if ((*p == '-') || (*p == '+'))
{
++p;
}
bool found_dot = false;
bool found_digit = false;
for (; *p; ++p)
{
if (*p == '.')
{
if (found_dot)
{
// only one dot
return false;
}
if (! *(p+1))
{
// dot can't be last
return false;
}
found_dot = true;
}
else if (QUtil::is_digit(*p))
{
found_digit = true;
}
else
{
return false;
}
}
return found_digit;
}