#include "wrap-string.hpp"
#include "wrap-vector.hpp"
#include "util.hpp"
#include "fast-float.hpp"
#include "luastack.hpp"

#include "../../ext/unicode-stuff.hpp"

#include <algorithm>
#include <sys/types.h>
#include <sys/stat.h>
#include <iomanip>
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <charconv>

namespace sv {

bool case_insensitive_eq(string_view s1, string_view s2) {
    if (s1.size() != s2.size()) return false;
    for (int i = 0; i < int(s1.size()); i++) {
        char c1 = s1[i];
        char c2 = s2[i];
        if (ascii_isupper(c1)) c1 += 'a'-'A';
        if (ascii_isupper(c2)) c2 += 'a'-'A';
        if (c1 != c2) return false;
    }
    return true;
}

bool valid_int64(string_view value) {
    int64_t result;
    const char *last = value.data() + value.size();
    auto r = std::from_chars(value.data(), last, result, 10);
    if (r.ec != std::errc()) return false;
    if (r.ptr != last) return false;
    return true;
}

bool valid_hex64(string_view value) {
    int64_t result;
    const char *last = value.data() + value.size();
    auto r = std::from_chars(value.data(), last, result, 16);
    if (r.ec != std::errc()) return false;
    if (r.ptr != last) return false;
    return true;
}

bool valid_double(string_view value) {
    double result;
    const char *last = value.data() + value.size();
    auto r = fast_float::from_chars(value.data(), last, result);
    if (r.ec != std::errc()) return false;
    if (r.ptr != last) return false;
    return true;
}

bool valid_hostname(string_view value) {
    bool start_word = true;
    bool last_dash = false;
    for (char c : value) {
        if (c == '.') {
            if (start_word) return false;
            if (last_dash) return false;
            start_word = true;
            last_dash = false;
        } else if (c == '-') {
            if (start_word) return false;
            start_word = false;
            last_dash = true;
        } else if (ascii_isalnum(c)) {
            start_word = false;
            last_dash = false;
        } else {
            return false;
        }
    }
    if (start_word || last_dash) return false;
    return true;
}

int64_t to_int64(string_view value, int64_t errval) {
    int64_t result;
    const char *p = value.data();
    const char *last = p + value.size();
    if ((p < last) && (*p == '+')) p++;
    auto r = std::from_chars(p, last, result, 10);
    if (r.ec != std::errc()) return errval;
    if (r.ptr != last) return errval;
    return result;
}

uint64_t to_hex64(string_view value, uint64_t errval) {
    uint64_t result;
    if (sv::zfront(value) == '-') return errval;
    const char *last = value.data() + value.size();
    auto r = std::from_chars(value.data(), last, result, 16);
    if (r.ec != std::errc()) return errval;
    if (r.ptr != last) return errval;
    return result;
}

double to_double(string_view value, double errval) {
    double result;
    const char *last = value.data() + value.size();
    auto r = fast_float::from_chars(value.data(), last, result);
    if (r.ec != std::errc()) return errval;
    if (r.ptr != last) return errval;
    return result;
}

string_view ltrim(string_view v) {
    while ((!v.empty()) && (ascii_isspace(v.front()))) {
        v.remove_prefix(1);
    }
    return v;
}

string_view rtrim(string_view v) {
    while ((!v.empty()) && (ascii_isspace(v.back()))) {
        v.remove_suffix(1);
    }
    return v;
}

string_view trim(string_view v) {
    while ((!v.empty()) && (ascii_isspace(v.front()))) {
        v.remove_prefix(1);
    }
    while ((!v.empty()) && (ascii_isspace(v.back()))) {
        v.remove_suffix(1);
    }
    return v;
}

string_view ltrim(string_view v, char c) {
    while ((!v.empty()) && (v.front() == c)) {
        v.remove_prefix(1);
    }
    return v;
}

string_view rtrim(string_view v, char c) {
    while ((!v.empty()) && (v.back() == c)) {
        v.remove_suffix(1);
    }
    return v;
}

string_view trim(string_view v, char c) {
    while ((!v.empty()) && (v.front() == c)) {
        v.remove_prefix(1);
    }
    while ((!v.empty()) && (v.back() == c)) {
        v.remove_suffix(1);
    }
    return v;
}

bool has_prefix(string_view s, string_view prefix) {
    return 0 == s.compare(0, prefix.size(), prefix);
}

bool has_suffix(string_view s, string_view suffix) {
    if (s.length() >= suffix.length()) {
        return (0 == s.compare (s.length() - suffix.length(), suffix.length(), suffix));
    } else {
        return false;
    }
}

int common_prefix_length(string_view a, string_view b) {
    int minlen = std::min(a.size(), b.size());
    for (int i = 0; i < minlen; i++) {
        if (a[i] != b[i]) return i;
    }
    return minlen;
}

bool is_lua_id(string_view str) {
    if (str.size() == 0) return false;
    char c=str[0];
    if (!ascii_isualpha(c)) return false;
    for (int i = 1; i < int(str.size()); i++) {
        char c = str[i];
        if (!ascii_isualnum(c)) return false;
    }
    return true;
}

bool is_lua_classname(string_view s) {
    if (s == "_G") return false;
    return is_lua_id(s);
}

bool is_lua_comment(string_view s) {
    int start = 0;
    while ((start < int(s.size())) && ((s[start]==' ') || (s[start]=='\t'))) start++;
    return s.substr(start, 2) == "--";
}

bool is_possible_long_lua_expression(string_view s) {
    read_space(s);
    string_view id = read_lua_identifier(s);
    if (id.empty()) return false;
    if ((id == "function") || (id == "if") || (id == "while") || (id == "for") || (id == "repeat") || (id == "do")) return true;
    if (id == "local")
    {
        read_space(s);
        id = read_lua_identifier(s);
    }
    read_space(s);
    read_prefix(s, "="); // If not present, returns false but we continue anyway.
    read_space(s);
    if (has_prefix(s, "[")) return true;
    if (has_prefix(s, "(")) return true;
    return false;
}

bool is_whitespace(string_view s) {
    for (int i = 0; i < int(s.size()); i++) {
        if (!ascii_isspace(s[i])) {
            return false;
        }
    }
    return true;
}

string_view lua_function_proto_name(string_view s) {
    // Skip any leading whitespace.
    read_space(s);

    // Skip over the word 'function'.  If it's not there, fail.
    if (!read_prefix(s, "function")) return string_view();

    // Skip whitespace.  If there isn't any, fail.
    string_view w = read_space(s);
    if (w.empty()) return string_view();

    // Read the function name
    string_view work = s;
    if (read_lua_identifier(work).empty()) return string_view();
    if (read_prefix(work, ".")) {
        if (read_lua_identifier(work).empty()) return string_view();
    }
    size_t namelen = s.size() - work.size();
    string_view fullname = s.substr(0, namelen);
    s.remove_prefix(namelen);

    // Skip whitespace
    read_space(s);

    // Make sure there's an open parentheses.
    if (!read_prefix(s, "(")) return string_view();

    // This is where we stop parsing.
    return fullname;
}

string_view read_space(string_view &source)
{
    size_t pos = 0;
    while ((pos < source.size()) && ascii_isspace(source[pos])) pos++;
    string_view result = source.substr(0, pos);
    source.remove_prefix(pos);
    return result;
}

string_view read_to_sep(string_view &source, char sep) {
    size_t pos = source.find(sep);
    string_view result;
    if (pos == string_view::npos) {
        result = source;
        source = string_view();
    } else {
        result = source.substr(0, pos);
        source = source.substr(pos + 1);
    }
    return result;
}

string_view read_to_line(string_view &source) {
    size_t pos = source.find('\n');
    string_view result;
    if (pos == string_view::npos) {
        result = source;
        source = string_view();
    } else {
        result = source.substr(0, pos);
        source = source.substr(pos + 1);
    }
    if ((!result.empty()) && (result.back() == '\r')) {
        result.remove_suffix(1);
    }
    return result;
}

bool read_prefix(string_view &source, string_view prefix) {
    if (0 == source.compare(0, prefix.size(), prefix)) {
        source.remove_prefix(prefix.size());
        return true;
    } else {
        return false;
    }
}

string_view read_to_space(string_view &source) {
    size_t pos1 = 0;
    while ((pos1 < source.size()) && (!ascii_isspace(source[pos1]))) {
        pos1 += 1;
    }
    string_view result = source.substr(0, pos1);
    if (pos1 == source.size()) {
        source = string_view();
        return result;
    }
    size_t pos2 = pos1 + 1;
    while ((pos2 < source.size()) && (ascii_isspace(source[pos2]))) {
        pos2 += 1;
    }
    source = source.substr(pos2);
    return result;
}

string_view read_nbytes(string_view &source, int nbytes) {
    if (nbytes < 0) nbytes = 0;
    if (nbytes > int(source.size())) nbytes = source.size();
    string_view result = source.substr(0, nbytes);
    source = source.substr(nbytes);
    return result;
}

string_view read_simple_identifier(string_view &source) {
    size_t len = 0;
    if ((len < source.size()) && (sv::ascii_isalpha(source[len]))) {
        len += 1;
        while ((len < source.size()) && (sv::ascii_isalnum(source[len]))) {
            len += 1;
        }
    }
    string_view result = source.substr(0, len);
    source.remove_prefix(len);
    return result;
}

string_view read_lua_identifier(string_view &source) {
    size_t len = 0;
    if ((len < source.size()) && (sv::ascii_isualpha(source[len]))) {
        len += 1;
        while ((len < source.size()) && (sv::ascii_isualnum(source[len]))) {
            len += 1;
        }
    }
    string_view result = source.substr(0, len);
    source.remove_prefix(len);
    return result;
}

std::string_view read_number(string_view &source, bool plus, bool minus, bool dec, bool exp) {
    const char *p = source.data();
    const char *l = p + source.size();
    if (p == l) return source.substr(0, 0);
    char sign = *p;
    if (sign == '+') {
        if (!plus) return source.substr(0, 0);
        p++;
    }
    if (sign == '-') {
        if (!minus) return source.substr(0, 0);
        p++;
    }
    if (p == l) return source.substr(0, 0);
    bool have_digits = false;
    while ((p < l) && (ascii_isdigit(*p))) {
        have_digits = true;
        p++;
    }
    if ((p < l) && dec && (*p == '.')) {
        p++;
        while ((p < l) && (ascii_isdigit(*p))) {
            have_digits = true;
            p++;
        }
    }
    if (!have_digits) return source.substr(0, 0);
    if ((p < l) && exp && ((*p == 'e')||(*p == 'E'))) {
        p++;
        if ((p < l) && ((*p == '+') || (*p == '-'))) {
            p++;
        }
        bool have_exp = false;
        while ((p < l) && (ascii_isdigit(*p))) {
            have_exp = true;
            p++;
        }
        if (!have_exp) return source.substr(0, 0);
    }
    string_view result = source.substr(0, p - source.data());
    source.remove_prefix(result.size());
    return result;
}

int32_t read_ascii_char(string_view &source) {
    if (source.empty()) return -1;
    int32_t result = source.front();
    source.remove_prefix(1);
    return result;
}

bool valid_number(string_view s, bool plus, bool minus, bool dec, bool exp) {
    read_number(s, plus, minus, dec, exp);
    return s.empty();
}

bool contains_substring_utf8(string_view haystack, string_view needle)
{
    // Case sensitive is easy.  Case insensitive is hard.
    return haystack.find(needle) != std::string::npos;
}

using UC = UnicodeStuff<eng::string, eng::u16string, eng::u32string>;

int32_t read_codepoint_utf8(string_view &source) { return UC::read_codepoint_utf8(source); }
bool valid_utf8(string_view s) { return UC::valid_utf8(s); }

} // namespace sv


