aboutsummaryrefslogtreecommitdiffstats
path: root/rawmemrelem_inv.c
blob: 6c16697e1c71b6c77c02218dd3addbb2f4762198 (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
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
104
105
106
107
108
109
110
111
/* See LICENSE file for copyright and license details. */
#include "libsimple.h"
#ifndef TEST


void *
libsimple_rawmemrelem_inv(const void *hay_, size_t hayn, const void *sub_, size_t subn)
{
	switch (subn) {
	case 0:
		abort();
	case 1:
		{
			uint8_t *hay = (void *)hay_;
			uint8_t sub = *(uint8_t *)sub_;
			for (hay += hayn; *--hay == sub;);
			return hay;
		}
	case 2:
		{
			uint16_t *hay = (void *)hay_;
			uint16_t sub = *(uint16_t *)sub_;
			for (hay += hayn; *--hay == sub;);
			return hay;
		}
	case 4:
		{
			uint32_t *hay = (void *)hay_;
			uint32_t sub = *(uint32_t *)sub_;
			for (hay += hayn; *--hay == sub;);
			return hay;
		}
	case 8:
		{
			uint64_t *hay = (void *)hay_;
			uint64_t sub = *(uint64_t *)sub_;
			for (hay += hayn; *--hay == sub;);
			return hay;
		}
	default:
		{
			char *hay = (void *)hay_;
			const char *sub = sub_;
			size_t i;
			for (hay += hayn * subn;;) {
				hay -= subn;
				for (i = 0; i < subn; i++)
					if (hay[i] != sub[i])
						return hay;
			}
		}
	}
}


#else
#include "test.h"

int
main(void)
{
	assert(!strcmpnul(libsimple_rawmemrelem_inv("aabbaabb", 8, "b", 1), "abb"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("aabbaabb", 8, "B", 1), "b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("AABBAABB", 8, "b", 1), "B"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("AABBAABB", 8, "B", 1), "ABB"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("aabbaabb", 8, "a", 1), "b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("aabbbb\0\0", 8, "\0", 1), "b"));

	assert(!strcmpnul(libsimple_rawmemrelem_inv("-a-a-b-b-a-a-b-b", 8, "-b", 2), "-a-b-b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("-a-a-b-b-a-a-b-b", 8, "-B", 2), "-b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("-A-A-B-B-A-A-B-B", 8, "-b", 2), "-B"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("-A-A-B-B-A-A-B-B", 8, "-B", 2), "-A-B-B"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("-a-a-b-b-a-a-b-b", 8, "-a", 2), "-b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("-a-a-b-b-b-b\0\0\0\0", 8, "\0\0", 2), "-b"));

	assert(!strcmpnul(libsimple_rawmemrelem_inv("--a--a--b--b--a--a--b--b", 8, "--b", 3), "--a--b--b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("--a--a--b--b--a--a--b--b", 8, "--B", 3), "--b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("--A--A--B--B--A--A--B--B", 8, "--b", 3), "--B"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("--A--A--B--B--A--A--B--B", 8, "--B", 3), "--A--B--B"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("--a--a--b--b--a--a--b--b", 8, "--a", 3), "--b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("--a--a--b--b--b--b\0\0\0\0\0\0", 8, "\0\0\0", 3), "--b"));

	assert(!strcmpnul(libsimple_rawmemrelem_inv("---a---a---b---b---a---a---b---b", 8, "---b", 4), "---a---b---b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("---a---a---b---b---a---a---b---b", 8, "---B", 4), "---b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("---A---A---B---B---A---A---B---B", 8, "---b", 4), "---B"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("---A---A---B---B---A---A---B---B", 8, "---B", 4), "---A---B---B"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("---a---a---b---b---a---a---b---b", 8, "---a", 4), "---b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("---a---a---b---b---b---b\0\0\0\0\0\0\0\0", 8, "\0\0\0\0", 4), "---b"));

	assert(!strcmpnul(libsimple_rawmemrelem_inv("-------a-------a-------b-------b-------a-------a-------b-------b", 8,
	                                            "-------b", 8), "-------a-------b-------b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("-------a-------a-------b-------b-------a-------a-------b-------b", 8,
	                                            "-------B", 8), "-------b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("-------A-------A-------B-------B-------A-------A-------B-------B", 8,
	                                            "-------b", 8), "-------B"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("-------A-------A-------B-------B-------A-------A-------B-------B", 8,
	                                            "-------B", 8), "-------A-------B-------B"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("-------a-------a-------b-------b-------a-------a-------b-------b", 8,
	                                            "-------a", 8), "-------b"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("-------a-------a-------b-------b-------b-------b"
	                                            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 8, "\0\0\0\0\0\0\0\0", 8), "-------b"));

	assert(!strcmpnul(libsimple_rawmemrelem_inv("aaaa-aaa", 4, "aa", 2), "-aaa"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("aaaaaa-aaaaa", 4, "aaa", 3), "-aaaaa"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("aaaaaaaa-aaaaaaa", 4, "aaaa", 4), "-aaaaaaa"));
	assert(!strcmpnul(libsimple_rawmemrelem_inv("aaaaaaaaaaaaaaaa-aaaaaaaaaaaaaaa", 4, "aaaaaaaa", 8), "-aaaaaaaaaaaaaaa"));

	return 0;
}

#endif