Add function to get a nsurl's hash value.

This commit is contained in:
Michael Drake 2013-05-17 12:25:04 +01:00
parent abebc6ae2b
commit 39cc1a6d4a
2 changed files with 74 additions and 0 deletions

View File

@ -154,6 +154,7 @@ struct nsurl {
struct nsurl_components components;
int count; /* Number of references to NetSurf URL object */
uint32_t hash; /* Hash value for nsurl identification */
size_t length; /* Length of string */
char string[FLEX_ARRAY_LEN_DECL]; /* Full URL as a string */
@ -1185,6 +1186,43 @@ static void nsurl_get_string(const struct nsurl_components *url, char *url_s,
}
/**
* Calculate hash value
*
* \param url NetSurf URL object to set hash value for
*/
static void nsurl_calc_hash(nsurl *url)
{
uint32_t hash = 0;
if (url->components.scheme)
hash ^= lwc_string_hash_value(url->components.scheme);
if (url->components.username)
hash ^= lwc_string_hash_value(url->components.username);
if (url->components.password)
hash ^= lwc_string_hash_value(url->components.password);
if (url->components.host)
hash ^= lwc_string_hash_value(url->components.host);
if (url->components.port)
hash ^= lwc_string_hash_value(url->components.port);
if (url->components.path)
hash ^= lwc_string_hash_value(url->components.path);
if (url->components.query)
hash ^= lwc_string_hash_value(url->components.query);
if (url->components.fragment)
hash ^= lwc_string_hash_value(url->components.fragment);
url->hash = hash;
}
#ifdef NSURL_DEBUG
/**
* Dump a NetSurf URL's internal components
@ -1282,6 +1320,9 @@ nserror nsurl_create(const char * const url_s, nsurl **url)
/* Fill out the url string */
nsurl_get_string(&c, (*url)->string, &str_len, str_flags);
/* Get the nsurl's hash */
nsurl_calc_hash(*url);
/* Give the URL a reference */
(*url)->count = 1;
@ -1614,6 +1655,15 @@ size_t nsurl_length(const nsurl *url)
}
/* exported interface, documented in nsurl.h */
uint32_t nsurl_hash(const nsurl *url)
{
assert(url != NULL);
return url->hash;
}
/* exported interface, documented in nsurl.h */
nserror nsurl_join(const nsurl *base, const char *rel, nsurl **joined)
{
@ -1819,6 +1869,9 @@ nserror nsurl_join(const nsurl *base, const char *rel, nsurl **joined)
/* Fill out the url string */
nsurl_get_string(&c, (*joined)->string, &str_len, str_flags);
/* Get the nsurl's hash */
nsurl_calc_hash(*joined);
/* Give the URL a reference */
(*joined)->count = 1;
@ -1871,6 +1924,9 @@ nserror nsurl_defragment(const nsurl *url, nsurl **no_frag)
pos += length;
*pos = '\0';
/* Get the nsurl's hash */
nsurl_calc_hash(*no_frag);
/* Give the URL a reference */
(*no_frag)->count = 1;
@ -1936,6 +1992,9 @@ nserror nsurl_refragment(const nsurl *url, lwc_string *frag, nsurl **new_url)
(*new_url)->components.scheme_type = url->components.scheme_type;
/* Get the nsurl's hash */
nsurl_calc_hash(*new_url);
/* Give the URL a reference */
(*new_url)->count = 1;
@ -2019,6 +2078,9 @@ nserror nsurl_replace_query(const nsurl *url, const char *query,
(*new_url)->components.scheme_type = url->components.scheme_type;
/* Get the nsurl's hash */
nsurl_calc_hash(*new_url);
/* Give the URL a reference */
(*new_url)->count = 1;
@ -2114,6 +2176,9 @@ nserror nsurl_parent(const nsurl *url, nsurl **new_url)
(*new_url)->components.scheme_type = url->components.scheme_type;
/* Get the nsurl's hash */
nsurl_calc_hash(*new_url);
/* Give the URL a reference */
(*new_url)->count = 1;

View File

@ -207,6 +207,15 @@ const char *nsurl_access_leaf(const nsurl *url);
size_t nsurl_length(const nsurl *url);
/**
* Get a URL's hash value
*
* \param url NetSurf URL get hash value for.
* \return the hash value
*/
uint32_t nsurl_hash(const nsurl *url);
/**
* Join a base url to a relative link part, creating a new NetSurf URL object
*