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

239 lines
4.8 KiB
Plaintext
Raw Normal View History

2025-09-10 19:31:08 +10:00
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
2025-09-11 16:32:50 +10:00
@end
2025-09-10 19:31:08 +10:00
endfun
fun -string !mid -string &str -int &idx -int &len
set &ans ""
lesser $idx 0 &store
2025-09-12 19:59:53 +10:00
if $store %negend
2025-09-11 20:37:45 +10:00
greater $len 0 &store
if $store %loop
return ""
2025-09-10 19:31:08 +10:00
@loop
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
2025-09-12 19:59:53 +10:00
@negend
error "Cannot start mid at a negative index"
2025-09-10 19:31:08 +10:00
endfun
fun -string !inBrackets -string &str -char &left -char &right -int &instance
2025-09-11 20:37:45 +10:00
# Get left idx
2025-09-10 19:31:08 +10:00
pusharg $str
pusharg $left
pusharg $instance
2025-09-11 20:37:45 +10:00
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
2025-09-10 19:31:08 +10:00
endfun
2025-09-11 16:32:50 +10:00
fun -bool !matchStart -string &str -string &instr
2025-09-10 19:31:08 +10:00
getstrsize $instr &store
pusharg $str
pusharg 0
pusharg $store
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
2025-09-11 16:32:50 +10:00
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
2025-09-11 20:00:33 +10:00
greater $inlen $len &store
if $store %false
2025-09-11 16:32:50 +10:00
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
2025-09-11 20:00:33 +10:00
endfun
fun -list !split -string &str -string &splitstr
setlist *list
getstrsize $splitstr &splitlen
@loop
pusharg $str
pusharg $splitstr
call !contains &store
if $store %continue
jump %end
@continue
# Find text before split char
pusharg $str
pusharg $splitstr
pusharg 0
call !findStringInstance &store
# Store amount of characters to remove
add $store $splitlen &store2
# Get split
pusharg $str
pusharg 0
pusharg $store
call !mid &store
listappend *list $store
# Remove first split
pusharg $str
pusharg $store2
call !removeLeft &str
jump %loop
@end
listappend *list $str
return *list
2025-09-10 19:31:08 +10:00
endfun