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(const char* s, const char* sep);
28 XBT_PUBLIC xbt_dynar_t xbt_str_split_quoted(const char* s);
29 XBT_PUBLIC xbt_dynar_t xbt_str_split_quoted_in_place(char* s);
31 XBT_PUBLIC long int xbt_str_parse_int(const char* str, const char* error_mesg);
32 XBT_PUBLIC double xbt_str_parse_double(const char* str, const char* error_mesg);
34 #define XBT_DJB2_HASH_FUNCTION
35 //#define XBT_FNV_HASH_FUNCTION
38 * @brief Returns the hash code of a string.
40 static inline unsigned int xbt_str_hash_ext(const char* str, int str_len)
42 #ifdef XBT_DJB2_HASH_FUNCTION
43 /* fast implementation of djb2 algorithm */
44 unsigned int hash = 5381;
48 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
50 # elif defined(XBT_FNV_HASH_FUNCTION)
51 unsigned int hash = 0x811c9dc5;
52 unsigned char *bp = (unsigned char *) str; /* start of buffer */
53 unsigned char *be = bp + str_len; /* beyond end of buffer */
56 /* multiply by the 32 bit FNV magic prime mod 2^32 */
58 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
61 /* xor the bottom with the current octet */
62 hash ^= (unsigned int) *bp++;
66 unsigned int hash = 0;
69 hash += (*str) * (*str);
78 * @brief Returns the hash code of a string.
80 static inline unsigned int xbt_str_hash(const char *str)
82 #ifdef XBT_DJB2_HASH_FUNCTION
83 /* fast implementation of djb2 algorithm */
85 unsigned int hash = 5381;
87 while ((c = *str++)) {
88 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
91 # elif defined(XBT_FNV_HASH_FUNCTION)
92 unsigned int hash = 0x811c9dc5;
95 /* multiply by the 32 bit FNV magic prime mod 2^32 */
96 hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
98 /* xor the bottom with the current byte */
99 hash ^= (unsigned int) *str++;
103 unsigned int hash = 0;
106 hash += (*str) * (*str);
115 #endif /* XBT_STR_H */