1 /*-------------------------------------------------------------------------
2 * drawElements Base Portability Library
3 * -------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Basic string operations.
22 *//*--------------------------------------------------------------------*/
23
24 #include "deString.h"
25
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include <stdio.h>
30 #include <stdarg.h>
31
32 DE_BEGIN_EXTERN_C
33
34 /*--------------------------------------------------------------------*//*!
35 * \brief Compute hash from string.
36 * \param str String to compute hash value for.
37 * \return Computed hash value.
38 *//*--------------------------------------------------------------------*/
deStringHash(const char * str)39 uint32_t deStringHash(const char *str)
40 {
41 /* \note [pyry] This hash is used in DT_GNU_HASH and is proven
42 to be robust for symbol hashing. */
43 /* \see http://sources.redhat.com/ml/binutils/2006-06/msg00418.html */
44 uint32_t hash = 5381;
45 unsigned int c;
46
47 DE_ASSERT(str);
48 while ((c = (unsigned int)*str++) != 0)
49 hash = (hash << 5) + hash + c;
50
51 return hash;
52 }
53
deStringHashLeading(const char * str,int numLeadingChars)54 uint32_t deStringHashLeading(const char *str, int numLeadingChars)
55 {
56 uint32_t hash = 5381;
57 unsigned int c;
58
59 DE_ASSERT(str);
60 while (numLeadingChars-- && (c = (unsigned int)*str++) != 0)
61 hash = (hash << 5) + hash + c;
62
63 return hash;
64 }
65
deMemoryHash(const void * ptr,size_t numBytes)66 uint32_t deMemoryHash(const void *ptr, size_t numBytes)
67 {
68 /* \todo [2010-05-10 pyry] Better generic hash function? */
69 const uint8_t *input = (const uint8_t *)ptr;
70 uint32_t hash = 5381;
71
72 DE_ASSERT(ptr);
73 while (numBytes--)
74 hash = (hash << 5) + hash + *input++;
75
76 return hash;
77 }
78
deStringBeginsWith(const char * str,const char * lead)79 bool deStringBeginsWith(const char *str, const char *lead)
80 {
81 const char *a = str;
82 const char *b = lead;
83
84 while (*b)
85 {
86 if (*a++ != *b++)
87 return false;
88 }
89
90 return true;
91 }
92
93 DE_END_EXTERN_C
94