Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add/update copyright notices.
[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 <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 xbt_getline() or
25  * asprintf()), while some other are a bit more exotic.
26  * @{
27  */
28 /* Our own implementation of getline, mainly useful on the platforms not enjoying this function */
29 #include <stdio.h>  /* FILE */
30 #include <stdlib.h> /* size_t, ssize_t */
31 XBT_PUBLIC(ssize_t) xbt_getline(char **lineptr, size_t * n, FILE * stream);
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(void) xbt_str_strip_spaces(char *);
53 XBT_PUBLIC(char *) xbt_str_diff(const char *a, const char *b);
54
55 XBT_PUBLIC(char *) xbt_str_from_file(FILE * file);
56
57 XBT_PUBLIC(int) xbt_str_start_with(const char* str, const char* start);
58
59 #define DJB2_HASH_FUNCTION
60 //#define FNV_HASH_FUNCTION
61
62 /**
63  * @brief Returns the hash code of a string.
64  */
65 static XBT_INLINE unsigned int xbt_str_hash_ext(const char *str, int str_len)
66 {
67
68 #ifdef DJB2_HASH_FUNCTION
69   /* fast implementation of djb2 algorithm */
70   int c;
71   register unsigned int hash = 5381;
72
73   while (str_len--) {
74     c = *str++;
75     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
76   }
77 # elif defined(FNV_HASH_FUNCTION)
78   register unsigned int hash = 0x811c9dc5;
79   unsigned char *bp = (unsigned char *) str;    /* start of buffer */
80   unsigned char *be = bp + str_len;     /* beyond end of buffer */
81
82   while (bp < be) {
83     /* multiply by the 32 bit FNV magic prime mod 2^32 */
84     hash +=
85         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
86         (hash << 24);
87
88     /* xor the bottom with the current octet */
89     hash ^= (unsigned int) *bp++;
90   }
91
92 # else
93   register unsigned int hash = 0;
94
95   while (str_len--) {
96     hash += (*str) * (*str);
97     str++;
98   }
99 #endif
100
101   return hash;
102 }
103
104 /**
105  * @brief Returns the hash code of a string.
106  */
107 static XBT_INLINE unsigned int xbt_str_hash(const char *str)
108 {
109 #ifdef DJB2_HASH_FUNCTION
110   /* fast implementation of djb2 algorithm */
111   int c;
112   register unsigned int hash = 5381;
113
114   while ((c = *str++)) {
115     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
116   }
117
118 # elif defined(FNV_HASH_FUNCTION)
119   register unsigned int hash = 0x811c9dc5;
120
121   while (*str) {
122     /* multiply by the 32 bit FNV magic prime mod 2^32 */
123     hash +=
124         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
125         (hash << 24);
126
127     /* xor the bottom with the current byte */
128     hash ^= (unsigned int) *str++;
129   }
130
131 # else
132   register unsigned int hash = 0;
133
134   while (*str) {
135     hash += (*str) * (*str);
136     str++;
137   }
138 #endif
139   return hash;
140 }
141                                                       
142 /**@}*/
143
144 SG_END_DECL()
145 #endif                          /* XBT_STR_H */