diff --git a/src/strbuff_prefixs.c b/src/strbuff_prefixs.c index eab0e3f..884d5bc 100644 --- a/src/strbuff_prefixs.c +++ b/src/strbuff_prefixs.c @@ -15,24 +15,35 @@ * * @details If dest or src buffer pointer is null, then the function returns 0. */ -// unsigned long strbuff_prefixs(const strbuff *dest, const char *src_str) -// { -// strbuff_check_not_null(dest, return 0); -// strbuff_check_not_null(src_str, return 0); +unsigned long strbuff_prefixs(const strbuff *dest, const char *src_str) +{ + strbuff_check_not_null(dest, return 0); + strbuff_check_not_null(src_str, return 0); -// size_t _dest_len = strbuff_len(dest); + size_t _dest_len = strbuff_len(dest); -// const char *s; + size_t _src_len = 0; + const char *s; -// for (s = src_str; *s; s++) { -// if (_dest_len == (strbuff_capacity(dest) - 1)) { -// break; -// } -// *(dest->str + _dest_len) = *s; -// _dest_len++; -// } + /* Compute the size of the string to add */ + for (s = src_str; *s; s++) { + if (_src_len == (strbuff_capacity(dest) - 1)) { + break; + } + _src_len++; + } -// *(dest->str + _dest_len) = 0; + /* If dest string buffer is not empty, move its content */ + if (_dest_len != 0) { + /* If the two contents do not fit, the end will be lost */ + if (_dest_len + _src_len >= strbuff_capacity(dest)) { + _dest_len = strbuff_capacity(dest) - 1 - _src_len; + } + memmove(dest->str + _src_len, dest->str, _src_len); + } -// return (size_t)(s - src_str); -// } + /* Copy the new begining of dest string buffer */ + memcpy(dest->str, src_str, _src_len); + + return _src_len; +} diff --git a/test/test_strbuff.c b/test/test_strbuff.c index 3a86250..265e63e 100644 --- a/test/test_strbuff.c +++ b/test/test_strbuff.c @@ -128,6 +128,7 @@ int main(void) test_strbuff_find(); test_strbuff_len(); test_strbuff_prefixb(); + test_strbuff_prefixs(); test_strbuff_printc(); test_strbuff_prints(); test_strbuff_revert(); diff --git a/test/test_strbuff.h b/test/test_strbuff.h index 7650245..0b70a30 100644 --- a/test/test_strbuff.h +++ b/test/test_strbuff.h @@ -77,6 +77,7 @@ void test_strbuff_end_with(void); void test_strbuff_find(void); void test_strbuff_len(void); void test_strbuff_prefixb(void); +void test_strbuff_prefixs(void); // void test_strbuff_printb(void); void test_strbuff_printc(void); // void test_strbuff_printf(void); diff --git a/test/test_strbuff_prefixs.c b/test/test_strbuff_prefixs.c new file mode 100644 index 0000000..f5396e8 --- /dev/null +++ b/test/test_strbuff_prefixs.c @@ -0,0 +1,44 @@ +// ========================================== +// String Buffer Project - A safe string library for C +// Copyright (c) 2023 Alex Fabre +// [Released under MIT License. Please refer to license.txt for details] +// ========================================== + +#include "test_strbuff.h" + +static void test_strbuff_prefixs_buffer_on_non_empty_buffer(void) +{ + rand_string(buffer1.str, strbuff_capacity(&buffer1) / 3); + + char tmp_buffer[TEST_BUFFER_SIZE]; + memcpy(tmp_buffer, buffer1.str, strbuff_capacity(&buffer1)); + + char tmp_buffer2[TEST_BUFFER_SIZE / 3]; + + rand_string(tmp_buffer2, sizeof(tmp_buffer2)); + + size_t n1 = strlen(buffer1.str); + size_t n2 = strlen(tmp_buffer2); + + TEST_ASSERT_EQUAL(n2, strbuff_prefix(&buffer1, tmp_buffer2)); + TEST_ASSERT_EQUAL((n1 + n2), strbuff_len(&buffer1)); + TEST_ASSERT_EQUAL_STRING_LEN(tmp_buffer2, buffer1.str, n2); + TEST_ASSERT_EQUAL_STRING_LEN(tmp_buffer, buffer1.str + n2, n1); +} + +static void test_strbuff_prefixs_buffer_on_filled_buffer(void) +{ + rand_string_fixed_length(buffer1.str, strbuff_capacity(&buffer1), strbuff_capacity(&buffer1) - 1); + + char tmp_buffer2[] = "Hello"; + + TEST_ASSERT_EQUAL(5, strbuff_prefix(&buffer1, tmp_buffer2)); + TEST_ASSERT_EQUAL(strbuff_capacity(&buffer1) - 1, strbuff_len(&buffer1)); + TEST_ASSERT_EQUAL_STRING_LEN("Hello", buffer1.str, 5); +} + +void test_strbuff_prefixs(void) +{ + RUN_TEST(test_strbuff_prefixs_buffer_on_non_empty_buffer); + RUN_TEST(test_strbuff_prefixs_buffer_on_filled_buffer); +}