Permalink
Browse files

add query_substitute, and rewrite clone in terms of that

  • Loading branch information...
1 parent 1a341c7 commit 0773d7f33d9dad96c91684eccfd1d3157d6fbf88 @wmorgan committed Mar 13, 2012
Showing with 16 additions and 3 deletions.
  1. +12 −2 query.c
  2. +4 −1 query.h
View
14 query.c
@@ -12,7 +12,17 @@ static wp_query* wp_query_new() {
return ret;
}
+static const char* identity(const char* field, const char* word) {
+ (void)field;
+ if(word) return strdup(word);
+ else return NULL;
+}
+
wp_query* wp_query_clone(wp_query* other) {
+ return wp_query_substitute(other, identity);
+}
+
+wp_query* wp_query_substitute(wp_query* other, const char *(*substituter)(const char* field, const char* word)) {
wp_query* ret = malloc(sizeof(wp_query));
ret->type = other->type;
ret->num_children = other->num_children;
@@ -21,12 +31,12 @@ wp_query* wp_query_clone(wp_query* other) {
if(other->field) ret->field = strdup(other->field);
else ret->field = NULL;
- if(other->word) ret->word = strdup(other->word);
+ if(other->field && other->word) ret->word = substituter(other->field, other->word);
else ret->word = NULL;
ret->children = ret->next = ret->last = NULL; // set below
for(wp_query* child = other->children; child != NULL; child = child->next) {
- wp_query* clone = wp_query_clone(child);
+ wp_query* clone = wp_query_substitute(child, substituter);
if(ret->last == NULL) ret->children = ret->last = clone;
else {
ret->last->next = clone;
View
5 query.h
@@ -64,9 +64,12 @@ wp_query* wp_query_new_empty();
// public: make an every-document query node.
wp_query* wp_query_new_every();
-// public: deep clone of a query, but dropping all search state.
+// public: deep clone of a query, dropping all search state.
wp_query* wp_query_clone(wp_query* other);
+// public: build a new query by substituting words from the old query, dropping all search state
+wp_query* wp_query_substitute(wp_query* other, const char *(*substituter)(const char* field, const char* word));
+
// public: add a query node as a child of another
wp_query* wp_query_add(wp_query* a, wp_query* b);

0 comments on commit 0773d7f

Please sign in to comment.