namespace util {

eng::string ascii_tolower(std::string_view s) {
    eng::string mod(s);
    for (int i = 0; i < int(mod.size()); i++) {
        if (sv::ascii_isupper(mod[i])) {
            mod[i] += 'a' - 'A';
        }
    }
    return mod;
}

eng::string ascii_toupper(std::string_view s) {
    eng::string mod(s);
    for (int i = 0; i < int(mod.size()); i++) {
        if (sv::ascii_islower(mod[i])) {
            mod[i] += 'A' - 'a';
        }
    }
    return mod;
}

void quote_string(const eng::string &s, std::ostream *os) {
    bool anysq = false;
    bool anydq = false;
    for (char c : s) {
        if (c == '\'') anysq = true;
        if (c == '"') anydq = true;
    }
    bool usesinglequote = (!anysq)||(anydq);
    (*os) << (usesinglequote ? '\'' : '"');
    std::string_view str(s);
    while (!str.empty()) {
        unsigned char c0 = (unsigned char)(str[0]);
        int cp = sv::read_codepoint_utf8(str);
        if (cp < 0) {
            (*os) << "\\" << dec.width(3).fill('0').val(c0);
            str.remove_prefix(1);
        } else if (cp < 32) {
            c0 = ((unsigned char)cp);
            switch (c0) {
            case '\n': (*os) << "\\n"; break;
            case '\t': (*os) << "\\t"; break;
            case '\r': (*os) << "\\r"; break;
            case '\b': (*os) << "\\b"; break;
            default:
                (*os) << "\\" << dec.width(3).fill('0').val(c0);
                break;
            }
        } else if (cp == '"') {
            (*os) << (usesinglequote ? "\"" : "\\\"");
        } else if (cp == '\'') {
            (*os) << (usesinglequote ? "\\'" : "'");
        } else if (cp == '\\') {
            (*os) << "\\\\"; 
        } else {
            write_codepoint_utf8(cp, os);
        }
    }
    (*os) << (usesinglequote ? '\'' : '"');
}

void base64_encode(std::string_view str, std::ostream *oss) {
    const char *encode_tab = 
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    const char *s = str.data();
    size_t size = str.size();
    for (size_t i = 0; i < size; i += 3) {
        uint32_t block = ((unsigned char)(s[i])) << 16;
        if (i + 1 < size) block |= ((unsigned char)(s[i + 1])) << 8;
        if (i + 2 < size) block |= ((unsigned char)(s[i + 2]));
        (*oss) << encode_tab[(block>>18)&0x3F];
        (*oss) << encode_tab[(block>>12)&0x3F];
        (*oss) << ((i + 1 < size) ? encode_tab[(block>>6)&0x3F] : '=');
        (*oss) << ((i + 2 < size) ? encode_tab[(block>>0)&0x3F] : '=');
    }
}

bool base64_decode(std::string_view str, std::ostream *oss) {
    uint32_t chunk = 0;
    int fill = 0;
    int skip = 0;
    bool clean = true;
    for (int i = 0; i < int(str.size()); i++) {
        char c = str[i];
        uint32_t value;

        if      ((c >= 'A') && (c <= 'Z')) value = c - 'A';
        else if ((c >= 'a') && (c <= 'z')) value = c - 'a' + 26;
        else if ((c >= '0') && (c <= '9')) value = c - '0' + 52;
        else if (c == '+') value = 62;
        else if (c == '/') value = 63;
        else if (c == '=') { value = 0; skip ++; }
        else { clean=false; continue; }

        chunk = (chunk << 6) | value;
        fill ++;
        if (fill == 4) {
            oss->put((chunk>>16) & 0xFF);
            if (skip < 2) oss->put((chunk>>8) & 0xFF);
            if (skip < 1) oss->put(chunk & 0xFF);
            chunk = 0; fill = 0; skip = 0;
        }
    }
    if (fill != 0) clean = false;
    return clean;
}

IdVector id_vector_create(int64_t id1, int64_t id2, int64_t id3, int64_t id4) {
    IdVector result;
    if (id1 >= 0) result.push_back(id1);
    if (id2 >= 0) result.push_back(id2);
    if (id3 >= 0) result.push_back(id3);
    if (id4 >= 0) result.push_back(id4);
    return result;
}

void print_id_vector(const IdVector &idv, std::ostream *os) {
    bool first = true;
    for (int64_t id : idv) {
        if (!first) (*os) << ",";
        (*os) << id;
        first = false;
    }
}

void print_id_vector(const std::vector<uint64_t> &idv, std::ostream *os) {
    bool first = true;
    for (int64_t id : idv) {
        if (!first) (*os) << ",";
        (*os) << id;
        first = false;
    }
}
    
eng::string id_vector_debug_string(const IdVector &idv) {
    eng::ostringstream oss;
    print_id_vector(idv, &oss);
    return oss.str();
}

IdVector sort_union_id_vectors(const IdVector &v1, const IdVector &v2) {
    IdVector result(v1.size() + v2.size());
    int next = 0;
    for (int64_t id : v1) result[next++] = id;
    for (int64_t id : v2) result[next++] = id;
    std::sort(result.begin(), result.end());
    int64_t prev = -1;
    int64_t count = 0;
    for (int64_t id : result) {
        if (id != prev) {
            prev = id;
            result[count++] = id;
        }
    }
    result.resize(count);
    return result;
}

HashValue hash_string(std::string_view s) {
    uint64_t hash1 = 0;
    uint64_t hash2 = 0;
    SpookyHash::ChainHash128(s.data(), s.size(), &hash1, &hash2);
    return util::HashValue(hash1, hash2);
}
 
HashValue hash_string(HashValue prev, std::string_view s) {
    uint64_t hash1 = prev.first;
    uint64_t hash2 = prev.second;
    SpookyHash::ChainHash128(s.data(), s.size(), &hash1, &hash2);
    return util::HashValue(hash1, hash2);
}

HashValue hash_id_vector(const IdVector &idv) {
    uint64_t hash1 = 0;
    uint64_t hash2 = 0;
    SpookyHash::ChainHash128(&idv[0], idv.size() * sizeof(int64_t), &hash1, &hash2);
    return util::HashValue(hash1, hash2);
}

eng::string hash_to_hex(const HashValue &hv) {
    eng::ostringstream oss;
    oss << hex64.val(hv.first) << hex64.val(hv.second);
    return oss.str();    
}
static inline uint64_t Rot64(uint64_t x, int k)
{
    return (x << k) | (x >> (64 - k));
}

uint64_t hash_ints(uint64_t a, uint64_t b, uint64_t c, uint64_t d) {
    uint64_t h0 = c ^ 0xc548cebf3714dbb9;
    uint64_t h1 = d ^ 0xd23a7edd44383f8d;
    uint64_t h2 = a ^ 0x7356f92e4b154df7;
    uint64_t h3 = b ^ 0x55ce09295766838d;
    
    h3 ^= h2;  h2 = Rot64(h2,15);  h3 += h2;
    h0 ^= h3;  h3 = Rot64(h3,52);  h0 += h3;
    h1 ^= h0;  h0 = Rot64(h0,26);  h1 += h0;
    h2 ^= h1;  h1 = Rot64(h1,51);  h2 += h1;
    h3 ^= h2;  h2 = Rot64(h2,28);  h3 += h2;
    h0 ^= h3;  h3 = Rot64(h3,9);   h0 += h3;
    h1 ^= h0;  h0 = Rot64(h0,47);  h1 += h0;
    h2 ^= h1;  h1 = Rot64(h1,54);  h2 += h1;
    h3 ^= h2;  h2 = Rot64(h2,32);  h3 += h2;
    h0 ^= h3;  h3 = Rot64(h3,25);  h0 += h3;
    h1 ^= h0;  h0 = Rot64(h0,63);  h1 += h0;

    return h1;
}

double hash_to_double(uint64_t hash) {
    return (hash >> (64-53)) * 0x1p-53;
}

StringVec split(const eng::string &s, char sep) {
    StringVec result;
    int start = 0;
    for (int i = 0; i < int(s.size()); i++) {
        if (s[i] == sep) {
            result.push_back(s.substr(start, i-start));
            start = i+1;
        }
    }
    if (start < int(s.size())) {
        result.push_back(s.substr(start));
    }
    return result;
}

static eng::string substr_nocr(const eng::string &s, int start, int len) {
    if ((len > 0) && (s[start + len - 1] == '\r')) {
        len -= 1;
    }
    return s.substr(start, len);
}

StringVec split_lines(const eng::string &s) {
    StringVec result;
    int start = 0;
    for (int i = 0; i < int(s.size()); i++) {
        if (s[i]=='\n') {
            result.push_back(substr_nocr(s, start, i-start));
            start = i + 1;
        }
    }
    if (start < int(s.size())) {
        result.push_back(substr_nocr(s, start, s.size()-start));
    }
    return result;
}

StringVec split_docstring(const eng::string &s) {
    StringVec result;
    int start = 0;
    for (int i = 0; i < int(s.size()); i++) {
        if (s[i]=='|') {
            int len = i-start;
            if ((len > 0)||(start > 0)) {
                result.push_back(s.substr(start, i-start));
            }
            start = i + 1;
        }
    }
    if (start < int(s.size())) {
        result.push_back(s.substr(start, s.size()-start));
    }
    return result;
}

eng::string join(const StringVec &strs, const eng::string &sep) {
    if (strs.empty()) return "";
    eng::ostringstream oss;
    oss << strs[0];
    for (int i = 1; i < int(strs.size()); i++) {
        oss << sep << strs[i];
    }
    return oss.str();
}

eng::string repeat_string(const eng::string &a, int n) {
    int len = a.size();
    eng::string result(len * n, ' ');
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < len; j++) {
            result[i*len + j] = a[j];
        }
    }
    return result;
}
    
