* @brief Inserts a substring into the String object at the specified position. * * This function inserts the contents of `strItem` into the String object at the specified position `pos`. * If the new size exceeds the current capacity, the function reallocates memory to accommodate the insertion. * * @param str The String object into which the substring will be inserted. Must not be NULL. * @
| 1090 | * @param buffer The substring to search for. Must not be NULL. |
| 1091 | * @param pos The ending position for the search. Must be within the bounds of the string. |
| 1092 | * |
| 1093 | * @return The index of the last occurrence of the substring, or -1 if not found or an error occurs. |
| 1094 | */ |
| 1095 | int string_rfind(const String *str, const char *buffer, size_t pos) { |
| 1096 | STRING_LOG("[string_rfind]: Searching for last occurrence of substring up to position %zu.\n", pos); |
| 1097 | |
| 1098 | if (str == NULL || str->dataStr == NULL) { |
| 1099 | STRING_LOG("[string_rfind]: Error - The String object or its data is NULL.\n"); |
| 1100 | return -1; |
| 1101 | } |
| 1102 | if (buffer == NULL) { |
| 1103 | STRING_LOG("[string_rfind]: Error - The buffer is NULL.\n"); |
| 1104 | return -1; |
| 1105 | } |
| 1106 | |
| 1107 | size_t bufferLen = strlen(buffer); |
| 1108 | if (bufferLen == 0) { |
| 1109 | STRING_LOG("[string_rfind]: Error - The buffer is empty.\n"); |
| 1110 | return -1; |
| 1111 | } |
| 1112 | if (bufferLen > str->size) { |
| 1113 | STRING_LOG("[string_rfind]: Substring longer than the string.\n"); |
| 1114 | return -1; |
| 1115 | } |
| 1116 | |
| 1117 | /* As std::string::rfind: pos is the LAST allowed start index of a match, |
| 1118 | so rfind("abc", "ab", 0) is 0. (It used to reject pos < len - 1.) */ |
| 1119 | size_t start = str->size - bufferLen; |
| 1120 | if (pos < start) { |
| 1121 | start = pos; |
| 1122 | } |
| 1123 | STRING_LOG("[string_rfind]: Adjusted search position is %zu.\n", start); |
| 1124 | |
| 1125 | for (size_t i = start + 1; i-- > 0; ) { |
| 1126 | if (memcmp(str->dataStr + i, buffer, bufferLen) == 0) { |
| 1127 | STRING_LOG("[string_rfind]: Substring found at position %zu.\n", i); |
| 1128 | return (int)i; |
| 1129 | } |
| 1130 | } |
| 1131 | STRING_LOG("[string_rfind]: Substring not found.\n"); |
| 1132 | return -1; |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | /** |
| 1137 | * @brief Finds the first occurrence of any character from the buffer in the string starting from the given position. |
| 1138 | * |
| 1139 | * This function searches the string starting from the specified position to find the first character |
| 1140 | * that matches any character in the buffer. If no such character is found, the function returns -1. |
nothing calls this directly
no test coverage detected