bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen#include "lib.h"
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen#include "unichar.h"
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen#include "fts-common.h"
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen#include "fts-filter-private.h"
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainenstatic unichar_t get_ending_utf8_char(const char *str, size_t *end_pos)
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen{
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen unichar_t c;
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen while (!UTF8_IS_START_SEQ(str[*end_pos])) {
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen i_assert(*end_pos > 0);
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen *end_pos -= 1;
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen }
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen if (uni_utf8_get_char(str + *end_pos, &c) <= 0)
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen i_unreached();
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen return c;
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen}
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainenstatic int
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainenfts_filter_english_possessive_filter(struct fts_filter *filter ATTR_UNUSED,
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen const char **token,
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen const char **error_r ATTR_UNUSED)
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen{
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen size_t len = strlen(*token);
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen unichar_t c;
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen if (len > 1 && ((*token)[len-1] == 's' || (*token)[len-1] == 'S')) {
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen len -= 2;
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen c = get_ending_utf8_char(*token, &len);
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen if (IS_APOSTROPHE(c))
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen *token = t_strndup(*token, len);
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen }
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen return 1;
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen}
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainenstatic const struct fts_filter fts_filter_english_possessive_real = {
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen .class_name = "english-possessive",
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen .v = {
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen NULL,
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen fts_filter_english_possessive_filter,
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen NULL
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen }
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen};
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainen
471167b9701fcc99b66f7a8bcae07bc4ac0dbbd4Timo Sirainenconst struct fts_filter *fts_filter_english_possessive = &fts_filter_english_possessive_real;