1 /* str.h - XBT string related functions. */
3 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include <xbt/dynar.h>
15 #include <stdarg.h> /* va_* */
19 /** @addtogroup XBT_str
20 * @brief String manipulation functions
22 * This module defines several string related functions. Looking at the diversity of string manipulation functions that
23 * are provided, you can see that several SimGrid core developers actually like Perl.
27 XBT_PUBLIC long int xbt_str_parse_int(const char* str, const char* error_mesg);
28 XBT_PUBLIC double xbt_str_parse_double(const char* str, const char* error_mesg);
30 #define XBT_DJB2_HASH_FUNCTION
31 //#define XBT_FNV_HASH_FUNCTION
34 * @brief Returns the hash code of a string.
36 static inline unsigned int xbt_str_hash_ext(const char* str, int str_len)
38 #ifdef XBT_DJB2_HASH_FUNCTION
39 /* fast implementation of djb2 algorithm */
40 unsigned int hash = 5381;
44 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
46 # elif defined(XBT_FNV_HASH_FUNCTION)
47 unsigned int hash = 0x811c9dc5;
48 unsigned char *bp = (unsigned char *) str; /* start of buffer */
49 unsigned char *be = bp + str_len; /* beyond end of buffer */
52 /* multiply by the 32 bit FNV magic prime mod 2^32 */
54 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
57 /* xor the bottom with the current octet */
58 hash ^= (unsigned int) *bp++;
62 unsigned int hash = 0;
65 hash += (*str) * (*str);
74 * @brief Returns the hash code of a string.
76 static inline unsigned int xbt_str_hash(const char *str)
78 #ifdef XBT_DJB2_HASH_FUNCTION
79 /* fast implementation of djb2 algorithm */
81 unsigned int hash = 5381;
83 while ((c = *str++)) {
84 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
87 # elif defined(XBT_FNV_HASH_FUNCTION)
88 unsigned int hash = 0x811c9dc5;
91 /* multiply by the 32 bit FNV magic prime mod 2^32 */
92 hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
94 /* xor the bottom with the current byte */
95 hash ^= (unsigned int) *str++;
99 unsigned int hash = 0;
102 hash += (*str) * (*str);
111 #endif /* XBT_STR_H */