1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
16 #ifndef SQUIRRELJME_UTIL_H
17 #define SQUIRRELJME_UTIL_H
19 #include "sjme/error.h"
20 #include "sjme/stdTypes.h"
24 #ifndef SJME_CXX_IS_EXTERNED
25 #define SJME_CXX_IS_EXTERNED
26 #define SJME_CXX_SQUIRRELJME_UTIL_H
28 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
29 #endif /* #ifdef __cplusplus */
31 /*--------------------------------------------------------------------------*/
34 * Function for returning the number of entries within a tree.
36 * @param in The tree to get the size of.
37 * @return The number of items in the tree.
40 typedef sjme_jint (*sjme_tree_findCount
)(sjme_pointer tree
);
43 * Function for returning the hash of the search item.
45 * @param what What to get the hash of.
46 * @return The hash of the given search item.
49 typedef sjme_jint (*sjme_tree_findHash
)(void* what
);
52 * Compares an entry in the tree at the given index with the given hash and
55 * @param tree The tree to search in.
56 * @param what What to being searched for in the tree.
57 * @param hash The hash generated from @c sjme_tree_findHash .
58 * @param withIndex Compare @c hash and @c what against the given tree.
59 * @return A negative value if lower, zero if equal, or a positive value if
63 typedef sjme_jint (*sjme_tree_findCompare
)(void* tree
, void* what
,
64 sjme_jint hash
, sjme_jint withIndex
);
67 * Random number state.
71 typedef struct sjme_random
73 /** The current seed value. */
78 * Tree finding functions, used with @c sjme_tree_find to determine how to
79 * search through a given tree.
83 typedef struct sjme_tree_findFunc
85 /** Count function. */
86 sjme_tree_findCount count
;
89 sjme_tree_findHash hash
;
91 /** Compare function. */
92 sjme_tree_findCompare compare
;
96 * Compares two null values, nulls are placed before non-nulls.
98 * @param a The first value.
99 * @param b The second value.
100 * @return The resultant comparison.
103 sjme_jint
sjme_compare_null(
104 sjme_attrInNullable sjme_cpointer a
,
105 sjme_attrInNullable sjme_cpointer b
);
108 * Initializes the random number generator.
110 * @param outRandom The random state to initialize.
111 * @param seedHi The high seed value.
112 * @param seedLo The low seed value.
113 * @return Returns @c SJME_JNI_TRUE on success.
116 sjme_errorCode
sjme_randomInit(
117 sjme_attrInOutNotNull sjme_random
* outRandom
,
118 sjme_attrInValue sjme_jint seedHi
,
119 sjme_attrInValue sjme_jint seedLo
);
122 * Initializes the random number generator.
124 * @param outRandom The random state to initialize.
125 * @param seed The seed value.
126 * @return Returns @c SJME_JNI_TRUE on success.
129 sjme_errorCode
sjme_randomInitL(
130 sjme_attrInOutNotNull sjme_random
* outRandom
,
131 sjme_attrInValue sjme_jlong seed
);
134 * Returns the next random value.
136 * @param random The random state.
137 * @param outValue The output value.
138 * @return Returns @c SJME_JNI_TRUE on success.
141 sjme_errorCode
sjme_randomNextInt(
142 sjme_attrInOutNotNull sjme_random
* random
,
143 sjme_attrOutNotNull sjme_jint
* outValue
);
146 * Returns the next random value within the given range.
148 * @param random The random state.
149 * @param outValue The output value.
150 * @param maxValue The maximum exclusive value.
151 * @return Returns @c SJME_JNI_TRUE on success.
154 sjme_errorCode
sjme_randomNextIntMax(
155 sjme_attrInOutNotNull sjme_random
* random
,
156 sjme_attrOutNotNull sjme_jint
* outValue
,
157 sjme_attrInPositiveNonZero sjme_jint maxValue
);
160 * Returns the character at the given index.
162 * @param string The string to get from.
163 * @param index The index within the string.
164 * @return The given character or @c -1 if not valid.
167 sjme_jint
sjme_string_charAt(sjme_lpcstr string
, sjme_jint index
);
170 * Compares two strings up to the given number of characters each, nulls are
171 * in the same order as @c sjme_compare_null() .
173 * @param aString A string.
174 * @param aLen A length.
175 * @param bString B string.
176 * @param bLen B length.
177 * @return The comparison between the two.
180 sjme_jint
sjme_string_compareN(sjme_lpcstr aString
, sjme_jint aLen
,
181 sjme_lpcstr bString
, sjme_jint bLen
);
184 * Decodes the given UTF-8 character.
186 * @param at The character sequence to decode.
187 * @param stringP Adjustable pointer to the string, when the character is
188 * decoded then this will increment accordingly.
189 * @return The decoded character or @c -1 if
193 sjme_jint
sjme_string_decodeChar(sjme_lpcstr at
, sjme_lpcstr
* stringP
);
196 * Hashes the given string in accordance to @c String::hashCode() .
198 * @param string The string to hash.
199 * @return The hashcode of the given string.
202 sjme_jint
sjme_string_hash(sjme_lpcstr string
);
205 * Hashes the given string in accordance to @c String::hashCode() .
207 * @param string The string to hash.
208 * @param limit The string limit.
209 * @return The hashcode of the given string.
212 sjme_jint
sjme_string_hashN(sjme_lpcstr string
, sjme_jint limit
);
215 * Returns the length of the string in accordance to @c String::length() .
217 * @param string The string to get the length of.
218 * @return The string length or @c -1 if it is not valid.
221 sjme_jint
sjme_string_length(sjme_lpcstr string
);
224 * Returns the length of the string in accordance to @c String::length() .
226 * @param string The string to get the length of.
227 * @param limit The length limit of the C string.
228 * @return The string length or @c -1 if it is not valid.
231 sjme_jint
sjme_string_lengthN(sjme_lpcstr string
, sjme_jint limit
);
234 * Swaps an unsigned integer value.
236 * @param in The input value.
237 * @return The swapped value.
240 static sjme_inline sjme_attrArtificial sjme_juint
sjme_swap_uint(
243 // 0xAABBCCDD -> 0xBBAADDCC
244 in
= (((in
& 0xFF00FF00) >> 8) | ((in
& 0x00FF00FF) << 8));
246 // 0xBBAADDCC -> 0xDDCCBBAA
247 return (in
>> 16) | (in
<< 16);
251 * Swaps an integer value.
253 * @param in The input value.
254 * @return The swapped value.
257 static sjme_inline sjme_attrArtificial sjme_jint
sjme_swap_int(
260 return (sjme_jint
)sjme_swap_uint((sjme_juint
)in
);
264 * Swaps a long value.
266 * @param in The input value.
267 * @return The swapped value.
270 static sjme_inline sjme_attrArtificial sjme_jlong
sjme_swap_long(
275 /* Swap high and low first. */
277 in
.part
.hi
= (sjme_jint
)in
.part
.lo
;
280 /* Then finish swap each side. */
281 in
.part
.hi
= sjme_swap_int(in
.part
.hi
);
282 in
.part
.lo
= sjme_swap_uint(in
.part
.lo
);
284 /* Return the result. */
289 * Swaps an unsigned short value.
291 * @param in The input value.
292 * @return The swapped value.
295 static sjme_inline sjme_attrArtificial sjme_jchar
sjme_swap_ushort(
298 return ((in
>> 8) | (in
<< 8));
302 * Swaps a short value.
304 * @param in The input value.
305 * @return The swapped value.
308 static sjme_inline sjme_attrArtificial sjme_jshort
sjme_swap_short(
311 return (sjme_jchar
)sjme_swap_ushort((sjme_jchar
)in
);
315 * Performs @c memmove() followed by shifting up by 8 the destination buffer,
316 * then following a byte swap.
318 * @param dest The destination.
319 * @param src The source.
320 * @param n The number of bytes to copy.
321 * @return Any resultant error, if any.
324 sjme_errorCode
sjme_swap_shu8_uint_memmove(
325 sjme_attrInNotNull
void* dest
,
326 sjme_attrInNotNull
void* src
,
327 sjme_attrInPositiveNonZero sjme_jint n
);
330 * Performs @c memmove() followed by swapping the destination buffer.
332 * @param dest The destination.
333 * @param src The source.
334 * @param n The number of bytes to copy.
335 * @return Any resultant error, if any.
338 sjme_errorCode
sjme_swap_uint_memmove(
339 sjme_attrInNotNull
void* dest
,
340 sjme_attrInNotNull
void* src
,
341 sjme_attrInPositiveNonZero sjme_jint n
);
344 * Locates an item within a tree.
346 * @param tree The tree to search in.
347 * @param what What is being searched for.
348 * @param functions Functions used for the tree search logic.
349 * @return The index where the item was found.
352 sjme_jint
sjme_tree_find(void* tree
, void* what
,
353 const sjme_tree_findFunc
* functions
);
356 * Returns the number of bits in the value.
358 * @param v The value to get the number of bits in.
359 * @return The number of bits in the value.
362 sjme_juint
sjme_util_intBitCountU(
363 sjme_attrInValue sjme_juint v
);
366 * Returns the value with the highest bit set.
368 * @param v The value to return the highest bit of.
369 * @return The highest bit of the value.
372 sjme_juint
sjme_util_intHighestOneBit(
373 sjme_attrInValue sjme_juint v
);
376 * Returns the number of leading zeroes in the value.
378 * @param v The value to check.
379 * @return The resultant number of leading zeroes.
382 sjme_juint
sjme_util_intLeadingZeroesU(
383 sjme_attrInValue sjme_juint v
);
386 * Allows for shifting left/right by 32 for certain CPUs.
388 * @param v The value to shift.
389 * @param sh The shift amount, positive is left shift and negative is right
391 * @return The resultant shifted value.
394 sjme_juint
sjme_util_intOverShiftU(
395 sjme_attrInValue sjme_juint v
,
396 sjme_attrInRange(-32, 32) sjme_jint sh
);
399 * Reverses the bits in the given integer value.
401 * @param v The input value.
402 * @return The value with the reversed bits.
405 sjme_jint
sjme_util_intReverse(
406 sjme_attrInValue sjme_jint v
);
409 * Reverses the bits in the given integer value.
411 * @param v The input value.
412 * @return The value with the reversed bits.
415 sjme_juint
sjme_util_intReverseU(
416 sjme_attrInValue sjme_juint v
);
419 * Converts integer value to binary.
421 * @param destBuf The destination buffer.
422 * @param destLen The destination length.
423 * @param inVal The value to convert to binary.
424 * @param bitCount The number of bits to output, if zero then this is all bits.
425 * @return Any resultant error, if any.
428 sjme_errorCode
sjme_util_intToBinary(
429 sjme_attrInNotNullBuf(destLen
) sjme_lpstr destBuf
,
430 sjme_attrInPositiveNonZero sjme_jint destLen
,
431 sjme_attrInValue sjme_juint inVal
,
432 sjme_attrInPositiveNonZero sjme_juint bitCount
);
435 * Trims ending whitespace from the end of the string.
437 * @param buf The buffer to trim from.
438 * @param length The length of the input buffer.
439 * @return On any resultant error, if any.
442 sjme_errorCode
sjme_util_lpstrTrimEnd(
443 sjme_attrInNotNullBuf(length
) sjme_lpstr buf
,
444 sjme_attrInPositiveNonZero sjme_jint length
);
446 /*--------------------------------------------------------------------------*/
450 #ifdef SJME_CXX_SQUIRRELJME_UTIL_H
452 #undef SJME_CXX_SQUIRRELJME_UTIL_H
453 #undef SJME_CXX_IS_EXTERNED
454 #endif /* #ifdef SJME_CXX_SQUIRRELJME_UTIL_H */
455 #endif /* #ifdef __cplusplus */
457 #endif /* SQUIRRELJME_UTIL_H */