debug.c needs dylib.h.
[SquirrelJME.git] / nanocoat / include / sjme / util.h
blobc921003c0b20ba21148facdc9bd40cac276db69d
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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 // -------------------------------------------------------------------------*/
10 /**
11 * Utilities.
13 * @since 2023/07/26
16 #ifndef SQUIRRELJME_UTIL_H
17 #define SQUIRRELJME_UTIL_H
19 #include "sjme/error.h"
20 #include "sjme/stdTypes.h"
22 /* Anti-C++. */
23 #ifdef __cplusplus
24 #ifndef SJME_CXX_IS_EXTERNED
25 #define SJME_CXX_IS_EXTERNED
26 #define SJME_CXX_SQUIRRELJME_UTIL_H
27 extern "C" {
28 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
29 #endif /* #ifdef __cplusplus */
31 /*--------------------------------------------------------------------------*/
33 /**
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.
38 * @since 2023/07/26
40 typedef sjme_jint (*sjme_tree_findCount)(sjme_pointer tree);
42 /**
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.
47 * @since 2023/07/26
49 typedef sjme_jint (*sjme_tree_findHash)(void* what);
51 /**
52 * Compares an entry in the tree at the given index with the given hash and
53 * item.
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
60 * greater.
61 * @since 2023/07/26
63 typedef sjme_jint (*sjme_tree_findCompare)(void* tree, void* what,
64 sjme_jint hash, sjme_jint withIndex);
66 /**
67 * Random number state.
69 * @since 2023/12/02
71 typedef struct sjme_random
73 /** The current seed value. */
74 sjme_jlong seed;
75 } sjme_random;
77 /**
78 * Tree finding functions, used with @c sjme_tree_find to determine how to
79 * search through a given tree.
81 * @since 2023/07/26
83 typedef struct sjme_tree_findFunc
85 /** Count function. */
86 sjme_tree_findCount count;
88 /** Hash function. */
89 sjme_tree_findHash hash;
91 /** Compare function. */
92 sjme_tree_findCompare compare;
93 } sjme_tree_findFunc;
95 /**
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.
101 * @since 2024/02/14
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.
114 * @since 2023/12/02
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.
127 * @since 2023/12/02
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.
139 * @since 2023/12/02
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.
152 * @since 2023/12/02
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.
165 * @since 2023/12/16
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.
178 * @since 2024/02/22
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
190 * it is not valid.
191 * @since 2023/07/27
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.
200 * @since 2023/07/26
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.
210 * @since 2024/02/20
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.
219 * @since 2023/07/29
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.
229 * @since 2024/02/20
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.
238 * @since 2024/01/05
240 static sjme_inline sjme_attrArtificial sjme_juint sjme_swap_uint(
241 sjme_juint in)
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.
255 * @since 2024/01/05
257 static sjme_inline sjme_attrArtificial sjme_jint sjme_swap_int(
258 sjme_jint in)
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.
268 * @since 2024/01/05
270 static sjme_inline sjme_attrArtificial sjme_jlong sjme_swap_long(
271 sjme_jlong in)
273 sjme_juint temp;
275 /* Swap high and low first. */
276 temp = in.part.hi;
277 in.part.hi = (sjme_jint)in.part.lo;
278 in.part.lo = temp;
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. */
285 return in;
289 * Swaps an unsigned short value.
291 * @param in The input value.
292 * @return The swapped value.
293 * @since 2024/01/05
295 static sjme_inline sjme_attrArtificial sjme_jchar sjme_swap_ushort(
296 sjme_jchar in)
298 return ((in >> 8) | (in << 8));
302 * Swaps a short value.
304 * @param in The input value.
305 * @return The swapped value.
306 * @since 2024/01/05
308 static sjme_inline sjme_attrArtificial sjme_jshort sjme_swap_short(
309 sjme_jshort in)
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.
322 * @since 2024/07/10
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.
336 * @since 2024/07/10
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.
350 * @since 2023/07/26
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.
360 * @since 2024/08/22
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.
370 * @since 2024/08/22
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.
380 * @since 2024/08/22
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
390 * shift.
391 * @return The resultant shifted value.
392 * @since 2024/08/29
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.
403 * @since 2024/08/18
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.
413 * @since 2024/08/18
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.
426 * @since 2024/08/24
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.
440 * @since 2024/09/06
442 sjme_errorCode sjme_util_lpstrTrimEnd(
443 sjme_attrInNotNullBuf(length) sjme_lpstr buf,
444 sjme_attrInPositiveNonZero sjme_jint length);
446 /*--------------------------------------------------------------------------*/
448 /* Anti-C++. */
449 #ifdef __cplusplus
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 */