diff --git a/packages/strings/readme.md b/packages/strings/readme.md index 1feb31e..b5a197c 100644 --- a/packages/strings/readme.md +++ b/packages/strings/readme.md @@ -22,10 +22,22 @@ Finds $len characters of $str, starting at $idx (no prerequisites). 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). -### fun -string !matchStart -string &str -string &instr +### fun -bool !matchStart -string &str -string &instr Checks if the start of $str matches $instr (requires !mid). ### fun -int !findStringInstance -string &str -string &instr -int &instance -The same as !findCharInstance, but allows a string to be parsed to be searched for (no prerequisites). \ No newline at end of file +The same as !findCharInstance, but allows a string to be parsed to be searched for (no prerequisites). + +### fun -string !removeLeft -string &string -int &num + +Removes the first $num characters from a string (no prerequisites). + +### fun -string !removeRight -string &string -int &num + +You'll never guess what this does (it removes the last $num characters from a string) (no prerequisites). + +### fun -bool !contains -string &str -string &instr + +Checks if $str contains $instr (requires !mid). \ No newline at end of file diff --git a/packages/strings/strings.grnd b/packages/strings/strings.grnd index 2a6ed0d..a2b28ef 100644 --- a/packages/strings/strings.grnd +++ b/packages/strings/strings.grnd @@ -40,7 +40,7 @@ fun -char !findCharInstance -string &str -char &char -int &instance @return return $idx - @end + @end endfun fun -string !mid -string &str -int &idx -int &len @@ -74,7 +74,7 @@ fun -string !inBrackets -string &str -char &left -char &right -int &instance stdlnout $store endfun -fun -string !matchStart -string &str -string &instr +fun -bool !matchStart -string &str -string &instr getstrsize $instr &store pusharg $str pusharg 0 @@ -119,4 +119,64 @@ fun -int !findStringInstance -string &str -string &instr -int &instance @end subtract $idx $len &idx return $idx +endfun + +fun -string !removeLeft -string &string -int &num + getstrsize $string &len + set &ans "" + @loop + equal $num $len &store + if $store %end + getstrcharat $string $num &store + add $ans $store &ans + add $num 1 &num + jump %loop + + @end + return $ans +endfun + +fun -string !removeRight -string &string -int &num + getstrsize $string &len + subtract $len $num &len + set &ans "" + set &num 0 + @loop + equal $num $len &store + if $store %end + getstrcharat $string $num &store + add $ans $store &ans + add $num 1 &num + jump %loop + + @end + return $ans +endfun + +fun -bool !contains -string &str -string &instr + getstrsize $str &len + getstrsize $instr &inlen + subtract $len $inlen &len + set &idx 0 + + @loop + equal $idx $len &store + if $store %false + + pusharg $str + pusharg $idx + pusharg $inlen + call !mid &store + + equal $store $instr &store + if $store %true + + add $idx 1 &idx + jump %loop + + @true + return true + + @false + return false endfun \ No newline at end of file