diff --git a/mwparserfromhell/parser/tokenizer.c b/mwparserfromhell/parser/tokenizer.c index 939f30c..df65d0e 100644 --- a/mwparserfromhell/parser/tokenizer.c +++ b/mwparserfromhell/parser/tokenizer.c @@ -23,9 +23,16 @@ SOFTWARE. #include "tokenizer.h" -double log2(double n) +/* + Given a context, return the heading level encoded within it. +*/ +static int heading_level_from_context(int n) { - return log(n) / log(2); + int level; + n /= LC_HEADING_LEVEL_1; + for (level = 1; n > 1; n >>= 1) + level++; + return level; } static PyObject* @@ -175,6 +182,9 @@ Tokenizer_push_textbuffer(Tokenizer* self) return 0; } +/* + Pop and deallocate the top token stack/context/textbuffer. +*/ static void Tokenizer_delete_top_of_stack(Tokenizer* self) { @@ -858,7 +868,7 @@ Tokenizer_handle_heading_end(Tokenizer* self) best++; self->head++; } - current = log2(self->topstack->context / LC_HEADING_LEVEL_1) + 1; + current = heading_level_from_context(self->topstack->context); level = current > best ? (best > 6 ? 6 : best) : (current > 6 ? 6 : current); after = (HeadingData*) Tokenizer_parse(self, self->topstack->context); diff --git a/mwparserfromhell/parser/tokenizer.h b/mwparserfromhell/parser/tokenizer.h index cdc0cca..1f58c49 100644 --- a/mwparserfromhell/parser/tokenizer.h +++ b/mwparserfromhell/parser/tokenizer.h @@ -181,6 +181,7 @@ typedef struct { /* Function prototypes: */ +static int heading_level_from_context(int); static PyObject* Tokenizer_new(PyTypeObject*, PyObject*, PyObject*); static struct Textbuffer* Textbuffer_new(void); static void Tokenizer_dealloc(Tokenizer*);