unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 4d3d97212eb6ca301b6767179bb88001c9465d6d 3022 bytes (raw)
name: lib/regexp-fields.cc 	 # note: path name is non-authoritative(*)

  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
 
/* regexp-fields.cc - "re:" field processor glue
 *
 * This file is part of notmuch.
 *
 * Copyright © 2015 Austin Clements
 * Copyright © 2016 David Bremner
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see https://www.gnu.org/licenses/ .
 *
 * Author: Austin Clements <aclements@csail.mit.edu>
 *                David Bremner <david@tethera.net>
 */

#include "regexp-fields.h"
#include "notmuch-private.h"

#if HAVE_XAPIAN_FIELD_PROCESSOR
RegexpPostingSource::RegexpPostingSource (Xapian::valueno slot, const std::string &regexp)
    : slot_ (slot)
{
    int err = regcomp (&regexp_, regexp.c_str (), REG_EXTENDED | REG_NOSUB);

    if (err != 0) {
	size_t len = regerror (err, &regexp_, NULL, 0);
	char *buffer = new char[len];
	std::string msg;
	(void) regerror (err, &regexp_, buffer, len);
	msg.assign (buffer, len);
	delete buffer;

	throw Xapian::QueryParserError (msg);
    }
}

RegexpPostingSource::~RegexpPostingSource ()
{
    regfree (&regexp_);
}

void
RegexpPostingSource::init (const Xapian::Database &db)
{
    db_ = db;
    it_ = db_.valuestream_begin (slot_);
    end_ = db.valuestream_end (slot_);
    started_ = false;
}

Xapian::doccount
RegexpPostingSource::get_termfreq_min () const
{
    return 0;
}

Xapian::doccount
RegexpPostingSource::get_termfreq_est () const
{
    return get_termfreq_max () / 2;
}

Xapian::doccount
RegexpPostingSource::get_termfreq_max () const
{
    return db_.get_value_freq (slot_);
}

Xapian::docid
RegexpPostingSource::get_docid () const
{
    return it_.get_docid ();
}

bool
RegexpPostingSource::at_end () const
{
    return it_ == end_;
}

void
RegexpPostingSource::next (unused (double min_wt))
{
    if (started_ && ! at_end ())
	++it_;
    started_ = true;

    for (; ! at_end (); ++it_) {
	std::string value = *it_;
	if (regexec (&regexp_, value.c_str (), 0, NULL, 0) == 0)
	    break;
    }
}

static Xapian::valueno
_find_slot (std::string prefix)
{
    if (prefix == "from")
	return NOTMUCH_VALUE_FROM;
    else if (prefix == "subject")
	return NOTMUCH_VALUE_SUBJECT;
    else
	throw Xapian::QueryParserError ("unsupported regexp field '" + prefix + "'");
}

Xapian::Query
RegexpFieldProcessor::operator() (const std::string & str)
{
    size_t pos = str.find_first_of (':');
    std::string prefix = str.substr (0, pos);
    std::string regexp = str.substr (pos + 1);

    postings = new RegexpPostingSource (_find_slot (prefix), regexp);
    return Xapian::Query (postings);
}
#endif

debug log:

solving 4d3d9721 ...
retrying 4d3d9721 as 4d3d972
found 4d3d972 in https://yhetil.org/notmuch/20161114214651.19770-1-david@tethera.net/ ||
	https://yhetil.org/notmuch/1467034387-16885-1-git-send-email-david@tethera.net/

applying [1/1] https://yhetil.org/notmuch/20161114214651.19770-1-david@tethera.net/
diff --git a/lib/regexp-fields.cc b/lib/regexp-fields.cc
new file mode 100644
index 0000000..4d3d972

Checking patch lib/regexp-fields.cc...
Applied patch lib/regexp-fields.cc cleanly.

skipping https://yhetil.org/notmuch/1467034387-16885-1-git-send-email-david@tethera.net/ for 4d3d972
index at:
100644 4d3d97212eb6ca301b6767179bb88001c9465d6d	lib/regexp-fields.cc

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).