get rid of most remaining ostd usage

master
Daniel Kolesa 2021-03-20 06:52:10 +01:00
parent 10b2a81cec
commit 68669413cc
4 changed files with 45 additions and 37 deletions

View File

@ -5,6 +5,7 @@
#include <cctype>
#include <cmath>
#include <iterator>
#include <algorithm>
namespace cscript {
@ -560,16 +561,15 @@ OSTD_EXPORT void list_find_item(cs_list_parse_state &ps) {
OSTD_EXPORT cs_strref value_list_concat(
cs_state &cs, std::span<cs_value> vals, std::string_view sep
) {
auto app = ostd::appender<cs_charbuf>(cs);
cs_charbuf buf{cs};
for (std::size_t i = 0; i < vals.size(); ++i) {
switch (vals[i].get_type()) {
case cs_value_type::INT:
case cs_value_type::FLOAT:
case cs_value_type::STRING: {
cs_value v{vals[i]};
for (auto c: v.force_str()) {
app.put(c);
}
auto str = v.force_str();
std::copy(str.begin(), str.end(), std::back_inserter(buf));
break;
}
default:
@ -578,11 +578,9 @@ OSTD_EXPORT cs_strref value_list_concat(
if (i == (vals.size() - 1)) {
break;
}
for (auto c: sep) {
app.put(c);
std::copy(sep.begin(), sep.end(), std::back_inserter(buf));
}
}
return cs_strref{cs, app.get().str()};
return cs_strref{cs, buf.str()};
}
} /* namespace cscript */

View File

@ -5,8 +5,6 @@
#include <unordered_map>
#include <vector>
#include <ostd/string.hh>
namespace cscript {
cs_int cs_parse_int(
@ -265,7 +263,7 @@ struct cs_strman {
*/
char const *find(std::string_view str) const;
/* a quick helper to make a proper ostd string range out of a ptr */
/* a quick helper to make a proper string view out of a ptr */
std::string_view get(char const *ptr) const;
/* this will allocate a buffer of the given length (plus one for

View File

@ -2,6 +2,7 @@
#include "cs_vm.hh"
#include "cs_util.hh"
#include <cstdio>
#include <limits>
namespace cscript {
@ -115,24 +116,25 @@ std::string_view cs_error::save_msg(
cs_gen_state *gs = cs.p_pstate;
if (gs) {
/* we can attach line number */
std::size_t sz = 0;
try {
ostd::char_range r(cs.p_errbuf, cs.p_errbuf + sizeof(cs.p_errbuf));
int sz;
if (!gs->src_name.empty()) {
sz = ostd::format(
ostd::counting_sink(r), "%s:%d: %s", gs->src_name,
gs->current_line, msg
).get_written();
sz = snprintf(
cs.p_errbuf, sizeof(cs.p_errbuf), "%.*s:%zu: %.*s",
int(gs->src_name.size()), gs->src_name.data(),
gs->current_line,
int(msg.size()), msg.data()
);
} else {
sz = ostd::format(
ostd::counting_sink(r), "%d: %s", gs->current_line, msg
).get_written();
sz = snprintf(
cs.p_errbuf, sizeof(cs.p_errbuf), "%zu: %.*s",
gs->current_line, int(msg.size()), msg.data()
);
}
} catch (...) {
memcpy(cs.p_errbuf, msg.data(), msg.size());
sz = msg.size();
if (sz <= 0) {
strncpy(cs.p_errbuf, "format error", sizeof(cs.p_errbuf));
sz = strlen(cs.p_errbuf);
}
return std::string_view{cs.p_errbuf, sz};
return std::string_view{cs.p_errbuf, std::size_t(sz)};
}
memcpy(cs.p_errbuf, msg.data(), msg.size());
return std::string_view{cs.p_errbuf, msg.size()};

View File

@ -131,16 +131,26 @@ void cs_init_lib_string(cs_state &cs) {
});
cs.new_command("tohex", "ii", [](auto &ccs, auto args, auto &res) {
auto r = ostd::appender<cs_charbuf>(ccs);
try {
ostd::format(
r, "0x%.*X", std::max(args[1].get_int(), cs_int(1)),
args[0].get_int()
);
} catch (ostd::format_error const &e) {
throw cs_internal_error{e.what()};
char buf[32];
/* use long long as the largest signed integer type */
auto val = static_cast<long long>(args[0].get_int());
int prec = std::max(int(args[1].get_int()), 1);
int n = snprintf(buf, sizeof(buf), "0x%.*llX", prec, val);
if (n >= int(sizeof(buf))) {
cs_charbuf s{ccs};
s.reserve(n + 1);
s.data()[0] = '\0';
int nn = snprintf(s.data(), n + 1, "0x%.*llX", prec, val);
if ((nn > 0) && (nn <= n)) {
res.set_str(std::string_view{s.data(), std::size_t(nn)});
return;
}
res.set_str(r.get().str());
} else if (n > 0) {
res.set_str(static_cast<char const *>(buf));
return;
}
/* should pretty much be unreachable */
throw cs_error{ccs, "format error"};
});
cs.new_command("substr", "siiN", [](auto &, auto args, auto &res) {