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 deUint32 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 deUint32 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 deUint32 deStringHashLeading (const char* str, int numLeadingChars)
55 {
56 deUint32 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 deUint32 deMemoryHash (const void* ptr, size_t numBytes)
67 {
68 /* \todo [2010-05-10 pyry] Better generic hash function? */
69 const deUint8* input = (const deUint8*)ptr;
70 deUint32 hash = 5381;
71
72 DE_ASSERT(ptr);
73 while (numBytes--)
74 hash = (hash << 5) + hash + *input++;
75
76 return hash;
77 }
78
deMemoryEqual(const void * ptr,const void * cmp,size_t numBytes)79 deBool deMemoryEqual (const void* ptr, const void* cmp, size_t numBytes)
80 {
81 return memcmp(ptr, cmp, numBytes) == 0;
82 }
83
84 /*--------------------------------------------------------------------*//*!
85 * \brief Compare two strings for equality.
86 * \param a First string.
87 * \param b Second string.
88 * \return True if strings equal, false otherwise.
89 *//*--------------------------------------------------------------------*/
deStringEqual(const char * a,const char * b)90 deBool deStringEqual (const char* a, const char* b)
91 {
92 DE_ASSERT(a && b);
93 return (strcmp(a, b) == 0);
94 }
95
deStringBeginsWith(const char * str,const char * lead)96 deBool deStringBeginsWith (const char* str, const char* lead)
97 {
98 const char* a = str;
99 const char* b = lead;
100
101 while (*b)
102 {
103 if (*a++ != *b++)
104 return DE_FALSE;
105 }
106
107 return DE_TRUE;
108 }
109
deVsprintf(char * string,size_t size,const char * format,va_list list)110 int deVsprintf (char* string, size_t size, const char* format, va_list list)
111 {
112 int res;
113
114 DE_ASSERT(string && format);
115
116 #if (DE_COMPILER == DE_COMPILER_MSC)
117 # if (DE_OS == DE_OS_WINCE)
118 res = _vsnprintf(string, size, format, list);
119 # else
120 res = vsnprintf_s(string, size, _TRUNCATE, format, list);
121 # endif
122 #else
123 res = vsnprintf(string, size, format, list);
124 #endif
125
126 return res;
127 }
128
129 /*--------------------------------------------------------------------*//*!
130 * \brief Safe string print
131 * \note This has the new safe signature, i.e., string length is a
132 * required parameter.
133 *//*--------------------------------------------------------------------*/
deSprintf(char * string,size_t size,const char * format,...)134 int deSprintf (char* string, size_t size, const char* format, ...)
135 {
136 va_list list;
137 int res;
138
139 DE_ASSERT(string && format);
140
141 va_start(list, format);
142
143 res = deVsprintf(string, size, format, list);
144
145 va_end(list);
146
147 return res;
148 }
149
150 /*--------------------------------------------------------------------*//*!
151 * \note This has the new safe signature, i.e., string length is a
152 * required parameter.
153 *//*--------------------------------------------------------------------*/
deStrcpy(char * dst,size_t size,const char * src)154 char* deStrcpy (char* dst, size_t size, const char* src)
155 {
156 #if (DE_COMPILER == DE_COMPILER_MSC) && (DE_OS != DE_OS_WINCE)
157 (void)strcpy_s(dst, size, src);
158 return dst;
159 #else
160 return strncpy(dst, src, size);
161 #endif
162 }
163
164 /*--------------------------------------------------------------------*//*!
165 * \note This has the new safe signature, i.e., string length is a
166 * required parameter.
167 *//*--------------------------------------------------------------------*/
deStrcat(char * s1,size_t size,const char * s2)168 char* deStrcat (char* s1, size_t size, const char* s2)
169 {
170 #if (DE_COMPILER == DE_COMPILER_MSC) && (DE_OS != DE_OS_WINCE)
171 (void)strcat_s(s1, size, s2);
172 return s1;
173 #else
174 return strncat(s1, s2, size);
175 #endif
176 }
177
deStrnlen(const char * string,size_t maxSize)178 size_t deStrnlen (const char* string, size_t maxSize)
179 {
180 #if ((DE_COMPILER == DE_COMPILER_MSC) && (DE_OS != DE_OS_WINCE))
181 return strnlen_s(string, maxSize);
182 #else
183 size_t len = 0;
184 while (len < maxSize && string[len] != 0)
185 ++len;
186 return len;
187 #endif
188 }
189
190 DE_END_EXTERN_C
191