1 /* str.h - XBT string related functions. */
3 /* Copyright (c) 2007-2018. 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 char* xbt_str_join_array(const char* const* strs, const char* sep);
33 XBT_PUBLIC long int xbt_str_parse_int(const char* str, const char* error_mesg);
34 XBT_PUBLIC double xbt_str_parse_double(const char* str, const char* error_mesg);
36 #define XBT_DJB2_HASH_FUNCTION
37 //#define XBT_FNV_HASH_FUNCTION
40 * @brief Returns the hash code of a string.
42 static inline unsigned int xbt_str_hash_ext(const char* str, int str_len)
44 #ifdef XBT_DJB2_HASH_FUNCTION
45 /* fast implementation of djb2 algorithm */
46 unsigned int hash = 5381;
50 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
52 # elif defined(XBT_FNV_HASH_FUNCTION)
53 unsigned int hash = 0x811c9dc5;
54 unsigned char *bp = (unsigned char *) str; /* start of buffer */
55 unsigned char *be = bp + str_len; /* beyond end of buffer */
58 /* multiply by the 32 bit FNV magic prime mod 2^32 */
60 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
63 /* xor the bottom with the current octet */
64 hash ^= (unsigned int) *bp++;
68 unsigned int hash = 0;
71 hash += (*str) * (*str);
80 * @brief Returns the hash code of a string.
82 static inline unsigned int xbt_str_hash(const char *str)
84 #ifdef XBT_DJB2_HASH_FUNCTION
85 /* fast implementation of djb2 algorithm */
87 unsigned int hash = 5381;
89 while ((c = *str++)) {
90 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
93 # elif defined(XBT_FNV_HASH_FUNCTION)
94 unsigned int hash = 0x811c9dc5;
97 /* multiply by the 32 bit FNV magic prime mod 2^32 */
98 hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
100 /* xor the bottom with the current byte */
101 hash ^= (unsigned int) *str++;
105 unsigned int hash = 0;
108 hash += (*str) * (*str);
117 #endif /* XBT_STR_H */