2018-12-16 05:11:41 +08:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) Yuxuan Shui <yshuiv7@gmail.com>
|
|
|
|
|
2018-12-16 01:42:37 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-12-16 02:47:21 +08:00
|
|
|
#include "compiler.h"
|
2018-12-16 01:42:37 +08:00
|
|
|
#include "string_utils.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate the space and join two strings.
|
|
|
|
*/
|
|
|
|
char *mstrjoin(const char *src1, const char *src2) {
|
2019-01-30 08:12:12 +08:00
|
|
|
auto len1 = strlen(src1);
|
|
|
|
auto len2 = strlen(src2);
|
|
|
|
auto len = len1 + len2 + 1;
|
|
|
|
auto str = ccalloc(len, char);
|
2018-12-16 01:42:37 +08:00
|
|
|
|
2019-01-30 08:12:12 +08:00
|
|
|
strncpy(str, src1, len1);
|
|
|
|
strncpy(str + len1, src2, len2);
|
|
|
|
str[len - 1] = '\0';
|
2018-12-16 01:42:37 +08:00
|
|
|
|
2019-01-30 08:12:12 +08:00
|
|
|
return str;
|
2018-12-16 01:42:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Concatenate a string on heap with another string.
|
|
|
|
*/
|
|
|
|
void mstrextend(char **psrc1, const char *src2) {
|
2019-01-30 08:12:12 +08:00
|
|
|
if (!*psrc1) {
|
|
|
|
*psrc1 = strdup(src2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto len1 = strlen(*psrc1);
|
|
|
|
auto len2 = strlen(src2);
|
|
|
|
auto len = len1 + len2 + 1;
|
|
|
|
*psrc1 = crealloc(*psrc1, len);
|
|
|
|
|
|
|
|
strncpy(*psrc1 + len1, src2, len2);
|
|
|
|
(*psrc1)[len - 1] = '\0';
|
2018-12-16 01:42:37 +08:00
|
|
|
}
|