* @brief Appends a single character to the end of the String object. * * This function appends the character `chItem` to the end of the String object. * If necessary, it reallocates memory to accommodate the additional character. * * @param str The String object to which the character will be appended. Must not be NULL. * @param chItem The character to append. */
| 994 | * @param chItem The character to append. |
| 995 | */ |
| 996 | void string_push_back(String* str, char chItem) { |
| 997 | STRING_LOG("[string_push_back]: Pushing back character '%c' into the String object.\n", chItem); |
| 998 | |
| 999 | if (str == NULL) { |
| 1000 | STRING_LOG("[string_push_back]: Error - The String object is NULL.\n"); |
| 1001 | return; |
| 1002 | } |
| 1003 | if (str->pool == NULL) { |
| 1004 | STRING_LOG("[string_push_back]: Error - The String has no memory pool.\n"); |
| 1005 | return; |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | MemoryPoolString* oldToFree = NULL; |
| 1010 | if (str->dataStr == NULL || str->size + 1 >= str->capacitySize) { |
| 1011 | size_t newCapacity = str->capacitySize ? str->capacitySize * 2 : 32; |
| 1012 | while (newCapacity < str->size + 2) { |
| 1013 | newCapacity *= 2; |
| 1014 | } |
| 1015 | |
| 1016 | bool ok; |
| 1017 | oldToFree = sstr_grow_keep_old(str, newCapacity, &ok); // grow, reclaiming the old buffer |
| 1018 | if (!ok) { |
| 1019 | STRING_LOG("[string_push_back]: Error - Memory allocation failed.\n"); |
| 1020 | return; |
| 1021 | } |
| 1022 | STRING_LOG("[string_push_back]: Resized the string to new capacity: %zu.\n", newCapacity); |
| 1023 | } |
| 1024 | str->dataStr[str->size] = chItem; |
| 1025 | str->size++; |
| 1026 | str->dataStr[str->size] = '\0'; |
| 1027 | if (oldToFree) { |
| 1028 | memory_pool_destroy(oldToFree); |
| 1029 | } |
| 1030 | |
| 1031 | STRING_LOG("[string_push_back]: Character added successfully, new size: %zu.\n", str->size); |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | /** |
no test coverage detected