Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Protect C++ code when building in C mode.
[simgrid.git] / include / xbt / str.h
1 /* str.h - XBT string related functions.                                    */
2
3 /* Copyright (c) 2007-2017. 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 SG_BEGIN_DECL()
20
21 /** @addtogroup XBT_str
22  *  @brief String manipulation functions
23  *
24  * This module defines several string related functions. Looking at the diversity of string manipulation functions that
25  * are provided, you can see that several SimGrid core developers actually like Perl.
26  * @{
27  */
28
29 XBT_PUBLIC(xbt_dynar_t) xbt_str_split(const char *s, const char *sep);
30 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted(const char *s);
31 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_quoted_in_place(char *s);
32
33 XBT_PUBLIC(char *) xbt_str_join(xbt_dynar_t dynar, const char *sep);
34 XBT_PUBLIC(char *) xbt_str_join_array(const char *const *strs, const char *sep);
35
36 XBT_PUBLIC(long int) xbt_str_parse_int(const char* str, const char* error_msg);
37 XBT_PUBLIC(double) xbt_str_parse_double(const char* str, const char* error_msg);
38
39 #define XBT_DJB2_HASH_FUNCTION
40 //#define XBT_FNV_HASH_FUNCTION
41
42 /**
43  * @brief Returns the hash code of a string.
44  */
45 static inline unsigned int xbt_str_hash_ext(const char *str, int str_len)
46 {
47 #ifdef XBT_DJB2_HASH_FUNCTION
48   /* fast implementation of djb2 algorithm */
49   unsigned int hash = 5381;
50
51   while (str_len--) {
52     int c = *str++;
53     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
54   }
55 # elif defined(XBT_FNV_HASH_FUNCTION)
56   unsigned int hash = 0x811c9dc5;
57   unsigned char *bp = (unsigned char *) str;    /* start of buffer */
58   unsigned char *be = bp + str_len;     /* beyond end of buffer */
59
60   while (bp < be) {
61     /* multiply by the 32 bit FNV magic prime mod 2^32 */
62     hash +=
63         (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
64         (hash << 24);
65
66     /* xor the bottom with the current octet */
67     hash ^= (unsigned int) *bp++;
68   }
69
70 # else
71   unsigned int hash = 0;
72
73   while (str_len--) {
74     hash += (*str) * (*str);
75     str++;
76   }
77 #endif
78
79   return hash;
80 }
81
82 /**
83  * @brief Returns the hash code of a string.
84  */
85 static inline unsigned int xbt_str_hash(const char *str)
86 {
87 #ifdef XBT_DJB2_HASH_FUNCTION
88   /* fast implementation of djb2 algorithm */
89   int c;
90   unsigned int hash = 5381;
91
92   while ((c = *str++)) {
93     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
94   }
95
96 # elif defined(XBT_FNV_HASH_FUNCTION)
97   unsigned int hash = 0x811c9dc5;
98
99   while (*str) {
100     /* multiply by the 32 bit FNV magic prime mod 2^32 */
101     hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
102
103     /* xor the bottom with the current byte */
104     hash ^= (unsigned int) *str++;
105   }
106
107 # else
108   unsigned int hash = 0;
109
110   while (*str) {
111     hash += (*str) * (*str);
112     str++;
113   }
114 #endif
115   return hash;
116 }
117
118 /**@}*/
119 SG_END_DECL()
120 #endif                          /* XBT_STR_H */