Skip to content

Commit

Permalink
Fixes #178, quadratic parsing bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgm committed Jan 4, 2017
1 parent b5c4a22 commit a8d5bd4
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/inlines.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,16 @@ static void process_emphasis(subject *subj, delimiter *stack_bottom) {
delimiter *old_closer;
bool opener_found;
bool odd_match;
delimiter *openers_bottom[128];
delimiter *openers_bottom[3][128];
int i;

// initialize openers_bottom:
openers_bottom['*'] = stack_bottom;
openers_bottom['_'] = stack_bottom;
openers_bottom['\''] = stack_bottom;
openers_bottom['"'] = stack_bottom;
for (i=0; i < 3; i++) {
openers_bottom[i]['*'] = stack_bottom;
openers_bottom[i]['_'] = stack_bottom;
openers_bottom[i]['\''] = stack_bottom;
openers_bottom[i]['"'] = stack_bottom;
}

// move back to first relevant delim.
while (closer != NULL && closer->previous != stack_bottom) {
Expand All @@ -537,7 +540,7 @@ static void process_emphasis(subject *subj, delimiter *stack_bottom) {
opener_found = false;
odd_match = false;
while (opener != NULL && opener != stack_bottom &&
opener != openers_bottom[closer->delim_char]) {
opener != openers_bottom[closer->length % 3][closer->delim_char]) {
if (opener->can_open && opener->delim_char == closer->delim_char) {
// interior closer of size 2 can't match opener of size 1
// or of size 1 can't match 2
Expand Down Expand Up @@ -574,13 +577,10 @@ static void process_emphasis(subject *subj, delimiter *stack_bottom) {
}
closer = closer->next;
}
if (!opener_found && !odd_match) {
if (!opener_found) {
// set lower bound for future searches for openers
// (we don't do this with 'odd_match' set because
// a ** that didn't match an earlier * might turn into
// an opener, and the * might be matched by something
// else.
openers_bottom[old_closer->delim_char] = old_closer->previous;
openers_bottom[old_closer->length % 3][old_closer->delim_char] =
old_closer->previous;
if (!old_closer->can_open) {
// we can remove a closer that can't be an
// opener, once we've seen there's no
Expand Down

0 comments on commit a8d5bd4

Please sign in to comment.