unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 41a6881d604f88519013861a15f4fcd8fef3c2ef 7713 bytes (raw)
name: util/string-map.c 	 # 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
 
/* string-map.c - associative arrays of strings
 *
 *
 * 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: David Bremner <david@tethera.net>
 */

#include "notmuch-private.h"

/* Create a new notmuch_string_map_t object, with 'ctx' as its
 * talloc owner.
 *
 * This function can return NULL in case of out-of-memory.
 */

typedef struct _notmuch_string_pair_t {
    char *key;
    char *value;
} notmuch_string_pair_t;

struct _notmuch_string_map {
    bool sorted;
    size_t length;
    notmuch_string_pair_t *pairs;
};

struct _notmuch_string_map_iterator {
    notmuch_string_pair_t *current;
    bool exact;
    const char *key;
};

notmuch_string_map_t *
_notmuch_string_map_create (const void *ctx)
{
    notmuch_string_map_t *map;

    map = talloc (ctx, notmuch_string_map_t);
    if (unlikely (map == NULL))
	return NULL;

    map->length = 0;
    map->pairs = NULL;
    map->sorted = true;

    return map;
}

void
_notmuch_string_map_append (notmuch_string_map_t *map,
			    const char *key,
			    const char *value)
{

    map->length++;
    map->sorted = false;

    if (map->pairs)
	map->pairs = talloc_realloc (map, map->pairs, notmuch_string_pair_t, map->length + 1);
    else
	map->pairs = talloc_array (map, notmuch_string_pair_t, map->length + 1);

    map->pairs[map->length - 1].key = talloc_strdup (map, key);
    map->pairs[map->length - 1].value = talloc_strdup (map, value);

    /* Add sentinel */
    map->pairs[map->length].key = NULL;
    map->pairs[map->length].value = NULL;

}

static int
cmppair (const void *pa, const void *pb)
{
    notmuch_string_pair_t *a = (notmuch_string_pair_t *) pa;
    notmuch_string_pair_t *b = (notmuch_string_pair_t *) pb;

    return strcmp (a->key, b->key);
}

static void
_notmuch_string_map_sort (notmuch_string_map_t *map)
{
    if (map->length == 0)
	return;

    if (map->sorted)
	return;

    qsort (map->pairs, map->length, sizeof (notmuch_string_pair_t), cmppair);

    map->sorted = true;
}

static bool
string_cmp (const char *a, const char *b, bool exact)
{
    if (exact)
	return (strcmp (a, b));
    else
	return (strncmp (a, b, strlen (a)));
}

static notmuch_string_pair_t *
bsearch_first (notmuch_string_pair_t *array, size_t len, const char *key, bool exact)
{
    size_t first = 0;
    size_t last = len - 1;
    size_t mid;

    if (len <= 0)
	return NULL;

    while (last > first) {
	mid = (first + last) / 2;
	int sign = string_cmp (key, array[mid].key, exact);

	if (sign <= 0)
	    last = mid;
	else
	    first = mid + 1;
    }


    if (string_cmp (key, array[first].key, exact) == 0)
	return array + first;
    else
	return NULL;

}

void
_notmuch_string_map_set (notmuch_string_map_t *map,
			     const char *key,
			     const char *value)
{

    notmuch_string_pair_t *pair;

    /* this means that calling append invalidates iterators */
    _notmuch_string_map_sort (map);

    pair = bsearch_first (map->pairs, map->length, key, true);
    if (! pair)
	_notmuch_string_map_append (map, key, value);

    pair->value = talloc_strdup (map, value);

}

const char *
_notmuch_string_map_get (notmuch_string_map_t *map, const char *key)
{
    notmuch_string_pair_t *pair;

    /* this means that calling append invalidates iterators */
    _notmuch_string_map_sort (map);

    pair = bsearch_first (map->pairs, map->length, key, true);
    if (! pair)
	return NULL;

    return pair->value;
}

notmuch_string_map_iterator_t *
_notmuch_string_map_iterator_create (notmuch_string_map_t *map, const char *key,
				     bool exact)
{
    notmuch_string_map_iterator_t *iter;

    _notmuch_string_map_sort (map);

    iter = talloc (map, notmuch_string_map_iterator_t);
    if (unlikely (iter == NULL))
	return NULL;

    if (unlikely (talloc_reference (iter, map) == NULL))
	return NULL;

    iter->key = talloc_strdup (iter, key);
    iter->exact = exact;
    iter->current = bsearch_first (map->pairs, map->length, key, exact);
    return iter;
}

bool
_notmuch_string_map_iterator_valid (notmuch_string_map_iterator_t *iterator)
{
    if (iterator->current == NULL)
	return false;

    /* sentinel */
    if (iterator->current->key == NULL)
	return false;

    return (0 == string_cmp (iterator->key, iterator->current->key, iterator->exact));

}

