1 /*-------------------------------------------------------------------------
2 * drawElements Base Portability Library
3 * -------------------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Basic string operations.
22 *//*--------------------------------------------------------------------*/
32 #if (DE_COMPILER == DE_COMPILER_MSC)
38 /*--------------------------------------------------------------------*//*!
39 * \brief Compute hash from string.
40 * \param str String to compute hash value for.
41 * \return Computed hash value.
42 *//*--------------------------------------------------------------------*/
43 deUint32 deStringHash (const char* str)
45 /* \note [pyry] This hash is used in DT_GNU_HASH and is proven
46 to be robust for symbol hashing. */
47 /* \see http://sources.redhat.com/ml/binutils/2006-06/msg00418.html */
52 while ((c = (unsigned int)*str++) != 0)
53 hash = (hash << 5) + hash + c;
58 deUint32 deStringHashLeading (const char* str, int numLeadingChars)
64 while (numLeadingChars-- && (c = (unsigned int)*str++) != 0)
65 hash = (hash << 5) + hash + c;
70 deUint32 deMemoryHash (const void* ptr, size_t numBytes)
72 /* \todo [2010-05-10 pyry] Better generic hash function? */
73 const deUint8* input = (const deUint8*)ptr;
78 hash = (hash << 5) + hash + *input++;
83 deBool deMemoryEqual (const void* ptr, const void* cmp, size_t numBytes)
85 return memcmp(ptr, cmp, numBytes) == 0;
88 /*--------------------------------------------------------------------*//*!
89 * \brief Compare two strings for equality.
90 * \param a First string.
91 * \param b Second string.
92 * \return True if strings equal, false otherwise.
93 *//*--------------------------------------------------------------------*/
94 deBool deStringEqual (const char* a, const char* b)
97 return (strcmp(a, b) == 0);
100 deBool deStringBeginsWith (const char* str, const char* lead)
103 const char* b = lead;
114 int deVsprintf (char* string, size_t size, const char* format, va_list list)
118 DE_ASSERT(string && format);
120 #if (DE_COMPILER == DE_COMPILER_MSC)
121 # if (DE_OS == DE_OS_WINCE)
122 res = _vsnprintf(string, size, format, list);
124 res = vsnprintf_s(string, size, _TRUNCATE, format, list);
127 res = vsnprintf(string, size, format, list);
133 /*--------------------------------------------------------------------*//*!
134 * \brief Safe string print
135 * \note This has the new safe signature, i.e., string length is a
136 * required parameter.
137 *//*--------------------------------------------------------------------*/
138 int deSprintf (char* string, size_t size, const char* format, ...)
143 DE_ASSERT(string && format);
145 va_start(list, format);
147 res = deVsprintf(string, size, format, list);
154 /*--------------------------------------------------------------------*//*!
155 * \note This has the new safe signature, i.e., string length is a
156 * required parameter.
157 *//*--------------------------------------------------------------------*/
158 char* deStrcpy (char* dst, size_t size, const char* src)
160 #if (DE_COMPILER == DE_COMPILER_MSC) && (DE_OS != DE_OS_WINCE)
161 (void)strcpy_s(dst, size, src);
164 return strncpy(dst, src, size);
168 /*--------------------------------------------------------------------*//*!
169 * \note This has the new safe signature, i.e., string length is a
170 * required parameter.
171 *//*--------------------------------------------------------------------*/
172 char* deStrcat (char* s1, size_t size, const char* s2)
174 #if (DE_COMPILER == DE_COMPILER_MSC) && (DE_OS != DE_OS_WINCE)
175 (void)strcat_s(s1, size, s2);
178 return strncat(s1, s2, size);
182 size_t deStrnlen (const char* string, size_t maxSize)
184 #if ((DE_COMPILER == DE_COMPILER_MSC) && (DE_OS != DE_OS_WINCE)) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L))
185 return strnlen_s(string, maxSize);
188 while (len < maxSize && string[len] != 0)