1 /* str.h - XBT string related functions. */
3 /* Copyright (c) 2007-2020. 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 xbt_dynar_t xbt_str_split_quoted(const char* s);
28 XBT_PUBLIC xbt_dynar_t xbt_str_split_quoted_in_place(char* s);
30 XBT_PUBLIC long int xbt_str_parse_int(const char* str, const char* error_mesg);
31 XBT_PUBLIC double xbt_str_parse_double(const char* str, const char* error_mesg);
33 #define XBT_DJB2_HASH_FUNCTION
34 //#define XBT_FNV_HASH_FUNCTION
37 * @brief Returns the hash code of a string.
39 static inline unsigned int xbt_str_hash_ext(const char* str, int str_len)
41 #ifdef XBT_DJB2_HASH_FUNCTION
42 /* fast implementation of djb2 algorithm */
43 unsigned int hash = 5381;
47 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
49 # elif defined(XBT_FNV_HASH_FUNCTION)
50 unsigned int hash = 0x811c9dc5;
51 unsigned char *bp = (unsigned char *) str; /* start of buffer */
52 unsigned char *be = bp + str_len; /* beyond end of buffer */
55 /* multiply by the 32 bit FNV magic prime mod 2^32 */
57 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
60 /* xor the bottom with the current octet */
61 hash ^= (unsigned int) *bp++;
65 unsigned int hash = 0;
68 hash += (*str) * (*str);
77 * @brief Returns the hash code of a string.
79 static inline unsigned int xbt_str_hash(const char *str)
81 #ifdef XBT_DJB2_HASH_FUNCTION
82 /* fast implementation of djb2 algorithm */
84 unsigned int hash = 5381;
86 while ((c = *str++)) {
87 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
90 # elif defined(XBT_FNV_HASH_FUNCTION)
91 unsigned int hash = 0x811c9dc5;
94 /* multiply by the 32 bit FNV magic prime mod 2^32 */
95 hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
97 /* xor the bottom with the current byte */
98 hash ^= (unsigned int) *str++;
102 unsigned int hash = 0;
105 hash += (*str) * (*str);
114 #endif /* XBT_STR_H */