utils.h
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include utils.h:
Misc functions and macros.
Miscellaneous functions and macros useful for different tasks, like endian byteswap, unaligned reads, marking unused vars, etc…
Defines
-
READ_32_ALIGNED(ptr) ((((uint8_t *) (ptr))[0] << 24) | (((uint8_t *) (ptr))[1] << 16) | (((uint8_t *) (ptr))[2] << 8) | ((uint8_t *) (ptr))[3])
-
READ_64_UNALIGNED(ptr) ((((uint64_t) ((uint8_t *) (ptr))[0]) << 56) | (((uint64_t) ((uint8_t *) (ptr))[1]) << 48) | (((uint64_t) ((uint8_t *) (ptr))[2]) << 40) | (((uint64_t) ((uint8_t *) (ptr))[3]) << 32) | (((uint64_t) ((uint8_t *) (ptr))[4]) << 24) | (((uint64_t) ((uint8_t *) (ptr))[5]) << 16) | (((uint64_t) ((uint8_t *) (ptr))[6]) << 8) | (((uint64_t) ((uint8_t *) (ptr))[7])))
-
WRITE_64_UNALIGNED(ptr, val)
{ \
((uint8_t *) (ptr))[0] = (((uint64_t) val) >> 56) & 0xff; \
((uint8_t *) (ptr))[1] = (((uint64_t) val) >> 48) & 0xff; \
((uint8_t *) (ptr))[2] = (((uint64_t) val) >> 40) & 0xff; \
((uint8_t *) (ptr))[3] = (((uint64_t) val) >> 32) & 0xff; \
((uint8_t *) (ptr))[4] = (((uint64_t) val) >> 24) & 0xff; \
((uint8_t *) (ptr))[5] = (((uint64_t) val) >> 16) & 0xff; \
((uint8_t *) (ptr))[6] = (((uint64_t) val) >> 8) & 0xff; \
((uint8_t *) (ptr))[7] = ((uint64_t) val) & 0xff; \
}
-
READ_32_UNALIGNED(ptr) ((((uint8_t *) (ptr))[0] << 24) | (((uint8_t *) (ptr))[1] << 16) | (((uint8_t *) (ptr))[2] << 8) | ((uint8_t *) (ptr))[3])
-
WRITE_32_UNALIGNED(ptr, val)
{ \
((uint8_t *) (ptr))[0] = (((uint32_t) val) >> 24) & 0xff; \
((uint8_t *) (ptr))[1] = (((uint32_t) val) >> 16) & 0xff; \
((uint8_t *) (ptr))[2] = (((uint32_t) val) >> 8) & 0xff; \
((uint8_t *) (ptr))[3] = ((uint32_t) val) & 0xff; \
}
-
READ_16_UNALIGNED(ptr) ((((uint8_t *) (ptr))[0] << 8) | ((uint8_t *) (ptr))[1])
-
WRITE_16_UNALIGNED(ptr, val)
{ \
((uint8_t *) (ptr))[0] = (((uint16_t) val) >> 8) & 0xff; \
((uint8_t *) (ptr))[1] = ((uint16_t) val) & 0xff; \
}
-
ENDIAN_SWAP_32(value) ((((value) &0xFF) << 24) | (((value) &0xFF00) << 8) | (((value) &0xFF0000) >> 8) | (((value) &0xFF000000) >> 24))
-
ENDIAN_SWAP_16(value) ((((value) &0xFF) << 8) | (((value) &0xFF00) >> 8))
-
UNUSED(x) (void) (x);
-
IS_NULL_PTR(x) ((x) == NULL)
-
LIKELY(x) (x)
-
UNLIKELY(x) (x)
-
HOT_FUNC
-
COLD_FUNC
-
MALLOC_LIKE
-
MUST_CHECK
-
AVM_ABORT() abort()
-
CAST_FUNC_TO_VOID_PTR(f) ((void *) (f))
-
CAST_VOID_TO_FUNC_PTR(f) ((func_ptr_t) (f))
-
CONTAINER_OF(ptr, type, member) ((type *) (((char *) (ptr)) - offsetof(type, member)))
-
PRINTF_FORMAT_ARGS(...)
-
NO_DISCARD(...)
-
UNREACHABLE(...)
-
ASSUME(...)
-
MAXI(A, B) ((A > B) ? (A) : (B))
-
MINI(A, B) ((A > B) ? (B) : (A))
-
INTPTR_WRITE_TO_ASCII_BUF_LEN (32 + 1)
Required buffer size for
intptr_tto ASCII conversion.Defines the maximum buffer size needed to hold any
intptr_tvalue converted to ASCII in any base (2-36), including sign character. This constant ensures safe buffer allocation forintptr_write_to_ascii_buf().See also
Note
Value depends on platform pointer size (33 bytes for 32-bit, 65 bytes for 64-bit)
Warning
Always use this constant to allocate buffers for
intptr_write_to_ascii_buf()
-
INT32_WRITE_TO_ASCII_BUF_LEN (32 + 1)
Required buffer size for
int32_tto ASCII conversion.Defines the maximum buffer size needed to hold any
int32_tvalue converted to ASCII in any base (2-36), including sign character. This constant ensures safe buffer allocation forint32_write_to_ascii_buf().See also
Note
Always 33 bytes (32 digits for base 2 plus sign)
Warning
Always use this constant to allocate buffers for
int32_write_to_ascii_buf()
-
INT64_WRITE_TO_ASCII_BUF_LEN (64 + 1)
Required buffer size for
int64_tto ASCII conversion.Defines the maximum buffer size needed to hold any
int64_tvalue converted to ASCII in any base (2-36), including sign character. This constant ensures safe buffer allocation forint64_write_to_ascii_buf().See also
Note
Always 65 bytes (64 digits for base 2 plus sign)
Warning
Always use this constant to allocate buffers for
int64_write_to_ascii_buf()
Typedefs
-
typedef void (*func_ptr_t)(void)
Enums
-
enum buf_to_int64_options_t
Options for integer parsing behavior.
Controls how
int64_parse_ascii_buf()handles signs and other parsing options. Options can be combined using bitwise OR.Values:
-
enumerator BufToInt64NoOptions
Default parsing behavior - accepts signs (+/-)
-
enumerator BufToInt64RejectSign
Reject sign characters - parse unsigned magnitude only.
-
enumerator BufToInt64NoOptions
Functions
-
static inline size_t size_align_up_pow2(size_t n, size_t align)
Align size up to power-of-2 boundary.
Rounds up a size value to the next multiple of a power-of-2 alignment. This function uses bit manipulation for efficient alignment calculation and is faster than the general-purpose
size_align_up().size_t aligned = size_align_up_pow2(17, 8); // Returns 24 size_t aligned = size_align_up_pow2(16, 8); // Returns 16 (already aligned)
See also
size_align_up() for arbitrary alignment values
Note
Result is always >= n
Warning
Undefined behavior if align is not a power of 2
Warning
Undefined behavior if align is 0
- Parameters:
n – Size value to align
align – Power-of-2 alignment boundary
- Returns:
Size rounded up to next multiple of align
- Pre:
align must be a power of 2 (e.g., 2, 4, 8, 16, 32, …)
-
static inline size_t size_align_up(size_t n, size_t align)
Align size up to arbitrary boundary.
Rounds up a size value to the next multiple of an alignment boundary. Works with any alignment value, not just powers of 2.
size_t aligned = size_align_up(17, 10); // Returns 20 size_t aligned = size_align_up(20, 10); // Returns 20 (already aligned) size_t aligned = size_align_up(17, 0); // Returns 17 (no alignment)
See also
size_align_up_pow2() for optimized power-of-2 alignment
See also
size_align_down() for rounding down instead of up
Note
Returns n unchanged if align is 0 (no alignment)
Note
Result is always >= n
Note
For power-of-2 alignments,
size_align_up_pow2()is more efficient- Parameters:
n – Size value to align
align – Alignment boundary (any positive value, or 0)
- Returns:
Size rounded up to next multiple of align, or n if align is 0
-
static inline size_t size_align_down(size_t n, size_t align)
Align size down to arbitrary boundary.
Rounds down a size value to the previous multiple of an alignment boundary. Works with any alignment value, not just powers of 2.
size_t aligned = size_align_down(17, 10); // Returns 10 size_t aligned = size_align_down(20, 10); // Returns 20 (already aligned) size_t aligned = size_align_down(7, 10); // Returns 0
See also
size_align_up() for rounding up instead of down
Note
Returns n unchanged if align is 0 (no alignment)
Note
Result is always <= n
Note
Commonly used for finding aligned base addresses within buffers
- Parameters:
n – Size value to align
align – Alignment boundary (any positive value, or 0)
- Returns:
Size rounded down to previous multiple of align, or n if align is 0
-
static inline int32_t int32_neg_unsigned(uint32_t u32)
Negate unsigned 32-bit value (
uint32_t) to signed integer (int32_t)Converts an unsigned 32-bit value to its negated signed representation. This function performs the negation operation while avoiding undefined behavior that would occur with direct cast and negation due to two’s complement asymmetry.
Note
Handles
INT32_MINcase correctly (u32 = 2147483648 returns -2147483648)Note
Useful for parsing negative integers from text where magnitude is parsed as unsigned to avoid overflow
Warning
Values greater than 2147483648 have undefined results
- Parameters:
u32 – Unsigned 32-bit value to negate
- Returns:
Negated value as signed 32-bit integer (
int32_t)
-
static inline int64_t int64_neg_unsigned(uint64_t u64)
Negate unsigned 64-bit value (
uint64_t) to signed integer (int64_t)Converts an unsigned 64-bit value to its negated signed representation. This function performs the negation operation while avoiding undefined behavior that would occur with direct cast and negation due to two’s complement asymmetry.
Note
Handles
INT64_MINcase correctly (u64 = 9223372036854775808 returnsINT64_MIN)Note
Useful for parsing negative integers from text where magnitude is parsed as unsigned to avoid overflow
Warning
Values greater than 9223372036854775808 have undefined results
- Parameters:
u64 – Unsigned 64-bit value to negate
- Returns:
Negated value as signed 64-bit integer (
int64_t)
-
static inline int32_t int32_cond_neg_unsigned(bool negative, uint32_t u32)
Conditionally negate unsigned 32-bit value (
uint32_t) to signed integer (int32_t)Converts an unsigned 32-bit value to signed, optionally negating based on a flag. This function safely handles the negation while avoiding undefined behavior from two’s complement asymmetry. Commonly used when parsing integers where the sign is determined separately from the magnitude.
See also
uint32_does_overflow_int32() to check for overflow before conversion
See also
int32_neg_unsigned() for unconditional negation
Warning
Caller must ensure the value fits in signed range using
uint32_does_overflow_int32()before calling this function- Parameters:
negative – If true, negate the value; if false, cast directly to signed
u32 – Unsigned 32-bit magnitude
- Returns:
Signed 32-bit integer (
int32_t), negated if negative flag is true
-
static inline int64_t int64_cond_neg_unsigned(bool negative, uint64_t u64)
Conditionally negate unsigned 64-bit value (
uint64_t) to signed integer (int64_t)Converts an unsigned 64-bit value to signed, optionally negating based on a flag. This function safely handles the negation while avoiding undefined behavior from two’s complement asymmetry. Commonly used when parsing integers where the sign is determined separately from the magnitude.
See also
uint64_does_overflow_int64() to check for overflow before conversion
See also
int64_neg_unsigned() for unconditional negation
Warning
Caller must ensure the value fits in signed range using
uint64_does_overflow_int64()before calling this function- Parameters:
negative – If true, negate the value; if false, cast directly to signed
u64 – Unsigned 64-bit magnitude
- Returns:
Signed 64-bit integer (
int64_t), negated if negative flag is true
-
static inline bool uint32_does_overflow_int32(uint32_t u32, bool is_negative)
Check if unsigned 32-bit value (
uint32_t) would overflow when converted to signed (int32_t)Tests whether an unsigned 32-bit value can be represented as a signed 32-bit integer, accounting for whether it will be negated. Negative values can represent one more magnitude (
INT32_MIN= -2147483648) than positive values (INT32_MAX= 2147483647).See also
int32_cond_neg_unsigned() to perform the conversion after checking
Note
Maximum representable positive: 2147483647 (
INT32_MAX)Note
Maximum representable negative magnitude: 2147483648 (|
INT32_MIN|)- Parameters:
u32 – Unsigned magnitude to check
is_negative – Whether the value will be negated
- Returns:
true if conversion would overflow, false if safe to convert
-
static inline bool uint64_does_overflow_int64(uint64_t u64, bool is_negative)
Check if unsigned 64-bit value (
uint64_t) would overflow when converted to signed (int64_t)Tests whether an unsigned 64-bit value can be represented as a signed 64-bit integer, accounting for whether it will be negated. Negative values can represent one more magnitude (
INT64_MIN) than positive values (INT64_MAX).See also
int64_cond_neg_unsigned() to perform the conversion after checking
Note
Maximum representable positive: 9223372036854775807 (
INT64_MAX)Note
Maximum representable negative magnitude: 9223372036854775808 (|
INT64_MIN|)- Parameters:
u64 – Unsigned magnitude to check
is_negative – Whether the value will be negated
- Returns:
true if conversion would overflow, false if safe to convert
-
static inline uint32_t int32_safe_unsigned_abs(int32_t i32)
Compute absolute value of signed 32-bit integer (
int32_t) as unsigned (uint32_t)Returns the absolute value of a signed 32-bit integer (
int32_t) as an unsigned 32-bit value (uint32_t). This function avoids undefined behavior that would occur when negatingINT32_MINin signed arithmetic.Note
Handles
INT32_MINcorrectly (returns 2147483648 as unsigned)Note
Caller can use result without concern for undefined behavior
- Parameters:
i32 – Signed integer (
int32_t) to get absolute value of
- Returns:
Absolute value as unsigned 32-bit integer (
uint32_t)
-
static inline uint64_t int64_safe_unsigned_abs(int64_t i64)
Compute absolute value of signed 64-bit integer (
int64_t) as unsigned (uint64_t)Returns the absolute value of a signed 64-bit integer (
int64_t) as an unsigned 64-bit value (uint64_t). This function avoids undefined behavior that would occur when negatingINT64_MINin signed arithmetic.Note
Handles
INT64_MINcorrectly (returns 9223372036854775808 as unsigned)Note
Caller can use result without concern for undefined behavior
- Parameters:
i64 – Signed integer (
int64_t) to get absolute value of
- Returns:
Absolute value as unsigned 64-bit integer (
uint64_t)
-
static inline int32_t int32_bsr(int32_t n, size_t rshift)
Perform arithmetic right shift on 32-bit signed integer (
int32_t)Performs a portable arithmetic right shift that preserves sign extension across different compilers and architectures. Unlike the C >> operator on signed integers (which has implementation-defined behavior for negative values), this function guarantees arithmetic shift semantics.
See also
int32_bsr_safe() for safe version with large shift handling
Note
Negative values are sign-extended (arithmetic shift)
Note
Positive values are zero-extended (logical shift)
Note
Portable replacement for implementation-defined signed right shift
Warning
For shift amounts >= 32, behavior is undefined. Use
int32_bsr_safe()for defined behavior with large shift amounts- Parameters:
n – Signed 32-bit integer (
int32_t) to shiftrshift – Number of bit positions to shift right
- Returns:
Right-shifted value with sign extension preserved
-
static inline int64_t int64_bsr(int64_t n, size_t rshift)
Perform arithmetic right shift on 64-bit signed integer (
int64_t)Performs a portable arithmetic right shift that preserves sign extension across different compilers and architectures. Unlike the C >> operator on signed integers (which has implementation-defined behavior for negative values), this function guarantees arithmetic shift semantics.
See also
int64_bsr_safe() for safe version with large shift handling
Note
Negative values are sign-extended (arithmetic shift)
Note
Positive values are zero-extended (logical shift)
Note
Portable replacement for implementation-defined signed right shift
Warning
For shift amounts >= 64, behavior is undefined. Use
int64_bsr_safe()for defined behavior with large shift amounts- Parameters:
n – Signed 64-bit integer (
int64_t) to shiftrshift – Number of bit positions to shift right
- Returns:
Right-shifted value with sign extension preserved
-
static inline int32_t int32_bsr_safe(int32_t n, size_t rshift)
Safely perform arithmetic right shift on 32-bit signed integer (
int32_t)Performs a portable arithmetic right shift with defined behavior for shift amounts >= 32 bits. This follows Erlang’s semantics where right shifts beyond the bit width converge to -1 for negative values and 0 for non-negative values.
See also
int32_bsr() for version without large shift protection
Note
For rshift >= 32: returns -1 if n < 0, returns 0 if n >= 0
Note
For rshift < 32: performs standard arithmetic right shift
Note
Erlang-inspired semantics for large shifts
- Parameters:
n – Signed 32-bit integer (
int32_t) to shiftrshift – Number of bit positions to shift right
- Returns:
Right-shifted value, or -1 (negative) / 0 (non-negative) for shifts >= 32
-
static inline int64_t int64_bsr_safe(int64_t n, size_t rshift)
Safely perform arithmetic right shift on 64-bit signed integer (
int64_t)Performs a portable arithmetic right shift with defined behavior for shift amounts >= 64 bits. This follows Erlang’s semantics where right shifts beyond the bit width converge to -1 for negative values and 0 for non-negative values.
See also
int64_bsr() for version without large shift protection
Note
For rshift >= 64: returns -1 if n < 0, returns 0 if n >= 0
Note
For rshift < 64: performs standard arithmetic right shift
Note
Erlang-inspired semantics for large shifts
- Parameters:
n – Signed 64-bit integer (
int64_t) to shiftrshift – Number of bit positions to shift right
- Returns:
Right-shifted value, or -1 (negative) / 0 (non-negative) for shifts >= 64
-
static inline bool int32_bsl_overflow(int32_t n, size_t lshift, int32_t *out)
Perform left shift on 32-bit signed integer (
int32_t) with overflow detection.Performs a left shift operation with overflow detection. The shift is always defined (even for shift amounts >= 32), and the function reports whether the operation would lose information. This provides safe, portable bit shifting with predictable overflow semantics.
See also
int32_bsr() used internally for overflow checking
Note
For lshift >= 32: sets *out to 0, returns true if n != 0
Note
For lshift < 32: performs shift and checks if reversible
- Parameters:
n – Signed 32-bit integer (
int32_t) to shiftlshift – Number of bit positions to shift left
out – [out] Result of the shift operation (0 for shifts >= 32)
- Returns:
true if overflow occurred (information lost), false if exact
- Pre:
out != NULL
-
static inline bool int64_bsl_overflow(int64_t n, size_t lshift, int64_t *out)
Perform left shift on 64-bit signed integer (
int64_t) with overflow detection.Performs a left shift operation with overflow detection. The shift is always defined (even for shift amounts >= 64), and the function reports whether the operation would lose information. This provides safe, portable bit shifting with predictable overflow semantics.
See also
int64_bsr() used internally for overflow checking
Note
For lshift >= 64: sets *out to 0, returns true if n != 0
Note
For lshift < 64: performs shift and checks if reversible
Note
Overflow detection works by shifting back and comparing with original
- Parameters:
n – Signed 64-bit integer (
int64_t) to shiftlshift – Number of bit positions to shift left
out – [out] Result of the shift operation (0 for shifts >= 64)
- Returns:
true if overflow occurred (information lost), false if exact
- Pre:
out != NULL
-
size_t intptr_write_to_ascii_buf(intptr_t n, unsigned int base, char *out_end)
Convert
intptr_tto ASCII representation in specified base.Writes the ASCII representation of a signed integer to a buffer, starting from the end and working backwards. The function returns the number of characters written. This design allows efficient conversion without requiring string reversal.
char buffer[INTPTR_WRITE_TO_ASCII_BUF_LEN]; size_t len = intptr_write_to_ascii_buf(-42, 10, buffer + INTPTR_WRITE_TO_ASCII_BUF_LEN); // Characters written at: buffer + INTPTR_WRITE_TO_ASCII_BUF_LEN - len // Result: "-42" (3 characters)
Note
Optimized implementations for base 10 and base 16
Note
Negative numbers include leading ‘-’ character
Note
Digits > 9 represented as uppercase letters (A-Z)
Warning
Buffer must be at least
INTPTR_WRITE_TO_ASCII_BUF_LENbytesWarning
Insufficient buffer size causes undefined behavior (buffer overflow)
Warning
Caller must add null terminator if using result as C string
- Parameters:
n – Integer value (
intptr_t) to convertbase – Number base for conversion (2-36)
out_end – Pointer to one-past-last position of output buffer
- Returns:
Number of characters written to buffer
- Pre:
base >= 2 && base <= 36
- Pre:
out_end points to valid buffer with at least
INTPTR_WRITE_TO_ASCII_BUF_LENbytes before it- Post:
Characters written to [(out_end - return_value), out_end)
- Post:
No null terminator is added
-
static inline size_t int32_write_to_ascii_buf(int32_t n, unsigned int base, char *out_end)
Convert
int32_tto ASCII representation in specified base.Writes the ASCII representation of a 32-bit signed integer to a buffer, starting from the end and working backwards. The function returns the number of characters written. This design allows efficient conversion without requiring string reversal.
char buffer[INT32_WRITE_TO_ASCII_BUF_LEN]; size_t len = int32_write_to_ascii_buf(-42, 10, buffer + INT32_WRITE_TO_ASCII_BUF_LEN); // Characters written at: buffer + INT32_WRITE_TO_ASCII_BUF_LEN - len // Result: "-42" (3 characters)
Note
Optimized implementations for base 10 and base 16
Note
Negative numbers include leading ‘-’ character
Note
Digits > 9 represented as uppercase letters (A-Z)
Warning
Buffer must be at least
INT32_WRITE_TO_ASCII_BUF_LENbytesWarning
Insufficient buffer size causes undefined behavior (buffer overflow)
Warning
Caller must add null terminator if using result as C string
- Parameters:
n – Integer value (
int32_t) to convertbase – Number base for conversion (2-36)
out_end – Pointer to one-past-last position of output buffer
- Returns:
Number of characters written to buffer
- Pre:
base >= 2 && base <= 36
- Pre:
out_end points to valid buffer with at least
INT32_WRITE_TO_ASCII_BUF_LENbytes before it- Post:
Characters written to [(out_end - return_value), out_end)
- Post:
No null terminator is added
-
static inline size_t int64_write_to_ascii_buf(int64_t n, unsigned int base, char *out_end)
Convert
int64_tto ASCII representation in specified base.Writes the ASCII representation of a 64-bit signed integer to a buffer, starting from the end and working backwards. The function returns the number of characters written. This design allows efficient conversion without requiring string reversal.
char buffer[INT64_WRITE_TO_ASCII_BUF_LEN]; size_t len = int64_write_to_ascii_buf(INT64_MIN, 10, buffer + INT64_WRITE_TO_ASCII_BUF_LEN); // Characters written at: buffer + INT64_WRITE_TO_ASCII_BUF_LEN - len // Result: "-9223372036854775808" (20 characters)
Note
Optimized implementations for base 10 and base 16
Note
Negative numbers include leading ‘-’ character
Note
Digits > 9 represented as uppercase letters (A-Z)
Warning
Buffer must be at least
INT64_WRITE_TO_ASCII_BUF_LENbytesWarning
Insufficient buffer size causes undefined behavior (buffer overflow)
Warning
Caller must add null terminator if using result as C string
- Parameters:
n – Integer value (
int64_t) to convertbase – Number base for conversion (2-36)
out_end – Pointer to one-past-last position of output buffer
- Returns:
Number of characters written to buffer
- Pre:
base >= 2 && base <= 36
- Pre:
out_end points to valid buffer with at least
INT64_WRITE_TO_ASCII_BUF_LENbytes before it- Post:
Characters written to [(out_end - return_value), out_end)
- Post:
No null terminator is added
-
int int64_parse_ascii_buf(const char buf[], size_t buf_len, unsigned int base, buf_to_int64_options_t options, int64_t *out)
Parse ASCII buffer to
int64_tin specified base.Parses an ASCII representation of an integer from a buffer (not necessarily null-terminated) into a 64-bit signed integer. Supports bases 2-36 with optimized paths for base 10 and 16. The function is designed to support parsing arbitrarily large integers by processing them in chunks - it returns the position where parsing stopped, allowing callers to continue parsing from that point.
// Simple parsing int64_t value; const char *number = "12345"; int pos = int64_parse_ascii_buf(number, strlen(number), 10, BufToInt64NoOptions, &value); if (pos == strlen(number)) { // Successfully parsed entire buffer: value = 12345 } // Parsing with overflow detection const char *big_num = "99999999999999999999999"; int pos = int64_parse_ascii_buf(big_num, strlen(big_num), 10, BufToInt64NoOptions, &value); if (pos < strlen(big_num)) { // Overflow occurred at position pos // value contains the maximum representable value before overflow } // Chunk parsing for arbitrarily large integers const char *chunks[] = {"12345", "67890", "12345"}; int64_t accumulated = 0; for (int i = 0; i < 3; i++) { int64_t chunk; int pos = int64_parse_ascii_buf(chunks[i], 5, 10, BufToInt64RejectSign, &chunk); // Process chunk value... }
See also
int64_write_to_ascii_buf() for the inverse operation
Note
Leading zeros are skipped automatically
Note
Signs (+/-) accepted unless
BufToInt64RejectSignis setNote
Case-insensitive for letter digits (a-z, A-Z)
Note
Optimized implementations for base 10 and base 16
Note
Stops parsing at first invalid character or overflow
Warning
Return value -1 indicates format error (invalid digit for base)
Warning
Return value < buf_len may indicate overflow or invalid character
- Parameters:
buf – Buffer containing ASCII digits to parse
buf_len – Length of buffer in bytes
base – Number base for parsing (2-36)
options – Parsing options (e.g., reject sign characters)
out – [out] Parsed integer value (valid even on overflow)
- Returns:
Position after last successfully parsed character, or -1 on format error
- Pre:
base >= 2 && base <= 36
- Pre:
buf != NULL when buf_len > 0 (NULL allowed only for zero-length buffer)
- Pre:
out != NULL
- Post:
On success: *out contains parsed value up to position returned
- Post:
On overflow: *out contains value parsed before overflow, returns position where overflow occurred
- Post:
On format error: returns -1, *out is undefined