Files
ground-programs/packages/strings/strings.grnd

85 lines
1.7 KiB
Plaintext

fun -char !findChar -string &str -char &char
set &idx 0
getstrsize $str &len
@loop
equal $idx $len &store
if $store %end
getstrcharat $str $idx &stridx
equal $stridx $char &store
if $store %return
add $idx 1 &idx
jump %loop
@return
return $idx
@end
endfun
fun -char !findCharInstance -string &str -char &char -int &instance
set &idx 0
getstrsize $str &len
@loop
equal $idx $len &store
if $store %end
getstrcharat $str $idx &stridx
equal $stridx $char &store
if $store %decrement
@increment
add $idx 1 &idx
jump %loop
@decrement
equal $instance 0 &store
if $store %return
subtract $instance 1 &instance
jump %increment
@return
return $idx
@end
endfun
fun -string !mid -string &str -int &idx -int &len
set &ans ""
lesser $idx 0 &store
if $store %end
@loop
getstrsize $str &store
equal $store $idx &store
if $store %end
getstrcharat $str $idx &store
add $ans $store &ans
add $idx 1 &idx
getstrsize $ans &store
equal $store $len &store
if $store %return
jump %loop
@return
return $ans
@end
endfun
fun -string !inBrackets -string &str -char &left -char &right -int &instance
# Get start idx
pusharg $str
pusharg $left
pusharg $instance
call !findCharInstance &store
stdlnout $store
endfun
fun -string !matchStart -string &str -string &instr
getstrsize $instr &store
pusharg $str
pusharg 0
pusharg $store
call !mid &store
equal $store $instr &store
return $store
endfun