1 /* str.h - XBT string related functions. */
3 /* Copyright (c) 2007-2015. The SimGrid Team.
4 * All rights reserved. */
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. */
13 #include "xbt/dynar.h"
16 #include <stdarg.h> /* va_* */
17 #include <stdio.h> /* FILE */
20 #define strcasecmp _stricmp
25 /** @addtogroup XBT_str
26 * @brief String manipulation functions
28 * This module defines several string related functions. Looking at the diversity of string manipulation functions that
29 * are provided, you can see that several SimGrid core developers actually like Perl.
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);
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);
42 XBT_PUBLIC(xbt_dynar_t) xbt_str_split_str(const char *s, const char *sep);
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);
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);
52 XBT_PUBLIC(char *) xbt_str_from_file(FILE * file);
54 XBT_PUBLIC(long int) xbt_str_parse_int(const char* str, const char* error_msg);
55 XBT_PUBLIC(double) xbt_str_parse_double(const char* str, const char* error_msg);
57 #define DJB2_HASH_FUNCTION
58 //#define FNV_HASH_FUNCTION
61 * @brief Returns the hash code of a string.
63 static inline unsigned int xbt_str_hash_ext(const char *str, int str_len)
66 #ifdef DJB2_HASH_FUNCTION
67 /* fast implementation of djb2 algorithm */
69 unsigned int hash = 5381;
73 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
75 # elif defined(FNV_HASH_FUNCTION)
76 unsigned int hash = 0x811c9dc5;
77 unsigned char *bp = (unsigned char *) str; /* start of buffer */
78 unsigned char *be = bp + str_len; /* beyond end of buffer */
81 /* multiply by the 32 bit FNV magic prime mod 2^32 */
83 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
86 /* xor the bottom with the current octet */
87 hash ^= (unsigned int) *bp++;
91 unsigned int hash = 0;
94 hash += (*str) * (*str);
103 * @brief Returns the hash code of a string.
105 static inline unsigned int xbt_str_hash(const char *str)
107 #ifdef DJB2_HASH_FUNCTION
108 /* fast implementation of djb2 algorithm */
110 unsigned int hash = 5381;
112 while ((c = *str++)) {
113 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
116 # elif defined(FNV_HASH_FUNCTION)
117 unsigned int hash = 0x811c9dc5;
120 /* multiply by the 32 bit FNV magic prime mod 2^32 */
122 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
125 /* xor the bottom with the current byte */
126 hash ^= (unsigned int) *str++;
130 unsigned int hash = 0;
133 hash += (*str) * (*str);
143 #endif /* XBT_STR_H */