eng::string tolower(eng::string input) {
    for (int i = 0; i < int(input.size()); i++) {
        input[i] = std::tolower(input[i]);
    }
    return input;
}

eng::string toupper(eng::string input) {
    for (int i = 0; i < int(input.size()); i++) {
        input[i] = std::toupper(input[i]);
    }
    return input;
}

static int buffer_codepoint_utf8(char32_t scp, char *buffer) {
    uint32_t cp = (uint32_t)scp;
    unsigned char *c = (unsigned char *)buffer;
    if (cp < 0) {
        return 0;
    }
    else if (cp <= 0x7F) {
        c[0] = cp;
        return 1;
    }
    else if (cp <= 0x7FF) {
        c[0] = (cp>>6)+192;
        c[1] = (cp&63)+128;
        return 2;
    }
    else if (cp <= 0xFFFF) {
        if ((cp >= 0xD800) && (cp <= 0xDFFF)) {
            return 0;
        }
        c[0] = (cp>>12)+224;
        c[1] = ((cp>>6)&63)+128;
        c[2] = (cp&63)+128;
        return 3;
    }
    else if (cp <= 0x10FFFF) {
        c[0] = (cp>>18)+240;
        c[1] = ((cp>>12)&63)+128;
        c[2] = ((cp>>6)&63)+128;
        c[3] = (cp&63)+128;
        return 4;
    } else {
        return 0;
    }
}

