Skip to content

Commit

Permalink
add function strbuff_revert
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexFabre committed Oct 16, 2023
1 parent 2542870 commit 25ad5b2
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ You now have access to all the functions below:
* **strbuff_find(buff, src)**
* **strbuff_revert(buff)** *not yet implemented*
* **strbuff_revert(buff)**
* **strbuff_capacity(buff)**
* **strbuff_len(buff)**
Expand Down
10 changes: 9 additions & 1 deletion src/strbuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern "C" {

#define STRBUFF_VERSION_MAJOR 0
#define STRBUFF_VERSION_MINOR 1
#define STRBUFF_VERSION_BUILD 0
#define STRBUFF_VERSION_BUILD 1
#define STRBUFF_VERSION ((STRBUFF_VERSION_MAJOR << 16) | (STRBUFF_VERSION_MINOR << 8) | STRBUFF_VERSION_BUILD)

#ifndef strbuff_error_printf
Expand Down Expand Up @@ -153,6 +153,14 @@ char *strbuff_find(const strbuff *dest, const strbuff *substring);
*/
size_t strbuff_len(const strbuff *buff);

/**
* Reverses the contents of a string buffer.
*
* @param buff Pointer to the string buffer to be reversed.
* @return The length of the string buffer.
*/
unsigned long strbuff_revert(const strbuff *buff);

/**
* @brief Sanitizes a string buffer by setting remaining characters (beyond AZT) to 0.
*
Expand Down
32 changes: 32 additions & 0 deletions src/strbuff_revert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ==========================================
// 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 "strbuff.h"

/**
* Reverses the contents of a string buffer.
*
* @param buff Pointer to the string buffer to be reversed.
* @return The length of the string buffer.
*/
unsigned long strbuff_revert(const strbuff *buff)
{
strbuff_check_not_null(buff, return 0);

size_t _buff_len = strbuff_len(buff);

// Reverse the characters in the buffer one by one
for (size_t i = 0; i < (_buff_len / 2); i++) {

char tmp = *(buff->str + i);

*(buff->str + i) = *(buff->str + _buff_len - i - 1);

*(buff->str + _buff_len - 1 - i) = tmp;
}

return _buff_len;
}
1 change: 1 addition & 0 deletions test/test_strbuff.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ int main(void)
test_strbuff_len();
test_strbuff_printc();
test_strbuff_prints();
test_strbuff_revert();
test_strbuff_sanitize();
}

Expand Down
1 change: 1 addition & 0 deletions test/test_strbuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ void test_strbuff_find(void);
void test_strbuff_printc(void);
void test_strbuff_prints(void);
void test_strbuff_len(void);
void test_strbuff_revert(void);
void test_strbuff_sanitize(void);
38 changes: 38 additions & 0 deletions test/test_strbuff_revert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ==========================================
// 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_revert_filled_buffer(void)
{
/* Fill buffer1 with random string ending with null terminating char */
rand_string(buffer1.str, strbuff_capacity(&buffer1));

/* Get the size of the random string */
size_t _len = strbuff_len(&buffer1);

/* Copy buffer1 to buffer2 up to this null terminating char*/
memcpy(buffer2.str, buffer1.str, _len + 1);

TEST_ASSERT_EQUAL(_len, strbuff_revert(&buffer1));
TEST_ASSERT_EQUAL(_len, strbuff_revert(&buffer1));
TEST_ASSERT_EQUAL(memcmp(buffer1.str, buffer2.str, _len), 0);
}

static void test_strbuff_revert_full_buffer(void)
{
/* Fill buffer1 with random string not ending with null temrinating char (dummy byte array) */
rand_string_fixed_length(buffer1.str, strbuff_capacity(&buffer1), strbuff_capacity(&buffer1));
*(buffer1.str + strbuff_capacity(&buffer1) - 1) = rand_char(false);

TEST_ASSERT_EQUAL(0, strbuff_revert(&buffer1));
}

void test_strbuff_revert(void)
{
RUN_TEST(test_strbuff_revert_filled_buffer);
RUN_TEST(test_strbuff_revert_full_buffer);
}

0 comments on commit 25ad5b2

Please sign in to comment.