added memset_bytes, fixed some stack comments, fixed splitby_char and finally implemented splitby_str so splitby if now fully functional

This commit is contained in:
IgorCielniak
2026-03-20 21:09:44 +01:00
parent f4d688cac1
commit e2159bbca2
4 changed files with 77 additions and 14 deletions

View File

@@ -42,7 +42,7 @@ word memcpy
r> dup -rot - swap r> dup -rot - swap
end end
#memset [*, value, len | addr] -> [*] #memset [*, addr, len | value] -> [*]
word memset word memset
swap swap
0 swap for 0 swap for
@@ -51,14 +51,23 @@ word memset
2drop drop 2drop drop
end end
#memdump [*, len | addr] -> [* | addr] # memset_bytes [*, addr, len | value] -> [*]
word memset_bytes
swap
0 swap for
-rot swap 2 pick + 2dup swap c! 1 + -rot swap
end
2drop drop
end
#memdump [*, addr | len] -> [* | addr]
word memdump word memdump
for for
dup @ puti cr 8 + dup @ puti cr 8 +
end end
end end
#memdump_bytes [*, len | addr] -> [* | addr] #memdump_bytes [*, addr | len] -> [* | addr]
word memdump_bytes word memdump_bytes
for for
dup c@ puti cr 1 + dup c@ puti cr 1 +

View File

@@ -414,11 +414,48 @@ word toascii
2drop 2drop
end end
word splitby_str # rm_zero_len_str [*, addr0, len0 ... addrN, lenN | N] -> [*, addrX, lenX ... addrY, lenY | Z]
"split_str isn't implemented yet" puts word rm_zero_len_str
dup for
swap dup 0 == if
drop nip 1 -
else
>r rswap swap >r rswap
end
end
dup 2 * for
rswap r> swap
end
end end
# splitby [*, addr, len, addr1 | len1] -> [*, addr1, len1 ... addrN | lenN] # emit_strs [*, addr | len] -> [*, addr0, len0 ... addrN | lenN]
# given an addr and len emits pairs (addr, len) of strings in the given memopry region
word emit_strs
0 >r
>r
while r@ 0 > do
dup strlen dup r> swap - >r
over over + rswap r> 1 + >r rswap
while dup c@ 0 == do 1 + r> 1 - >r end
end
drop rdrop
end
# splitby_str [*, addr, len, addr1, len1] -> [*, addr0, len0 ... addrN, lenN | N]
# splits a string by another string and emmits a sequence of the new (addr, len) pairs on to the stack as well as the number of strings the oprtation resulted in.
word splitby_str
2 pick for
3 pick 0 2 pick 4 pick swap
strcmp 1 == if 3 pick over 0 memset_bytes end
>r >r swap 1 + swap r> r>
end
2drop 2dup - >r nip r> swap emit_strs r>
rm_zero_len_str
end
# splitby [*, addr, len, addr1 | len1] -> [*, addr0, len0 ... addrN, lenN | N]
# split a string by another string, delegates to either splitby_char or splitby_str based on the length of the delimiter.
word splitby word splitby
dup 1 == if dup 1 == if
splitby_char splitby_char
@@ -427,25 +464,25 @@ word splitby
end end
end end
# splitby_char [*, addr, len, addr1 | len] -> [*, addr1, len1 ... addrN | lenN] # splitby_char [*, addr, len, addr1 | len] -> [*, addr1, len1 ... addrN, lenN | N]
# split a string by a given character, the resulting (addr, len) pairs are pushed on to the stack followed by the number of the pushed strings.
word splitby_char word splitby_char
2 pick >r
>r >r 2dup r> r> 2swap 2dup >r >r 2dup r> r> 2swap 2dup
>r >r toascii 1 rpick nrot r> r> >r >r toascii 1 rpick nrot r> r>
dup 3 + pick c@ dup 3 + pick c@
0 >r
swap for swap for
dup dup
3 pick == if rswap r> 1 + >r rswap over 0 c! end 3 pick == if over 0 c! end
swap 1 + swap >r nip r> swap 1 + swap >r nip r>
end end
2drop 2drop drop 2drop 2drop drop
r> 1 + for r>
dup strlen 2dup emit_strs
1 + + r>
end rm_zero_len_str
drop
end end

View File

@@ -1,3 +1,13 @@
1 1
0 0
hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world
hello
hello
hello
hello
o
d he
o wor
d he
o wor
he

View File

@@ -14,4 +14,11 @@ word main
2dup 2dup
puts puts
free free
"hello world hello" "world" splitby
for puts end
"hello world hello world" "world" splitby
for puts end
"hello world hello world hello" "l" splitby
for puts end
end end