Skip to content

Commit

Permalink
add strbuff_prefixs
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexFabre committed Nov 1, 2023
1 parent faac2ec commit c227a2c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/strbuff_prefixs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions test/test_strbuff.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions test/test_strbuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions test/test_strbuff_prefixs.c
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit c227a2c

Please sign in to comment.