Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST + PATCH] utstring_clone() #161

Open
silvioprog opened this issue Sep 9, 2018 · 3 comments
Open

[FEATURE REQUEST + PATCH] utstring_clone() #161

silvioprog opened this issue Sep 9, 2018 · 3 comments

Comments

@silvioprog
Copy link

silvioprog commented Sep 9, 2018

Hi.

It would be nice to provide a macro for list cloning, something like this code below:

#ifdef __GNUC__

#define utstring_clone(src)               \
({                                        \
  UT_string *dst = NULL;                  \
  if ((src)->d != NULL) {                 \
    utstring_new(dst);                    \
    (dst)->n = (src)->n;                  \
    (dst)->i = (src)->i;                  \
    memcpy((dst)->d, (src)->d, (src)->n); \
  }                                       \
  dst;                                    \
})

#else

UT_string *utstring_clone(UT_string *src) {
  UT_string *dst = NULL;
  if (src->d != NULL) {
    utstring_new(dst);
    dst->n = src->n;
    dst->i = src->i;
    memcpy(dst->d, src->d, src->n);
  }
  return dst;
}

#endif

What do you thing? If you agree this code I can create a pull request. :-)

Thank you!

@Quuxplusone
Copy link
Collaborator

I would think that a more consistent definition for utstring_clone would be simply

#define utstring_clone(dst,src) \
do {                            \
  utstring_clear(dst);          \
  utstring_concat(dst, src);    \
} while (0)

Would such a macro be useful to you, or would it be useful only if it returned the value?

I'd be a little worried about having utstring_clone return a value, not just because it would be inconsistent with the rest of the library, but also because it seems like that would encourage people to write

UT_string *foo; utstring_new(foo); utstring_bincpy("foo", 3);
some_client_function(foo, utstring_clone(foo));
utstring_free(foo);

and forget to utstring_free the anonymous cloned string.

@silvioprog
Copy link
Author

#define utstring_clone(dst,src) \
do {                            \
  utstring_clear(dst);          \
  utstring_concat(dst, src);    \
} while (0)

Awesome definition, following the same utstring_concat(dst,src) signature. It may be useful because the "cloning" feature is very common in many programming languages. :-)

I have a similar question, but regarding uthash.h. Would it be possible to have a "clone" feature too?

@Quuxplusone
Copy link
Collaborator

For uthash.h, you'd have to be more specific about what you want this "clone" operation to do. I imagine that you'd be looking for HASH_SELECT, which already exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants