get rid of some unnecessary string allocs

master
Daniel Kolesa 2021-03-19 00:01:57 +01:00
parent d3ec4a47dd
commit b534a6d9af
2 changed files with 9 additions and 8 deletions

View File

@ -183,7 +183,7 @@ void cs_init_lib_list(cs_state &gcs) {
if (offset > 0) { if (offset > 0) {
list_find_item(p); list_find_item(p);
} }
res.set_str(cs_string{p.input}); res.set_str(p.input);
return; return;
} }
@ -207,7 +207,7 @@ void cs_init_lib_list(cs_state &gcs) {
int n = -1; int n = -1;
for (cs_list_parse_state p{args[1].get_str()}; list_parse(p, cs);) { for (cs_list_parse_state p{args[1].get_str()}; list_parse(p, cs);) {
++n; ++n;
idv.set_str(cs_string{p.item}); idv.set_str(p.item);
idv.push(); idv.push();
if (cs.run_bool(body)) { if (cs.run_bool(body)) {
res.set_int(cs_int(n)); res.set_int(cs_int(n));
@ -226,7 +226,7 @@ void cs_init_lib_list(cs_state &gcs) {
int n = -1; int n = -1;
for (cs_list_parse_state p{args[1].get_str()}; list_parse(p, cs);) { for (cs_list_parse_state p{args[1].get_str()}; list_parse(p, cs);) {
++n; ++n;
idv.set_str(cs_string{p.item}); idv.set_str(p.item);
idv.push(); idv.push();
if (cs.run_bool(body)) { if (cs.run_bool(body)) {
if (list_parse(p, cs)) { if (list_parse(p, cs)) {

View File

@ -52,7 +52,8 @@ void cs_init_lib_string(cs_state &cs) {
}); });
cs.new_command("codestr", "i", [](auto &, auto args, auto &res) { cs.new_command("codestr", "i", [](auto &, auto args, auto &res) {
res.set_str(cs_string(1, char(args[0].get_int()))); char const p[2] = { char(args[0].get_int()), '\0' };
res.set_str(ostd::string_range{static_cast<char const *>(p)});
}); });
cs.new_command("strlower", "s", [](auto &, auto args, auto &res) { cs.new_command("strlower", "s", [](auto &, auto args, auto &res) {
@ -141,11 +142,11 @@ void cs_init_lib_string(cs_state &cs) {
cs_int start = args[1].get_int(), count = args[2].get_int(); cs_int start = args[1].get_int(), count = args[2].get_int();
cs_int numargs = args[3].get_int(); cs_int numargs = args[3].get_int();
cs_int len = cs_int(s.size()), offset = std::clamp(start, cs_int(0), len); cs_int len = cs_int(s.size()), offset = std::clamp(start, cs_int(0), len);
res.set_str(cs_string{ res.set_str(ostd::string_range{
&s[offset], &s[offset],
(numargs >= 3) &s[offset] + ((numargs >= 3)
? size_t(std::clamp(count, cs_int(0), len - offset)) ? size_t(std::clamp(count, cs_int(0), len - offset))
: size_t(len - offset) : size_t(len - offset))
}); });
}); });
@ -181,7 +182,7 @@ void cs_init_lib_string(cs_state &cs) {
} }
cs_string buf; cs_string buf;
if (!oldval.size()) { if (!oldval.size()) {
res.set_str(cs_string{s}); res.set_str(s);
return; return;
} }
for (size_t i = 0;; ++i) { for (size_t i = 0;; ++i) {