MCPcopy Create free account
hub / github.com/KaisenAmin/c_std / string_replace

Function string_replace

string/std_string.c:1182–1237  ·  view source on GitHub ↗

* @brief Replaces the first occurrence of a substring within the String object with another substring. * * This function searches for the first occurrence of `oldStr` in `str1` and replaces it with `newStr`. * If `oldStr` is not found, the function does nothing and logs a warning. * * @param str1 The String object in which the replacement is to occur. Must not be NULL. * @param oldStr The su

Source from the content-addressed store, hash-verified

1180 return -1;
1181 }
1182 if (pos >= str->size) {
1183 pos = str->size - 1;
1184 }
1185 bool set[256];
1186 sstr_make_set(buffer, set);
1187 const unsigned char* s = (const unsigned char*)str->dataStr;
1188 for (size_t i = pos + 1; i-- > 0; ) {
1189 if (set[s[i]] && i <= (size_t)INT_MAX) {
1190 return (int)i;
1191 }
1192 }
1193 return -1;
1194}
1195
1196
1197/**
1198 * @brief Finds the first character in the string that does not match any character in the buffer starting from the given position.
1199 *
1200 * This function searches the string starting from the specified position to find the first character
1201 * that does not match any character in the buffer. If all characters match, the function returns -1.
1202 *
1203 * @param str The String object to search in. Must not be NULL.
1204 * @param buffer The buffer containing characters to exclude. Must not be NULL.
1205 * @param pos The starting position for the search. Must be within the bounds of the string.
1206 *
1207 * @return The index of the first non-matching character, or -1 if all characters match or an error occurs.
1208 */
1209int string_find_first_not_of(const String *str, const char *buffer, size_t pos) {
1210 if (str == NULL || str->dataStr == NULL || buffer == NULL || pos >= str->size) {
1211 STRING_LOG("[string_find_first_not_of]: Error - invalid arguments.\n");
1212 return -1;
1213 }
1214 bool set[256];
1215 sstr_make_set(buffer, set);
1216 const unsigned char* s = (const unsigned char*)str->dataStr;
1217 for (size_t i = pos; i < str->size && i <= (size_t)INT_MAX; ++i) {
1218 if (!set[s[i]]) {
1219 return (int)i;
1220 }
1221 }
1222 return -1;
1223}
1224
1225
1226/**
1227 * @brief Finds the last position in the string that does not match any character in the buffer.
1228 *
1229 * This function searches from the given position backwards to find the first character in the string
1230 * that does not match any of the characters in the buffer. If no such character is found, the function returns -1.
1231 *
1232 * @param str The String object to search in. Must not be NULL.
1233 * @param buffer The buffer containing characters to exclude. Must not be NULL or empty.
1234 * @param pos The starting position for the search. If the position is larger than the string, it starts from the end.
1235 *
1236 * @return The index of the last character not in the buffer, or -1 if all characters match or an error occurs.
1237 */
1238int string_find_last_not_of(const String *str, const char *buffer, size_t pos) {
1239 if (str == NULL || str->dataStr == NULL || buffer == NULL || str->size == 0) {

Callers 1

string_reverseFunction · 0.85

Calls 1

memory_pool_allocateFunction · 0.85

Tested by

no test coverage detected