/* * 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 #include #include 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(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; } FledastyError fledasty_string_insert_at_index(FledastyString *current_string, const size_t index, 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 (index > current_string->size) { return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; } 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; current_string->character_string = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(char)); if (current_string->character_string == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index); hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size); current_string->size += character_string_size; current_string->character_string[current_string->size] = '\0'; return FLEDASTY_ERROR_NONE; } FledastyError fledasty_string_insert_before_string(FledastyString *current_string, char *before_character_string, const size_t before_character_string_size, char *character_string, const size_t character_string_size) { if (current_string == NULL || before_character_string == NULL || before_character_string_size == 0 || character_string == NULL || character_string_size == 0) { return FLEDASTY_ERROR_INVALID_POINTER; } size_t index = 0; while (index < (current_string->size - before_character_string_size)) { if (hallocy_compare_memory(current_string->character_string + index, before_character_string, before_character_string_size)) { 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(char)); if (current_string->character_string == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index); hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size); current_string->size += character_string_size; current_string->character_string[current_string->size] = '\0'; return FLEDASTY_ERROR_NONE; } index += 1; } return FLEDASTY_ERROR_VALUE_NOT_FOUND; } FledastyError fledasty_string_insert_after_string(FledastyString *current_string, char *after_character_string, const size_t after_character_string_size, char *character_string, const size_t character_string_size) { if (current_string == NULL || after_character_string == NULL || after_character_string_size == 0 || character_string == NULL || character_string_size == 0) { return FLEDASTY_ERROR_INVALID_POINTER; } size_t index = 0; while (index < (current_string->size - after_character_string_size)) { if (hallocy_compare_memory(current_string->character_string + index, after_character_string, after_character_string_size)) { 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(char)); if (current_string->character_string == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } index += after_character_string_size; hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index + 1); hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size); current_string->size += character_string_size; current_string->character_string[current_string->size] = '\0'; return FLEDASTY_ERROR_NONE; } index += 1; } return FLEDASTY_ERROR_VALUE_NOT_FOUND; } FledastyError fledasty_string_pop(FledastyString *current_string) { if (current_string == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } current_string->size -= 1; current_string->character_string[current_string->size] = '\0'; return FLEDASTY_ERROR_NONE; } FledastyError fledasty_string_remove(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; } size_t index = 0; while (index < (current_string->size - character_string_size)) { if (hallocy_compare_memory(current_string->character_string + index, character_string, character_string_size)) { hallocy_move_memory(current_string->character_string + index, current_string->character_string + index + character_string_size, current_string->size - (index + character_string_size)); current_string->size -= character_string_size; current_string->character_string[current_string->size] = '\0'; return FLEDASTY_ERROR_NONE; } index += 1; } return FLEDASTY_ERROR_VALUE_NOT_FOUND; } FledastyError fledasty_string_remove_range(FledastyString *current_string, const size_t start_index, const size_t end_index) { if (current_string == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } if (start_index >= end_index || end_index >= current_string->size) { return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; } hallocy_move_memory(current_string->character_string + start_index, current_string->character_string + end_index, current_string->size - end_index); current_string->size -= end_index - start_index; current_string->character_string[current_string->size] = '\0'; return FLEDASTY_ERROR_NONE; } FledastyError fledasty_string_replace_string(FledastyString *current_string, char *replace_character_string, const size_t replace_character_string_size, char *character_string, const size_t character_string_size) { if (current_string == NULL || replace_character_string == NULL || replace_character_string_size == 0 || character_string == NULL || character_string_size == 0) { return FLEDASTY_ERROR_INVALID_POINTER; } size_t index = 0; while (index < current_string->size - replace_character_string_size) { if (hallocy_compare_memory(current_string->character_string + index, replace_character_string, replace_character_string_size)) { const size_t new_size = current_string->size + (character_string_size - replace_character_string_size); if (current_string->capacity <= new_size) { current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size; current_string->character_string = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(char)); if (current_string->character_string == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } hallocy_move_memory(current_string->character_string + index + character_string_size, current_string->character_string + index + replace_character_string_size, current_string->size - (index + replace_character_string_size)); hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size); current_string->size = new_size; current_string->character_string[current_string->size] = '\0'; return FLEDASTY_ERROR_NONE; } index += 1; } return FLEDASTY_ERROR_VALUE_NOT_FOUND; } FledastyError fledasty_string_clear(FledastyString *current_string) { if (current_string == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } current_string->size = 0; current_string->character_string[0] = '\0'; return FLEDASTY_ERROR_NONE; } FledastyError fledasty_string_shrink_to_fit(FledastyString *current_string) { if (current_string == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } char *previous_string = current_string->character_string; current_string->capacity = current_string->size + 1; current_string->character_string = (char*)hallocy_malloc(current_string->capacity * sizeof(char*)); if (current_string->character_string == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } hallocy_copy_memory(current_string->character_string, previous_string, current_string->size); current_string->character_string[current_string->size] = '\0'; if (hallocy_free(previous_string) != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } return FLEDASTY_ERROR_NONE; }