Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
05da4c68078714483927068d04fd4eea66e540f7
[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 #ifdef _MSC_VER
20 #define strcasecmp _stricmp
21 #endif
22
23 SG_BEGIN_DECL()
24
25 /** @addtogroup XBT_str
26  *  @brief String manipulation functions
27  *
28  * This module defines several string related functions. Looking at the diversity of string manipulation functions that
29  * are provided, you can see that several SimGrid core developers actually like Perl.
30  * @{
31  */
32
33 /* Trim related functions */
34 XBT_PUBLIC(void) xbt_str_rtrim(char *s, const char *char_list);
35 XBT_PUBLIC(void) xbt_str_ltrim(char *s, const char *char_list);
36 XBT_PUBLIC(void) xbt_str_trim(char *s, const char *char_list);
37
38 XBT_PUBLIC(xbt_dynar_t) xbt_str_split(const char *s, const char *sep);
39 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted(const char *s);
40 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted_in_place(char *s);
41
42 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_str(const char *s, const char *sep);
43
44 XBT_PUBLIC(char *) xbt_str_join(xbt_dynar_t dynar, const char *sep);
45 XBT_PUBLIC(char *) xbt_str_join_array(const char *const *strs, const char *sep);
46
47 /* */
48 XBT_PUBLIC(void) xbt_str_subst(char *str, char from, char to, int amount);
49 XBT_PUBLIC(char *) xbt_str_varsubst(const char *str, xbt_dict_t patterns);
50
51 /* */
52 XBT_PUBLIC(char *) xbt_str_from_file(FILE * file);
53
54 XBT_PUBLIC(long int) xbt_str_parse_int(const char* str, const char* error_msg);
55 XBT_PUBLIC(double) xbt_str_parse_double(const char* str, const char* error_msg);
56
57 #define DJB2_HASH_FUNCTION
58 //#define FNV_HASH_FUNCTION
59
60 /**
61  * @brief Returns the hash code of a string.
62  */
63 static inline unsigned int xbt_str_hash_ext(const char *str, int str_len)
64 {
65
66 #ifdef DJB2_HASH_FUNCTION
67   /* fast implementation of djb2 algorithm */
68   int c;
69   unsigned int hash = 5381;
70
71   while (str_len--) {
72     c = *str++;
73     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
74   }
75 # elif defined(FNV_HASH_FUNCTION)
76   unsigned int hash = 0x811c9dc5;
77   unsigned char *bp = (unsigned char *) str;    /* start of buffer */
78   unsigned char *be = bp + str_len;     /* beyond end of buffer */
79
80   while (bp < be) {
81     /* multiply by the 32 bit FNV magic prime mod 2^32 */
82     hash +=
83         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
84         (hash << 24);
85
86     /* xor the bottom with the current octet */
87     hash ^= (unsigned int) *bp++;
88   }
89
90 # else
91   unsigned int hash = 0;
92
93   while (str_len--) {
94     hash += (*str) * (*str);
95     str++;
96   }
97 #endif
98
99   return hash;
100 }
101
102 /**
103  * @brief Returns the hash code of a string.
104  */
105 static inline unsigned int xbt_str_hash(const char *str)
106 {
107 #ifdef DJB2_HASH_FUNCTION
108   /* fast implementation of djb2 algorithm */
109   int c;
110   unsigned int hash = 5381;
111
112   while ((c = *str++)) {
113     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
114   }
115
116 # elif defined(FNV_HASH_FUNCTION)
117   unsigned int hash = 0x811c9dc5;
118
119   while (*str) {
120     /* multiply by the 32 bit FNV magic prime mod 2^32 */
121     hash +=
122         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
123         (hash << 24);
124
125     /* xor the bottom with the current byte */
126     hash ^= (unsigned int) *str++;
127   }
128
129 # else
130   unsigned int hash = 0;
131
132   while (*str) {
133     hash += (*str) * (*str);
134     str++;
135   }
136 #endif
137   return hash;
138 }
139
140 /**@}*/
141
142 SG_END_DECL()
143 #endif                          /* XBT_STR_H */