Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add query_substitute, and rewrite clone in terms of that

  • Loading branch information...
commit 0773d7f33d9dad96c91684eccfd1d3157d6fbf88 1 parent 1a341c7
William Morgan authored

Showing 2 changed files with 16 additions and 3 deletions. Show diff stats Hide diff stats

  1. +12 2 query.c
  2. +4 1 query.h
14 query.c
@@ -12,7 +12,17 @@ static wp_query* wp_query_new() {
12 12 return ret;
13 13 }
14 14
  15 +static const char* identity(const char* field, const char* word) {
  16 + (void)field;
  17 + if(word) return strdup(word);
  18 + else return NULL;
  19 +}
  20 +
15 21 wp_query* wp_query_clone(wp_query* other) {
  22 + return wp_query_substitute(other, identity);
  23 +}
  24 +
  25 +wp_query* wp_query_substitute(wp_query* other, const char *(*substituter)(const char* field, const char* word)) {
16 26 wp_query* ret = malloc(sizeof(wp_query));
17 27 ret->type = other->type;
18 28 ret->num_children = other->num_children;
@@ -21,12 +31,12 @@ wp_query* wp_query_clone(wp_query* other) {
21 31 if(other->field) ret->field = strdup(other->field);
22 32 else ret->field = NULL;
23 33
24   - if(other->word) ret->word = strdup(other->word);
  34 + if(other->field && other->word) ret->word = substituter(other->field, other->word);
25 35 else ret->word = NULL;
26 36
27 37 ret->children = ret->next = ret->last = NULL; // set below
28 38 for(wp_query* child = other->children; child != NULL; child = child->next) {
29   - wp_query* clone = wp_query_clone(child);
  39 + wp_query* clone = wp_query_substitute(child, substituter);
30 40 if(ret->last == NULL) ret->children = ret->last = clone;
31 41 else {
32 42 ret->last->next = clone;
5 query.h
@@ -64,9 +64,12 @@ wp_query* wp_query_new_empty();
64 64 // public: make an every-document query node.
65 65 wp_query* wp_query_new_every();
66 66
67   -// public: deep clone of a query, but dropping all search state.
  67 +// public: deep clone of a query, dropping all search state.
68 68 wp_query* wp_query_clone(wp_query* other);
69 69
  70 +// public: build a new query by substituting words from the old query, dropping all search state
  71 +wp_query* wp_query_substitute(wp_query* other, const char *(*substituter)(const char* field, const char* word));
  72 +
70 73 // public: add a query node as a child of another
71 74 wp_query* wp_query_add(wp_query* a, wp_query* b);
72 75

0 comments on commit 0773d7f

Please sign in to comment.
Something went wrong with that request. Please try again.