1 /* str.h - XBT string related functions. */
3 /* Copyright (c) 2007-2013. 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. */
12 #include <stdarg.h> /* va_* */
14 #include "xbt/dynar.h"
16 #include "simgrid_config.h" /* FILE for getline */
20 /** @addtogroup XBT_str
21 * @brief String manipulation functions
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.
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);
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(void) xbt_str_strip_spaces(char *);
53 XBT_PUBLIC(char *) xbt_str_diff(const char *a, const char *b);
55 XBT_PUBLIC(char *) xbt_str_from_file(FILE * file);
57 XBT_PUBLIC(int) xbt_str_start_with(const char* str, const char* start);
59 #define DJB2_HASH_FUNCTION
60 //#define FNV_HASH_FUNCTION
63 * @brief Returns the hash code of a string.
65 static XBT_INLINE unsigned int xbt_str_hash_ext(const char *str, int str_len)
68 #ifdef DJB2_HASH_FUNCTION
69 /* fast implementation of djb2 algorithm */
71 register unsigned int hash = 5381;
75 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
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 */
83 /* multiply by the 32 bit FNV magic prime mod 2^32 */
85 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
88 /* xor the bottom with the current octet */
89 hash ^= (unsigned int) *bp++;
93 register unsigned int hash = 0;
96 hash += (*str) * (*str);
105 * @brief Returns the hash code of a string.
107 static XBT_INLINE unsigned int xbt_str_hash(const char *str)
109 #ifdef DJB2_HASH_FUNCTION
110 /* fast implementation of djb2 algorithm */
112 register unsigned int hash = 5381;
114 while ((c = *str++)) {
115 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
118 # elif defined(FNV_HASH_FUNCTION)
119 register unsigned int hash = 0x811c9dc5;
122 /* multiply by the 32 bit FNV magic prime mod 2^32 */
124 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
127 /* xor the bottom with the current byte */
128 hash ^= (unsigned int) *str++;
132 register unsigned int hash = 0;
135 hash += (*str) * (*str);
145 #endif /* XBT_STR_H */