feat(utf-8 string): added utf-8 string structure and implemented initialize and destroy

This commit is contained in:
Mineplay 2025-05-02 08:09:46 -05:00
parent b9e4104468
commit c4db620fb4
3 changed files with 108 additions and 0 deletions

View file

@ -0,0 +1,38 @@
/*
* 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: UTF8String.h
* Description:
* This file contains the UTF8String structure and the functions for modifying it.
* 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 <stddef.h>
#include <stdbool.h>
#include "../Utils/Error.h"
typedef struct {
size_t size, capacity;
unsigned char *character_string;
} FledastyUtf8String;
FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_length);
FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string);

65
Src/Strings/UTF8String.c Normal file
View file

@ -0,0 +1,65 @@
/*
* 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: UTF8String.c
* Description:
* This file contains the functions for modifying the UTF-8 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/UTF8String.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_length) {
if (new_string == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (character_string == NULL || character_string_length == 0) {
new_string->size = 0;
new_string->capacity = 10;
new_string->character_string = hallocy_malloc(new_string->capacity);
} else {
new_string->size = character_string_length;
new_string->capacity = new_string->size + new_string->size;
new_string->character_string = hallocy_malloc(new_string->capacity);
}
new_string->character_string[new_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string) {
if (current_string == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
HallocyError result = hallocy_free(current_string->character_string);
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
current_string->character_string = NULL;
return FLEDASTY_ERROR_NONE;
}

View file

@ -26,6 +26,7 @@
#include <Fledasty/Core/LinkedList.h>
#include <Fledasty/Core/DoublyLinkedList.h>
#include <Fledasty/Core/HashTable.h>
#include <Fledasty/Strings/UTF8String.h>
static inline size_t integer_hash_function(void *key) { return *(size_t*)key; }
@ -221,6 +222,10 @@ int main() {
}
fledasty_hash_table_destroy(&test_hash_table);
FledastyUtf8String test_utf8_string;
fledasty_utf8_string_initialize(&test_utf8_string, "€Testing", 11);
fledasty_utf8_string_destroy(&test_utf8_string);
printf("Done\n");
return 0;
}