summaryrefslogtreecommitdiffstats
path: root/maildir/maildirsearch.h
blob: 64b257c118858e7f7603a39c929bf4d1b3ba35f0 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef maildir_search_h
#define maildir_search_h

/*
** Copyright 2002-2011 Double Precision, Inc.
** See COPYING for distribution information.
*/


/*
** A deterministic automaton-based search mechanism.  Search for a particular
** string in a middle of a larger body of text.
**
** Allocate a struct maildir_searchengine by yourself, and call
** maildir_search_init() to initialize.
**
** Call maildir_search_destroy() to release any allocated memory.
**
** Call maildir_search_start_str() to prep the structure for a particular search
** string. Alternatively, call maildir_search_start_unicode() to specify the
** search string as unicode characters. Alternatively,
** call maildir_search_start_str_chset() to specify the search string in
** a specific character. The search string get converted to unicode, then
** converted to lowercase characters, removing leading/trailing whitespace,
** and replacing multiple occurences of whitespace in the search string with
** a single space character.
**
** Call maildir_search_reset() to start the search, then call
** maildir_search_step() for each character in the text. Use
** maildir_search_step_unicode() if the search string was specified via
** maildir_search_start_unicode(). Use maildir_search_step_unicode_lc() if
** the search string was specified using via maildir_search_start_str_chset().
**
** Call maildir_search_found() to check if the search string is found.
*/

#include "config.h"

#include <courier-unicode.h>

#include <string.h>
#include <stdlib.h>

#ifdef  __cplusplus
extern "C" {
#endif

struct maildir_searchengine {
	char32_t *string;
	size_t string_l;
	const char32_t *ptr;
	unsigned *r;    /* Retry backoff indexes */
	unsigned i;
	int spc;
	} ;

#define maildir_search_init(sei) (memset((sei), 0, sizeof(struct maildir_searchengine)))

#define maildir_search_destroy(sei) do { if ((sei)->string) free((sei)->string); if ( (sei)->r) free( (sei)->r); } while (0)

int maildir_search_start_str(struct maildir_searchengine *engine,
			     const char *string);
int maildir_search_start_str_chset(struct maildir_searchengine *engine,
				   const char *string,
				   const char *chset);
int maildir_search_start_unicode(struct maildir_searchengine *engine,
				 const char32_t *string);


#define	maildir_search_reset(si)	((si)->i=0, (si)->ptr=(si)->string)

#define	maildir_search_found(si)	((si)->ptr && \
					(si)->ptr[(si)->i] == '\0')
#define maildir_search_len(si)		((si)->string_l)

#define maildir_search_step_unicode(sie,ch) do \
        {\
                if ( (sie)->ptr && (sie)->ptr[(sie)->i]) \
                {\
                        for (;;) \
                        {\
                                if ( (char32_t)(sie)->ptr[(sie)->i] == (char32_t)(ch) )\
                                        { (sie)->i++; break; }\
                                if ( (sie)->i == 0) break;\
                                (sie)->i=(sie)->r[(sie)->i];\
                        }\
                }\
        } while (0)

#define maildir_search_step_unicode_lc(sie,ch) do	\
	{						\
		char32_t c=(ch);			\
		int spc=0;				\
							\
									\
		if (c == ' ' || c == '\t' || c == '\r' || c == '\n')	\
		{							\
			c=' ';						\
			spc=1;						\
		}							\
									\
		if (spc && (sie)->spc)					\
			;						\
		else							\
		{							\
			c=unicode_lc(c);				\
			maildir_search_step_unicode((sie),c);		\
		}							\
									\
		(sie)->spc=spc;						\
	} while(0)

#define maildir_search_step(sie,ch) \
	maildir_search_step_unicode((sie), ((unsigned char)(ch)))

#define maildir_search_atstart(sie) ((sie)->i == 0)

#ifdef  __cplusplus
}

/* A C++ wrapper for the above */

#if HAVE_VECTOR
#include <vector>
#else
#if HAVE_VECTOR_H
#include <vector.h>
#endif
#endif

#include <string>

namespace mail {

class Search {

	struct maildir_searchengine sei;

	std::string String;

	std::vector<unsigned> rbuf;
 public:
	Search();
	virtual ~Search();

	size_t getSearchLen()
	{
		return maildir_search_len(&sei);
	}

	bool setString(std::string s, std::string chset)
	{
		String=s;
		return maildir_search_start_str_chset(&sei, s.c_str(),
						      chset.c_str()) == 0;
	}

	void reset()
	{
		maildir_search_reset(&sei);
	}

	void operator<<(char c) { maildir_search_step(&sei, c); }

	void operator<<(char32_t ch)
	{
		maildir_search_step_unicode_lc(&sei, ch);
	}

	bool atstart() { return maildir_search_atstart(&sei); }
	operator bool() { return maildir_search_found(&sei); }

	bool operator !() { return ! operator bool(); }

 private:
	Search(const Search &); /* UNDEFINED */

	Search &operator=(const Search &); /* UNDEFINED */

};

}

#endif

#endif