Skip to content

Commit

Permalink
GUACAMOLE-1686: Add convenience function for converting integer to st…
Browse files Browse the repository at this point in the history
…ring.
  • Loading branch information
necouchman committed Jan 12, 2024
1 parent 5190f82 commit ea6dece
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/libguac/guacamole/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@
#include <stddef.h>
#include <string.h>

/**
* 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,
Expand Down
14 changes: 14 additions & 0 deletions src/libguac/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "guacamole/mem.h"

#include <stddef.h>
#include <stdio.h>
#include <string.h>

/**
Expand All @@ -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
Expand Down

0 comments on commit ea6dece

Please sign in to comment.