Refactor strings (avoid using dynamically allocated memory)

Refactored strings functions to make them use supplied string
instead of allocating new ones.

Additionally, split String/Int.c into several files to avoid define
mess that was present before.
This commit is contained in:
2025-03-27 22:20:21 +03:00
parent 9025c3d945
commit f9ebeabb18
10 changed files with 519 additions and 384 deletions

View File

@@ -6,27 +6,9 @@
/**
* Frees dynamically allocated \a string.
*
* \param string String pointer
*/
void BH_StringFree(char *string);
/**
* Creates a copy of the input \a string.
*
* \param string String pointer
*
* \return On success, returns new string pointer.
* \return On failure, returns NULL pointer.
*/
char *BH_StringCopy(const char *string);
/**
* Formats a double \a value into a string using the provided \a format and
* \a precision.
* Formats a double \a value into a zero terminated \a string (limited by
* \a size) using the provided \a format and \a precision and stores \a actual
* length (if it's provided).
*
* Formats supported:
* - Scientific or fixed format ('g' or 'G')
@@ -38,16 +20,182 @@ char *BH_StringCopy(const char *string);
*
* This function follows IEEE 754 round to even to break ties during rounding.
*
* \param string String
* \param size String size
* \param value Value
* \param format Format
* \param precision Precision
* \param actual Written size (optional)
*
* \return On success, returns new string pointer.
* \return On failure, returns NULL pointer.
* \return On success, returns zero.
* \return On failure, returns error code.
*/
char *BH_StringFromDouble(double value,
char format,
int precision);
int BH_StringFromDouble(char *string,
size_t size,
double value,
char format,
int precision,
size_t *actual);
/**
* Formats signed 8-bit \a value into a \a string (limited by \a size) with
* provided \a base and stores \a actual length (if it's provided).
*
* \param string String
* \param size String size
* \param value Value
* \param base Base
* \param actual Written size (optional)
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int BH_StringFromInt8s(char *string,
size_t size,
int8_t value,
int base,
size_t *actual);
/**
* Formats signed 16-bit \a value into a \a string (limited by \a size) with
* provided \a base and stores \a actual length (if it's provided).
*
* \param string String
* \param size String size
* \param value Value
* \param base Base
* \param actual Written size (optional)
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int BH_StringFromInt16s(char *string,
size_t size,
int16_t value,
int base,
size_t *actual);
/**
* Formats signed 32-bit \a value into a \a string (limited by \a size) with
* provided \a base and stores \a actual length (if it's provided).
*
* \param string String
* \param size String size
* \param value Value
* \param base Base
* \param actual Written size (optional)
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int BH_StringFromInt32s(char *string,
size_t size,
int32_t value,
int base,
size_t *actual);
/**
* Formats signed 64-bit \a value into a \a string (limited by \a size) with
* provided \a base and stores \a actual length (if it's provided).
*
* \param string String
* \param size String size
* \param value Value
* \param base Base
* \param actual Written size (optional)
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int BH_StringFromInt64s(char *string,
size_t size,
int64_t value,
int base,
size_t *actual);
/**
* Formats unsigned 8-bit \a value into a \a string (limited by \a size) with
* provides \a base and stores \a actual length (if it's provided).
*
* \param string String
* \param size String size
* \param value Value
* \param base Base
* \param actual Written size (optional)
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int BH_StringFromInt8u(char *string,
size_t size,
uint8_t value,
int base,
size_t *actual);
/**
* Formats unsigned 16-bit \a value into a \a string (limited by \a size) with
* provides \a base and stores \a actual length (if it's provided).
*
* \param string String
* \param size String size
* \param value Value
* \param base Base
* \param actual Written size (optional)
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int BH_StringFromInt16u(char *string,
size_t size,
uint16_t value,
int base,
size_t *actual);
/**
* Formats unsigned 32-bit \a value into a \a string (limited by \a size) with
* provided \a base and stores \a actual length (if it's provided).
*
* \param string String
* \param size String size
* \param value Value
* \param base Base
* \param actual Written size (optional)
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int BH_StringFromInt32u(char *string,
size_t size,
uint32_t value,
int base,
size_t *actual);
/**
* Formats unsigned 64-bit \a value into a \a string (limited by \a size) with
* provided \a base and stores \a actual length (if it's provided).
*
* \param string String
* \param size String size
* \param value Value
* \param base Base
* \param actual Written size (optional)
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int BH_StringFromInt64u(char *string,
size_t size,
uint64_t value,
int base,
size_t *actual);
/**
@@ -65,114 +213,12 @@ double BH_StringToDouble(const char *string,
size_t *size);
/**
* Formats signed 8bit \a value into string with the specified \a base.
*
* \param value Value
* \param base Base
*
* \return On success, returns new string pointer.
* \return On failure, returns NULL pointer.
*/
char *BH_StringFromInt8s(int8_t value,
int base);
/**
* Formats signed 16bit \a value into string with the specified \a base.
*
* \param value Value
* \param base Base
*
* \return On success, returns new string pointer.
* \return On failure, returns NULL pointer.
*/
char *BH_StringFromInt16s(int16_t value,
int base);
/**
* Formats signed 32bit \a value into string with the specified \a base.
*
* \param value Value
* \param base Base
*
* \return On success, returns new string pointer.
* \return On failure, returns NULL pointer.
*/
char *BH_StringFromInt32s(int32_t value,
int base);
/**
* Formats signed 64bit \a value into string with the specified \a base.
*
* \param value Value
* \param base Base
*
* \return On success, returns new string pointer.
* \return On failure, returns NULL pointer.
*/
char *BH_StringFromInt64s(int64_t value,
int base);
/**
* Formats unsigned 8bit \a value into string with the specified \a base.
*
* \param value Value
* \param base Base
*
* \return On success, returns new string pointer.
* \return On failure, returns NULL pointer.
*/
char *BH_StringFromInt8u(uint8_t value,
int base);
/**
* Formats unsigned 16bit \a value into string with the specified \a base.
*
* \param value Value
* \param base Base
*
* \return On success, returns new string pointer.
* \return On failure, returns NULL pointer.
*/
char *BH_StringFromInt16u(uint16_t value,
int base);
/**
* Formats unsigned 32bit \a value into string with the specified \a base.
*
* \param value Value
* \param base Base
*
* \return On success, returns new string pointer.
* \return On failure, returns NULL pointer.
*/
char *BH_StringFromInt32u(uint32_t value,
int base);
/**
* Formats unsigned 64bit \a value into string with the specified \a base.
*
* \param value Value
* \param base Base
*
* \return On success, returns new string pointer.
* \return On failure, returns NULL pointer.
*/
char *BH_StringFromInt64u(uint64_t value,
int base);
/**
* Reads \a string containing value in specified \a base, optionally reports
* \a size amount of characters consumed and returns the result value.
*
* If base is 0, function will automaticly detect input base by the prefix:
* - Base 2 if prefix is 0b
* - Base 8 if prefix is 0
* - Base 16 if prefix is 0x
* - Base 10 in other cases
@@ -194,6 +240,7 @@ int8_t BH_StringToInt8s(const char *string,
* \a size amount of characters consumed and returns the result value.
*
* If base is 0, function will automaticly detect input base by the prefix:
* - Base 2 if prefix is 0b
* - Base 8 if prefix is 0
* - Base 16 if prefix is 0x
* - Base 10 in other cases
@@ -215,6 +262,7 @@ int16_t BH_StringToInt16s(const char *string,
* \a size amount of characters consumed and returns the result value.
*
* If base is 0, function will automaticly detect input base by the prefix:
* - Base 2 if prefix is 0b
* - Base 8 if prefix is 0
* - Base 16 if prefix is 0x
* - Base 10 in other cases
@@ -236,6 +284,7 @@ int32_t BH_StringToInt32s(const char *string,
* \a size amount of characters consumed and returns the result value.
*
* If base is 0, function will automaticly detect input base by the prefix:
* - Base 2 if prefix is 0b
* - Base 8 if prefix is 0
* - Base 16 if prefix is 0x
* - Base 10 in other cases
@@ -257,6 +306,7 @@ int64_t BH_StringToInt64s(const char *string,
* \a size amount of characters consumed and returns the result value.
*
* If base is 0, function will automaticly detect input base by the prefix:
* - Base 2 if prefix is 0b
* - Base 8 if prefix is 0
* - Base 16 if prefix is 0x
* - Base 10 in other cases
@@ -278,6 +328,7 @@ uint8_t BH_StringToInt8u(const char *string,
* \a size amount of characters consumed and returns the result value.
*
* If base is 0, function will automaticly detect input base by the prefix:
* - Base 2 if prefix is 0b
* - Base 8 if prefix is 0
* - Base 16 if prefix is 0x
* - Base 10 in other cases
@@ -299,6 +350,7 @@ uint16_t BH_StringToInt16u(const char *string,
* \a size amount of characters consumed and returns the result value.
*
* If base is 0, function will automaticly detect input base by the prefix:
* - Base 2 if prefix is 0b
* - Base 8 if prefix is 0
* - Base 16 if prefix is 0x
* - Base 10 in other cases
@@ -320,6 +372,7 @@ uint32_t BH_StringToInt32u(const char *string,
* \a size amount of characters consumed and returns the result value.
*
* If base is 0, function will automaticly detect input base by the prefix:
* - Base 2 if prefix is 0b
* - Base 8 if prefix is 0
* - Base 16 if prefix is 0x
* - Base 10 in other cases