Merge origin/release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / gmxlib / string2.c
index 36e177b14dd76ba164809b2cdb9b7018526d72f5..36caebca45abca73089bf8c4bdaf55b99e02933e 100644 (file)
@@ -231,42 +231,53 @@ void nice_header (FILE *out,const char *fn)
   fprintf (out,"%c\n",COMMENTSIGN);
 }
 
+
 int gmx_strcasecmp_min(const char *str1, const char *str2)
 {
-  char ch1,ch2;
-  
-  do
+    char ch1,ch2;
+
+    do
     {
-      do
-       ch1=toupper(*(str1++));
-      while ((ch1=='-') || (ch1=='_'));
-      do 
-       ch2=toupper(*(str2++));
-      while ((ch2=='-') || (ch2=='_'));
-      if (ch1!=ch2) return (ch1-ch2);
+        do
+        {
+            ch1=toupper(*(str1++));
+        }
+        while ((ch1=='-') || (ch1=='_'));
+        do
+        {
+            ch2=toupper(*(str2++));
+        }
+        while ((ch2=='-') || (ch2=='_'));
+
+        if (ch1!=ch2) return (ch1-ch2);
     }
-  while (ch1);
-  return 0; 
+    while (ch1);
+    return 0;
 }
 
 int gmx_strncasecmp_min(const char *str1, const char *str2, int n)
 {
-  char ch1,ch2;
-  char *stri1, *stri2;
+    char ch1,ch2;
+    char *stri1, *stri2;
 
-  stri1=(char *)str1;
-  stri2=(char *)str2;  
-  do
+    stri1=(char *)str1;
+    stri2=(char *)str2;
+    do
     {
-      do
-       ch1=toupper(*(str1++));
-      while ((ch1=='-') || (ch1=='_'));
-      do 
-       ch2=toupper(*(str2++));
-      while ((ch2=='-') || (ch2=='_'));
-      if (ch1!=ch2) return (ch1-ch2);
+        do
+        {
+            ch1=toupper(*(str1++));
+        }
+        while ((ch1=='-') || (ch1=='_'));
+        do
+        {
+            ch2=toupper(*(str2++));
+        }
+        while ((ch2=='-') || (ch2=='_'));
+
+        if (ch1!=ch2) return (ch1-ch2);
     }
-  while (ch1 && (str1-stri1<n) && (str2-stri2<n));
+    while (ch1 && (str1-stri1<n) && (str2-stri2<n));
   return 0; 
 }
 
@@ -329,6 +340,25 @@ gmx_strndup(const char *src, int n)
     return dest;
 }
 
+/* Magic hash init number for Dan J. Bernsteins algorithm.
+ * Do NOT use any other value unless you really know what you are doing.
+ */
+const unsigned int
+gmx_string_hash_init = 5381;
+
+
+unsigned int
+gmx_string_hash_func(const char *s, unsigned int hash_init)
+{
+    int c;
+
+    while ((c = toupper(*s++)) != '\0')
+    {
+        if(isalnum(c)) hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
+    }
+    return hash_init;
+}
+
 int
 gmx_wcmatch(const char *pattern, const char *str)
 {