summaryrefslogtreecommitdiffstats
path: root/tags.h
blob: b140ef36b654960fd4a4f705f79e3db7e4a74714 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* Generated file, do not edit */

#ifndef TAGS_H
#define TAGS_H

#include <string.h>

typedef enum {
	TAG_UNKNOWN,
	TAG_TEXT,
	TAG_A,
	TAG_ARTICLE,
	TAG_BODY,
	TAG_DIV,
	TAG_FOOTER,
	TAG_H1,
	TAG_HEAD,
	TAG_HEADER,
	TAG_HTML,
	TAG_LI,
	TAG_LINK,
	TAG_META,
	TAG_P,
	TAG_TIME,
	TAG_TITLE,
	TAG_UL,
} tag_type;

#define TAGLEN 18

static const struct {
	const char *name;
	tag_type    tag;
} tag_map[] = {
	{ "a",      	TAG_A },
	{ "article",	TAG_ARTICLE },
	{ "body",   	TAG_BODY },
	{ "div",    	TAG_DIV },
	{ "footer", 	TAG_FOOTER },
	{ "h1",     	TAG_H1 },
	{ "head",   	TAG_HEAD },
	{ "header", 	TAG_HEADER },
	{ "html",   	TAG_HTML },
	{ "li",     	TAG_LI },
	{ "link",   	TAG_LINK },
	{ "meta",   	TAG_META },
	{ "p",      	TAG_P },
	{ "time",   	TAG_TIME },
	{ "title",  	TAG_TITLE },
	{ "ul",     	TAG_UL },
};

static inline tag_type
str_to_tag(const char *name, size_t len)
{
	int lo, hi, mid, cmp;

	lo = 0;
	hi = 16 - 1;

	while (lo <= hi) {
		mid = (lo + hi) / 2;
		cmp = strncmp(name, tag_map[mid].name, len);
		if (cmp == 0) {
			if (tag_map[mid].name[len] == '\0')
				return tag_map[mid].tag;
			hi = mid - 1;
		} else if (cmp < 0)
			hi = mid - 1;
		else
			lo = mid + 1;
	}

	return TAG_UNKNOWN;
}

static inline const char *
tag_to_str(tag_type tag)
{
	switch (tag) {
	case TAG_UNKNOWN: return "unknown";
	case TAG_TEXT: return "text";
	case TAG_A: return "a";
	case TAG_ARTICLE: return "article";
	case TAG_BODY: return "body";
	case TAG_DIV: return "div";
	case TAG_FOOTER: return "footer";
	case TAG_H1: return "h1";
	case TAG_HEAD: return "head";
	case TAG_HEADER: return "header";
	case TAG_HTML: return "html";
	case TAG_LI: return "li";
	case TAG_LINK: return "link";
	case TAG_META: return "meta";
	case TAG_P: return "p";
	case TAG_TIME: return "time";
	case TAG_TITLE: return "title";
	case TAG_UL: return "ul";
	default: return "unknown";
	}
}

#endif /* TAGS_H */