diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2014-08-12 10:45:18 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2014-08-25 17:58:12 -0700 |
commit | 749fec734c4583153fb2dbc80f1d21db2c2ab457 (patch) | |
tree | 7628de9c7426008d06e78ad05b05949b4bb55c5b | |
parent | 31e906c1fbaf3bc9128138302d8db549e87769a7 (diff) | |
download | tmwa-749fec734c4583153fb2dbc80f1d21db2c2ab457.tar.gz tmwa-749fec734c4583153fb2dbc80f1d21db2c2ab457.tar.bz2 tmwa-749fec734c4583153fb2dbc80f1d21db2c2ab457.tar.xz tmwa-749fec734c4583153fb2dbc80f1d21db2c2ab457.zip |
Kill those dumb strings
-rw-r--r-- | src/generic/dumb_ptr.hpp | 82 | ||||
-rw-r--r-- | src/generic/fwd.hpp | 1 | ||||
-rw-r--r-- | src/map/magic-expr.cpp | 36 | ||||
-rw-r--r-- | src/map/magic-interpreter-base.cpp | 4 | ||||
-rw-r--r-- | src/map/magic-interpreter.hpp | 2 | ||||
-rw-r--r-- | src/map/magic-v2.cpp | 2 | ||||
-rw-r--r-- | src/map/script.cpp | 117 | ||||
-rw-r--r-- | src/map/script.hpp | 2 | ||||
-rw-r--r-- | src/shared/lib.cpp | 2 |
9 files changed, 72 insertions, 176 deletions
diff --git a/src/generic/dumb_ptr.hpp b/src/generic/dumb_ptr.hpp index 39c8c8b..72247d5 100644 --- a/src/generic/dumb_ptr.hpp +++ b/src/generic/dumb_ptr.hpp @@ -20,15 +20,9 @@ #include "fwd.hpp" -#include <cstring> - #include <algorithm> #include <utility> -#include "../strings/astring.hpp" -#include "../strings/zstring.hpp" -#include "../strings/xstring.hpp" - namespace tmwa { @@ -184,80 +178,4 @@ public: return !(l == r); } }; - -struct dumb_string -{ - dumb_ptr<char[]> impl; - - dumb_string() - : impl() - {} - dumb_string(char *) = delete; - // copy ctor, copy assign, and dtor are all default - - static dumb_string copy(const char *b, const char *e) - { - dumb_string rv; - rv.impl.new_((e - b) + 1); - std::copy(b, e, &rv.impl[0]); - return rv; - } - static dumb_string copys(XString s) - { - return dumb_string::copy(&*s.begin(), &*s.end()); - } - static -#ifndef __clang__ - __attribute__((warning("shouldn't use this - slice instead"))) -#endif - dumb_string copyn(const char *sn, size_t n) - { - return dumb_string::copy(sn, sn + strnlen(sn, n)); - } - - dumb_string dup() const - { - return dumb_string::copy(&impl[0], &impl[0] + impl.size()); - } - void delete_() - { - impl.delete_(); - } - - const char *c_str() const - { - return &impl[0]; - } - - operator ZString() const - { - return ZString(strings::really_construct_from_a_pointer, c_str(), nullptr); - } - - AString str() const - { - return ZString(*this); - } - - char& operator[](size_t i) const - { - return impl[i]; - } - - explicit - operator bool() const - { - return bool(impl); - } - bool operator !() const - { - return !impl; - } -}; - -inline -const char *convert_for_printf(dumb_string ds) -{ - return ds.c_str(); -} } // namespace tmwa diff --git a/src/generic/fwd.hpp b/src/generic/fwd.hpp index 81c4e26..3215903 100644 --- a/src/generic/fwd.hpp +++ b/src/generic/fwd.hpp @@ -26,5 +26,4 @@ namespace tmwa // meh, add more when I feel like it template<class T> class dumb_ptr; -class dumb_string; } // namespace tmwa diff --git a/src/map/magic-expr.cpp b/src/map/magic-expr.cpp index 306126e..a453c05 100644 --- a/src/map/magic-expr.cpp +++ b/src/map/magic-expr.cpp @@ -120,7 +120,7 @@ void magic_copy_var(val_t *dest, const val_t *src) } CASE (const ValString&, s) { - *dest = ValString{s.v_string.dup()}; + *dest = ValString{s.v_string}; } CASE (const ValEntityInt&, s) { @@ -167,7 +167,7 @@ void magic_clear_var(val_t *v) { CASE (ValString&, s) { - s.v_string.delete_(); + (void)s; } CASE (const ValArea&, a) { @@ -271,7 +271,7 @@ void stringify(val_t *v) } } - *v = ValString{dumb_string::copys(buf)}; + *v = ValString{buf}; } static @@ -368,7 +368,7 @@ int fun_add(dumb_ptr<env_t>, val_t *result, Slice<val_t> args) MString m; m += ARGSTR(0); m += ARGSTR(1); - *result = ValString{dumb_string::copys(AString(m))}; + *result = ValString{AString(m)}; } return 0; } @@ -785,17 +785,17 @@ int fun_name_of(dumb_ptr<env_t>, val_t *result, Slice<val_t> args) { if (args[0].is<ValEntityPtr>()) { - *result = ValString{dumb_string::copys(show_entity(ARGENTITY(0)))}; + *result = ValString{show_entity(ARGENTITY(0))}; return 0; } else if (args[0].is<ValSpell>()) { - *result = ValString{dumb_string::copys(ARGSPELL(0)->name)}; + *result = ValString{ARGSPELL(0)->name}; return 0; } else if (args[0].is<ValInvocationPtr>()) { - *result = ValString{dumb_string::copys(ARGINVOCATION(0)->spell->name)}; + *result = ValString{ARGINVOCATION(0)->spell->name}; return 0; } return 1; @@ -1195,7 +1195,7 @@ int fun_read_script_str(dumb_ptr<env_t>, val_t *result, Slice<val_t> args) if (subject_p->bl_type != BL::PC) return 1; - *result = ValString{dumb_string::copys(get_script_var_s(subject_p->is_player(), var_name, array_index))}; + *result = ValString{get_script_var_s(subject_p->is_player(), var_name, array_index)}; return 0; } @@ -1281,8 +1281,7 @@ int fun_strlen(dumb_ptr<env_t>, val_t *result, Slice<val_t> args) static int fun_substr(dumb_ptr<env_t>, val_t *result, Slice<val_t> args) { - const char *src = ARGSTR(0).c_str(); - const int slen = strlen(src); + RString src = ARGSTR(0); int offset = ARGINT(1); int len = ARGINT(2); @@ -1291,15 +1290,15 @@ int fun_substr(dumb_ptr<env_t>, val_t *result, Slice<val_t> args) if (offset < 0) offset = 0; - if (offset > slen) - offset = slen; + if (offset > src.size()) + offset = src.size(); - if (offset + len > slen) - len = slen - offset; + if (offset + len > src.size()) + len = src.size() - offset; - const char *begin = src + offset; - const char *end = begin + len; - *result = ValString{dumb_string::copy(begin, end)}; + auto begin = src.begin() + offset; + auto end = begin + len; + *result = ValString{RString(begin, end)}; return 0; } @@ -1863,10 +1862,9 @@ AString magic_eval_str(dumb_ptr<env_t> env, dumb_ptr<expr_t> expr) if (result.is<ValFail>() || result.is<ValUndef>()) return "?"_s; - // is this a memory leak? stringify(&result); - return result.get_if<ValString>()->v_string.str(); + return result.get_if<ValString>()->v_string; } } // namespace magic } // namespace tmwa diff --git a/src/map/magic-interpreter-base.cpp b/src/map/magic-interpreter-base.cpp index 1ac391a..be9a61a 100644 --- a/src/map/magic-interpreter-base.cpp +++ b/src/map/magic-interpreter-base.cpp @@ -55,7 +55,7 @@ void set_dir(val_t *v, DIR d) static -void set_string(val_t *v, dumb_string x) +void set_string(val_t *v, RString x) { *v = ValString{x}; } @@ -164,7 +164,7 @@ dumb_ptr<env_t> spell_create_env(magic_conf_t *conf, dumb_ptr<spell_t> spell, { case SPELLARG::STRING: - set_string(&env->varu[spell->arg], dumb_string::copys(param)); + set_string(&env->varu[spell->arg], param); break; case SPELLARG::PC: diff --git a/src/map/magic-interpreter.hpp b/src/map/magic-interpreter.hpp index 01775b3..526617f 100644 --- a/src/map/magic-interpreter.hpp +++ b/src/map/magic-interpreter.hpp @@ -106,7 +106,7 @@ struct ValDir }; struct ValString { - dumb_string v_string; + RString v_string; }; struct ValEntityInt { diff --git a/src/map/magic-v2.cpp b/src/map/magic-v2.cpp index 73b7534..5b375b2 100644 --- a/src/map/magic-v2.cpp +++ b/src/map/magic-v2.cpp @@ -415,7 +415,7 @@ namespace magic_v2 case sexpr::STRING: { val_t val; - val = ValString{dumb_string::copys(x._str)}; + val = ValString{x._str}; out = dumb_ptr<expr_t>::make(std::move(val)); return true; diff --git a/src/map/script.cpp b/src/map/script.cpp index f890adf..c099339 100644 --- a/src/map/script.cpp +++ b/src/map/script.cpp @@ -1022,24 +1022,23 @@ void get_val(dumb_ptr<map_session_data> sd, struct script_data *data) } if (postfix == '$') { - dumb_string str; + RString str; if (prefix == '@') { if (sd) - str = dumb_string::copys(pc_readregstr(sd, u.reg)); + str = pc_readregstr(sd, u.reg); } else if (prefix == '$') { RString *s = mapregstr_db.search(u.reg); - str = s ? dumb_string::copys(*s) : dumb_string(); + if (s) + str = *s; } else { PRINTF("script: get_val: illegal scope string variable.\n"_fmt); - str = dumb_string::copys("!!ERROR!!"_s); + str = "!!ERROR!!"_s; } - if (!str) - str = dumb_string::copys(""_s); *data = ScriptDataStr{str}; } else @@ -1119,14 +1118,14 @@ void set_reg(dumb_ptr<map_session_data> sd, VariableCode type, SIR reg, struct s if (postfix == '$') { - dumb_string str = vd.get_if<ScriptDataStr>()->str; + RString str = vd.get_if<ScriptDataStr>()->str; if (prefix == '@') { - pc_setregstr(sd, reg, str.str()); + pc_setregstr(sd, reg, str); } else if (prefix == '$') { - mapreg_setregstr(reg, str.str()); + mapreg_setregstr(reg, str); } else { @@ -1166,7 +1165,7 @@ void set_reg(dumb_ptr<map_session_data> sd, VariableCode type, SIR reg, int id) } static -void set_reg(dumb_ptr<map_session_data> sd, VariableCode type, SIR reg, dumb_string zd) +void set_reg(dumb_ptr<map_session_data> sd, VariableCode type, SIR reg, RString zd) { struct script_data vd = ScriptDataStr{zd}; set_reg(sd, type, reg, vd); @@ -1177,14 +1176,14 @@ void set_reg(dumb_ptr<map_session_data> sd, VariableCode type, SIR reg, dumb_str *------------------------------------------ */ static __attribute__((warn_unused_result)) -dumb_string conv_str(ScriptState *st, struct script_data *data) +RString conv_str(ScriptState *st, struct script_data *data) { get_val(st, data); assert (!data->is<ScriptDataRetInfo>()); if (auto *u = data->get_if<ScriptDataInt>()) { AString buf = STRPRINTF("%d"_fmt, u->numi); - *data = ScriptDataStr{dumb_string::copys(buf)}; + *data = ScriptDataStr{buf}; } return data->get_if<ScriptDataStr>()->str; } @@ -1200,9 +1199,8 @@ int conv_num(ScriptState *st, struct script_data *data) assert (!data->is<ScriptDataRetInfo>()); if (auto *u = data->get_if<ScriptDataStr>()) { - dumb_string p = u->str; + RString p = u->str; *data = ScriptDataInt{atoi(p.c_str())}; - p.delete_(); } // TODO see if I also need to return for other types? return data->get_if<ScriptDataInt>()->numi; @@ -1248,7 +1246,7 @@ void push_script(struct script_stack *stack, const ScriptBuffer *code) template<class T> static -void push_str(struct script_stack *stack, dumb_string str) +void push_str(struct script_stack *stack, RString str) { static_assert(first_type_is_any<T, ScriptDataStr>(), "not str type"); @@ -1260,19 +1258,12 @@ static void push_copy(struct script_stack *stack, int pos_) { script_data csd = stack->stack_datav[pos_]; - if (auto *u = csd.get_if<ScriptDataStr>()) - u->str = u->str.dup(); stack->stack_datav.push_back(csd); } static void pop_stack(struct script_stack *stack, int start, int end) { - for (int i = start; i < end; i++) - { - if (auto *u = stack->stack_datav[i].get_if<ScriptDataStr>()) - u->str.delete_(); - } auto it = stack->stack_datav.begin(); stack->stack_datav.erase(it + start, it + end); } @@ -1291,8 +1282,8 @@ void pop_stack(struct script_stack *stack, int start, int end) static void builtin_mes(ScriptState *st) { - dumb_string mes = conv_str(st, &AARGO2(2)); - clif_scriptmes(script_rid2sd(st), st->oid, ZString(mes)); + RString mes = conv_str(st, &AARGO2(2)); + clif_scriptmes(script_rid2sd(st), st->oid, mes); } /*========================================== @@ -1320,8 +1311,8 @@ void builtin_goto(ScriptState *st) static void builtin_callfunc(ScriptState *st) { - dumb_string str = conv_str(st, &AARGO2(2)); - const ScriptBuffer *scr = userfunc_db.get(str.str()); + RString str = conv_str(st, &AARGO2(2)); + const ScriptBuffer *scr = userfunc_db.get(str); if (scr) { @@ -1436,10 +1427,10 @@ void builtin_menu(ScriptState *st) MString buf; for (int i = st->start + 2; i < st->end; i += 2) { - dumb_string choice_str = conv_str(st, &AARGO2(i - st->start)); - if (!choice_str[0]) + RString choice_str = conv_str(st, &AARGO2(i - st->start)); + if (!choice_str) break; - buf += ZString(choice_str); + buf += choice_str; buf += ':'; } @@ -1653,7 +1644,7 @@ void builtin_input(ScriptState *st) sd->state.menu_or_input = 0; if (postfix == '$') { - set_reg(sd, VariableCode::VARIABLE, reg, dumb_string::copys(sd->npc_str)); + set_reg(sd, VariableCode::VARIABLE, reg, sd->npc_str); } else { @@ -1736,7 +1727,7 @@ void builtin_set(ScriptState *st) if (postfix == '$') { // 文字列 - dumb_string str = conv_str(st, &AARGO2(3)); + RString str = conv_str(st, &AARGO2(3)); set_reg(sd, VariableCode::VARIABLE, reg, str); } else @@ -2204,14 +2195,14 @@ void builtin_getcharid(ScriptState *st) *------------------------------------------ */ static -dumb_string builtin_getpartyname_sub(PartyId party_id) +RString builtin_getpartyname_sub(PartyId party_id) { PartyPair p = party_search(party_id); if (p) - return dumb_string::copys(p->name); + return p->name; - return dumb_string(); + return RString(); } /*========================================== @@ -2228,21 +2219,21 @@ void builtin_strcharinfo(ScriptState *st) num = conv_num(st, &AARGO2(2)); if (num == 0) { - dumb_string buf = dumb_string::copys(sd->status_key.name.to__actual()); + RString buf = sd->status_key.name.to__actual(); push_str<ScriptDataStr>(st->stack, buf); } if (num == 1) { - dumb_string buf = builtin_getpartyname_sub(sd->status.party_id); + RString buf = builtin_getpartyname_sub(sd->status.party_id); if (buf) push_str<ScriptDataStr>(st->stack, buf); else - push_str<ScriptDataStr>(st->stack, dumb_string::copys(""_s)); + push_str<ScriptDataStr>(st->stack, ""_s); } if (num == 2) { // was: guild name - push_str<ScriptDataStr>(st->stack, dumb_string::copys(""_s)); + push_str<ScriptDataStr>(st->stack, ""_s); } } @@ -2326,7 +2317,7 @@ void builtin_getequipname(ScriptState *st) { buf = STRPRINTF("%s-[%s]"_fmt, pos_str[num - 1], pos_str[10]); } - push_str<ScriptDataStr>(st->stack, dumb_string::copys(buf)); + push_str<ScriptDataStr>(st->stack, buf); } @@ -3158,7 +3149,7 @@ void builtin_sc_check(ScriptState *st) static void builtin_debugmes(ScriptState *st) { - dumb_string mes = conv_str(st, &AARGO2(2)); + RString mes = conv_str(st, &AARGO2(2)); PRINTF("script debug : %d %d : %s\n"_fmt, st->rid, st->oid, mes); } @@ -3467,11 +3458,11 @@ void builtin_getitemname(ScriptState *st) i_data = itemdb_search(item_id); } - dumb_string item_name; + RString item_name; if (i_data) - item_name = dumb_string::copys(i_data->jname); + item_name = i_data->jname; else - item_name = dumb_string::copys("Unknown Item"_s); + item_name = "Unknown Item"_s; push_str<ScriptDataStr>(st->stack, item_name); } @@ -3479,13 +3470,13 @@ void builtin_getitemname(ScriptState *st) static void builtin_getspellinvocation(ScriptState *st) { - dumb_string name = conv_str(st, &AARGO2(2)); + RString name = conv_str(st, &AARGO2(2)); - AString invocation = magic::magic_find_invocation(name.str()); + AString invocation = magic::magic_find_invocation(name); if (!invocation) invocation = "..."_s; - push_str<ScriptDataStr>(st->stack, dumb_string::copys(invocation)); + push_str<ScriptDataStr>(st->stack, invocation); } static @@ -3758,7 +3749,7 @@ void builtin_gmcommand(ScriptState *st) dumb_ptr<map_session_data> sd; sd = script_rid2sd(st); - dumb_string cmd = conv_str(st, &AARGO2(2)); + RString cmd = conv_str(st, &AARGO2(2)); is_atcommand(sd->sess, sd, cmd, GmLevel::from(-1U)); @@ -3831,11 +3822,11 @@ static void builtin_npctalk(ScriptState *st) { dumb_ptr<npc_data> nd = map_id_is_npc(st->oid); - dumb_string str = conv_str(st, &AARGO2(2)); + RString str = conv_str(st, &AARGO2(2)); if (nd) { - clif_message(nd, XString(str)); + clif_message(nd, str); } } @@ -3903,7 +3894,7 @@ void builtin_getsavepoint(ScriptState *st) { case 0: { - dumb_string mapname = dumb_string::copys(sd->status.save_point.map_); + RString mapname = sd->status.save_point.map_; push_str<ScriptDataStr>(st->stack, mapname); } break; @@ -4068,7 +4059,7 @@ void builtin_getmap(ScriptState *st) { dumb_ptr<map_session_data> sd = script_rid2sd(st); - push_str<ScriptDataStr>(st->stack, dumb_string::copys(sd->bl_m->name_)); + push_str<ScriptDataStr>(st->stack, sd->bl_m->name_); } static @@ -4160,16 +4151,12 @@ void op_add(ScriptState *st) } else { - dumb_string sb = conv_str(st, &back); - dumb_string sb1 = conv_str(st, &back1); + RString sb = conv_str(st, &back); + RString sb1 = conv_str(st, &back1); MString buf; - buf += ZString(sb1); - buf += ZString(sb); - if (auto *u = back1.get_if<ScriptDataStr>()) - u->str.delete_(); - if (auto *u = back.get_if<ScriptDataStr>()) - u->str.delete_(); - back1 = ScriptDataStr{.str= dumb_string::copys(AString(buf))}; + buf += sb1; + buf += sb; + back1 = ScriptDataStr{.str= AString(buf)}; } } @@ -4178,10 +4165,8 @@ void op_add(ScriptState *st) *------------------------------------------ */ static -void op_2str(ScriptState *st, ByteCode op, dumb_string s1_, dumb_string s2_) +void op_2str(ScriptState *st, ByteCode op, ZString s1, ZString s2) { - ZString s1 = ZString(s1_); - ZString s2 = ZString(s2_); int a = 0; switch (op) @@ -4295,8 +4280,6 @@ void op_2(ScriptState *st, ByteCode op) { // ss => op_2str op_2str(st, op, d1.get_if<ScriptDataStr>()->str, d2.get_if<ScriptDataStr>()->str); - d1.get_if<ScriptDataStr>()->str.delete_(); - d2.get_if<ScriptDataStr>()->str.delete_(); } else if (!(isstr(d1) || isstr(d2))) { @@ -4653,7 +4636,7 @@ void run_script_main(ScriptState *st, const ScriptBuffer *rootscript) push_int<ScriptDataArg>(stack, 0); break; case ByteCode::STR: - push_str<ScriptDataStr>(stack, dumb_string::copys(st->scriptp.pops())); + push_str<ScriptDataStr>(stack, st->scriptp.pops()); break; case ByteCode::FUNC: run_func(st); @@ -5079,7 +5062,7 @@ void set_script_var_s(dumb_ptr<map_session_data> sd, VarName var, int e, XString { size_t k = variable_names.intern(var); SIR reg = SIR::from(k, e); - set_reg(sd, VariableCode::VARIABLE, reg, dumb_string::copys(val)); + set_reg(sd, VariableCode::VARIABLE, reg, val); } int get_script_var_i(dumb_ptr<map_session_data> sd, VarName var, int e) { diff --git a/src/map/script.hpp b/src/map/script.hpp index 77ffaa1..d5200a6 100644 --- a/src/map/script.hpp +++ b/src/map/script.hpp @@ -167,7 +167,7 @@ struct ScriptDataParam }; struct ScriptDataStr { - dumb_string str; + RString str; }; struct ScriptDataArg { diff --git a/src/shared/lib.cpp b/src/shared/lib.cpp index a9dc9b1..0eebf17 100644 --- a/src/shared/lib.cpp +++ b/src/shared/lib.cpp @@ -34,8 +34,6 @@ namespace tmwa { -#define PRINT_VAR(var) PRINTF("%s = %s\n"_fmt, #var##_s, var) - static void try_read(const io::DirFd& dirfd, ZString filename) { |