diff --git a/packages/strings/readme.md b/packages/strings/readme.md index b5e207e..1feb31e 100644 --- a/packages/strings/readme.md +++ b/packages/strings/readme.md @@ -2,26 +2,30 @@ This library adds extra string manipulation functions to ground. It is entirely coded in ground. -Please note that you cannot copy a single function from the package, as some functions rely on other functions in the library. +Please note that you cannot copy a single function from the package, as some functions rely on other functions in the library (reliance on other functions is detailed below). ## Functions ### fun -char !findChar -string &str -char &char -Finds the index of $char in $string +Finds the index of $char in $string (no prerequisites). ### fun -char !findCharInstance -string &str -char &char -int &instance -Finds the index of the `$instance`th instance of $char in $str +Finds the index of the `$instance`th instance of $char in $str (no prerequisites). ### fun -string !mid -string &str -int &idx -int &len -Finds $len characters of $str, starting at $idx. +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. +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 -Checks if the start of $str matches $instr. \ No newline at end of file +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 diff --git a/packages/strings/strings.grnd b/packages/strings/strings.grnd index 862914b..2a6ed0d 100644 --- a/packages/strings/strings.grnd +++ b/packages/strings/strings.grnd @@ -82,4 +82,41 @@ fun -string !matchStart -string &str -string &instr call !mid &store equal $store $instr &store return $store +endfun + +fun -int !findStringInstance -string &str -string &instr -int &instance + getstrsize $instr &len + subtract $len 1 &len + set &idx $len + + @loop + set &tempstr "" + subtract $idx $len &tempidx + subtract $tempidx 1 &tempidx + @subloop + equal $tempidx $idx &store + if $store %continue + add $tempidx 1 &tempidx + + getstrcharat $str $tempidx &store + add $tempstr $store &tempstr + jump %subloop + + + @continue + equal $tempstr $instr &store + if $store %decrement + add $idx 1 &idx + jump %loop + + @decrement + equal $instance 0 &store + if $store %end + subtract $instance 1 &instance + add $idx 1 &idx + jump %loop + + @end + subtract $idx $len &idx + return $idx endfun \ No newline at end of file