cstring · Section 3
cstring_rfind
NAME
cstring_rfind - find the last occurrence of a substring
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cstring.h> size_t cstring_rfind(cstr s, const char *needle);
DESCRIPTION
This function looks for the last occurrence of needle in the string and returns the zero-based index where it starts. Overlapping occurrences count, so the last "aba" in "ababa" starts at index 2. For a string of 256 bytes or more the search goes from the end of the string toward its start and stops at the first match it meets, so it never reads the bytes to the left of that match. Its time is then linear in the length of the string plus the length of needle, whatever the two hold. A shorter string is searched from its start, because at that length that is faster.
PARAMETERS
s String to search needle Substring to find
RETURN VALUE
Zero-based index of the last occurrence cstring_length(s) if needle is an empty string (the past-the-end position) ccol_invalid_size ((size_t)-1) if needle is not found, or if needle is NULL
NOTES
Asserts if s is NULL A needle of up to 128 bytes needs no memory. With a longer needle the search can take one temporary table (one size_t per byte of needle) from the allocator of s, and it frees that table before the call returns. If the allocator refuses the table the answer is the same, but the time can then grow with the product of the two lengths