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

Function string_push_back

string/std_string.c:996–1032  ·  view source on GitHub ↗

* @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. */

Source from the content-addressed store, hash-verified

994 * @param chItem The character to append.
995 */
996void 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/**

Callers 2

string_from_hexFunction · 0.85
string_base64_encodeFunction · 0.85

Calls 2

sstr_grow_keep_oldFunction · 0.85
memory_pool_destroyFunction · 0.85

Tested by

no test coverage detected