Merge pull request 'f16-implement-basic-string' (#26) from f16-implement-basic-string into main
Reviewed-on: #26
This commit is contained in:
commit
fb7bbe00bc
4 changed files with 449 additions and 4 deletions
60
Include/Fledasty/Strings/String.h
Normal file
60
Include/Fledasty/Strings/String.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* 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.h
|
||||
* Description:
|
||||
* This file contains the String 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
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#ifndef FLEDASTY_STRING
|
||||
#define FLEDASTY_STRING
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
typedef struct {
|
||||
size_t size, capacity;
|
||||
char *character_string;
|
||||
} FledastyString;
|
||||
|
||||
FledastyError fledasty_string_free(FledastyString *current_string);
|
||||
|
||||
FledastyError fledasty_string_append(FledastyString *current_string, char *character_string, const size_t character_string_size);
|
||||
FledastyError fledasty_string_insert_at_index(FledastyString *current_string, const size_t index, char *character_string, const size_t character_string_size);
|
||||
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);
|
||||
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);
|
||||
|
||||
FledastyError fledasty_string_pop(FledastyString *current_string);
|
||||
FledastyError fledasty_string_remove(FledastyString *current_string, char *character_string, const size_t character_string_size);
|
||||
FledastyError fledasty_string_remove_range(FledastyString *current_string, const size_t start_index, const size_t end_index);
|
||||
|
||||
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);
|
||||
|
||||
FledastyError fledasty_string_clear(FledastyString *current_string);
|
||||
FledastyError fledasty_string_shrink_to_fit(FledastyString *current_string);
|
||||
|
||||
bool fledasty_string_has_string(const FledastyString *current_string, char *character_string, const size_t character_string_size);
|
||||
static inline bool fledasty_string_is_empty(const FledastyString *current_string) { return current_string == NULL || current_string->size == 0; }
|
||||
|
||||
size_t fledasty_string_get_size(const char *character_string);
|
||||
|
||||
#endif
|
||||
299
Src/Strings/String.c
Normal file
299
Src/Strings/String.c
Normal file
|
|
@ -0,0 +1,299 @@
|
|||
/*
|
||||
* 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(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;
|
||||
}
|
||||
|
||||
bool fledasty_string_has_string(const FledastyString *current_string, char *character_string, const size_t character_string_size) {
|
||||
if (current_string == NULL || character_string == NULL || character_string_size == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
index += 1;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t fledasty_string_get_size(const char *character_string) {
|
||||
size_t size = 0;
|
||||
while (character_string[size] != '\0') {
|
||||
size += 1;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
|
@ -24,7 +24,6 @@
|
|||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include "../../Include/Fledasty/Strings/UTF8String.h"
|
||||
#include "Fledasty/Utils/Error.h"
|
||||
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
|
|
@ -57,6 +56,10 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un
|
|||
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 = (unsigned 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);
|
||||
|
|
@ -83,6 +86,10 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s
|
|||
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 = (unsigned 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_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
|
||||
|
|
@ -103,17 +110,23 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr
|
|||
}
|
||||
|
||||
size_t index = 0;
|
||||
while (index < (current_string->size - before_character_string_size) && !hallocy_compare_memory(current_string->character_string + index, before_character_string, before_character_string_size)) {
|
||||
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 = (unsigned 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_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;
|
||||
}
|
||||
|
||||
|
|
@ -138,6 +151,10 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre
|
|||
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 = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
|
||||
|
||||
if (current_string->character_string == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
index += after_character_string_size;
|
||||
|
|
@ -145,6 +162,8 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
@ -190,6 +209,7 @@ FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, un
|
|||
|
||||
current_string->size -= character_string_size;
|
||||
current_string->character_string[current_string->size] = '\0';
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
|
|
@ -212,6 +232,7 @@ FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_stri
|
|||
|
||||
current_string->size -= end_index - start_index;
|
||||
current_string->character_string[current_string->size] = '\0';
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
|
|
@ -231,6 +252,10 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st
|
|||
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 = (unsigned 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_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));
|
||||
|
|
@ -238,8 +263,10 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st
|
|||
|
||||
current_string->size = new_size;
|
||||
current_string->character_string[current_string->size] = '\0';
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
index += 1;
|
||||
}
|
||||
|
||||
|
|
@ -265,11 +292,18 @@ FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_str
|
|||
unsigned char *previous_string = current_string->character_string;
|
||||
|
||||
current_string->capacity = current_string->size + 1;
|
||||
current_string->character_string = (unsigned char*)hallocy_malloc(current_string->capacity);
|
||||
current_string->character_string = (unsigned char*)hallocy_malloc(current_string->capacity * sizeof(unsigned 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';
|
||||
|
||||
hallocy_free(previous_string);
|
||||
if (hallocy_free(previous_string) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
|
|
@ -306,6 +340,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
|
|||
if (utf8_string.capacity <= string_index) {
|
||||
utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE;
|
||||
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
|
||||
if (utf8_string.character_string == NULL) {
|
||||
return utf8_string;
|
||||
}
|
||||
}
|
||||
|
||||
utf8_string.character_string[string_index] = unicode[index];
|
||||
|
|
@ -314,6 +352,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
|
|||
if (utf8_string.capacity <= string_index + 2) {
|
||||
utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE;
|
||||
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
|
||||
if (utf8_string.character_string == NULL) {
|
||||
return utf8_string;
|
||||
}
|
||||
}
|
||||
|
||||
utf8_string.character_string[string_index] = 0xC0 | ((unicode[index] >> 6) & 0x07);
|
||||
|
|
@ -323,6 +365,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
|
|||
if (utf8_string.capacity <= string_index + 3) {
|
||||
utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE;
|
||||
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
|
||||
if (utf8_string.character_string == NULL) {
|
||||
return utf8_string;
|
||||
}
|
||||
}
|
||||
|
||||
utf8_string.character_string[string_index] = 0xE0 | ((unicode[index] >> 12) & 0x07);
|
||||
|
|
@ -333,6 +379,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
|
|||
if (utf8_string.capacity <= string_index + 4) {
|
||||
utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE;
|
||||
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
|
||||
if (utf8_string.character_string == NULL) {
|
||||
return utf8_string;
|
||||
}
|
||||
}
|
||||
|
||||
utf8_string.character_string[string_index] = 0xF0 | ((unicode[index] >> 18) & 0x07);
|
||||
|
|
@ -361,6 +411,10 @@ uint32_t *fledasty_utf8_string_decode(const FledastyUtf8String *current_string,
|
|||
(*unicode_string_size) = 0;
|
||||
size_t index = 0;
|
||||
uint32_t *unicode_string = (uint32_t*)hallocy_malloc(current_string->size * sizeof(uint32_t));
|
||||
if (unicode_string == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (index < current_string->size) {
|
||||
if ((current_string->character_string[index] & 0xF0) == 0xF0) {
|
||||
unicode_string[*unicode_string_size] = ((current_string->character_string[index] & 0x07) << 18) | ((current_string->character_string[index + 1] & 0x3F) << 12) | ((current_string->character_string[index + 2] & 0x3F) << 6) | (current_string->character_string[index + 3] & 0x3F);
|
||||
|
|
|
|||
32
Tests/Main.c
32
Tests/Main.c
|
|
@ -28,6 +28,7 @@
|
|||
#include <Fledasty/Core/DoublyLinkedList.h>
|
||||
#include <Fledasty/Core/HashTable.h>
|
||||
#include <Fledasty/Strings/UTF8String.h>
|
||||
#include <Fledasty/Strings/String.h>
|
||||
#include <Fledasty/Algorithms/Hashing.h>
|
||||
|
||||
static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; }
|
||||
|
|
@ -281,6 +282,37 @@ int main() {
|
|||
fledasty_utf8_string_free(&encoded_string);
|
||||
|
||||
fledasty_utf8_string_free(&test_utf8_string);
|
||||
|
||||
FledastyString normal_test_string = { 0, 0, NULL };
|
||||
|
||||
fledasty_string_append(&normal_test_string, "Testing", 7);
|
||||
printf("Append: %s\n", normal_test_string.character_string);
|
||||
fledasty_string_insert_at_index(&normal_test_string, normal_test_string.size, "index", 5);
|
||||
printf("Insert at index: %s\n", normal_test_string.character_string);
|
||||
fledasty_string_insert_before_string(&normal_test_string, "nd", 2, "Hello", 5);
|
||||
printf("Insert before: %s\n", normal_test_string.character_string);
|
||||
fledasty_string_insert_after_string(&normal_test_string, "nd", 2, "Bye", 3);
|
||||
printf("Insert after: %s\n", normal_test_string.character_string);
|
||||
fledasty_string_pop(&normal_test_string);
|
||||
printf("Pop: %s\n", normal_test_string.character_string);
|
||||
fledasty_string_remove(&normal_test_string, "Bye", 3);
|
||||
printf("Removed string: %s\n", normal_test_string.character_string);
|
||||
fledasty_string_remove_range(&normal_test_string, 0, 6);
|
||||
printf("Remove range: %s\n", normal_test_string.character_string);
|
||||
fledasty_string_replace_string(&normal_test_string, "Hello", 5, "Goodbye", 7);
|
||||
printf("Replace: %s\n", normal_test_string.character_string);
|
||||
|
||||
fledasty_string_shrink_to_fit(&normal_test_string);
|
||||
if (fledasty_string_has_string(&normal_test_string, "bye", 3)) {
|
||||
printf("String contains bye!\n");
|
||||
}
|
||||
|
||||
fledasty_string_clear(&normal_test_string);
|
||||
if (fledasty_string_is_empty(&normal_test_string)) {
|
||||
printf("String is empty!\n");
|
||||
}
|
||||
|
||||
fledasty_string_free(&normal_test_string);
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue