1*2fdd1d9eSIngo Weinhold /* 2*2fdd1d9eSIngo Weinhold * Copyright 2002-2013, Haiku, Inc. All rights reserved. 3*2fdd1d9eSIngo Weinhold * Distributed under the terms of the MIT License. 4*2fdd1d9eSIngo Weinhold * 5*2fdd1d9eSIngo Weinhold * Copyright 2001, Travis Geiselbrecht. All rights reserved. 6*2fdd1d9eSIngo Weinhold * Distributed under the terms of the NewOS License. 7*2fdd1d9eSIngo Weinhold */ 8*2fdd1d9eSIngo Weinhold 9*2fdd1d9eSIngo Weinhold 10*2fdd1d9eSIngo Weinhold #include <util/StringHash.h> 11*2fdd1d9eSIngo Weinhold 12*2fdd1d9eSIngo Weinhold 13*2fdd1d9eSIngo Weinhold uint32 14*2fdd1d9eSIngo Weinhold hash_hash_string(const char* string) 15*2fdd1d9eSIngo Weinhold { 16*2fdd1d9eSIngo Weinhold uint32 hash = 0; 17*2fdd1d9eSIngo Weinhold char c; 18*2fdd1d9eSIngo Weinhold 19*2fdd1d9eSIngo Weinhold // we assume hash to be at least 32 bits 20*2fdd1d9eSIngo Weinhold while ((c = *string++) != 0) { 21*2fdd1d9eSIngo Weinhold hash ^= hash >> 28; 22*2fdd1d9eSIngo Weinhold hash <<= 4; 23*2fdd1d9eSIngo Weinhold hash ^= c; 24*2fdd1d9eSIngo Weinhold } 25*2fdd1d9eSIngo Weinhold 26*2fdd1d9eSIngo Weinhold return hash; 27*2fdd1d9eSIngo Weinhold } 28*2fdd1d9eSIngo Weinhold 29*2fdd1d9eSIngo Weinhold 30*2fdd1d9eSIngo Weinhold uint32 31*2fdd1d9eSIngo Weinhold hash_hash_string_part(const char* string, size_t maxLength) 32*2fdd1d9eSIngo Weinhold { 33*2fdd1d9eSIngo Weinhold uint32 hash = 0; 34*2fdd1d9eSIngo Weinhold char c; 35*2fdd1d9eSIngo Weinhold 36*2fdd1d9eSIngo Weinhold // we assume hash to be at least 32 bits 37*2fdd1d9eSIngo Weinhold while (maxLength-- > 0 && (c = *string++) != 0) { 38*2fdd1d9eSIngo Weinhold hash ^= hash >> 28; 39*2fdd1d9eSIngo Weinhold hash <<= 4; 40*2fdd1d9eSIngo Weinhold hash ^= c; 41*2fdd1d9eSIngo Weinhold } 42*2fdd1d9eSIngo Weinhold 43*2fdd1d9eSIngo Weinhold return hash; 44*2fdd1d9eSIngo Weinhold } 45