summaryrefslogtreecommitdiffstats
path: root/src/dstring.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-06-29 13:53:03 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-06-29 13:53:03 -0400
commit174a2027ff0db579e18d5efacb17c3addf6473f6 (patch)
tree3d9839faac282c4fbc1a9fd5c08d9f74027c4da1 /src/dstring.h
parent4ad724391daeff5b86ad420f1e1a8b35d65fd7e0 (diff)
downloadbfs-174a2027ff0db579e18d5efacb17c3addf6473f6.tar.xz
dstring: Add some exact-size utility functions
Diffstat (limited to 'src/dstring.h')
-rw-r--r--src/dstring.h99
1 files changed, 94 insertions, 5 deletions
diff --git a/src/dstring.h b/src/dstring.h
index ee3b345..2673f1b 100644
--- a/src/dstring.h
+++ b/src/dstring.h
@@ -39,11 +39,30 @@ char *dstrdup(const char *str);
char *dstrndup(const char *str, size_t n);
/**
+ * Create a dynamic copy of a dynamic string.
+ *
+ * @param dstr
+ * The dynamic string to copy.
+ */
+char *dstrddup(const char *dstr);
+
+/**
+ * Create an exact-sized dynamic copy of a string.
+ *
+ * @param str
+ * The string to copy.
+ * @param len
+ * The length of the string, which may include internal NUL bytes.
+ */
+char *dstrxdup(const char *str, size_t len);
+
+/**
* Get a dynamic string's length.
*
* @param dstr
* The string to measure.
- * @return The length of dstr.
+ * @return
+ * The length of dstr.
*/
size_t dstrlen(const char *dstr);
@@ -54,7 +73,8 @@ size_t dstrlen(const char *dstr);
* The dynamic string to preallocate.
* @param capacity
* The new capacity for the string.
- * @return 0 on success, -1 on failure.
+ * @return
+ * 0 on success, -1 on failure.
*/
int dstreserve(char **dstr, size_t capacity);
@@ -65,7 +85,8 @@ int dstreserve(char **dstr, size_t capacity);
* The dynamic string to resize.
* @param length
* The new length for the dynamic string.
- * @return 0 on success, -1 on failure.
+ * @return
+ * 0 on success, -1 on failure.
*/
int dstresize(char **dstr, size_t length);
@@ -89,7 +110,8 @@ int dstrcat(char **dest, const char *src);
* The string to append.
* @param n
* The maximum number of characters to take from src.
- * @return 0 on success, -1 on failure.
+ * @return
+ * 0 on success, -1 on failure.
*/
int dstrncat(char **dest, const char *src, size_t n);
@@ -106,17 +128,84 @@ int dstrncat(char **dest, const char *src, size_t n);
int dstrdcat(char **dest, const char *src);
/**
+ * Append to a dynamic string.
+ *
+ * @param dest
+ * The destination dynamic string.
+ * @param src
+ * The string to append.
+ * @param len
+ * The exact number of characters to take from src.
+ * @return
+ * 0 on success, -1 on failure.
+ */
+int dstrxcat(char **dest, const char *src, size_t len);
+
+/**
* Append a single character to a dynamic string.
*
* @param str
* The string to append to.
* @param c
* The character to append.
- * @return 0 on success, -1 on failure.
+ * @return
+ * 0 on success, -1 on failure.
*/
int dstrapp(char **str, char c);
/**
+ * Copy a string into a dynamic string.
+ *
+ * @param dest
+ * The destination dynamic string.
+ * @param src
+ * The string to copy.
+ * @returns
+ * 0 on success, -1 on failure.
+ */
+int dstrcpy(char **dest, const char *str);
+
+/**
+ * Copy a dynamic string into another one.
+ *
+ * @param dest
+ * The destination dynamic string.
+ * @param src
+ * The dynamic string to copy.
+ * @returns
+ * 0 on success, -1 on failure.
+ */
+int dstrdcpy(char **dest, const char *str);
+
+/**
+ * Copy a string into a dynamic string.
+ *
+ * @param dest
+ * The destination dynamic string.
+ * @param src
+ * The dynamic string to copy.
+ * @param n
+ * The maximum number of characters to take from src.
+ * @returns
+ * 0 on success, -1 on failure.
+ */
+int dstrncpy(char **dest, const char *str, size_t n);
+
+/**
+ * Copy a string into a dynamic string.
+ *
+ * @param dest
+ * The destination dynamic string.
+ * @param src
+ * The dynamic string to copy.
+ * @param len
+ * The exact number of characters to take from src.
+ * @returns
+ * 0 on success, -1 on failure.
+ */
+int dstrxcpy(char **dest, const char *str, size_t len);
+
+/**
* Create a dynamic string from a format string.
*
* @param format