Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill the now useless xbt_os_thread_{get,set}_extra_data()
[simgrid.git] / include / xbt / str.h
1 /* str.h - XBT string related functions.                                    */
2
3 /* Copyright (c) 2007-2018. 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 char* xbt_str_join_array(const char* const* strs, const char* sep);
32
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);
35
36 #define XBT_DJB2_HASH_FUNCTION
37 //#define XBT_FNV_HASH_FUNCTION
38
39 /**
40  * @brief Returns the hash code of a string.
41  */
42 static inline unsigned int xbt_str_hash_ext(const char* str, int str_len)
43 {
44 #ifdef XBT_DJB2_HASH_FUNCTION
45   /* fast implementation of djb2 algorithm */
46   unsigned int hash = 5381;
47
48   while (str_len--) {
49     int c = *str++;
50     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
51   }
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 */
56
57   while (bp < be) {
58     /* multiply by the 32 bit FNV magic prime mod 2^32 */
59     hash +=
60         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
61         (hash << 24);
62
63     /* xor the bottom with the current octet */
64     hash ^= (unsigned int) *bp++;
65   }
66
67 # else
68   unsigned int hash = 0;
69
70   while (str_len--) {
71     hash += (*str) * (*str);
72     str++;
73   }
74 #endif
75
76   return hash;
77 }
78
79 /**
80  * @brief Returns the hash code of a string.
81  */
82 static inline unsigned int xbt_str_hash(const char *str)
83 {
84 #ifdef XBT_DJB2_HASH_FUNCTION
85   /* fast implementation of djb2 algorithm */
86   int c;
87   unsigned int hash = 5381;
88
89   while ((c = *str++)) {
90     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
91   }
92
93 # elif defined(XBT_FNV_HASH_FUNCTION)
94   unsigned int hash = 0x811c9dc5;
95
96   while (*str) {
97     /* multiply by the 32 bit FNV magic prime mod 2^32 */
98     hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
99
100     /* xor the bottom with the current byte */
101     hash ^= (unsigned int) *str++;
102   }
103
104 # else
105   unsigned int hash = 0;
106
107   while (*str) {
108     hash += (*str) * (*str);
109     str++;
110   }
111 #endif
112   return hash;
113 }
114
115 /**@}*/
116 SG_END_DECL()
117 #endif                          /* XBT_STR_H */