Merge branch 'db/vcs-svn-incremental' into svn-fe
[git] / vcs-svn / string_pool.c
1 /*
2  * Licensed under a two-clause BSD-style license.
3  * See LICENSE for details.
4  */
5
6 #include "git-compat-util.h"
7 #include "quote.h"
8 #include "trp.h"
9 #include "obj_pool.h"
10 #include "string_pool.h"
11
12 static struct trp_root tree = { ~0 };
13
14 struct node {
15         uint32_t offset;
16         struct trp_node children;
17 };
18
19 /* Two memory pools: one for struct node, and another for strings */
20 obj_pool_gen(node, struct node, 4096)
21 obj_pool_gen(string, char, 4096)
22
23 static char *node_value(struct node *node)
24 {
25         return node ? string_pointer(node->offset) : NULL;
26 }
27
28 static int node_cmp(struct node *a, struct node *b)
29 {
30         return strcmp(node_value(a), node_value(b));
31 }
32
33 /* Build a Treap from the node structure (a trp_node w/ offset) */
34 trp_gen(static, tree_, struct node, children, node, node_cmp)
35
36 const char *pool_fetch(uint32_t entry)
37 {
38         return node_value(node_pointer(entry));
39 }
40
41 uint32_t pool_intern(const char *key)
42 {
43         /* Canonicalize key */
44         struct node *match = NULL, *node;
45         uint32_t key_len;
46         if (key == NULL)
47                 return ~0;
48         key_len = strlen(key) + 1;
49         node = node_pointer(node_alloc(1));
50         node->offset = string_alloc(key_len);
51         strcpy(node_value(node), key);
52         match = tree_search(&tree, node);
53         if (!match) {
54                 tree_insert(&tree, node);
55         } else {
56                 node_free(1);
57                 string_free(key_len);
58                 node = match;
59         }
60         return node_offset(node);
61 }
62
63 uint32_t pool_tok_r(char *str, const char *delim, char **saveptr)
64 {
65         char *token = strtok_r(str, delim, saveptr);
66         return token ? pool_intern(token) : ~0;
67 }
68
69 void pool_print_seq(uint32_t len, const uint32_t *seq, char delim, FILE *stream)
70 {
71         uint32_t i;
72         for (i = 0; i < len && ~seq[i]; i++) {
73                 fputs(pool_fetch(seq[i]), stream);
74                 if (i < len - 1 && ~seq[i + 1])
75                         fputc(delim, stream);
76         }
77 }
78
79 void pool_print_seq_q(uint32_t len, const uint32_t *seq, char delim, FILE *stream)
80 {
81         uint32_t i;
82         for (i = 0; i < len && ~seq[i]; i++) {
83                 quote_c_style(pool_fetch(seq[i]), NULL, stream, 1);
84                 if (i < len - 1 && ~seq[i + 1])
85                         fputc(delim, stream);
86         }
87 }
88
89 uint32_t pool_tok_seq(uint32_t sz, uint32_t *seq, const char *delim, char *str)
90 {
91         char *context = NULL;
92         uint32_t token = ~0;
93         uint32_t length;
94
95         if (sz == 0)
96                 return ~0;
97         if (str)
98                 token = pool_tok_r(str, delim, &context);
99         for (length = 0; length < sz; length++) {
100                 seq[length] = token;
101                 if (token == ~0)
102                         return length;
103                 token = pool_tok_r(NULL, delim, &context);
104         }
105         seq[sz - 1] = ~0;
106         return sz;
107 }
108
109 void pool_reset(void)
110 {
111         node_reset();
112         string_reset();
113 }