Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'models_type_rework_part2_try2' into 'master'
[simgrid.git] / include / xbt / str.h
1 /* str.h - XBT string related functions.                                    */
2
3 /* Copyright (c) 2007-2021. 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 long int xbt_str_parse_int(const char* str, const char* error_mesg);
28 XBT_PUBLIC double xbt_str_parse_double(const char* str, const char* error_mesg);
29
30 #define XBT_DJB2_HASH_FUNCTION
31 //#define XBT_FNV_HASH_FUNCTION
32
33 /**
34  * @brief Returns the hash code of a string.
35  */
36 static inline unsigned int xbt_str_hash_ext(const char* str, int str_len)
37 {
38 #ifdef XBT_DJB2_HASH_FUNCTION
39   /* fast implementation of djb2 algorithm */
40   unsigned int hash = 5381;
41
42   while (str_len--) {
43     int c = *str++;
44     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
45   }
46 # elif defined(XBT_FNV_HASH_FUNCTION)
47   unsigned int hash = 0x811c9dc5;
48   unsigned char *bp = (unsigned char *) str;    /* start of buffer */
49   unsigned char *be = bp + str_len;     /* beyond end of buffer */
50
51   while (bp < be) {
52     /* multiply by the 32 bit FNV magic prime mod 2^32 */
53     hash +=
54         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
55         (hash << 24);
56
57     /* xor the bottom with the current octet */
58     hash ^= (unsigned int) *bp++;
59   }
60
61 # else
62   unsigned int hash = 0;
63
64   while (str_len--) {
65     hash += (*str) * (*str);
66     str++;
67   }
68 #endif
69
70   return hash;
71 }
72
73 /**
74  * @brief Returns the hash code of a string.
75  */
76 static inline unsigned int xbt_str_hash(const char *str)
77 {
78 #ifdef XBT_DJB2_HASH_FUNCTION
79   /* fast implementation of djb2 algorithm */
80   int c;
81   unsigned int hash = 5381;
82
83   while ((c = *str++)) {
84     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
85   }
86
87 # elif defined(XBT_FNV_HASH_FUNCTION)
88   unsigned int hash = 0x811c9dc5;
89
90   while (*str) {
91     /* multiply by the 32 bit FNV magic prime mod 2^32 */
92     hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
93
94     /* xor the bottom with the current byte */
95     hash ^= (unsigned int) *str++;
96   }
97
98 # else
99   unsigned int hash = 0;
100
101   while (*str) {
102     hash += (*str) * (*str);
103     str++;
104   }
105 #endif
106   return hash;
107 }
108
109 /**@}*/
110 SG_END_DECL
111 #endif                          /* XBT_STR_H */