Mercurial > lbo > hg > ylisp
changeset 124:9fa6d6b4eb69
base/str: Implement ystr_clone()
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 01 Sep 2019 19:20:30 +0200 |
parents | f2ba3e8e04f7 |
children | ed74caa03e11 |
files | src/base/str.c src/base/str.h |
diffstat | 2 files changed, 18 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/src/base/str.c Sun Sep 01 19:20:08 2019 +0200 +++ b/src/base/str.c Sun Sep 01 19:20:30 2019 +0200 @@ -171,6 +171,12 @@ yvec_copy(&src->inner.big, &dst->inner.big); } +ystr_t ystr_clone(ystr_t* src) { + ystr_t new; + ystr_init(&new, ystr_str(src)); + return new; +} + #define YSTR_BUILD_BUF_SIZE 128 void ystr_build(ystr_t *s, const char *fmt, ...) {
--- a/src/base/str.h Sun Sep 01 19:20:08 2019 +0200 +++ b/src/base/str.h Sun Sep 01 19:20:30 2019 +0200 @@ -30,9 +30,10 @@ } ystr_t; /** - * @brief Create a new string and return it. It must be freed with `ystr_destroy()` after use. + * @brief Create a new string and return it. It must be freed with + * `ystr_destroy()` after use. */ -ystr_t ystr_new(const char* src); +ystr_t ystr_new(const char *src); /** * @brief Initialize a `ystr_t` with `src`, which can be `NULL`. The string @@ -76,7 +77,7 @@ /** * @brief Get character at `index`. */ -char* ystr_at(ystr_t *s, size_t index); +char *ystr_at(ystr_t *s, size_t index); /** * @brief Compare to `ystr_t` values. If `s1` is lexically lesser than `s2`, -1 @@ -114,9 +115,15 @@ void ystr_resize(ystr_t *s, size_t new); /** - * @brief Create a copy of `src` in `dst`, potentially freeing dst before copying. + * @brief Create a copy of `src` in `dst`, potentially freeing dst before + * copying. */ -void ystr_copy(ystr_t* src, ystr_t* dst); +void ystr_copy(ystr_t *src, ystr_t *dst); + +/** + * @brief Return a cloned copy of `src`. + */ +ystr_t ystr_clone(ystr_t *src); /** * @brief Append a C string to string, with possible formatting directives like