<commit_before>// Utility functions

#include "SAMdisk.h"
#include "utils.h"

namespace util
{

std::ofstream log;
LogHelper cout(&std::cout);


std::string fmt(const char* fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    auto len = std::vsnprintf(nullptr, 0, fmt, args);
    va_end(args);

    std::vector<char> bytes(len + 1); // +1 for \0

    va_start(args, fmt);
    std::vsnprintf(&bytes[0], bytes.size(), fmt, args);
    va_end(args);

    return std::string(bytes.data(), len);
}

std::vector<std::string> split(const std::string& str, char delim, bool skip_empty)
{
    std::vector<std::string> items;
    std::stringstream ss(str);
    std::string s;

    while (std::getline(ss, s, delim))
    {
        if (!skip_empty || !s.empty())
            items.emplace_back(std::move(s));
    }

    return items;
}

std::string trim(const std::string& str)
{
    std::string s(str.c_str());
    s.erase(0, s.find_first_not_of(" \t\r\n"));
    s.erase(s.find_last_not_of(" \t\r\n") + 1);
    return s;
}

void bit_reverse(uint8_t* pb, int len)
{
    while (len-- > 0)
    {
        auto& b = *pb++;
        b = ((b & 1) << 7) |
            ((b & 2) << 5) |
            ((b & 4) << 3) |
            ((b & 8) << 1) |
            ((b & 16) >> 1) |
            ((b & 32) >> 3) |
            ((b & 64) >> 5) |
            ((b & 128) >> 7);
    }
}

std::string resource_dir()
{
#ifdef RESOURCE_DIR
    return RESOURCE_DIR;
#elif defined(_WIN32)
    char sz[MAX_PATH];
    if (GetModuleFileName(NULL, sz, arraysize(sz)))
    {
        auto s = std::string(sz);
        s.erase(s.find_last_of('\\') + 1);
        return s;

    }
#endif

    return "";
}

bool is_stdout_a_tty()
{
#ifdef _WIN32
    static bool ret = _isatty(_fileno(stdout)) != 0;
#else
    static bool ret = isatty(fileno(stdout)) != 0;
#endif
    return ret;
}


std::string lowercase(const std::string& str)
{
    std::string ret = str;<commit_msg>Changed Win32 to use EXE location for resources<commit_after>// Utility functions

#include "SAMdisk.h"
#include "utils.h"

namespace util
{

std::ofstream log;
LogHelper cout(&std::cout);


std::string fmt(const char* fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    auto len = std::vsnprintf(nullptr, 0, fmt, args);
    va_end(args);

    std::vector<char> bytes(len + 1); // +1 for \0

    va_start(args, fmt);
    std::vsnprintf(&bytes[0], bytes.size(), fmt, args);
    va_end(args);

    return std::string(bytes.data(), len);
}

std::vector<std::string> split(const std::string& str, char delim, bool skip_empty)
{
    std::vector<std::string> items;
    std::stringstream ss(str);
    std::string s;

    while (std::getline(ss, s, delim))
    {
        if (!skip_empty || !s.empty())
            items.emplace_back(std::move(s));
    }

    return items;
}

std::string trim(const std::string& str)
{
    std::string s(str.c_str());
    s.erase(0, s.find_first_not_of(" \t\r\n"));
    s.erase(s.find_last_not_of(" \t\r\n") + 1);
    return s;
}

void bit_reverse(uint8_t* pb, int len)
{
    while (len-- > 0)
    {
        auto& b = *pb++;
        b = ((b & 1) << 7) |
            ((b & 2) << 5) |
            ((b & 4) << 3) |
            ((b & 8) << 1) |
            ((b & 16) >> 1) |
            ((b & 32) >> 3) |
            ((b & 64) >> 5) |
            ((b & 128) >> 7);
    }
}

std::string resource_dir()
{
#if defined(_WIN32)
    char sz[MAX_PATH];
    if (GetModuleFileName(NULL, sz, arraysize(sz)))
    {
        auto s = std::string(sz);
        s.erase(s.find_last_of('\\') + 1);
        return s;
    }
#elif defined(RESOURCE_DIR)
    return RESOURCE_DIR;
#endif

    return "";
}

bool is_stdout_a_tty()
{
#ifdef _WIN32
    static bool ret = _isatty(_fileno(stdout)) != 0;
#else
    static bool ret = isatty(fileno(stdout)) != 0;
#endif
    return ret;
}


std::string lowercase(const std::string& str)
{
    std::string ret = str;
{'fraction_non_alphanumeric': 0.18677643426807305, 'fraction_numerical': 0.019552354000514537, 'mean_word_length': 2.5058611361587015, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, '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': '1491916', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1469, 'n_words': 338}