Fledasty/Src/Strings/String.c

66 lines
2.6 KiB
C
Raw Normal View History

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* -----------------------------------------------------------------------------
* File: String.c
* Description:
* This file contains the functions for modifying the String. It includes
* functions to append, Insert at index, insert before character,
* insert before string, insert after character, insert after string, replace,
* copy, pop, remove, remove range, clear, check if contains string, check if
* empty.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Strings/String.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
FledastyError fledasty_string_free(FledastyString *current_string) {
if (current_string == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (hallocy_free(current_string->character_string) != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
current_string->character_string = NULL;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_string_append(FledastyString *current_string, char *character_string, const size_t character_string_size) {
if (current_string == NULL || character_string == NULL || character_string_size == 0) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (current_string->capacity <= current_string->size + character_string_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size + 1;
current_string->character_string = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
if (current_string->character_string == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
hallocy_copy_memory(current_string->character_string + current_string->size, character_string, character_string_size);
current_string->size += character_string_size;
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}