Compare commits
2 commits
b5fd577504
...
8714361e85
| Author | SHA1 | Date | |
|---|---|---|---|
| 8714361e85 | |||
| f27562c23e |
4 changed files with 133 additions and 1 deletions
41
Include/Fledasty/Strings/String.h
Normal file
41
Include/Fledasty/Strings/String.h
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
#endif
|
||||||
43
Src/Strings/String.c
Normal file
43
Src/Strings/String.c
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
@ -24,7 +24,6 @@
|
||||||
* -----------------------------------------------------------------------------
|
* -----------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
#include "../../Include/Fledasty/Strings/UTF8String.h"
|
#include "../../Include/Fledasty/Strings/UTF8String.h"
|
||||||
#include "Fledasty/Utils/Error.h"
|
|
||||||
|
|
||||||
#include <Hallocy/Core/Allocator.h>
|
#include <Hallocy/Core/Allocator.h>
|
||||||
#include <Hallocy/Core/Memory.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) {
|
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->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));
|
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);
|
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) {
|
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->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));
|
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_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
|
||||||
|
|
@ -108,6 +115,10 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr
|
||||||
if (current_string->capacity <= current_string->size + 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->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));
|
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_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
|
||||||
|
|
@ -138,6 +149,10 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre
|
||||||
if (current_string->capacity <= current_string->size + 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->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));
|
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;
|
index += after_character_string_size;
|
||||||
|
|
@ -231,6 +246,10 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st
|
||||||
if (current_string->capacity <= new_size) {
|
if (current_string->capacity <= new_size) {
|
||||||
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : 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));
|
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));
|
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));
|
||||||
|
|
@ -266,6 +285,10 @@ FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_str
|
||||||
|
|
||||||
current_string->capacity = current_string->size + 1;
|
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);
|
||||||
|
if (current_string->character_string == NULL) {
|
||||||
|
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||||
|
}
|
||||||
|
|
||||||
hallocy_copy_memory(current_string->character_string, previous_string, current_string->size);
|
hallocy_copy_memory(current_string->character_string, previous_string, current_string->size);
|
||||||
current_string->character_string[current_string->size] = '\0';
|
current_string->character_string[current_string->size] = '\0';
|
||||||
|
|
||||||
|
|
@ -306,6 +329,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
|
||||||
if (utf8_string.capacity <= string_index) {
|
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.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);
|
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];
|
utf8_string.character_string[string_index] = unicode[index];
|
||||||
|
|
@ -314,6 +341,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
|
||||||
if (utf8_string.capacity <= string_index + 2) {
|
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.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);
|
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);
|
utf8_string.character_string[string_index] = 0xC0 | ((unicode[index] >> 6) & 0x07);
|
||||||
|
|
@ -323,6 +354,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
|
||||||
if (utf8_string.capacity <= string_index + 3) {
|
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.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);
|
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);
|
utf8_string.character_string[string_index] = 0xE0 | ((unicode[index] >> 12) & 0x07);
|
||||||
|
|
@ -333,6 +368,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
|
||||||
if (utf8_string.capacity <= string_index + 4) {
|
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.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);
|
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);
|
utf8_string.character_string[string_index] = 0xF0 | ((unicode[index] >> 18) & 0x07);
|
||||||
|
|
@ -361,6 +400,10 @@ uint32_t *fledasty_utf8_string_decode(const FledastyUtf8String *current_string,
|
||||||
(*unicode_string_size) = 0;
|
(*unicode_string_size) = 0;
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
uint32_t *unicode_string = (uint32_t*)hallocy_malloc(current_string->size * sizeof(uint32_t));
|
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) {
|
while (index < current_string->size) {
|
||||||
if ((current_string->character_string[index] & 0xF0) == 0xF0) {
|
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);
|
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);
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@
|
||||||
#include <Fledasty/Core/DoublyLinkedList.h>
|
#include <Fledasty/Core/DoublyLinkedList.h>
|
||||||
#include <Fledasty/Core/HashTable.h>
|
#include <Fledasty/Core/HashTable.h>
|
||||||
#include <Fledasty/Strings/UTF8String.h>
|
#include <Fledasty/Strings/UTF8String.h>
|
||||||
|
#include <Fledasty/Strings/String.h>
|
||||||
#include <Fledasty/Algorithms/Hashing.h>
|
#include <Fledasty/Algorithms/Hashing.h>
|
||||||
|
|
||||||
static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; }
|
static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; }
|
||||||
|
|
@ -281,6 +282,10 @@ int main() {
|
||||||
fledasty_utf8_string_free(&encoded_string);
|
fledasty_utf8_string_free(&encoded_string);
|
||||||
|
|
||||||
fledasty_utf8_string_free(&test_utf8_string);
|
fledasty_utf8_string_free(&test_utf8_string);
|
||||||
|
|
||||||
|
FledastyString normal_test_string = { 0, 0, NULL };
|
||||||
|
|
||||||
|
fledasty_string_free(&normal_test_string);
|
||||||
printf("Done\n");
|
printf("Done\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue