Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use xbt_malloc (and friends) instead of direct malloc()
[simgrid.git] / src / xbt / xbt_str.c
index 920df6c..4c361b5 100644 (file)
@@ -6,9 +6,6 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#define DJB2_HASH_FUNCTION
-//#define FNV_HASH_FUNCTION
-
 #include "portable.h"
 #include "xbt/misc.h"
 #include "xbt/sysdep.h"
  * Strips the whitespaces from the end of s.
  * By default (when char_list=NULL), these characters get stripped:
  *
- *     - " "           (ASCII 32       (0x20)) space.
- *     - "\t"          (ASCII 9        (0x09)) tab.
- *     - "\n"          (ASCII 10       (0x0A)) line feed.
- *     - "\r"          (ASCII 13       (0x0D)) carriage return.
- *     - "\0"          (ASCII 0        (0x00)) NULL.
- *     - "\x0B"        (ASCII 11       (0x0B)) vertical tab.
+ *  - " "    (ASCII 32  (0x20))  space.
+ *  - "\t"    (ASCII 9  (0x09))  tab.
+ *  - "\n"    (ASCII 10  (0x0A))  line feed.
+ *  - "\r"    (ASCII 13  (0x0D))  carriage return.
+ *  - "\0"    (ASCII 0  (0x00))  NULL.
+ *  - "\x0B"  (ASCII 11  (0x0B))  vertical tab.
  *
  * @param s The string to strip. Modified in place.
  * @param char_list A string which contains the characters you want to strip.
@@ -65,12 +62,12 @@ void xbt_str_rtrim(char *s, const char *char_list)
  * Strips the whitespaces from the begining of s.
  * By default (when char_list=NULL), these characters get stripped:
  *
- *     - " "           (ASCII 32       (0x20)) space.
- *     - "\t"          (ASCII 9        (0x09)) tab.
- *     - "\n"          (ASCII 10       (0x0A)) line feed.
- *     - "\r"          (ASCII 13       (0x0D)) carriage return.
- *     - "\0"          (ASCII 0        (0x00)) NULL.
- *     - "\x0B"        (ASCII 11       (0x0B)) vertical tab.
+ *  - " "    (ASCII 32  (0x20))  space.
+ *  - "\t"    (ASCII 9  (0x09))  tab.
+ *  - "\n"    (ASCII 10  (0x0A))  line feed.
+ *  - "\r"    (ASCII 13  (0x0D))  carriage return.
+ *  - "\0"    (ASCII 0  (0x00))  NULL.
+ *  - "\x0B"  (ASCII 11  (0x0B))  vertical tab.
  *
  * @param s The string to strip. Modified in place.
  * @param char_list A string which contains the characters you want to strip.
@@ -106,12 +103,12 @@ void xbt_str_ltrim(char *s, const char *char_list)
  * Strips the whitespaces from both the beginning and the end of s.
  * By default (when char_list=NULL), these characters get stripped:
  *
- *     - " "           (ASCII 32       (0x20)) space.
- *     - "\t"          (ASCII 9        (0x09)) tab.
- *     - "\n"          (ASCII 10       (0x0A)) line feed.
- *     - "\r"          (ASCII 13       (0x0D)) carriage return.
- *     - "\0"          (ASCII 0        (0x00)) NULL.
- *     - "\x0B"        (ASCII 11       (0x0B)) vertical tab.
+ *  - " "    (ASCII 32  (0x20))  space.
+ *  - "\t"    (ASCII 9  (0x09))  tab.
+ *  - "\n"    (ASCII 10  (0x0A))  line feed.
+ *  - "\r"    (ASCII 13  (0x0D))  carriage return.
+ *  - "\0"    (ASCII 0  (0x00))  NULL.
+ *  - "\x0B"  (ASCII 11  (0x0B))  vertical tab.
  *
  * @param s The string to strip.
  * @param char_list A string which contains the characters you want to strip.
@@ -226,12 +223,12 @@ char *xbt_str_varsubst(const char *str, xbt_dict_t patterns)
  *
  * By default (with sep=NULL), these characters are used as separator:
  *
- *     - " "           (ASCII 32       (0x20)) space.
- *     - "\t"          (ASCII 9        (0x09)) tab.
- *     - "\n"          (ASCII 10       (0x0A)) line feed.
- *     - "\r"          (ASCII 13       (0x0D)) carriage return.
- *     - "\0"          (ASCII 0        (0x00)) NULL.
- *     - "\x0B"        (ASCII 11       (0x0B)) vertical tab.
+ *  - " "    (ASCII 32  (0x20))  space.
+ *  - "\t"    (ASCII 9  (0x09))  tab.
+ *  - "\n"    (ASCII 10  (0x0A))  line feed.
+ *  - "\r"    (ASCII 13  (0x0D))  carriage return.
+ *  - "\0"    (ASCII 0  (0x00))  NULL.
+ *  - "\x0B"  (ASCII 11  (0x0B))  vertical tab.
  */
 
 xbt_dynar_t xbt_str_split(const char *s, const char *sep)
