diff --git a/packages/strings/readme.md b/packages/strings/readme.md index b5a197c..9f531cd 100644 --- a/packages/strings/readme.md +++ b/packages/strings/readme.md @@ -20,7 +20,7 @@ Finds $len characters of $str, starting at $idx (no prerequisites). ### fun -string !inBrackets -string &str -char &left -char &right -int &instance -Finds the content of the `$instance`th instance of brackets, with the left bracket being the $left char and the right bracket of $right char (requires !findCharInstance). +Finds the content of the `$instance`th instance of brackets, with the left bracket being the $left char and the right bracket of $right char (requires !findCharInstance and !mid). ### fun -bool !matchStart -string &str -string &instr @@ -40,4 +40,8 @@ You'll never guess what this does (it removes the last $num characters from a st ### fun -bool !contains -string &str -string &instr -Checks if $str contains $instr (requires !mid). \ No newline at end of file +Checks if $str contains $instr (requires !mid). + +### fun -list !split -string &str -string &splitstr + +Returns a list of $str split apart depending on $splitstr, e.g. `split("a->b->c", "->")` returns `[a, b, c]` (requires !contains, !findStringInstance, !mid, !removeLeft). \ No newline at end of file diff --git a/packages/strings/strings.grnd b/packages/strings/strings.grnd index 9d8afc3..376ced5 100644 --- a/packages/strings/strings.grnd +++ b/packages/strings/strings.grnd @@ -47,6 +47,9 @@ fun -string !mid -string &str -int &idx -int &len set &ans "" lesser $idx 0 &store if $store %end + greater $len 0 &store + if $store %loop + return "" @loop getstrsize $str &store equal $store $idx &store @@ -66,11 +69,26 @@ fun -string !mid -string &str -int &idx -int &len endfun fun -string !inBrackets -string &str -char &left -char &right -int &instance - # Get start idx + # Get left idx pusharg $str pusharg $left pusharg $instance - call !findCharInstance &store + call !findCharInstance &left + + # Get right idx + pusharg $str + pusharg $right + pusharg $instance + call !findCharInstance &right + + # Get inner string + add $left 1 &left + subtract $right $left &store + pusharg $str + pusharg $left + pusharg $store + call !mid &store + return $store endfun fun -bool !matchStart -string &str -string &instr