comparison th_string.c @ 696:9bbacd72df3d

Allow src and dst to "overlap" for th_pstr_cpy().
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Mar 2020 19:13:52 +0200
parents 953e16582f25
children d687bbb54c1a
comparison
equal deleted inserted replaced
695:a04b8fe158b9 696:9bbacd72df3d
526 /* Copy a given string over in *pdst. 526 /* Copy a given string over in *pdst.
527 */ 527 */
528 int th_pstr_cpy(th_char_t **pdst, const th_char_t *src) 528 int th_pstr_cpy(th_char_t **pdst, const th_char_t *src)
529 { 529 {
530 size_t slen; 530 size_t slen;
531 th_char_t *tmp;
531 532
532 if (pdst == NULL || src == NULL) 533 if (pdst == NULL || src == NULL)
533 return THERR_NULLPTR; 534 return THERR_NULLPTR;
534 535
536 slen = th_strlen(src);
537 if ((tmp = th_malloc((slen + 1) * sizeof(th_char_t))) == NULL)
538 return THERR_MALLOC;
539
540 memcpy(tmp, src, (slen + 1) * sizeof(th_char_t));
541
535 th_free(*pdst); 542 th_free(*pdst);
536 543 *pdst = tmp;
537 slen = th_strlen(src);
538 if ((*pdst = th_malloc((slen + 1) * sizeof(th_char_t))) == NULL)
539 return THERR_MALLOC;
540
541 memcpy(*pdst, src, (slen + 1) * sizeof(th_char_t));
542 544
543 return THERR_OK; 545 return THERR_OK;
544 } 546 }
545 547
546 548