Replace nlohmann/json with native C++20 JSON engine, cut build RAM/time, fix 32-bit build, enable -O2 -march=native
This commit is contained in:
+68
-3
@@ -11,22 +11,83 @@
|
||||
* std::map, std::span) instead of raw memory areas.
|
||||
* - Eliminate hand-rolled allocation/string code; rely on RAII so there
|
||||
* is far less room for memory-allocation related vulnerabilities.
|
||||
* - Depend only on libcurl (HTTP) plus the vendored single-header
|
||||
* nlohmann/json library; the unsafe json-c C API is no longer used.
|
||||
* - Depend only on libcurl (HTTP) and GNU readline (interactive line
|
||||
* editing); JSON parsing/serialization and Markdown rendering are
|
||||
* implemented natively in modern C++20 with zero external bloat.
|
||||
*
|
||||
* Feature macros: we rely on POSIX.1-2008 interfaces. */
|
||||
#ifndef BANTAM_HPP
|
||||
#define BANTAM_HPP
|
||||
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Minimal RFC 8259 JSON value, parser and serializer (json.cpp). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
enum class JType { Null, Bool, Int, Float, Str, Arr, Obj };
|
||||
|
||||
struct Json {
|
||||
JType type = JType::Null;
|
||||
bool bval = false;
|
||||
int64_t ival = 0;
|
||||
double dval = 0.0;
|
||||
std::string sval;
|
||||
std::vector<Json> aval;
|
||||
std::vector<std::pair<std::string, Json>> oval;
|
||||
|
||||
Json();
|
||||
~Json();
|
||||
Json(const Json &);
|
||||
Json(Json &&) noexcept;
|
||||
Json &operator=(const Json &);
|
||||
Json &operator=(Json &&) noexcept;
|
||||
|
||||
Json(std::nullptr_t);
|
||||
Json(bool b);
|
||||
Json(int n);
|
||||
Json(int64_t n);
|
||||
Json(double d);
|
||||
Json(const char *s);
|
||||
Json(std::string s);
|
||||
Json(std::string_view s);
|
||||
|
||||
static Json object();
|
||||
static Json array();
|
||||
|
||||
bool is_null() const { return type == JType::Null; }
|
||||
bool is_bool() const { return type == JType::Bool; }
|
||||
bool is_number_integer() const { return type == JType::Int; }
|
||||
bool is_number() const { return type == JType::Int || type == JType::Float; }
|
||||
bool is_string() const { return type == JType::Str; }
|
||||
bool is_array() const { return type == JType::Arr; }
|
||||
bool is_object() const { return type == JType::Obj; }
|
||||
|
||||
bool empty() const;
|
||||
size_t size() const;
|
||||
bool contains(std::string_view k) const;
|
||||
Json &operator[](std::string_view k);
|
||||
const Json &operator[](std::string_view k) const;
|
||||
Json &at(size_t i);
|
||||
const Json &at(size_t i) const;
|
||||
void push_back(Json v);
|
||||
|
||||
std::string str(std::string_view def = "") const;
|
||||
int64_t as_int64(int64_t def = 0) const;
|
||||
int as_int(int def = 0) const;
|
||||
|
||||
std::string dump(int indent = -1) const;
|
||||
static bool parse(std::string_view src, Json &out);
|
||||
};
|
||||
|
||||
/* Markdown renderer state (code-fence tracking). */
|
||||
struct MdState {
|
||||
bool in_code = false;
|
||||
@@ -98,6 +159,8 @@ extern volatile int g_interrupted; /* SIGINT flag */
|
||||
void setup_signal_handler();
|
||||
int is_terminal(int fd);
|
||||
int to_int_val(std::string_view s, int def);
|
||||
bool read_file_str(std::string_view path, std::string &out);
|
||||
bool write_file_str(std::string_view path, std::string_view data);
|
||||
|
||||
std::string sfmt(const char *fmt, ...);
|
||||
void str_appendf(std::string &s, const char *fmt, ...);
|
||||
@@ -133,8 +196,10 @@ std::string tools_dir(const Cfg &c);
|
||||
std::string skills_dir(const Cfg &c);
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* json.cpp — request building & response parsing (nlohmann/json). */
|
||||
/* json.cpp — JSON engine, request building & response parsing. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
void serialize_message_json(Json &obj, const Message &m);
|
||||
void parse_message_json(const Json &obj, Message &m);
|
||||
std::string build_request_json(const Cfg &cfg, const Messages &msgs,
|
||||
bool use_tools, bool stream);
|
||||
bool parse_nonstream_response(std::string_view body, Message &out, Usage &usage);
|
||||
|
||||
Reference in New Issue
Block a user