Logo AND Algorithmique Numérique Distribuée

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