aboutsummaryrefslogtreecommitdiffstats
path: root/libwc/search.c
blob: 655d1e04584e0ef478a98a94b098330aeefe3c64 (plain) (blame)
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
#include <stdlib.h>
#include "wc.h"

static int
map_cmp(const void *a, const void *b)
{
    return *(wc_uint16 *)a - ((wc_map *)b)->code;
}

static int
map3_cmp(const void *a, const void *b)
{
    return *(wc_uint32 *)a - (((wc_uint32)((wc_map3 *)b)->code << 16) | ((wc_map3 *)b)->code2);
}

static int
map_range_cmp(const void *a, const void *b)
{
    return (*(wc_uint16 *)a < ((wc_map *)b)->code) ? -1
	: ((*(wc_uint16 *)a > ((wc_map *)b)->code2) ? 1 : 0);
}

static int
map2_range_cmp(const void *a, const void *b)
{
    return (*(wc_uint16 *)a < ((wc_map *)b)->code) ? -1
	: ((*(wc_uint16 *)a >= ((wc_map *)b + 1)->code) ? 1 : 0);
}

static int
map3_range_cmp(const void *a, const void *b)
{
    return (*(wc_uint16 *)a < ((wc_map3 *)b)->code) ? -1
	: ((*(wc_uint16 *)a > ((wc_map3 *)b)->code2) ? 1 : 0);
}

wc_map *
wc_map_search(wc_uint16 code, wc_map *map, size_t n)
{
    return (wc_map *)bsearch((void *)&code, (void *)map, n, sizeof(wc_map),
	map_cmp);
}

wc_map3 *
wc_map3_search(wc_uint16 c1, wc_uint16 c2, wc_map3 *map, size_t n)
{
    wc_uint32 code = ((wc_uint32)c1 << 16) | c2;
    return (wc_map3 *)bsearch((void *)&code, (void *)map, n, sizeof(wc_map3),
	map3_cmp);
}

wc_map *
wc_map_range_search(wc_uint16 code, wc_map *map, int n)
{
    return (wc_map *)bsearch((void *)&code, (void *)map, n, sizeof(wc_map),
	map_range_cmp);
}

wc_map *
wc_map2_range_search(wc_uint16 code, wc_map *map, size_t n)
{
    return (wc_map *)bsearch((void *)&code, (void *)map, n, sizeof(wc_map),
	map2_range_cmp);
}

wc_map3 *
wc_map3_range_search(wc_uint16 code, wc_map3 *map, size_t n)
{
    return (wc_map3 *)bsearch((void *)&code, (void *)map, n, sizeof(wc_map3),
	map3_range_cmp);
}