#pragma once

#include <utility>
#include <vector>
#include <string>
#include <algorithm>

inline std::vector<std::string> split_string(const std::string& in, const std::string& delimiter) {
    std::vector<std::string> result{};
    size_t last{0}, next;
    while ((next = in.find(delimiter, last)) != std::string::npos) {
        result.push_back(in.substr(last, next - last));
        last = next + delimiter.size();
    }
    result.push_back(in.substr(last));
    return result;
}

inline std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") {
    str.erase(0, str.find_first_not_of(chars));
    return str;
}

inline std::string ltrim(std::string&& str, const std::string& chars = "\t\n\v\f\r ") {
    str.erase(0, str.find_first_not_of(chars));
    return str;
}

inline std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") {
    str.erase(str.find_last_not_of(chars) + 1);
    return str;
}

inline std::string rtrim(std::string&& str, const std::string& chars = "\t\n\v\f\r ") {
    str.erase(str.find_last_not_of(chars) + 1);
    return str;
}

inline std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ") {
    return ltrim(rtrim(str, chars), chars);
}

inline std::string trim(std::string&& str, const std::string& chars = "\t\n\v\f\r ") {
    return ltrim(rtrim(str, chars), chars);
}

inline std::string& to_upper(std::string& str) {
    std::transform(str.begin(), str.end(), str.begin(),
                   [](unsigned char c) { return std::toupper(c); });
    return str;
}

inline std::string& to_lower(std::string& str) {
    std::transform(str.begin(), str.end(), str.begin(),
                   [](unsigned char c) { return std::tolower(c); });
    return str;
}

inline std::string to_upper(std::string&& str) {
    auto result = str;
    return to_upper(result);
}

inline std::string to_lower(std::string&& str) {
    auto result = str;
    return to_lower(result);
}

inline bool string_contains(const std::string& str, const std::string& needle) {
    return str.find(needle) != std::string::npos;
}

inline std::string remove_spaces(std::string str) {
    str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
    return str;
}

inline bool starts_with(const std::string& line, const std::string& prefix) {
    return line.rfind(prefix, 0) == 0;
}
