Optimize startup, HTTP/SSE streaming, and fix macOS libedit bindings
This commit is contained in:
@@ -12,11 +12,15 @@
|
||||
|
||||
/* ----------------------------- formatting ----------------------------- */
|
||||
static std::string vfmt(const char *fmt, va_list ap) {
|
||||
char stack_buf[512];
|
||||
va_list cp;
|
||||
va_copy(cp, ap);
|
||||
int need = std::vsnprintf(nullptr, 0, fmt, cp);
|
||||
int need = std::vsnprintf(stack_buf, sizeof stack_buf, fmt, cp);
|
||||
va_end(cp);
|
||||
if (need < 0) return {};
|
||||
if ((size_t)need < sizeof stack_buf) {
|
||||
return std::string(stack_buf, (size_t)need);
|
||||
}
|
||||
std::string s;
|
||||
s.resize((size_t)need);
|
||||
std::vsnprintf(s.data(), (size_t)need + 1, fmt, ap);
|
||||
@@ -30,11 +34,22 @@ std::string sfmt(const char *fmt, ...) {
|
||||
return s;
|
||||
}
|
||||
void str_appendf(std::string &s, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
char stack_buf[512];
|
||||
va_list ap, cp;
|
||||
va_start(ap, fmt);
|
||||
std::string more = vfmt(fmt, ap);
|
||||
va_copy(cp, ap);
|
||||
int need = std::vsnprintf(stack_buf, sizeof stack_buf, fmt, cp);
|
||||
va_end(cp);
|
||||
if (need > 0) {
|
||||
if ((size_t)need < sizeof stack_buf) {
|
||||
s.append(stack_buf, (size_t)need);
|
||||
} else {
|
||||
size_t old_sz = s.size();
|
||||
s.resize(old_sz + (size_t)need);
|
||||
std::vsnprintf(s.data() + old_sz, (size_t)need + 1, fmt, ap);
|
||||
}
|
||||
}
|
||||
va_end(ap);
|
||||
s += more;
|
||||
}
|
||||
|
||||
/* ----------------------------- UTF-8 ---------------------------------- */
|
||||
@@ -102,6 +117,19 @@ std::string filter_text(std::string_view s) {
|
||||
const unsigned char *p = (const unsigned char *)s.data();
|
||||
size_t avail = s.size();
|
||||
while (avail > 0) {
|
||||
/* Fast path: scan contiguous printable ASCII plus '\t' and '\n'. */
|
||||
size_t run = 0;
|
||||
while (run < avail) {
|
||||
unsigned char b = p[run];
|
||||
if ((b >= 0x20 && b <= 0x7e) || b == '\n' || b == '\t') run++;
|
||||
else break;
|
||||
}
|
||||
if (run > 0) {
|
||||
out.append((const char *)p, run);
|
||||
p += run;
|
||||
avail -= run;
|
||||
if (avail == 0) break;
|
||||
}
|
||||
size_t pl;
|
||||
int cp = utf8_decode(p, avail, &pl);
|
||||
if (cp == 0xFFFD && pl == 1) { p++; avail--; continue; }
|
||||
@@ -183,10 +211,12 @@ int rune_width(int cp) {
|
||||
void mkdir_p(std::string_view dir) {
|
||||
if (dir.empty()) return;
|
||||
std::string path(dir);
|
||||
struct stat st;
|
||||
if (stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) return;
|
||||
for (size_t i = 1; i < path.size(); i++) {
|
||||
if (path[i] == '/') {
|
||||
path[i] = '\0';
|
||||
mkdir(path.c_str(), 0755);
|
||||
if (stat(path.c_str(), &st) != 0) mkdir(path.c_str(), 0755);
|
||||
path[i] = '/';
|
||||
}
|
||||
}
|
||||
@@ -218,12 +248,15 @@ std::string tool_res_dir(std::string_view sess_id) {
|
||||
return p;
|
||||
}
|
||||
std::string project_id() {
|
||||
char cwd[4096];
|
||||
if (!getcwd(cwd, sizeof cwd)) std::strcpy(cwd, ".");
|
||||
char *abs = realpath(cwd, nullptr);
|
||||
std::string src = abs ? abs : cwd;
|
||||
if (abs) free(abs);
|
||||
return md5_hex(src);
|
||||
static const std::string cached_id = []() {
|
||||
char cwd[4096];
|
||||
if (!getcwd(cwd, sizeof cwd)) std::strcpy(cwd, ".");
|
||||
char *abs = realpath(cwd, nullptr);
|
||||
std::string src = abs ? abs : cwd;
|
||||
if (abs) free(abs);
|
||||
return md5_hex(src);
|
||||
}();
|
||||
return cached_id;
|
||||
}
|
||||
|
||||
/* ----------------------------- MD5 ------------------------------------ */
|
||||
@@ -322,7 +355,11 @@ bool read_file_str(std::string_view path, std::string &out) {
|
||||
std::string p(path);
|
||||
FILE *f = std::fopen(p.c_str(), "rb");
|
||||
if (!f) return false;
|
||||
char buf[4096];
|
||||
struct stat st;
|
||||
if (fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode) && st.st_size > 0) {
|
||||
out.reserve((size_t)st.st_size);
|
||||
}
|
||||
char buf[16384];
|
||||
size_t n;
|
||||
while ((n = std::fread(buf, 1, sizeof buf, f)) > 0) {
|
||||
out.append(buf, n);
|
||||
|
||||
Reference in New Issue
Block a user