eng::string get_codepoint_utf8(uint32_t cp) {
    char buffer[4];
    int len = buffer_codepoint_utf8(cp, buffer);
    return eng::string(buffer, len);
}

bool write_codepoint_utf8(int32_t cp, std::ostream *s) {
    char buffer[4];
    int len = buffer_codepoint_utf8(cp, buffer);
    (*s) << std::string_view(buffer, len);
    return (len > 0);
}

double distance_squared(double x1, double y1, double x2, double y2) {
    double dx = x1 - x2;
    double dy = y1 - y2;
    return dx*dx + dy*dy;
}

LuaSourcePtr make_lua_source(const eng::string &code) {
    LuaSourcePtr result(new LuaSourceVec);
    eng::string fn = "file.lua";
    result->push_back(std::make_pair(fn, code));
    return result;
}



void (*dprint_hook)(const char *oneline, size_t size);

void hook_dprint(void (*func)(const char *oneline, size_t size)) {
    dprint_hook = func;
}

void dprintview(std::string_view view) {
    // Drop the final newline, if any.  We're going
    // to automatically end with a newline and we don't
    // want to double up.
    if ((view.size() > 0) && (view.back() == '\n')) {
        view.remove_suffix(1);
    }

    // Chop it up into lines and call the hook one line
    // at a time.
    const char *buffer = view.data();
    const char *base = buffer;
    for (int i = 0; i < int(view.size()); i++) {
        if ((buffer[i] == '\n') || (buffer[i] == 0)) {
            size_t sz = buffer + i - base;
            if (dprint_hook == nullptr) {
                fwrite(base, 1, sz, stderr);
                fwrite("\n", 1, 1, stderr);
            } else {
                dprint_hook(base, sz);
            }
            base = buffer + i + 1;
        }
    }

    // Output the final line.
    size_t sz = buffer + view.size() - base;
    if (sz > 0) {
        if (dprint_hook == nullptr) {
            fwrite(base, 1, sz, stderr);
            fwrite("\n", 1, 1, stderr);
        } else {
            dprint_hook(base, sz);
        }
    }
    
    // If we're sending to stderr, flush.  If not, then
    // the hook routine is responsible for flushing its own
    // output.
    if (dprint_hook == nullptr) {
        fflush(stderr);
    }
}

