mirror of
https://github.com/theoludwig/libcproject.git
synced 2025-05-21 23:21:15 +02:00
feat: add string_last_position_of
This commit is contained in:
12
lib/string.c
12
lib/string.c
@@ -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
lib/string.h
13
lib/string.h
@@ -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
|
||||
|
Reference in New Issue
Block a user