Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make room for the upcoming network energy plugin
[simgrid.git] / include / xbt / str.h
1 /* str.h - XBT string related functions.                                    */
2
3 /* Copyright (c) 2007-2015. 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/misc.h"
13 #include "xbt/dynar.h"
14 #include "xbt/dict.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 /* Trim related functions */
30 XBT_PUBLIC(void) xbt_str_rtrim(char *s, const char *char_list);
31 XBT_PUBLIC(void) xbt_str_ltrim(char *s, const char *char_list);
32 XBT_PUBLIC(void) xbt_str_trim(char *s, const char *char_list);
33
34 XBT_PUBLIC(xbt_dynar_t) xbt_str_split(const char *s, const char *sep);
35 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted(const char *s);
36 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted_in_place(char *s);
37
38 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_str(const char *s, const char *sep);
39
40 XBT_PUBLIC(char *) xbt_str_join(xbt_dynar_t dynar, const char *sep);
41 XBT_PUBLIC(char *) xbt_str_join_array(const char *const *strs, const char *sep);
42
43 XBT_PUBLIC(void) xbt_str_subst(char *str, char from, char to, int amount);
44 XBT_PUBLIC(char *) xbt_str_varsubst(const char *str, xbt_dict_t patterns);
45
46 XBT_PUBLIC(char *) xbt_str_from_file(FILE * file);
47
48 XBT_PUBLIC(long int) xbt_str_parse_int(const char* str, const char* error_msg);
49 XBT_PUBLIC(double) xbt_str_parse_double(const char* str, const char* error_msg);
50
51 #define XBT_DJB2_HASH_FUNCTION
52 //#define XBT_FNV_HASH_FUNCTION
53
54 /**
55  * @brief Returns the hash code of a string.
56  */
57 static inline unsigned int xbt_str_hash_ext(const char *str, int str_len)
58 {
59 #ifdef XBT_DJB2_HASH_FUNCTION
60   /* fast implementation of djb2 algorithm */
61   int c;
62   unsigned int hash = 5381;
63
64   while (str_len--) {
65     c = *str++;
66     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
67   }
68 # elif defined(XBT_FNV_HASH_FUNCTION)
69   unsigned int hash = 0x811c9dc5;
70   unsigned char *bp = (unsigned char *) str;    /* start of buffer */
71   unsigned char *be = bp + str_len;     /* beyond end of buffer */
72
73   while (bp < be) {
74     /* multiply by the 32 bit FNV magic prime mod 2^32 */
75     hash +=
76         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
77         (hash << 24);
78
79     /* xor the bottom with the current octet */
80     hash ^= (unsigned int) *bp++;
81   }
82
83 # else
84   unsigned int hash = 0;
85
86   while (str_len--) {
87     hash += (*str) * (*str);
88     str++;
89   }
90 #endif
91
92   return hash;
93 }
94
95 /**
96  * @brief Returns the hash code of a string.
97  */
98 static inline unsigned int xbt_str_hash(const char *str)
99 {
100 #ifdef XBT_DJB2_HASH_FUNCTION
101   /* fast implementation of djb2 algorithm */
102   int c;
103   unsigned int hash = 5381;
104
105   while ((c = *str++)) {
106     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
107   }
108
109 # elif defined(XBT_FNV_HASH_FUNCTION)
110   unsigned int hash = 0x811c9dc5;
111
112   while (*str) {
113     /* multiply by the 32 bit FNV magic prime mod 2^32 */
114     hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
115
116     /* xor the bottom with the current byte */
117     hash ^= (unsigned int) *str++;
118   }
119
120 # else
121   unsigned int hash = 0;
122
123   while (*str) {
124     hash += (*str) * (*str);
125     str++;
126   }
127 #endif
128   return hash;
129 }
130
131 /**@}*/
132 SG_END_DECL()
133 #endif                          /* XBT_STR_H */