void
_notmuch_string_map_iterator_move_to_next (notmuch_string_map_iterator_t *iterator)
{

    if (! _notmuch_string_map_iterator_valid (iterator))
	return;

    (iterator->current)++;
}

const char *
_notmuch_string_map_iterator_key (notmuch_string_map_iterator_t *iterator)
{
    if (! _notmuch_string_map_iterator_valid (iterator))
	return NULL;

    return iterator->current->key;
}

const char *
_notmuch_string_map_iterator_value (notmuch_string_map_iterator_t *iterator)
{
    if (! _notmuch_string_map_iterator_valid (iterator))
	return NULL;

    return iterator->current->value;
}

void
_notmuch_string_map_iterator_destroy (notmuch_string_map_iterator_t *iterator)
{
    talloc_free (iterator);
}

static const char *
_append_escaped (void *ctx, const char *dest, const char *str) {
    /* At worst we escape everything */
    char *buf = talloc_zero_size (ctx, 2 * strlen(str) + 1);
    char *ret;
    int j = 0;
    for (const char *cur = str; *cur; cur++){
	if (*cur == '\n') {
	    buf[j++] = '\\';
	    buf[j++] = 'n';
	} else if (*cur == '\\' && *(cur+1)== 'n') {
	    buf[j++] = '\\';
	    buf[j++] = '\\';
	} else {
	    buf[j++] = *cur;
	}
    }
    /* this NUL is already there, but better safe than sorry */
    buf[j] = '\0';
    ret = talloc_asprintf (ctx, "%s%s", dest, buf);
    talloc_free (buf);
    return ret;
}

const char *
_notmuch_string_map_serialize (void* ctx, notmuch_string_map_t *map)
{
    const char *ret;

    _notmuch_string_map_sort (map);

    ret=talloc_strdup(ctx, "");
    for (size_t i=0; i < map->length; i++) {
	ret = _append_escaped (ctx, ret, map->pairs[i].key);
	ret = talloc_asprintf(ctx, "%s\n", ret);
	ret = _append_escaped (ctx, ret, map->pairs[i].value);
	ret = talloc_asprintf(ctx, "%s\n", ret);
    }

    return ret;
}

static char *
unescape_newlines (void *ctx, const char *in, size_t len) {
    size_t i,j;
    /* removing escapes only makes things shorter */
    char *out = talloc_zero_size (ctx, len+1);
    for (i=0, j=0; i<len; i++) {
	if (in[i] == '\\' && i < len - 1) {
	    switch (in[i+1]) {
	    case '\\':
		i++;
		out[j++] = '\\';
		break;
	    case 'n':
		i++;
		out[j++] = '\n';
		break;
	    default:
		out[j++] = '\\';
	    }
	} else {
	    out[j++] = in[i];
	}
    }
    out[j]='\0';
    return out;
}

notmuch_string_map_t *
_notmuch_string_map_deserialize (void *ctx, const char *str)
{
    const char *tok = str;
    const char *delim = "\n";
    size_t tok_len = 0;
    const char *pair [2];
    size_t step = 0;

    notmuch_string_map_t *map = _notmuch_string_map_create (ctx);

    while ((tok = strtok_len_c (tok + tok_len, delim, &tok_len)) != NULL) {
	pair[step] = unescape_newlines (ctx, tok, tok_len);
	step++;
	if (step == 2) {
	    step = 0;
	    _notmuch_string_map_append (map, pair[0], pair[1]);
	}
    }
    return map;
}

debug log:

solving 41a6881d ...
found 41a6881d in https://yhetil.org/notmuch/20180623014247.17834-8-david@tethera.net/
found b29a9ba0 in https://yhetil.org/notmuch/20180623014247.17834-6-david@tethera.net/
found ab0c42ab in https://yhetil.org/notmuch/20180623014247.17834-5-david@tethera.net/
found ad818207 in https://yhetil.org/notmuch.git/
preparing index
index prepared:
100644 ad818207f5d4a91450336a99af2518ffcd2b8bc3	util/string-map.c

applying [1/3] https://yhetil.org/notmuch/20180623014247.17834-5-david@tethera.net/
diff --git a/util/string-map.c b/util/string-map.c
index ad818207..ab0c42ab 100644


applying [2/3] https://yhetil.org/notmuch/20180623014247.17834-6-david@tethera.net/
diff --git a/util/string-map.c b/util/string-map.c
index ab0c42ab..b29a9ba0 100644


applying [3/3] https://yhetil.org/notmuch/20180623014247.17834-8-david@tethera.net/
diff --git a/util/string-map.c b/util/string-map.c
index b29a9ba0..41a6881d 100644

Checking patch util/string-map.c...
Applied patch util/string-map.c cleanly.
Checking patch util/string-map.c...
Applied patch util/string-map.c cleanly.
Checking patch util/string-map.c...
Applied patch util/string-map.c cleanly.

index at:
100644 41a6881d604f88519013861a15f4fcd8fef3c2ef	util/string-map.c

(*) 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).