Skip to content

Commit

Permalink
feat: add string_last_position_of
Browse files Browse the repository at this point in the history
  • Loading branch information
theoludwig committed Dec 26, 2023
1 parent ec6e748 commit 1e0bf99
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,15 @@ size_t string_position_of(const string_t string, const char character) {
}
return position_found;
}

size_t string_last_position_of(const string_t string, const char character) {
size_t position_found = 0;
size_t string_length = string_get_length(string);
while (string_length > 0 && position_found == 0) {
if (string[string_length - 1] == character) {
position_found = string_length;
}
string_length--;
}
return position_found;
}
13 changes: 12 additions & 1 deletion lib/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,20 @@ bool string_ends_with(const string_t string, const string_t prefix);
* @param string
* @param substring
* @return size_t
* @example string_position_of("hello world", "e") // 2
* @example string_position_of("hello world", 'e') // 2
* @since v4.2.0
*/
size_t string_position_of(const string_t string, const char character);

/**
* @brief Returns the position (index + 1) within the string of the last occurrence of the specified substring (0 if not found).
*
* @param string
* @param character
* @return size_t
* @example string_last_position_of("hello world", 'o') // 8
* @since v4.2.0
*/
size_t string_last_position_of(const string_t string, const char character);

#endif
14 changes: 14 additions & 0 deletions test/string_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void string_test() {
string_starts_with_test();
string_ends_with_test();
string_position_of_test();
string_last_position_of_test();
}

void string_get_length_test() {
Expand Down Expand Up @@ -281,6 +282,7 @@ void string_ends_with_test() {

void string_position_of_test() {
assert(string_position_of("hello world", 'e') == 2);
assert(string_position_of("hello world", 'o') == 5);
assert(string_position_of("abcdef", 'a') == 1);
assert(string_position_of("abcdef", 'b') == 2);
assert(string_position_of("abcdef", 'c') == 3);
Expand All @@ -289,3 +291,15 @@ void string_position_of_test() {
assert(string_position_of("abcdef", 'f') == 6);
assert(string_position_of("abcdef", 'g') == 0);
}

void string_last_position_of_test() {
assert(string_last_position_of("hello world", 'e') == 2);
assert(string_last_position_of("hello world", 'o') == 8);
assert(string_last_position_of("abcdef", 'a') == 1);
assert(string_last_position_of("abcdef", 'b') == 2);
assert(string_last_position_of("abcdef", 'c') == 3);
assert(string_last_position_of("abcdef", 'd') == 4);
assert(string_last_position_of("abcdef", 'e') == 5);
assert(string_last_position_of("abcdef", 'f') == 6);
assert(string_last_position_of("abcdef", 'g') == 0);
}
2 changes: 2 additions & 0 deletions test/string_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ void string_ends_with_test();

void string_position_of_test();

void string_last_position_of_test();

#endif

0 comments on commit 1e0bf99

Please sign in to comment.