Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite the platform_script.lua used by the test for lua console.
[simgrid.git] / include / xbt / str.h
1 /* str.h - XBT string related functions.                                    */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. 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 <stdarg.h>             /* va_* */
13 #include "xbt/misc.h"
14 #include "xbt/dynar.h"
15 #include "xbt/dict.h"
16 #include "simgrid_config.h"     /* FILE for getline */
17
18 SG_BEGIN_DECL()
19
20 /** @addtogroup XBT_str
21  *  @brief String manipulation functions
22  *
23  * This module defines several string related functions. We redefine some quite classical
24  * functions on the platforms were they are not nativaly defined (such as getline() or
25  * asprintf()), while some other are a bit more exotic.
26  * @{
27  */
28 /* Trim related functions */
29 XBT_PUBLIC(void) xbt_str_rtrim(char *s, const char *char_list);
30 XBT_PUBLIC(void) xbt_str_ltrim(char *s, const char *char_list);
31 XBT_PUBLIC(void) xbt_str_trim(char *s, const char *char_list);
32
33 XBT_PUBLIC(xbt_dynar_t) xbt_str_split(const char *s, const char *sep);
34 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted(const char *s);
35 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted_in_place(char *s);
36
37 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_str(const char *s, const char *sep);
38
39 XBT_PUBLIC(char *) xbt_str_join(xbt_dynar_t dynar, const char *sep);
40 XBT_PUBLIC(char *) xbt_str_join_array(const char *const *strs, const char *sep);
41
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 /* */
47 XBT_PUBLIC(void) xbt_str_strip_spaces(char *);
48 XBT_PUBLIC(char *) xbt_str_diff(const char *a, const char *b);
49
50 XBT_PUBLIC(char *) xbt_str_from_file(FILE * file);
51
52 #define DJB2_HASH_FUNCTION
53 //#define FNV_HASH_FUNCTION
54
55 /**
56  * @brief Returns the hash code of a string.
57  */
58 static XBT_INLINE unsigned int xbt_str_hash_ext(const char *str, int str_len)
59 {
60
61 #ifdef DJB2_HASH_FUNCTION
62   /* fast implementation of djb2 algorithm */
63   int c;
64   register unsigned int hash = 5381;
65
66   while (str_len--) {
67     c = *str++;
68     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
69   }
70 # elif defined(FNV_HASH_FUNCTION)
71   register unsigned int hash = 0x811c9dc5;
72   unsigned char *bp = (unsigned char *) str;    /* start of buffer */
73   unsigned char *be = bp + str_len;     /* beyond end of buffer */
74
75   while (bp < be) {
76     /* multiply by the 32 bit FNV magic prime mod 2^32 */
77     hash +=
78         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
79         (hash << 24);
80
81     /* xor the bottom with the current octet */
82     hash ^= (unsigned int) *bp++;
83   }
84
85 # else
86   register unsigned int hash = 0;
87
88   while (str_len--) {
89     hash += (*str) * (*str);
90     str++;
91   }
92 #endif
93
94   return hash;
95 }
96
97 /**
98  * @brief Returns the hash code of a string.
99  */
100 static XBT_INLINE unsigned int xbt_str_hash(const char *str)
101 {
102 #ifdef DJB2_HASH_FUNCTION
103   /* fast implementation of djb2 algorithm */
104   int c;
105   register unsigned int hash = 5381;
106
107   while ((c = *str++)) {
108     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
109   }
110
111 # elif defined(FNV_HASH_FUNCTION)
112   register unsigned int hash = 0x811c9dc5;
113
114   while (*str) {
115     /* multiply by the 32 bit FNV magic prime mod 2^32 */
116     hash +=
117         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
118         (hash << 24);
119
120     /* xor the bottom with the current byte */
121     hash ^= (unsigned int) *str++;
122   }
123
124 # else
125   register unsigned int hash = 0;
126
127   while (*str) {
128     hash += (*str) * (*str);
129     str++;
130   }
131 #endif
132   return hash;
133 }
134                                                       
135 /** @brief Classical alias to (char*)
136  *
137  * This of almost no use, beside cosmetics and the XBT datadesc parsing macro (see \ref XBT_dd_auto).
138  */
139 typedef char *xbt_string_t;
140
141 /**@}*/
142
143 SG_END_DECL()
144 #endif                          /* XBT_STR_H */