Esempio n. 1
0
void dstr_cat_dstr(struct dstr *dst, const struct dstr *str)
{
	size_t new_len;
	if (!str->len)
		return;

	new_len = dst->len + str->len;

	dstr_ensure_capacity(dst, new_len + 1);
	memcpy(dst->array+dst->len, str->array, str->len + 1);
	dst->len = new_len;
}
Esempio n. 2
0
void dstr_vprintf(struct dstr *dst, const char *format, va_list args)
{
	dstr_ensure_capacity(dst, 4096);
	vsnprintf(dst->array, 4095, format, args);

	if (!*dst->array) {
		dstr_free(dst);
		return;
	}

	dst->len = strlen(dst->array);
}
Esempio n. 3
0
void dstr_ncat(struct dstr *dst, const char *array, const size_t len)
{
	size_t new_len;
	if (!array || !*array || !len)
		return;

	new_len = dst->len + len;

	dstr_ensure_capacity(dst, new_len + 1);
	memcpy(dst->array+dst->len, array, len);

	dst->len = new_len;
	dst->array[new_len] = 0;
}
Esempio n. 4
0
void dstr_copy(struct dstr *dst, const char *array)
{
	size_t len;

	if (!array || !*array) {
		dstr_free(dst);
		return;
	}

	len = strlen(array);
	dstr_ensure_capacity(dst, len + 1);
	memcpy(dst->array, array, len + 1);
	dst->len = len;
}
Esempio n. 5
0
void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str, const size_t len)
{
	size_t new_len, in_len;
	if (!str->array || !*str->array || !len)
		return;

	in_len = size_min(len, str->len);
	new_len = dst->len + in_len;

	dstr_ensure_capacity(dst, new_len + 1);
	memcpy(dst->array+dst->len, str->array, in_len);

	dst->len = new_len;
	dst->array[new_len] = 0;
}
Esempio n. 6
0
void dstr_insert_dstr(struct dstr *dst, const size_t idx,
		const struct dstr *str)
{
	size_t new_len;
	if (!str->len)
		return;
	if (idx == dst->len) {
		dstr_cat_dstr(dst, str);
		return;
	}

	new_len = dst->len + str->len;

	dstr_ensure_capacity(dst, (new_len+1));
	dst->len = new_len;

	memmove(dst->array+idx+str->len, dst->array+idx, dst->len - idx + 1);
	memcpy(dst->array+idx, str->array, str->len);
}
Esempio n. 7
0
void dstr_insert(struct dstr *dst, const size_t idx, const char *array)
{
	size_t new_len, len;
	if (!array || !*array)
		return;
	if (idx == dst->len) {
		dstr_cat(dst, array);
		return;
	}

	len = strlen(array);
	new_len = dst->len + len;

	dstr_ensure_capacity(dst, new_len + 1);
	dst->len   = new_len;

	memmove(dst->array+idx+len, dst->array+idx, dst->len - idx + 1);
	memcpy(dst->array+idx, array, len);
}
Esempio n. 8
0
void dstr_vcatf(struct dstr *dst, const char *format, va_list args)
{
	va_list args_cp;
	va_copy(args_cp, args);

	int len = vsnprintf(NULL, 0, format, args_cp);
	va_end(args_cp);

	if (len < 0) len = 4095;
	
	dstr_ensure_capacity(dst, dst->len + ((size_t)len) + 1);
	len = vsnprintf(dst->array + dst->len, ((size_t)len) + 1, format, args);

	if (!*dst->array) {
		dstr_free(dst);
		return;
	}

	dst->len += len < 0 ? strlen(dst->array + dst->len) : (size_t)len;
}
Esempio n. 9
0
void dstr_replace(struct dstr *str, const char *find,
		const char *replace)
{
	size_t find_len, replace_len;
	char *temp;

	if (!replace)
		replace = "";

	find_len    = strlen(find);
	replace_len = strlen(replace);
	temp = str->array;

	if (replace_len < find_len) {
		unsigned long count = 0;

		while ((temp = strstr(temp, find)) != NULL) {
			char *end = temp+find_len;
			size_t end_len = strlen(end);

			if (end_len) {
				memmove(temp+replace_len, end, end_len + 1);
				if (replace_len)
					memcpy(temp, replace, replace_len);
			} else {
				strcpy(temp, replace);
			}

			temp += replace_len;
			++count;
		}

		if (count)
			str->len += (replace_len-find_len) * count;

	} else if (replace_len > find_len) {
		unsigned long count = 0;

		while ((temp = strstr(temp, find)) != NULL) {
			temp += find_len;
			++count;
		}

		if (!count)
			return;

		str->len += (replace_len-find_len) * count;
		dstr_ensure_capacity(str, str->len + 1);
		temp = str->array;

		while ((temp = strstr(temp, find)) != NULL) {
			char *end   = temp+find_len;
			size_t end_len = strlen(end);

			if (end_len) {
				memmove(temp+replace_len, end, end_len + 1);
				memcpy(temp, replace, replace_len);
			} else {
				strcpy(temp, replace);
			}

			temp += replace_len;
		}

	} else {
		while ((temp = strstr(temp, find)) != NULL) {
			memcpy(temp, replace, replace_len);
			temp += replace_len;
		}
	}
}