stringencoders now has memory-free query-string tokenizer

05 Apr 2012

stringencoders has a new function modp_qsiter .  It's a URL query string tokenizer in C.  Why another one?  Many libraries have dependencies on a data structures library, a specialized string library or worse do memory allocation for you, typically causing overhead and needless copying.  Not this one.  It's up to you do make the copy into whatever string library you want, or put it into a STL map<stirng,string> or not (and define behavior when a key is duplicated).



One a related note, modp_burl.h also has a new function modp_burl_decode_raw which is just like modp_burl_decode except that "+" is preserved (compare to PHP url decode and rawurldecode



More detail are from modp_qsiter.h:































/**
 * Query string key value pair iterator.  Uses no heap, makes no copy, makes
 *  no modification of input.  Think of this as a super-strtok_r.  This
 *  also does not do query-string un-escaping.
 *
 * qsiiter_t qsi;
 * const char* qs = "foo=bar&ding=bar";
 * qsiter_reset(&qsi, qs, strlen(qs));
 * while  (qsiter_next(&qsi)) {
 *    // we only get start and length of key,value
 *    // up to you how to copy it or not, on heap or stack
 *    // with strcpy, strncpy, strndup, memcpy, mempcpy, strlcpy, whatever
 *    // callers job to alloc/free memory
 *
 *    const char* key = (const char*) malloc(qsi.keylen + 1);
 *    strcpy(key, qsi->key, qsi->keylen);  
 *    const char* val = (const char*) malloc(qsi.vallen + 1);
 *    strcpy(val, qsi->val, qsi->vallen);
 *    printf("key = %s, value = %s\n", key, val);
 *    free(key);
 *    free(value);
 * }
 *
 *
 */