@@ -308,14 +305,14 @@ xbt_dynar_t xbt_str_split_str(const char *s, const char *sep)
     //if substring was not found add the entire string
     if (NULL == q) {
       v = strlen(p);
-      to_push = malloc(v + 1);
+      to_push = xbt_malloc(v + 1);
       memcpy(to_push, p, v);
       to_push[v] = '\0';
       xbt_dynar_push(res, &to_push);
       done = 1;
     } else {
       //get the appearance
-      to_push = malloc(q - p + 1);
+      to_push = xbt_malloc(q - p + 1);
       memcpy(to_push, p, q - p);
       //add string terminator
       to_push[q - p] = '\0';
@@ -330,11 +327,14 @@ xbt_dynar_t xbt_str_split_str(const char *s, const char *sep)
  *
  * The string passed as argument must be writable (not const)
  * The elements of the dynar are just parts of the string passed as argument.
+ * So if you don't store that argument elsewhere, you should free it in addition
+ * to freeing the dynar. This can be done by simply freeing the first argument
+ * of the dynar:
+ *  free(xbt_dynar_get_ptr(dynar,0));
  *
- * To free the structure constructed by this function, free the first element and free the dynar:
- *
- * free(xbt_dynar_get_ptr(dynar,0));
- * xbt_dynar_free(&dynar);
+ * Actually this function puts a bunch of \0 in the memory area you passed as
+ * argument to separate the elements, and pushes the address of each chunk
+ * in the resulting dynar. Yes, that's uneven. Yes, that's gory. But that's efficient.
  */
 xbt_dynar_t xbt_str_split_quoted_in_place(char *s) {
   xbt_dynar_t res = xbt_dynar_new(sizeof(char *), NULL);
@@ -348,7 +348,7 @@ xbt_dynar_t xbt_str_split_quoted_in_place(char *s) {
 
   beg = s;
 
-  /* do not trim leading spaces: caller responsability to clean his cruft */
+  /* do not trim leading spaces: caller responsibility to clean his cruft */
   end = beg;
 
   while (!done) {
@@ -906,85 +906,26 @@ char *xbt_str_from_file(FILE * file)
   return res;
 }
 
-/**
- * @brief Returns the hash code of a string.
- */
-XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str,
-                                                 int str_len)
-{
-
-#ifdef DJB2_HASH_FUNCTION
-  /* fast implementation of djb2 algorithm */
-  int c;
-  register unsigned int hash = 5381;
-
-  while (str_len--) {
-    c = *str++;
-    hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
-  }
-# elif defined(FNV_HASH_FUNCTION)
-  register unsigned int hash = 0x811c9dc5;
-  unsigned char *bp = (unsigned char *) str;    /* start of buffer */
-  unsigned char *be = bp + str_len;     /* beyond end of buffer */
-
-  while (bp < be) {
-    /* multiply by the 32 bit FNV magic prime mod 2^32 */
-    hash +=
-        (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
-        (hash << 24);
-
-    /* xor the bottom with the current octet */
-    hash ^= (unsigned int) *bp++;
-  }
-
-# else
-  register unsigned int hash = 0;
-
-  while (str_len--) {
-    hash += (*str) * (*str);
-    str++;
-  }
-#endif
-
-  return hash;
-}
-
-/**
- * @brief Returns the hash code of a string.
+/* @brief Retrun 1 if string 'str' starts with string 'start'
+ *
+ * \param str a string
+ * \param start the string to search in str
+ *
+ * \return 1 if 'str' starts with 'start'
  */
-XBT_INLINE unsigned int xbt_dict_hash(const char *str)
+int xbt_str_start_with(const char* str, const char* start)
 {
-#ifdef DJB2_HASH_FUNCTION
-  /* fast implementation of djb2 algorithm */
-  int c;
-  register unsigned int hash = 5381;
-
-  while ((c = *str++)) {
-    hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
-  }
-
-# elif defined(FNV_HASH_FUNCTION)
-  register unsigned int hash = 0x811c9dc5;
+  int i;
+  size_t l_str = strlen(str);
+  size_t l_start = strlen(start);
 
-  while (*str) {
-    /* multiply by the 32 bit FNV magic prime mod 2^32 */
-    hash +=
-        (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
-        (hash << 24);
+  if(l_start > l_str) return 0;
 
-    /* xor the bottom with the current byte */
-    hash ^= (unsigned int) *str++;
+  for(i = 0; i< l_start; i++){
+    if(str[i] != start[i]) return 0;
   }
 
-# else
-  register unsigned int hash = 0;
-
-  while (*str) {
-    hash += (*str) * (*str);
-    str++;
-  }
-#endif
-  return hash;
+  return 1;
 }
 
 #ifdef SIMGRID_TEST