void dprintf(const char *format, ...) {
    char buffer[256];
    va_list args;
    va_start (args, format);
    int n = vsnprintf(buffer, 256, format, args);
    if (n <= 255) {
        dprintview(std::string_view(buffer, n));
    } else {
        char *lbuffer = (char *)malloc(n + 1);
        vsnprintf(lbuffer, n+1, format, args);
        dprintview(std::string_view(lbuffer, n));
        free(lbuffer);
    }
}

} // namespace util


static std::string_view read_number_x(const char *p, bool plus, bool minus, bool dec, bool exp) {
    std::string_view source = p;
    return sv::read_number(source, plus, minus, dec, exp);
}

eng::string util::decode_token(uint64_t value) {
    static const char encoding[] =
        "\0_0123456789abcdefghijklmnopqrstuvwxyz";
    uint64_t n = value;
    char buffer[13] = {};
    for (int i = 11; i >= 0; i--) {
        buffer[i] = encoding[n % 38];
        n /= 38;
    }
    return eng::string(buffer);
}

LuaDefine(unittests_util, "", "some unit tests") {
    // Test valid_hostname
    LuaAssert(L, sv::valid_hostname("foo123"));
    LuaAssert(L, !sv::valid_hostname("foo%123"));
    LuaAssert(L, sv::valid_hostname("foo-bar"));
    LuaAssert(L, !sv::valid_hostname("-foo"));
    LuaAssert(L, !sv::valid_hostname("foo-"));
    LuaAssert(L, sv::valid_hostname("foo.bar.baz"));
    LuaAssert(L, sv::valid_hostname("foo-bar.baz"));
    LuaAssert(L, !sv::valid_hostname("foo-bar-.baz"));
    LuaAssert(L, !sv::valid_hostname("foo.-bar-baz"));

    // test str_to_int64, str_to_double
    LuaAssert(L, sv::to_int64("123") == 123);
    LuaAssert(L, sv::to_int64("123.4") == INT64_MAX);
    LuaAssert(L, sv::to_int64("12ab") == INT64_MAX);
    LuaAssert(L, sv::to_int64("") == INT64_MAX);
    LuaAssert(L, sv::to_double("123.5") == 123.5);
    LuaAssert(L, std::isnan(sv::to_double("12ab")));
    LuaAssert(L, std::isnan(sv::to_double("")));
    
    // Test trim, ltrim, rtrim
    LuaAssert(L, sv::ltrim("  foo  ") == "foo  ");
    LuaAssert(L, sv::rtrim("  foo  ") == "  foo");
    LuaAssert(L, sv::trim("  foo  ") == "foo");
    LuaAssert(L, sv::trim("foo") == "foo");
    LuaAssert(L, sv::trim("") == "");
    LuaAssert(L, sv::ltrim("**foo**", '*') == "foo**");
    LuaAssert(L, sv::rtrim("**foo**", '*') == "**foo");
    LuaAssert(L, sv::trim("**foo**", '*') == "foo");
    LuaAssert(L, sv::trim("foo", '*') == "foo");
    LuaAssert(L, sv::trim("", '*') == "");
    
    // Test read_to_line
    std::string_view v = "foo\nbar\r\nbaz";
    
    std::string_view v1 = sv::read_to_line(v);
    LuaAssertStrEq(L, v1, "foo");
    LuaAssertStrEq(L, v, "bar\r\nbaz");

    std::string_view v2 = sv::read_to_line(v);
    LuaAssertStrEq(L, v2, "bar");
    LuaAssertStrEq(L, v, "baz");

    std::string_view v3 = sv::read_to_line(v);
    LuaAssertStrEq(L, v3, "baz");
    LuaAssert(L, sv::isnull(v));

    // Test read_to_space
    v = "foo  bar  baz";

    std::string_view s1 = sv::read_to_space(v);
    LuaAssertStrEq(L, s1, "foo");
    LuaAssertStrEq(L, v, "bar  baz");

    std::string_view s2 = sv::read_to_space(v);
    LuaAssertStrEq(L, s2, "bar");
    LuaAssertStrEq(L, v, "baz");

    std::string_view s3 = sv::read_to_space(v);
    LuaAssertStrEq(L, s3, "baz");
    LuaAssert(L, sv::isnull(v));

    // Test the unioning of ID vectors.
    util::IdVector idv1,idv2;
    idv1.push_back(1);
    idv1.push_back(6);
    idv1.push_back(4);
    idv2.push_back(5);
    idv2.push_back(1);
    idv2.push_back(6);
    util::IdVector joined = util::sort_union_id_vectors(idv1, idv2);
    LuaAssert(L, joined.size() == 4);
    LuaAssert(L, joined[0] == 1);
    LuaAssert(L, joined[1] == 4);
    LuaAssert(L, joined[2] == 5);
    LuaAssert(L, joined[3] == 6);

    // Test the string split routine.
    util::StringVec sv1 = util::split("foo,bar,baz", ',');
    LuaAssert(L, sv1.size() == 3);
    LuaAssert(L, sv1[0] == "foo");
    LuaAssert(L, sv1[1] == "bar");
    LuaAssert(L, sv1[2] == "baz");
    util::StringVec sv2 = util::split(",foo,,bar", ',');
    LuaAssert(L, sv2.size() == 4);
    LuaAssert(L, sv2[0]=="");
    LuaAssert(L, sv2[1]=="foo");
    LuaAssert(L, sv2[2]=="");
    LuaAssert(L, sv2[3]=="bar");

    // Test the split_lines routine.
    util::StringVec sv3 = util::split_lines("foo\n\nbar\r\nbaz\r\n\r\n");
    LuaAssert(L, sv3.size() == 5);
    LuaAssert(L, sv3[0] == "foo");
    LuaAssert(L, sv3[1] == "");
    LuaAssert(L, sv3[2] == "bar");
    LuaAssert(L, sv3[3] == "baz");
    LuaAssert(L, sv3[4] == "");

    // Test the repeat string routine.
    LuaAssertStrEq(L, util::repeat_string("abc", 3), "abcabcabc");

    // test toupper and tolower
    LuaAssert(L, util::toupper("fooBar") == "FOOBAR");
    LuaAssert(L, util::tolower("fooBar") == "foobar");

    // Test distance_squared
    LuaAssert(L, util::distance_squared(1, 1, 5, 4) == 25.0);
    LuaAssert(L, util::distance_squared(5, 4, 1, 1) == 25.0);

    // Test XYZ.
    util::XYZ xyza(3,4,5), xyzb(3,4,5), xyzc(3,4,6);
    LuaAssert(L, xyza.x == 3);
    LuaAssert(L, xyza.y == 4);
    LuaAssert(L, xyza.z == 5);
    LuaAssert(L, xyza == xyzb);
    LuaAssert(L, xyza != xyzc);
    LuaAssert(L, xyza.debug_string() == "(3,4,5)");
    
    // Test hash_to_string
    LuaAssertStrEq(L, util::hash_to_hex(util::HashValue(0x1234,0x789a)),
        "0000000000001234000000000000789a");
    
    // Test hash_to_double
    LuaAssert(L, util::hash_to_double(0x1000000000000000) == 1.0/16.0);
    LuaAssert(L, util::hash_to_double(0x7000000000000000) == 7.0/16.0);
    LuaAssert(L, util::hash_to_double(0xF000000000000000) == 15.0/16.0);

    // Test read_number allowing everything.
    LuaAssert(L, read_number_x("123x", true, true, true, true) == "123");
    LuaAssert(L, read_number_x("123.3x", true, true, true, true) == "123.3");
    LuaAssert(L, read_number_x("123.x", true, true, true, true) == "123.");
    LuaAssert(L, read_number_x("123..x", true, true, true, true) == "123.");
    LuaAssert(L, read_number_x("-123x", true, true, true, true) == "-123");
    LuaAssert(L, read_number_x("+123x", true, true, true, true) == "+123");
    LuaAssert(L, read_number_x("+-123x", true, true, true, true) == "");
    LuaAssert(L, read_number_x("-123.02e05x", true, true, true, true) == "-123.02e05");
    LuaAssert(L, read_number_x("-123e-5x", true, true, true, true) == "-123e-5");
    LuaAssert(L, read_number_x("-123e+5x", true, true, true, true) == "-123e+5");
    LuaAssert(L, read_number_x("-123e+x", true, true, true, true) == "");
    
    // Test read_codepoint_utf8.
    std::string_view str("𝞮ὥπq");
    LuaAssert(L, str.size() == 10);
    LuaAssert(L, sv::read_codepoint_utf8(str) == 0x1D7AE); // 4-byte char
    LuaAssert(L, str.size() == 6);
    LuaAssert(L, sv::read_codepoint_utf8(str) == 0x1F65); // 3-byte char
    LuaAssert(L, str.size() == 3);
    LuaAssert(L, sv::read_codepoint_utf8(str) == 0x3C0); // 2-byte char
    LuaAssert(L, str.size() == 1);
    LuaAssert(L, sv::read_codepoint_utf8(str) == 0x71); // 1-byte char
    LuaAssert(L, str.size() == 0);
    LuaAssert(L, sv::read_codepoint_utf8(str) == -1); // EOF

    // Test read_codepoint_utf8 on an invalid unicode sequence.
    std::string_view strbad("\xC0\xC0");
    LuaAssert(L, sv::read_codepoint_utf8(strbad) == -2);

    return 0;
}

