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

Function string_append

string/std_string.c:937–984  ·  view source on GitHub ↗

* @brief Appends a C-string to the end of the String object. * * This function appends the contents of `strItem` to the end of the String object. * If necessary, it reallocates memory to accommodate the additional characters. * * @param str The String object to which the string will be appended. Must not be NULL. * @param strItem The C-string to append. Must not be NULL. */

Source from the content-addressed store, hash-verified

935 * @param strItem The C-string to append. Must not be NULL.
936 */
937void string_append(String *str, const char *strItem) {
938 STRING_LOG("[string_append]: Appending string '%s' to the String object.\n", strItem);
939
940 if (str == NULL) {
941 STRING_LOG("[string_append]: Error - The String object is NULL.\n");
942 return;
943 }
944 if (str->pool == NULL) {
945 STRING_LOG("[string_append]: Error - The String has no memory pool.\n");
946 return;
947 }
948 if (strItem == NULL) {
949 STRING_LOG("[string_append]: Error - The strItem is NULL.\n");
950 return;
951 }
952 size_t strItemLength = strlen(strItem);
953 if (strItemLength == 0) {
954 STRING_LOG("[string_append]: The strItem is empty, nothing to append.\n");
955 return;
956 }
957
958 MemoryPoolString* oldToFree = NULL;
959 if (str->size + strItemLength >= str->capacitySize) {
960 size_t needed = str->size + strItemLength + 1;
961 size_t newCapacity = str->capacitySize ? str->capacitySize : 32;
962
963 while (newCapacity < needed) {
964 newCapacity *= 2;
965 }
966
967 bool ok;
968 oldToFree = sstr_grow_keep_old(str, newCapacity, &ok);
969 if (!ok) {
970 STRING_LOG("[string_append]: Error - Memory allocation failed.\n");
971 return;
972 }
973 STRING_LOG("[string_append]: Resized the string to new capacity: %zu.\n", newCapacity);
974 }
975
976 memcpy(str->dataStr + str->size, strItem, strItemLength);
977 str->dataStr[str->size + strItemLength] = '\0';
978 str->size += strItemLength;
979 if (oldToFree) {
980 memory_pool_destroy(oldToFree);
981 }
982
983 STRING_LOG("[string_append]: Appended successfully, new size: %zu.\n", str->size);
984}
985
986
987/**

Callers 4

string_concatenateFunction · 0.85
string_joinFunction · 0.85
string_replace_allFunction · 0.85
string_to_hexFunction · 0.85

Calls 2

sstr_grow_keep_oldFunction · 0.85
memory_pool_destroyFunction · 0.85

Tested by

no test coverage detected