Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[MSVC] silence some warnings
[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 <stdarg.h>             /* va_* */
13 #include <stdio.h>  /* FILE */
14 #include "xbt/misc.h"
15 #include "xbt/dynar.h"
16 #include "xbt/dict.h"
17
18 SG_BEGIN_DECL()
19
20 /** @addtogroup XBT_str
21  *  @brief String manipulation functions
22  *
23  * This module defines several string related functions. Looking at the diversity of string
24  * manipulation functions that are provided, you can see that several SimGrid core developers
25  * 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 /* */
44 XBT_PUBLIC(void) xbt_str_subst(char *str, char from, char to, int amount);
45 XBT_PUBLIC(char *) xbt_str_varsubst(const char *str, xbt_dict_t patterns);
46
47 /* */
48 XBT_PUBLIC(void) xbt_str_strip_spaces(char *);
49 XBT_PUBLIC(char *) xbt_str_diff(const char *a, const char *b);
50
51 XBT_PUBLIC(char *) xbt_str_from_file(FILE * file);
52
53 XBT_PUBLIC(int) xbt_str_start_with(const char* str, const char* start);
54
55 #define DJB2_HASH_FUNCTION
56 //#define FNV_HASH_FUNCTION
57
58 /**
59  * @brief Returns the hash code of a string.
60  */
61 static XBT_INLINE unsigned int xbt_str_hash_ext(const char *str, int str_len)
62 {
63
64 #ifdef DJB2_HASH_FUNCTION
65   /* fast implementation of djb2 algorithm */
66   int c;
67   unsigned int hash = 5381;
68
69   while (str_len--) {
70     c = *str++;
71     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
72   }
73 # elif defined(FNV_HASH_FUNCTION)
74   unsigned int hash = 0x811c9dc5;
75   unsigned char *bp = (unsigned char *) str;    /* start of buffer */
76   unsigned char *be = bp + str_len;     /* beyond end of buffer */
77
78   while (bp < be) {
79     /* multiply by the 32 bit FNV magic prime mod 2^32 */
80     hash +=
81         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
82         (hash << 24);
83
84     /* xor the bottom with the current octet */
85     hash ^= (unsigned int) *bp++;
86   }
87
88 # else
89   unsigned int hash = 0;
90
91   while (str_len--) {
92     hash += (*str) * (*str);
93     str++;
94   }
95 #endif
96
97   return hash;
98 }
99
100 /**
101  * @brief Returns the hash code of a string.
102  */
103 static XBT_INLINE unsigned int xbt_str_hash(const char *str)
104 {
105 #ifdef DJB2_HASH_FUNCTION
106   /* fast implementation of djb2 algorithm */
107   int c;
108   unsigned int hash = 5381;
109
110   while ((c = *str++)) {
111     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
112   }
113
114 # elif defined(FNV_HASH_FUNCTION)
115   unsigned int hash = 0x811c9dc5;
116
117   while (*str) {
118     /* multiply by the 32 bit FNV magic prime mod 2^32 */
119     hash +=
120         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
121         (hash << 24);
122
123     /* xor the bottom with the current byte */
124     hash ^= (unsigned int) *str++;
125   }
126
127 # else
128   unsigned int hash = 0;
129
130   while (*str) {
131     hash += (*str) * (*str);
132     str++;
133   }
134 #endif
135   return hash;
136 }
137
138 /**@}*/
139
140 SG_END_DECL()
141 #endif                          /* XBT_STR_H */