#include <stdint.h>
#include "strings.h"
#include "memory.h"


void prepend(uint8_t* array, uint8_t c) {
    // Ensure that size is within bounds and there's space to prepend
    
    int size = sizeof(array);

    // Shift all elements to the right
    for (int i = size; i > 0; i--) {
        array[i] = array[i - 1];
    }

    // Insert the new character at the beginning
    array[0] = c;
}

void append(uint8_t* array, uint8_t c) {
    // Ensure that size is within bounds and there's space to append
    int size = sizeof(array);

    // Insert the new character at the end
    array[size] = c;
}


void append_first(uint8_t* array, uint8_t c) {
    // Shift all elements to the right
    int size = sizeof(array);
    for (int i = size - 1; i > 0; i--) {
        array[i] = array[i - 1];
    }
    
    // Insert the new character at the beginning
    array[0] = c;
}


void append_last(uint8_t* array, uint8_t c) {
    int size = sizeof(array);
    // Shift all elements to the left
    
    for (int i = 0; i < size - 1; i++) {
        array[i] = array[i + 1];
    }
    
    // Insert the new character at the end
    array[0] = c;
}

char** split_string(const char* str, char delimiter) {
    if (str == NULL) {
        return NULL;
    }

    // Count the number of tokens
    int* num_tokens = 1;
    for (const char* p = str; *p; ++p) {
        if (*p == delimiter) {
            (*num_tokens)++;
        }
    }
    
    printf("Tokens: %d", num_tokens);

    // Allocate memory for the array of char*
    char** tokens = (char**)malloc((*num_tokens + 1) * sizeof(char*)); // +1 for NULL terminator
    if (tokens == NULL) {
        return NULL;
    }

    // Allocate memory for each token and split the string
    size_t start = 0;
    size_t end = 0;
    int index = 0;
    while (str[end] != '\0') {
        if (str[end] == delimiter) {
            size_t length = end - start;
            tokens[index] = (char*)malloc((length + 1) * sizeof(char));  // +1 for null terminator
            if (tokens[index] == NULL) {
                // Free previously allocated memory in case of failure
                for (int i = 0; i < index; i++) {
                    free(tokens[i]);
                }
                free(tokens);
                return NULL;
            }
            strncpy(tokens[index], &str[start], length);
            tokens[index][length] = '\0';
            index++;
            start = end + 1;
        }
        end++;
    }

    // Add the last token
    size_t length = end - start;
    tokens[index] = (char*)malloc((length + 1) * sizeof(char));  // +1 for null terminator
    if (tokens[index] == NULL) {
        // Free previously allocated memory in case of failure
        for (int i = 0; i < index; i++) {
            free(tokens[i]);
        }
        free(tokens);
        return NULL;
    }
    strncpy(tokens[index], &str[start], length);
    tokens[index][length] = '\0';
    tokens[index + 1] = NULL;  // Null-terminate the array

    return tokens;
}

// Function to free the memory allocated for the array of strings
void free_split_strings(char** tokens, int num_tokens) {
    if (tokens == NULL) {
        return;
    }
    for (int i = 0; i < num_tokens; i++) {
        free(tokens[i]);
    }
    free(tokens);
}


