Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duk_heap_hashstring.c File Reference
#include "duk_internal.h"

Go to the source code of this file.

Functions

DUK_INTERNAL duk_uint32_t duk_heap_hashstring (duk_heap *heap, const duk_uint8_t *str, duk_size_t len)
 

Function Documentation

◆ duk_heap_hashstring()

DUK_INTERNAL duk_uint32_t duk_heap_hashstring ( duk_heap * heap,
const duk_uint8_t * str,
duk_size_t len )

Definition at line 88 of file duktape-1.8.0/src-separate/duk_heap_hashstring.c.

88 {
89 duk_uint32_t hash;
90 duk_size_t step;
91 duk_size_t off;
92
93 /* Slightly modified "Bernstein hash" from:
94 *
95 * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
96 *
97 * Modifications: string skipping and reverse direction similar to
98 * Lua 5.1.5, and different hash initializer.
99 *
100 * The reverse direction ensures last byte it always included in the
101 * hash which is a good default as changing parts of the string are
102 * more often in the suffix than in the prefix.
103 */
104
105 hash = heap->hash_seed ^ ((duk_uint32_t) len); /* Bernstein hash init value is normally 5381 */
106 step = (len >> DUK_USE_STRHASH_SKIP_SHIFT) + 1;
107 for (off = len; off >= step; off -= step) {
108 DUK_ASSERT(off >= 1); /* off >= step, and step >= 1 */
109 hash = (hash * 33) + str[off - 1];
110 }
111
112#if defined(DUK_USE_STRHASH16)
113 /* Truncate to 16 bits here, so that a computed hash can be compared
114 * against a hash stored in a 16-bit field.
115 */
116 hash &= 0x0000ffffUL;
117#endif
118 return hash;
119}

References DUK_ASSERT, DUK_USE_STRHASH_SKIP_SHIFT, and duk_heap::hash_seed.