summaryrefslogtreecommitdiffstats
path: root/src/lib-fts/fts-filter-english-possessive.c
blob: 783b6d4effae12564cd678f476578581bc81d1ca (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
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "unichar.h"
#include "fts-common.h"
#include "fts-filter-private.h"

static unichar_t get_ending_utf8_char(const char *str, size_t *end_pos)
{
	unichar_t c;

	while (!UTF8_IS_START_SEQ(str[*end_pos])) {
		i_assert(*end_pos > 0);
		*end_pos -= 1;
	}
	if (uni_utf8_get_char(str + *end_pos, &c) <= 0)
		i_unreached();
	return c;
}

static int
fts_filter_english_possessive_filter(struct fts_filter *filter ATTR_UNUSED,
				     const char **token,
				     const char **error_r ATTR_UNUSED)
{
	size_t len = strlen(*token);
	unichar_t c;

	if (len > 1 && ((*token)[len-1] == 's' || (*token)[len-1] == 'S')) {
		len -= 2;
		c = get_ending_utf8_char(*token, &len);
		if (IS_APOSTROPHE(c))
			*token = t_strndup(*token, len);
	}
	return 1;
}

static const struct fts_filter fts_filter_english_possessive_real = {
	.class_name = "english-possessive",
	.v = {
		NULL,
		fts_filter_english_possessive_filter,
		NULL
	}
};

const struct fts_filter *fts_filter_english_possessive = &fts_filter_english_possessive_real;