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

View File

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

View File

@ -2,6 +2,7 @@
#include "cs_vm.hh" #include "cs_vm.hh"
#include "cs_util.hh" #include "cs_util.hh"
#include <cstdio>
#include <limits> #include <limits>
namespace cscript { namespace cscript {
@ -115,24 +116,25 @@ std::string_view cs_error::save_msg(
cs_gen_state *gs = cs.p_pstate; cs_gen_state *gs = cs.p_pstate;
if (gs) { if (gs) {
/* we can attach line number */ /* we can attach line number */
std::size_t sz = 0; int sz;
try { if (!gs->src_name.empty()) {
ostd::char_range r(cs.p_errbuf, cs.p_errbuf + sizeof(cs.p_errbuf)); sz = snprintf(
if (!gs->src_name.empty()) { cs.p_errbuf, sizeof(cs.p_errbuf), "%.*s:%zu: %.*s",
sz = ostd::format( int(gs->src_name.size()), gs->src_name.data(),
ostd::counting_sink(r), "%s:%d: %s", gs->src_name, gs->current_line,
gs->current_line, msg int(msg.size()), msg.data()
).get_written(); );
} else { } else {
sz = ostd::format( sz = snprintf(
ostd::counting_sink(r), "%d: %s", gs->current_line, msg cs.p_errbuf, sizeof(cs.p_errbuf), "%zu: %.*s",
).get_written(); gs->current_line, int(msg.size()), msg.data()
} );
} catch (...) {
memcpy(cs.p_errbuf, msg.data(), msg.size());
sz = msg.size();
} }
return std::string_view{cs.p_errbuf, sz}; 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, std::size_t(sz)};
} }
memcpy(cs.p_errbuf, msg.data(), msg.size()); memcpy(cs.p_errbuf, msg.data(), msg.size());
return std::string_view{cs.p_errbuf, 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) { cs.new_command("tohex", "ii", [](auto &ccs, auto args, auto &res) {
auto r = ostd::appender<cs_charbuf>(ccs); char buf[32];
try { /* use long long as the largest signed integer type */
ostd::format( auto val = static_cast<long long>(args[0].get_int());
r, "0x%.*X", std::max(args[1].get_int(), cs_int(1)), int prec = std::max(int(args[1].get_int()), 1);
args[0].get_int() int n = snprintf(buf, sizeof(buf), "0x%.*llX", prec, val);
); if (n >= int(sizeof(buf))) {
} catch (ostd::format_error const &e) { cs_charbuf s{ccs};
throw cs_internal_error{e.what()}; 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;
}
} else if (n > 0) {
res.set_str(static_cast<char const *>(buf));
return;
} }
res.set_str(r.get().str()); /* should pretty much be unreachable */
throw cs_error{ccs, "format error"};
}); });
cs.new_command("substr", "siiN", [](auto &, auto args, auto &res) { cs.new_command("substr", "siiN", [](auto &, auto args, auto &res) {