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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/* code-query-spec.c
*
* Copyright 2025 Christian Hergert <chergert@redhat.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library 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
* Lesser 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#define _GNU_SOURCE
#include <string.h>
#include "code-index.h"
#include "code-query-spec-private.h"
#include "code-result-private.h"
typedef enum _CodeQueryAstType
{
CODE_QUERY_AST_CONTAINS = 1,
CODE_QUERY_AST_REGEX,
} CodeQueryAstType;
typedef struct _CodeQueryAst
{
gpointer data;
gsize datalen;
struct _CodeQueryAst *left;
struct _CodeQueryAst *right;
CodeQueryAstType type;
} CodeQueryAst;
struct _CodeQuerySpec
{
GObject parent_instance;
CodeQueryAst *tree;
};
G_DEFINE_FINAL_TYPE (CodeQuerySpec, code_query_spec, G_TYPE_OBJECT)
static CodeQueryAst *
code_query_ast_new (CodeQueryAstType type,
gpointer data,
gsize datalen)
{
CodeQueryAst *ast;
ast = g_new (CodeQueryAst, 1);
ast->type = type;
ast->data = data;
ast->datalen = datalen;
ast->left = NULL;
ast->right = NULL;
return ast;
}
static void
code_query_ast_free (CodeQueryAst *ast)
{
g_clear_pointer (&ast->left, code_query_ast_free);
g_clear_pointer (&ast->right, code_query_ast_free);
switch (ast->type)
{
case CODE_QUERY_AST_CONTAINS:
g_clear_pointer (&ast->data, g_free);
break;
case CODE_QUERY_AST_REGEX:
g_clear_pointer (&ast->data, g_regex_unref);
break;
default:
break;
}
g_free (ast);
}
static gboolean
code_query_ast_matches_contains (CodeQueryAst *ast,
const guint8 *data,
gsize len)
{
return memmem (data, len, ast->data, ast->datalen) != NULL;
}
static gboolean
code_query_ast_matches_regex (CodeQueryAst *ast,
const guint8 *data,
gsize len)
{
return g_regex_match_full (ast->data,
(const char *)data, len, 0,
G_REGEX_MATCH_DEFAULT,
NULL,
NULL);
}
static inline gboolean
code_query_ast_matches (CodeQueryAst *ast,
const char *path,
GBytes *bytes)
{
gsize len;
const guint8 *data = g_bytes_get_data (bytes, &len);
if (ast->type == CODE_QUERY_AST_CONTAINS)
return code_query_ast_matches_contains (ast, data, len);
if (ast->type == CODE_QUERY_AST_REGEX)
return code_query_ast_matches_regex (ast, data, len);
return FALSE;
}
static inline void
code_query_ast_collect_trigrams_regex (CodeQueryAst *ast,
CodeSparseSet *set)
{
/* TODO: We need to deconstruct the pattern so that we can fill
* in some ranges of trigrams for things like "c[a-z]de"
* as well as skip things like ^ or $ and escapes.
*/
}
static inline void
code_query_ast_collect_trigrams_contains (CodeQueryAst *ast,
CodeSparseSet *set)
{
CodeTrigramIter iter;
CodeTrigram trigram;
code_trigram_iter_init (&iter, ast->data, ast->datalen);
while (code_trigram_iter_next (&iter, &trigram))
{
guint trigram_id = code_trigram_encode (&trigram);
code_sparse_set_add (set, trigram_id);
}
}
static inline void
code_query_ast_collect_trigrams (CodeQueryAst *ast,
CodeSparseSet *set)
{
if (ast->type == CODE_QUERY_AST_CONTAINS)
return code_query_ast_collect_trigrams_contains (ast, set);
if (ast->type == CODE_QUERY_AST_REGEX)
return code_query_ast_collect_trigrams_regex (ast, set);
}
static void
code_query_spec_finalize (GObject *object)
{
CodeQuerySpec *self = (CodeQuerySpec *)object;
g_clear_pointer (&self->tree, code_query_ast_free);
G_OBJECT_CLASS (code_query_spec_parent_class)->finalize (object);
}
static void
code_query_spec_class_init (CodeQuerySpecClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = code_query_spec_finalize;
}
static void
code_query_spec_init (CodeQuerySpec *self)
{
}
CodeQuerySpec *
code_query_spec_new_contains (const char *string)
{
CodeQuerySpec *spec;
g_return_val_if_fail (string != NULL, NULL);
spec = g_object_new (CODE_TYPE_QUERY_SPEC, NULL);
spec->tree = code_query_ast_new (CODE_QUERY_AST_CONTAINS, g_strdup (string), strlen (string));
return spec;
}
CodeQuerySpec *
code_query_spec_new_for_regex (GRegex *regex)
{
CodeQuerySpec *spec;
g_return_val_if_fail (regex != NULL, NULL);
spec = g_object_new (CODE_TYPE_QUERY_SPEC, NULL);
spec->tree = code_query_ast_new (CODE_QUERY_AST_REGEX, g_regex_ref (regex), 0);
return spec;
}
void
_code_query_spec_collect_trigrams (CodeQuerySpec *spec,
CodeSparseSet *set)
{
code_query_ast_collect_trigrams (spec->tree, set);
}
gboolean
_code_query_spec_matches (CodeQuerySpec *spec,
const char *path,
GBytes *bytes)
{
return code_query_ast_matches (spec->tree, path, bytes);
}
|