1 /* str.h - XBT string related functions. */
3 /* Copyright (c) 2007-2017. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "xbt/dynar.h"
16 #include <stdarg.h> /* va_* */
17 #include <stdio.h> /* FILE */
21 /** @addtogroup XBT_str
22 * @brief String manipulation functions
24 * This module defines several string related functions. Looking at the diversity of string manipulation functions that
25 * are provided, you can see that several SimGrid core developers actually like Perl.
29 XBT_PUBLIC(xbt_dynar_t) xbt_str_split(const char *s, const char *sep);
30 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted(const char *s);
31 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted_in_place(char *s);
33 XBT_PUBLIC(char *) xbt_str_join_array(const char *const *strs, const char *sep);
35 XBT_PUBLIC(long int) xbt_str_parse_int(const char* str, const char* error_msg);
36 XBT_PUBLIC(double) xbt_str_parse_double(const char* str, const char* error_msg);
38 #define XBT_DJB2_HASH_FUNCTION
39 //#define XBT_FNV_HASH_FUNCTION
42 * @brief Returns the hash code of a string.
44 static inline unsigned int xbt_str_hash_ext(const char *str, int str_len)
46 #ifdef XBT_DJB2_HASH_FUNCTION
47 /* fast implementation of djb2 algorithm */
48 unsigned int hash = 5381;
52 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
54 # elif defined(XBT_FNV_HASH_FUNCTION)
55 unsigned int hash = 0x811c9dc5;
56 unsigned char *bp = (unsigned char *) str; /* start of buffer */
57 unsigned char *be = bp + str_len; /* beyond end of buffer */
60 /* multiply by the 32 bit FNV magic prime mod 2^32 */
62 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
65 /* xor the bottom with the current octet */
66 hash ^= (unsigned int) *bp++;
70 unsigned int hash = 0;
73 hash += (*str) * (*str);
82 * @brief Returns the hash code of a string.
84 static inline unsigned int xbt_str_hash(const char *str)
86 #ifdef XBT_DJB2_HASH_FUNCTION
87 /* fast implementation of djb2 algorithm */
89 unsigned int hash = 5381;
91 while ((c = *str++)) {
92 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
95 # elif defined(XBT_FNV_HASH_FUNCTION)
96 unsigned int hash = 0x811c9dc5;
99 /* multiply by the 32 bit FNV magic prime mod 2^32 */
100 hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
102 /* xor the bottom with the current byte */
103 hash ^= (unsigned int) *str++;
107 unsigned int hash = 0;
110 hash += (*str) * (*str);
119 #endif /* XBT_STR_H */