From 6ecf8daef776cc51c9e1c507980781c33b3ae0d4 Mon Sep 17 00:00:00 2001 From: suchmememanyskill Date: Sun, 25 Jul 2021 21:39:45 +0200 Subject: [PATCH] add str.split --- source/script/StringClass.c | 25 +++++++++++++++++++++++++ source/script/parser.h | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/source/script/StringClass.c b/source/script/StringClass.c index 1180421..9530453 100644 --- a/source/script/StringClass.c +++ b/source/script/StringClass.c @@ -2,6 +2,7 @@ #include "compat.h" #include "intClass.h" #include "scriptError.h" +#include "parser.h" #include char* getStringValue(Variable_t* var) { @@ -88,6 +89,28 @@ ClassFunction(stringInEq){ return newIntVariablePtr(strcmp(caller->string.value, args[0]->string.value)); } +ClassFunction(stringSplit) { + int valLen = strlen(args[0]->string.value); + + char* start = caller->string.value; + char* find = NULL; + Vector_t arr = newVec(sizeof(char**), 1); + char* temp; + + while ((find = (strstr(start, args[0]->string.value))) != NULL) { + temp = utils_copyStringSize(start, find - start); + vecAdd(&arr, temp); + + start = find + valLen; + } + + temp = utils_copyStringSize(start, caller->string.value + strlen(caller->string.value) - start); + vecAdd(&arr, temp); + + Variable_t a = { .variableType = StringArrayClass, .solvedArray.vector = arr }; + return copyVariableToPtr(a); +} + u8 strOneIntArg[] = { IntClass }; u8 oneStringArg[] = { StringClass }; @@ -100,6 +123,8 @@ ClassFunctionTableEntry_t stringFunctions[] = { {"-", stringMinusInt, 1, strOneIntArg}, {"==", stringEq, 1, oneStringArg}, {"!=", stringInEq, 1, oneStringArg}, + {"split", stringSplit, 1, oneStringArg}, + {"/", stringSplit, 1, oneStringArg}, }; Variable_t getStringMember(Variable_t* var, char* memberName) { diff --git a/source/script/parser.h b/source/script/parser.h index c81e3a2..51dc4d4 100644 --- a/source/script/parser.h +++ b/source/script/parser.h @@ -12,4 +12,5 @@ typedef struct { void exitStaticVars(Vector_t* v); void exitFunction(Operator_t* start, u32 len); -ParserRet_t parseScript(char* in, u32 len); \ No newline at end of file +ParserRet_t parseScript(char* in, u32 len); +char* utils_copyStringSize(const char* in, int size); \ No newline at end of file