aboutsummaryrefslogtreecommitdiff
path: root/include/BH/String.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/BH/String.h')
-rw-r--r--include/BH/String.h205
1 files changed, 129 insertions, 76 deletions
diff --git a/include/BH/String.h b/include/BH/String.h
index 0ce62c4..1484889 100644
--- a/include/BH/String.h
+++ b/include/BH/String.h
@@ -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,134 +20,197 @@ 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);
/**
- * Reads \a string containing double value in fixed or scientific format,
- * optionally reports \a size amount of characters consumed and returns
- * the result value.
+ * 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 Optional size
+ * \param size String size
+ * \param value Value
+ * \param base Base
+ * \param actual Written size (optional)
*
- * \return On success, returns parsed double value.
- * \return On failure, returns zero.
+ * \return On success, returns zero.
+ * \return On failure, returns error code.
*/
-double BH_StringToDouble(const char *string,
- size_t *size);
+int BH_StringFromInt8s(char *string,
+ size_t size,
+ int8_t value,
+ int base,
+ size_t *actual);
/**
- * Formats signed 8bit \a value into string with the specified \a base.
+ * 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 new string pointer.
- * \return On failure, returns NULL pointer.
+ * \return On success, returns zero.
+ * \return On failure, returns error code.
*/
-char *BH_StringFromInt8s(int8_t value,
- int base);
+int BH_StringFromInt16s(char *string,
+ size_t size,
+ int16_t value,
+ int base,
+ size_t *actual);
/**
- * Formats signed 16bit \a value into string with the specified \a base.
+ * 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 new string pointer.
- * \return On failure, returns NULL pointer.
+ * \return On success, returns zero.
+ * \return On failure, returns error code.
*/
-char *BH_StringFromInt16s(int16_t value,
- int base);
+int BH_StringFromInt32s(char *string,
+ size_t size,
+ int32_t value,
+ int base,
+ size_t *actual);
/**
- * Formats signed 32bit \a value into string with the specified \a base.
+ * 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 new string pointer.
- * \return On failure, returns NULL pointer.
+ * \return On success, returns zero.
+ * \return On failure, returns error code.
*/
-char *BH_StringFromInt32s(int32_t value,
- int base);
+int BH_StringFromInt64s(char *string,
+ size_t size,
+ int64_t value,
+ int base,
+ size_t *actual);
+
/**
- * Formats signed 64bit \a value into string with the specified \a base.
+ * 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 new string pointer.
- * \return On failure, returns NULL pointer.
+ * \return On success, returns zero.
+ * \return On failure, returns error code.
*/
-char *BH_StringFromInt64s(int64_t value,
- int base);
+int BH_StringFromInt8u(char *string,
+ size_t size,
+ uint8_t value,
+ int base,
+ size_t *actual);
/**
- * Formats unsigned 8bit \a value into string with the specified \a base.
+ * 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 new string pointer.
- * \return On failure, returns NULL pointer.
+ * \return On success, returns zero.
+ * \return On failure, returns error code.
*/
-char *BH_StringFromInt8u(uint8_t value,
- int base);
+int BH_StringFromInt16u(char *string,
+ size_t size,
+ uint16_t value,
+ int base,
+ size_t *actual);
/**
- * Formats unsigned 16bit \a value into string with the specified \a base.
+ * 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 new string pointer.
- * \return On failure, returns NULL pointer.
+ * \return On success, returns zero.
+ * \return On failure, returns error code.
*/
-char *BH_StringFromInt16u(uint16_t value,
- int base);
+int BH_StringFromInt32u(char *string,
+ size_t size,
+ uint32_t value,
+ int base,
+ size_t *actual);
/**
- * Formats unsigned 32bit \a value into string with the specified \a base.
+ * 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 new string pointer.
- * \return On failure, returns NULL pointer.
+ * \return On success, returns zero.
+ * \return On failure, returns error code.
*/
-char *BH_StringFromInt32u(uint32_t value,
- int base);
+int BH_StringFromInt64u(char *string,
+ size_t size,
+ uint64_t value,
+ int base,
+ size_t *actual);
/**
- * Formats unsigned 64bit \a value into string with the specified \a base.
+ * Reads \a string containing double value in fixed or scientific format,
+ * optionally reports \a size amount of characters consumed and returns
+ * the result value.
*
- * \param value Value
- * \param base Base
+ * \param string String
+ * \param size Optional size
*
- * \return On success, returns new string pointer.
- * \return On failure, returns NULL pointer.
+ * \return On success, returns parsed double value.
+ * \return On failure, returns zero.
*/
-char *BH_StringFromInt64u(uint64_t value,
- int base);
+double BH_StringToDouble(const char *string,
+ size_t *size);
/**
@@ -173,6 +218,7 @@ char *BH_StringFromInt64u(uint64_t value,
* \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