-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |