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 Apr 16, 2024
1 parent 6ffa031 commit 8d6f8a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/libguac/guacamole/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@
#include <stddef.h>
#include <string.h>

/**
* Convert the provided unsigned 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, which should already be
* allocated and at a size that can handle the string representation of the
* inteer.
*
* @param integer
* The unsigned integer to convert to a string.
*
* @return
* The number of characters written into the dest string.
*/
int guac_itoa(char* restrict dest, unsigned 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
15 changes: 15 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,20 @@
*/
#define REMAINING(n, length) (((n) < (length)) ? 0 : ((n) - (length)))

int guac_itoa(char* restrict dest, unsigned int integer) {

/* Determine size of string. */
int str_size = snprintf(dest, 0, "%i", integer);

/* If an error occurs, just return that and skip the conversion. */
if (str_size < 0)
return str_size;

/* Do the conversion and return. */
return snprintf(dest, (str_size + 1), "%i", integer);

}

size_t guac_strlcpy(char* restrict dest, const char* restrict src, size_t n) {

#ifdef HAVE_STRLCPY
Expand Down

0 comments on commit 8d6f8a7

Please sign in to comment.