eliminate the strref friend kludge
This commit is contained in:
parent
6821260c37
commit
f52aeead32
|
@ -297,8 +297,7 @@ LIBCUBESCRIPT_EXPORT bool cs_code_is_empty(cs_bcode *code);
|
|||
|
||||
struct LIBCUBESCRIPT_EXPORT cs_strref {
|
||||
friend struct cs_value;
|
||||
/* FIXME: eliminate this */
|
||||
friend inline cs_strref cs_make_strref(char const *p, cs_shared_state *cs);
|
||||
friend struct cs_strman;
|
||||
|
||||
cs_strref() = delete;
|
||||
cs_strref(cs_shared_state *cs, std::string_view str);
|
||||
|
|
|
@ -42,7 +42,7 @@ char const *cs_strman::ref(char const *ptr) {
|
|||
return ptr;
|
||||
}
|
||||
|
||||
char const *cs_strman::steal(char *ptr) {
|
||||
cs_strref cs_strman::steal(char *ptr) {
|
||||
auto *ss = get_ref_state(ptr);
|
||||
auto sr = std::string_view{ptr, ss->length};
|
||||
/* much like add(), but we already have memory */
|
||||
|
@ -50,15 +50,14 @@ char const *cs_strman::steal(char *ptr) {
|
|||
if (it != counts.end()) {
|
||||
auto *st = it->second;
|
||||
if (st) {
|
||||
++st->refcount;
|
||||
/* the buffer is superfluous now */
|
||||
cstate->alloc(ss, ss->length + sizeof(cs_strref_state) + 1, 0);
|
||||
return reinterpret_cast<char const *>(st + 1);
|
||||
return cs_strref{reinterpret_cast<char const *>(st + 1), cstate};
|
||||
}
|
||||
}
|
||||
ss->refcount = 1;
|
||||
ss->refcount = 0; /* cs_strref will increment it */
|
||||
counts.emplace(sr, ss);
|
||||
return ptr;
|
||||
return cs_strref{ptr, cstate};
|
||||
}
|
||||
|
||||
void cs_strman::unref(char const *ptr) {
|
||||
|
|
|
@ -58,7 +58,7 @@ struct cs_strman {
|
|||
/* this will use the provided memory, assuming it is a fresh string that
|
||||
* is yet to be added; the memory must be allocated with alloc_buf()
|
||||
*/
|
||||
char const *steal(char *ptr);
|
||||
cs_strref steal(char *ptr);
|
||||
|
||||
/* decrements the reference count and removes it from the system if
|
||||
* that reaches zero; likewise, only safe with pointers that are managed
|
||||
|
|
|
@ -18,13 +18,6 @@ cs_float cs_parse_float(
|
|||
std::string_view input, std::string_view *end = nullptr
|
||||
);
|
||||
|
||||
struct cs_strman;
|
||||
struct cs_shared_state;
|
||||
|
||||
inline cs_strref cs_make_strref(char const *p, cs_shared_state *cs) {
|
||||
return cs_strref{p, cs};
|
||||
}
|
||||
|
||||
} /* namespace cscript */
|
||||
|
||||
#endif /* LIBCUBESCRIPT_CS_UTIL_HH */
|
||||
|
|
|
@ -65,10 +65,7 @@ void cs_init_lib_string(cs_state &cs) {
|
|||
for (std::size_t i = 0; i < inps.size(); ++i) {
|
||||
buf[i] = tolower(inps[i]);
|
||||
}
|
||||
auto const *cbuf = ics->strman->steal(buf);
|
||||
auto sr = cs_make_strref(cbuf, ics);
|
||||
ics->strman->unref(cbuf);
|
||||
res.set_str(sr);
|
||||
res.set_str(ics->strman->steal(buf));
|
||||
});
|
||||
|
||||
cs.new_command("strupper", "s", [](auto &ccs, auto args, auto &res) {
|
||||
|
@ -78,10 +75,7 @@ void cs_init_lib_string(cs_state &cs) {
|
|||
for (std::size_t i = 0; i < inps.size(); ++i) {
|
||||
buf[i] = toupper(inps[i]);
|
||||
}
|
||||
auto const *cbuf = ics->strman->steal(buf);
|
||||
auto sr = cs_make_strref(cbuf, ics);
|
||||
ics->strman->unref(cbuf);
|
||||
res.set_str(sr);
|
||||
res.set_str(ics->strman->steal(buf));
|
||||
});
|
||||
|
||||
cs.new_command("escape", "s", [](auto &ccs, auto args, auto &res) {
|
||||
|
|
Loading…
Reference in a new issue