summaryrefslogtreecommitdiff
path: root/pkg/netsurf/libhubbub/entities.awk
blob: 43ccc89b03bbd9facf14471a6a9df1ca223f2802 (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
BEGIN {
	for (i = 32; i <= 126; ++i)
		ord[sprintf("%c", i)] = i
	n = -1
}

function makenode(c) {
	++n
	splits[n] = c
	left[n] = -1
	equal[n] = -1
	right[n] = -1
	values[n] = 0
}

function insert(nodes, id, c) {
	nextid = nodes[id]
	if (nextid != -1)
		return nextid
	makenode(c)
	nodes[id] = n
	return n
}

/^(#|$)/ {next}

{
	id = 0
	c = ord[substr($1, 1, 1)]
	if (n < 0)
		makenode(c)
	for (i = 1; i <= length($1) + 1;) {
		if (c < splits[id]) {
			id = insert(left, id, c)
		} else if (c == splits[id]) {
			if (i >= length($1))
				values[id] = $2
			if (c == 0)
				break
			++i
			c = i <= length($1) ? ord[substr($1, i, 1)] : 0
			id = insert(equal, id, c)
		} else {
			id = insert(right, id, c)
		}
	}
}

END {
	print "static hubbub_entity_node dict[] = {"
	for (i = 0; i <= n; ++i)
		print "\t{ "splits[i]", "left[i]", "equal[i]", "right[i]", "values[i]" },"
	print "};"
	print "static int32_t dict_root = 0;"
}