From 6d6ac01e3cfb54dffed86ecce0f39a92931c3c66 Mon Sep 17 00:00:00 2001 From: gumi Date: Mon, 22 Oct 2018 15:18:28 -0400 Subject: add buildin_data_to_string and buildin_string_to_data --- src/map/script.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/map/script.c b/src/map/script.c index d1839676f..348d81a85 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -16725,6 +16725,63 @@ static BUILDIN(getdatatype) return true; } +static BUILDIN(data_to_string) +{ + if (script_hasdata(st, 2)) { + struct script_data *data = script_getdata(st, 2); + + if (data_isstring(data)) { + script_pushcopy(st, 2); + } else if (data_isint(data)) { + char *str = NULL; + + CREATE(str, char, 20); + safesnprintf(str, 20, "%"PRId64"", data->u.num); + script_pushstr(st, str); + } else if (data_islabel(data)) { + const char *str = ""; + + // XXX: because all we have is the label pos we can't be sure which + // one is the correct label if more than one has the same pos. + // We might want to store both the pos and str_data index in + // data->u.num, similar to how C_NAME stores both the array + // index and str_data index in u.num with bitmasking. This + // would also avoid the awkward for() loops as we could + // directly access the string with script->get_str(). + + if (st->oid) { + struct npc_data *nd = map->id2nd(st->oid); + + for (int i = 0; i < nd->u.scr.label_list_num; ++i) { + if (nd->u.scr.label_list[i].pos == data->u.num) { + str = nd->u.scr.label_list[i].name; + break; + } + } + } else { + for (int i = LABEL_START; script->str_data[i].next != 0; i = script->str_data[i].next) { + if (script->str_data[i].label == data->u.num) { + str = script->get_str(i); + break; + } + } + } + + script_pushconststr(st, str); + } else if (data_isreference(data)) { + script_pushstrcopy(st, reference_getname(data)); + } else { + ShowWarning("script:data_to_string: unknown data type!\n"); + script->reportdata(data); + script_pushconststr(st, ""); + } + } else { + script_pushconststr(st, ""); // NIL + } + + return true; +} + //======================================================= // chr //------------------------------------------------------- @@ -25199,6 +25256,8 @@ static void script_parse_builtin(void) BUILDIN_DEF(charat,"si"), BUILDIN_DEF(isstr,"v"), BUILDIN_DEF(getdatatype, "?"), + BUILDIN_DEF(data_to_string, "?"), + BUILDIN_DEF2(getd, "string_to_data", "?"), BUILDIN_DEF(chr,"i"), BUILDIN_DEF(ord,"s"), BUILDIN_DEF(setchar,"ssi"), -- cgit v1.2.3-70-g09d2 From 5219e2e3d004e52a8a08cb96536e30ced7001b95 Mon Sep 17 00:00:00 2001 From: gumi Date: Mon, 22 Oct 2018 15:26:36 -0400 Subject: add unit tests for data_to_string --- npc/dev/test.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/npc/dev/test.txt b/npc/dev/test.txt index c2f07ab2f..2822ee65c 100644 --- a/npc/dev/test.txt +++ b/npc/dev/test.txt @@ -775,6 +775,16 @@ function script HerculesSelfTestHelper { callsub(OnCheck, "Getdatatype (getarg integer value)", callsub(OnTestGetdatatype, 5), DATATYPE_INT); callsub(OnCheck, "Getdatatype (getarg string)", callsub(OnTestGetdatatype, "foo"), DATATYPE_STR | DATATYPE_CONST); + callsub(OnCheck, "data_to_string (NIL)", data_to_string(), ""); + callsub(OnCheck, "data_to_string (empty string)", data_to_string(""), ""); + callsub(OnCheck, "data_to_string (string)", data_to_string("foo"), "foo"); + callsub(OnCheck, "data_to_string (integer)", data_to_string(5), "5"); + callsub(OnCheck, "data_to_string (parameter)", data_to_string(Hp), "Hp"); + callsub(OnCheck, "data_to_string (constant)", data_to_string(DATATYPE_CONST), "DATATYPE_CONST"); + callsub(OnCheck, "data_to_string (label)", data_to_string(OnTestGetdatatype), "OnTestGetdatatype"); + callsub(OnCheck, "data_to_string (string variable)", data_to_string(.@x$), ".@x$"); + callsub(OnCheck, "data_to_string (integer variable)", data_to_string(.@x), ".@x"); + if (.errors) { debugmes "Script engine self-test [ \033[0;31mFAILED\033[0m ]"; debugmes "**** The test was completed with " + .errors + " errors. ****"; -- cgit v1.2.3-70-g09d2 From b0a321c309521a6b0bc35c8f9c58ce57f3f38cbd Mon Sep 17 00:00:00 2001 From: gumi Date: Mon, 22 Oct 2018 15:33:22 -0400 Subject: add documentation for buildin_data_to_string --- doc/script_commands.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/doc/script_commands.txt b/doc/script_commands.txt index 33d1ee426..219c71205 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -8604,6 +8604,18 @@ Example: --------------------------------------- +*data_to_string() + +Returns a string representation of the given data, similar to the .toString() +method in JavaScript. + +Example: + + data_to_string(DATATYPE_VAR) // "DATATYPE_VAR" + data_to_string(.@foo) // ".@foo" + +--------------------------------------- + *charisalpha("", ) This function will return true if the character number Position in the given -- cgit v1.2.3-70-g09d2