<commit_before>#include "util.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>

#define BUFSIZE 4096

bool streq(char* a, char* b) {
    return strcmp(a, b) == 0;
}

char* strrev(char* str) {
    int len = strlen(str);
    char* newstr = malloc(len + 1);

    char* start = newstr;
    char* end = str + len - 1;

    for (int i = 0; i < len; i++) {
        *start = *end;
        start++;
        end--;
    }
    return newstr;
}

char* strsubstr(char* str, int start, int end) {
    int len = end - start;
    char* buffer = malloc(len + 1);
    memcpy(buffer, &str[start], len);
    buffer[len] = '\0';
    return buffer;
}

char* get_executable_path() {
    /* TODO: reading /proc is definitely not cross platform */
    char* path = malloc(BUFSIZE);
    if (readlink("/proc/self/exe", path, BUFSIZE) == -1) {
        free(path);
        path = NULL;
    }
    return path;
}

char* get_base_path() {
    // Executable is in 'bin' directory, so we need to go up twice
    char* exe_path = get_executable_path();
    char* base_path = path_join(dirname(exe_path), "../");
    free(exe_path);
    return base_path;
}

char* path_join(char* a, char* b) {
    char* buffer = malloc(BUFSIZE);
    snprintf(buffer, BUFSIZE, "%s/%s", a, b);
    return buffer;<commit_msg>Fix missing null-terminator in util functions<commit_after>#include "util.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>

#define BUFSIZE 4096

bool streq(char* a, char* b) {
    return strcmp(a, b) == 0;
}

char* strrev(char* str) {
    int len = strlen(str);
    char* newstr = malloc(len + 1);

    char* start = newstr;
    char* end = str + len - 1;

    for (int i = 0; i < len; i++) {
        *start = *end;
        start++;
        end--;
    }
    newstr[len] = '\0';
    return newstr;
}

char* strsubstr(char* str, int start, int end) {
    int len = end - start;
    char* buffer = malloc(len + 1);
    memcpy(buffer, &str[start], len);
    buffer[len] = '\0';
    return buffer;
}

char* get_executable_path() {
    /* TODO: reading /proc is definitely not cross platform */
    char* path = malloc(BUFSIZE);

    int len = readlink("/proc/self/exe", path, BUFSIZE);
    if (len == -1) {
        free(path);
        path = NULL;
    } else {
        path[len] = '\0';
    }
    return path;
}

char* get_base_path() {
    // Executable is in 'bin' directory, so we need to go up twice
    char* exe_path = get_executable_path();
    char* base_path = path_join(dirname(exe_path), "../");
    free(exe_path);
    return base_path;
}

char* path_join(char* a, char* b) {
    char* buffer = malloc(BUFSIZE);
    snprintf(buffer, BUFSIZE, "%s/%s", a, b);
    return buffer;
{'fraction_non_alphanumeric': 0.1618978630930822, 'fraction_numerical': 0.008692502716407098, 'mean_word_length': 2.6582781456953644, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5948598', 'n_tokens_mistral': 1048, 'n_tokens_neox': 1007, 'n_words': 288}