#pragma once
#include <string>

namespace xml {

inline std::string escape(const std::string& s) {
    std::string r;
    r.reserve(s.size());
    for (char c : s) {
        switch (c) {
            case '<': r += "&lt;"; break;
            case '>': r += "&gt;"; break;
            case '&': r += "&amp;"; break;
            case '\'': r += "&apos;"; break;
            case '"': r += "&quot;"; break;
            default: r += c; break;
        }
    }
    return r;
}

inline std::string tag(const std::string& name, const std::string& value) {
    return "<" + name + ">" + escape(value) + "</" + name + ">";
}

inline std::string tag(const std::string& name, int64_t value) {
    return "<" + name + ">" + std::to_string(value) + "</" + name + ">";
}

inline std::string tag(const std::string& name, double value) {
    char buf[32];
    std::snprintf(buf, sizeof(buf), "%.6f", value);
    return "<" + name + ">" + buf + "</" + name + ">";
}

inline std::string element(const std::string& name, const std::string& children) {
    return "<" + name + ">" + children + "</" + name + ">";
}

} // namespace xml
