aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2017-07-23 01:23:52 +0200
committerJay Berkenbilt <ejb@ql.org>2017-07-27 19:59:56 +0200
commitdd8dad74f47b6068281dd605a04bc2d0b6283423 (patch)
treedb7d72b522591b4f47bf44569713dc9752416b63
parent0a745021e7d6676ded2a344134b68b180fb3be60 (diff)
downloadqpdf-dd8dad74f47b6068281dd605a04bc2d0b6283423.tar.zst
Move lexer helper functions to QUtil
-rw-r--r--include/qpdf/QUtil.hh15
-rw-r--r--libqpdf/QPDFTokenizer.cc65
-rw-r--r--libqpdf/QUtil.cc60
3 files changed, 80 insertions, 60 deletions
diff --git a/include/qpdf/QUtil.hh b/include/qpdf/QUtil.hh
index f92cc7fc..eba4ef61 100644
--- a/include/qpdf/QUtil.hh
+++ b/include/qpdf/QUtil.hh
@@ -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__
diff --git a/libqpdf/QPDFTokenizer.cc b/libqpdf/QPDFTokenizer.cc
index 90961006..40f04f34 100644
--- a/libqpdf/QPDFTokenizer.cc
+++ b/libqpdf/QPDFTokenizer.cc
@@ -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;
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc
index 37cfda9e..86de07f2 100644
--- a/libqpdf/QUtil.cc
+++ b/libqpdf/QUtil.cc
@@ -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;
+}