Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix Windows build
[simgrid.git] / include / xbt / str.h
1 /* str.h - XBT string related functions.                                    */
2
3 /* Copyright (c) 2007-2014. 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 <stdint.h> /* ssize_t */
13 #include <stdarg.h>             /* va_* */
14 #include "xbt/misc.h"
15 #include "xbt/dynar.h"
16 #include "xbt/dict.h"
17 #include "simgrid_config.h"     /* FILE for getline */
18
19 SG_BEGIN_DECL()
20
21 /** @addtogroup XBT_str
22  *  @brief String manipulation functions
23  *
24  * This module defines several string related functions. We redefine some quite classical
25  * functions on the platforms were they are not nativaly defined (such as xbt_getline() or
26  * asprintf()), while some other are a bit more exotic.
27  * @{
28  */
29 /* Our own implementation of getline, mainly useful on the platforms not enjoying this function */
30 #include <stdio.h>  /* FILE */
31 #include <stdlib.h> /* size_t, ssize_t */
32 XBT_PUBLIC(ssize_t) xbt_getline(char **lineptr, size_t * n, FILE * stream);
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 #define DJB2_HASH_FUNCTION
61 //#define FNV_HASH_FUNCTION
62
63 /**
64  * @brief Returns the hash code of a string.
65  */
66 static XBT_INLINE unsigned int xbt_str_hash_ext(const char *str, int str_len)
67 {
68
69 #ifdef DJB2_HASH_FUNCTION
70   /* fast implementation of djb2 algorithm */
71   int c;
72   unsigned int hash = 5381;
73
74   while (str_len--) {
75     c = *str++;
76     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
77   }
78 # elif defined(FNV_HASH_FUNCTION)
79   unsigned int hash = 0x811c9dc5;
80   unsigned char *bp = (unsigned char *) str;    /* start of buffer */
81   unsigned char *be = bp + str_len;     /* beyond end of buffer */
82
83   while (bp < be) {
84     /* multiply by the 32 bit FNV magic prime mod 2^32 */
85     hash +=
86         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
87         (hash << 24);
88
89     /* xor the bottom with the current octet */
90     hash ^= (unsigned int) *bp++;
91   }
92
93 # else
94   unsigned int hash = 0;
95
96   while (str_len--) {
97     hash += (*str) * (*str);
98     str++;
99   }
100 #endif
101
102   return hash;
103 }
104
105 /**
106  * @brief Returns the hash code of a string.
107  */
108 static XBT_INLINE unsigned int xbt_str_hash(const char *str)
109 {
110 #ifdef DJB2_HASH_FUNCTION
111   /* fast implementation of djb2 algorithm */
112   int c;
113   unsigned int hash = 5381;
114
115   while ((c = *str++)) {
116     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
117   }
118
119 # elif defined(FNV_HASH_FUNCTION)
120   unsigned int hash = 0x811c9dc5;
121
122   while (*str) {
123     /* multiply by the 32 bit FNV magic prime mod 2^32 */
124     hash +=
125         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
126         (hash << 24);
127
128     /* xor the bottom with the current byte */
129     hash ^= (unsigned int) *str++;
130   }
131
132 # else
133   unsigned int hash = 0;
134
135   while (*str) {
136     hash += (*str) * (*str);
137     str++;
138   }
139 #endif
140   return hash;
141 }
142
143 /**@}*/
144
145 SG_END_DECL()
146 #endif                          /* XBT_STR_H */