speedup in iskword
This commit is contained in:
@@ -116,7 +116,7 @@ when executed, the output will be:
|
|||||||
The code is very portable so it's possible to run it everywhere, although the build proces was only tested on Linux.
|
The code is very portable so it's possible to run it everywhere, although the build proces was only tested on Linux.
|
||||||
|
|
||||||
## Install the library
|
## Install the library
|
||||||
There is no particular way to install the library. The code is so small that you can just drop `c2html.c` and `c2html.h` in your project and use then as they were your own.
|
There is no particular way to install the library. The code is so small that you can just drop `c2html.c` and `c2html.h` in your project and use them as they were your own.
|
||||||
|
|
||||||
## Install the command-line interface
|
## Install the command-line interface
|
||||||
To build the CLI, run
|
To build the CLI, run
|
||||||
|
|||||||
@@ -78,21 +78,28 @@ static bool isoperat(char c)
|
|||||||
|
|
||||||
static bool iskword(const char *str, long len)
|
static bool iskword(const char *str, long len)
|
||||||
{
|
{
|
||||||
static const char *keywords[] = {
|
static const struct {
|
||||||
"auto", "break", "case", "char",
|
int len;
|
||||||
"const", "continue", "default",
|
char str[9]; // Maximum length of a keyword.
|
||||||
"do", "double", "else", "enum",
|
} keywords[] = {
|
||||||
"extern", "float", "for", "goto",
|
#define KWORD(lit) { sizeof(lit)-1, lit }
|
||||||
"if", "int", "long", "register",
|
KWORD("auto"), KWORD("break"), KWORD("case"),
|
||||||
"return", "short", "signed",
|
KWORD("char"), KWORD("const"), KWORD("continue"),
|
||||||
"sizeof", "static", "struct",
|
KWORD("default"), KWORD("do"), KWORD("double"),
|
||||||
"switch", "typedef", "union",
|
KWORD("else"), KWORD("enum"), KWORD("extern"),
|
||||||
"unsigned", "void", "volatile",
|
KWORD("float"), KWORD("for"), KWORD("goto"),
|
||||||
"while"
|
KWORD("if"), KWORD("int"), KWORD("long"),
|
||||||
|
KWORD("register"), KWORD("return"), KWORD("short"),
|
||||||
|
KWORD("signed"), KWORD("sizeof"), KWORD("static"),
|
||||||
|
KWORD("struct"), KWORD("switch"), KWORD("typedef"),
|
||||||
|
KWORD("union"), KWORD("unsigned"),KWORD("void"),
|
||||||
|
KWORD("volatile"), KWORD("while"),
|
||||||
|
#undef KWORD
|
||||||
};
|
};
|
||||||
|
|
||||||
const int num_keywords = sizeof(keywords)/sizeof(keywords[0]);
|
const int num_keywords = sizeof(keywords)/sizeof(keywords[0]);
|
||||||
for(int i = 0; i < num_keywords; i += 1)
|
for(int i = 0; i < num_keywords; i += 1)
|
||||||
if((int) strlen(keywords[i]) == len && !strncmp(keywords[i], str, len))
|
if(keywords[i].len == len && !strncmp(keywords[i].str, str, len))
|
||||||
return 1;
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,39 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "c2html.h"
|
#include "c2html.h"
|
||||||
|
|
||||||
|
#ifdef C2H_TIMING
|
||||||
|
#include <time.h>
|
||||||
|
char *timed_c2html(const char *str, long len,
|
||||||
|
const char *prefix, const char **error)
|
||||||
|
{
|
||||||
|
if(str == NULL)
|
||||||
|
str = "";
|
||||||
|
|
||||||
|
if(len < 0)
|
||||||
|
len = strlen(str);
|
||||||
|
|
||||||
|
clock_t beg, end;
|
||||||
|
char *res;
|
||||||
|
|
||||||
|
beg = clock();
|
||||||
|
res = c2html(str, len, prefix, error);
|
||||||
|
end = clock();
|
||||||
|
|
||||||
|
double time_spent = (double) (end - beg)
|
||||||
|
/ CLOCKS_PER_SEC;
|
||||||
|
fprintf(stdout,
|
||||||
|
"-- Timing results ----\n"
|
||||||
|
"Size: %ldb\n"
|
||||||
|
"Time: %fs\n"
|
||||||
|
"Rate: %.2f Mb/s\n"
|
||||||
|
"----------------------\n",
|
||||||
|
len, time_spent,
|
||||||
|
(double) len/(time_spent * 1000000));
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
#define c2html timed_c2html
|
||||||
|
#endif
|
||||||
|
|
||||||
static char *load_file(const char *file, long *size)
|
static char *load_file(const char *file, long *size)
|
||||||
{
|
{
|
||||||
FILE *fp = fopen(file, "rb");
|
FILE *fp = fopen(file, "rb");
|
||||||
@@ -88,7 +121,6 @@ static int fileconv(const char *input_file, const char *output_file,
|
|||||||
/* ..and write the converted string to it */
|
/* ..and write the converted string to it */
|
||||||
fwrite(output, 1, strlen(output), fp);
|
fwrite(output, 1, strlen(output), fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
fprintf(stderr, "OK\n");
|
|
||||||
|
|
||||||
/* All done! :^) */
|
/* All done! :^) */
|
||||||
free(output);
|
free(output);
|
||||||
@@ -107,7 +139,7 @@ static void print_help(FILE *fp, char *name) {
|
|||||||
" to the generated output, you get the highliting!\n"
|
" to the generated output, you get the highliting!\n"
|
||||||
"\n"
|
"\n"
|
||||||
" The usage is:\n"
|
" The usage is:\n"
|
||||||
" $ %s -i file.c -o file.html [--no-table] [--style file.css] [-p <prefix>]\n"
|
" $ %s -i file.c -o file.html [--style file.css] [-p <prefix>]\n"
|
||||||
"\n"
|
"\n"
|
||||||
" ..and here's a table of all available options:\n"
|
" ..and here's a table of all available options:\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user