2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-02-02 20:08:24 +00:00

Code tidy: replace if with case statement in QPDFTokenizer::presentCharacter

This commit is contained in:
m-holger 2022-08-18 16:53:16 +01:00
parent cf945eeabf
commit 91fb61eda5

View File

@ -198,12 +198,6 @@ QPDFTokenizer::resolveLiteral()
void void
QPDFTokenizer::presentCharacter(char ch) QPDFTokenizer::presentCharacter(char ch)
{ {
if (this->state == st_token_ready) {
throw std::logic_error(
"INTERNAL ERROR: QPDF tokenizer presented character "
"while token is waiting");
}
char orig_ch = ch; char orig_ch = ch;
// State machine is implemented such that some characters may be // State machine is implemented such that some characters may be
@ -211,7 +205,14 @@ QPDFTokenizer::presentCharacter(char ch)
// the character that caused a state change in the new state. // the character that caused a state change in the new state.
bool handled = true; bool handled = true;
if (this->state == st_top) {
switch (this->state) {
case (st_token_ready):
throw std::logic_error(
"INTERNAL ERROR: QPDF tokenizer presented character "
"while token is waiting");
case (st_top):
// Note: we specifically do not use ctype here. It is // Note: we specifically do not use ctype here. It is
// locale-dependent. // locale-dependent.
if (isSpace(ch)) { if (isSpace(ch)) {
@ -258,7 +259,9 @@ QPDFTokenizer::presentCharacter(char ch)
this->state = st_literal; this->state = st_literal;
} }
} }
} else if (this->state == st_in_space) { break;
case st_in_space:
// We only enter this state if include_ignorable is true. // We only enter this state if include_ignorable is true.
if (!isSpace(ch)) { if (!isSpace(ch)) {
this->type = tt_space; this->type = tt_space;
@ -268,7 +271,9 @@ QPDFTokenizer::presentCharacter(char ch)
} else { } else {
this->val += ch; this->val += ch;
} }
} else if (this->state == st_in_comment) { break;
case st_in_comment:
if ((ch == '\r') || (ch == '\n')) { if ((ch == '\r') || (ch == '\n')) {
if (this->include_ignorable) { if (this->include_ignorable) {
this->type = tt_comment; this->type = tt_comment;
@ -281,7 +286,9 @@ QPDFTokenizer::presentCharacter(char ch)
} else if (this->include_ignorable) { } else if (this->include_ignorable) {
this->val += ch; this->val += ch;
} }
} else if (this->state == st_lt) { break;
case st_lt:
if (ch == '<') { if (ch == '<') {
this->val += "<<"; this->val += "<<";
this->type = tt_dict_open; this->type = tt_dict_open;
@ -290,7 +297,9 @@ QPDFTokenizer::presentCharacter(char ch)
handled = false; handled = false;
this->state = st_in_hexstring; this->state = st_in_hexstring;
} }
} else if (this->state == st_gt) { break;
case st_gt:
if (ch == '>') { if (ch == '>') {
this->val += ">>"; this->val += ">>";
this->type = tt_dict_close; this->type = tt_dict_close;
@ -304,7 +313,10 @@ QPDFTokenizer::presentCharacter(char ch)
this->char_to_unread = ch; this->char_to_unread = ch;
this->state = st_token_ready; this->state = st_token_ready;
} }
} else if (this->state == st_in_string) { break;
case st_in_string:
{
if (this->string_ignoring_newline && (ch != '\n')) { if (this->string_ignoring_newline && (ch != '\n')) {
this->string_ignoring_newline = false; this->string_ignoring_newline = false;
} }
@ -314,9 +326,10 @@ QPDFTokenizer::presentCharacter(char ch)
if ((bs_num_count == 3) || ((bs_num_count > 0) && (!ch_is_octal))) { if ((bs_num_count == 3) || ((bs_num_count > 0) && (!ch_is_octal))) {
// We've accumulated \ddd. PDF Spec says to ignore // We've accumulated \ddd. PDF Spec says to ignore
// high-order overflow. // high-order overflow.
this->val += this->val += static_cast<char>(
static_cast<char>(strtol(this->bs_num_register, nullptr, 8)); strtol(this->bs_num_register, nullptr, 8));
memset(this->bs_num_register, '\0', sizeof(this->bs_num_register)); memset(
this->bs_num_register, '\0', sizeof(this->bs_num_register));
bs_num_count = 0; bs_num_count = 0;
} }
@ -387,8 +400,12 @@ QPDFTokenizer::presentCharacter(char ch)
this->last_char_was_cr = this->last_char_was_cr =
((!this->string_ignoring_newline) && (ch == '\r')); ((!this->string_ignoring_newline) && (ch == '\r'));
this->last_char_was_bs = ((!this->last_char_was_bs) && (ch == '\\')); this->last_char_was_bs =
} else if (this->state == st_literal) { ((!this->last_char_was_bs) && (ch == '\\'));
}
break;
case st_literal:
if (isDelimiter(ch)) { if (isDelimiter(ch)) {
// A C-locale whitespace character or delimiter terminates // A C-locale whitespace character or delimiter terminates
// token. It is important to unread the whitespace // token. It is important to unread the whitespace
@ -405,16 +422,19 @@ QPDFTokenizer::presentCharacter(char ch)
} else { } else {
this->val += ch; this->val += ch;
} }
} else if (this->state == st_inline_image) { break;
case st_inline_image:
this->val += ch; this->val += ch;
size_t len = this->val.length(); if (this->val.length() == this->inline_image_bytes) {
if (len == this->inline_image_bytes) {
QTC::TC("qpdf", "QPDFTokenizer found EI by byte count"); QTC::TC("qpdf", "QPDFTokenizer found EI by byte count");
this->type = tt_inline_image; this->type = tt_inline_image;
this->inline_image_bytes = 0; this->inline_image_bytes = 0;
this->state = st_token_ready; this->state = st_token_ready;
} }
} else { break;
default:
handled = false; handled = false;
} }