New functions for strings package

This commit is contained in:
2025-09-11 16:32:50 +10:00
parent e63e01e87b
commit 98fdadfcd8
2 changed files with 76 additions and 4 deletions

View File

@@ -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).
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).

View File

@@ -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