Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
deprecate xbt_mutex and xbt_cond
[simgrid.git] / include / xbt / str.h
1 /* str.h - XBT string related functions.                                    */
2
3 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 #ifndef XBT_STR_H
9 #define XBT_STR_H
10
11 #include <xbt/dict.h>
12 #include <xbt/dynar.h>
13 #include <xbt/misc.h>
14
15 #include <stdarg.h> /* va_* */
16
17 SG_BEGIN_DECL()
18
19 /** @addtogroup XBT_str
20  *  @brief String manipulation functions
21  *
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.
24  * @{
25  */
26
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);
30
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);
33
34 #define XBT_DJB2_HASH_FUNCTION
35 //#define XBT_FNV_HASH_FUNCTION
36
37 /**
38  * @brief Returns the hash code of a string.
39  */
40 static inline unsigned int xbt_str_hash_ext(const char* str, int str_len)
41 {
42 #ifdef XBT_DJB2_HASH_FUNCTION
43   /* fast implementation of djb2 algorithm */
44   unsigned int hash = 5381;
45
46   while (str_len--) {
47     int c = *str++;
48     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
49   }
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 */
54
55   while (bp < be) {
56     /* multiply by the 32 bit FNV magic prime mod 2^32 */
57     hash +=
58         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
59         (hash << 24);
60
61     /* xor the bottom with the current octet */
62     hash ^= (unsigned int) *bp++;
63   }
64
65 # else
66   unsigned int hash = 0;
67
68   while (str_len--) {
69     hash += (*str) * (*str);
70     str++;
71   }
72 #endif
73
74   return hash;
75 }
76
77 /**
78  * @brief Returns the hash code of a string.
79  */
80 static inline unsigned int xbt_str_hash(const char *str)
81 {
82 #ifdef XBT_DJB2_HASH_FUNCTION
83   /* fast implementation of djb2 algorithm */
84   int c;
85   unsigned int hash = 5381;
86
87   while ((c = *str++)) {
88     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
89   }
90
91 # elif defined(XBT_FNV_HASH_FUNCTION)
92   unsigned int hash = 0x811c9dc5;
93
94   while (*str) {
95     /* multiply by the 32 bit FNV magic prime mod 2^32 */
96     hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
97
98     /* xor the bottom with the current byte */
99     hash ^= (unsigned int) *str++;
100   }
101
102 # else
103   unsigned int hash = 0;
104
105   while (*str) {
106     hash += (*str) * (*str);
107     str++;
108   }
109 #endif
110   return hash;
111 }
112
113 /**@}*/
114 SG_END_DECL()
115 #endif                          /* XBT_STR_H */