diff --git a/src/libguac/guacamole/string.h b/src/libguac/guacamole/string.h index 27bf7b10cc..f9983b611a 100644 --- a/src/libguac/guacamole/string.h +++ b/src/libguac/guacamole/string.h @@ -29,6 +29,23 @@ #include #include +/** + * Convert the provided integer into a string, returning the number of + * characters written into the destination string, or a negative value if an + * error occurs. + * + * @param dest + * The destination string to copy the data into. This need not be initialized + * before calling this function - this function will allocate the memory. + * + * @param integer + * The integer to convert to a string. + * + * @return + * The number of characters written into the dest string. + */ +size_t guac_itoa(char* restrict dest, int integer); + /** * Copies a limited number of bytes from the given source string to the given * destination buffer. The resulting buffer will always be null-terminated, diff --git a/src/libguac/string.c b/src/libguac/string.c index 2a7ec2cd4d..5c0851d2e7 100644 --- a/src/libguac/string.c +++ b/src/libguac/string.c @@ -22,6 +22,7 @@ #include "guacamole/mem.h" #include +#include #include /** @@ -44,6 +45,19 @@ */ #define REMAINING(n, length) (((n) < (length)) ? 0 : ((n) - (length))) +size_t guac_itoa(char* restrict dest, int integer) { + + /* Determine size of string. */ + int str_size = snprintf(dest, 0, "%u", integer); + + /* Allocate the memory. */ + dest = guac_mem_alloc(str_size + 1); + + /* Do the conversion and return. */ + return snprintf(dest, str_size, "%u", integer); + +} + size_t guac_strlcpy(char* restrict dest, const char* restrict src, size_t n) { #ifdef HAVE_STRLCPY