Skip to content

Commit

Permalink
the zero-length hash key is valid. use (-1) as the discrimnator for
Browse files Browse the repository at this point in the history
auto-computed string-length hash keys


git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@60583 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
gstein committed Oct 16, 2000
1 parent de3f6d9 commit 7de5dec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 14 additions & 6 deletions include/apr_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ extern "C" {

#include "apr_pools.h"

/*
* When passing a key to apr_hash_set or apr_hash_get, this value can be
* passed to indicate a string-valued key, and have apr_hash compute the
* length automatically.
*
* Note: apr_hash will use strlen(key)+1 for the length. This allows
* apr_hash_this() to return a null-terminated string as the key.
*/
#define APR_HASH_KEY_STRING (-1)

/*
* Abstract type for hash tables.
*/
Expand All @@ -91,26 +101,24 @@ APR_EXPORT(apr_hash_t *) apr_make_hash(apr_pool_t *pool);
* Associate a value with a key in a hash table.
* @param ht The hash table
* @param key Pointer to the key
* @param klen Length of the key
* If the length is 0 it is assumed to be strlen(key)+1
* @param klen Length of the key. Can be APR_HASH_KEY_STRING.
* @param val Value to associate with the key
* @tip If the value is NULL the hash entry is deleted.
* @deffunc void apr_hash_set(apr_hash_t *ht, const void *key, apr_size_t klen, const void *val)
*/
APR_EXPORT(void) apr_hash_set(apr_hash_t *ht, const void *key,
apr_size_t klen, const void *val);
apr_ssize_t klen, const void *val);

/**
* Look up the value associated with a key in a hash table.
* @param ht The hash table
* @param key Pointer to the key
* @param klen Length of the key
* If the length is 0 it is assumed to be strlen(key)+1
* @param klen Length of the key. Can be APR_HASH_KEY_STRING.
* @return Returns NULL if the key is not present.
* @deffunc void *apr_hash_get(apr_hash_t *ht, const void *key, apr_size_t klen)
*/
APR_EXPORT(void*) apr_hash_get(apr_hash_t *ht, const void *key,
apr_size_t klen);
apr_ssize_t klen);

/**
* Start iterating over the entries in a hash table.
Expand Down
8 changes: 4 additions & 4 deletions tables/apr_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ static void resize_array(apr_hash_t *ht)

static apr_hash_entry_t **find_entry(apr_hash_t *ht,
const void *key,
apr_size_t klen,
apr_ssize_t klen,
const void *val)
{
apr_hash_entry_t **hep, *he;
const unsigned char *p;
int hash;
int i;

if (klen == 0)
if (klen == APR_HASH_KEY_STRING)
klen = strlen(key) + 1;

/*
Expand Down Expand Up @@ -282,7 +282,7 @@ static apr_hash_entry_t **find_entry(apr_hash_t *ht,

APR_EXPORT(void *) apr_hash_get(apr_hash_t *ht,
const void *key,
apr_size_t klen)
apr_ssize_t klen)
{
apr_hash_entry_t *he;
he = *find_entry(ht, key, klen, NULL);
Expand All @@ -294,7 +294,7 @@ APR_EXPORT(void *) apr_hash_get(apr_hash_t *ht,

APR_EXPORT(void) apr_hash_set(apr_hash_t *ht,
const void *key,
apr_size_t klen,
apr_ssize_t klen,
const void *val)
{
apr_hash_entry_t **hep;
Expand Down

0 comments on commit 7de5dec

Please sign in to comment.