Logo AND Algorithmique Numérique Distribuée

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