RosettaCodeData/Task/Tokenize-a-string/Action-/tokenize-a-string.action

95 lines
1.6 KiB
Plaintext

CARD EndProg ;required for ALLOCATE.ACT
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
DEFINE PTR="CARD"
BYTE FUNC Split(CHAR ARRAY s CHAR c PTR ARRAY items)
BYTE i,count,start,len
CHAR ARRAY item
IF s(0)=0 THEN RETURN (0) FI
i=1 count=0
WHILE i<s(0)
DO
start=i
WHILE i<=s(0) AND s(i)#c
DO
i==+1
OD
len=i-start
item=Alloc(len+1)
SCopyS(item,s,start,i-1)
items(count)=item
count==+1
i==+1
OD
RETURN (count)
PROC Join(PTR ARRAY items BYTE count CHAR c CHAR ARRAY s)
BYTE i,pos
CHAR POINTER srcPtr,dstPtr
CHAR ARRAY item
s(0)=0
IF count=0 THEN RETURN FI
pos=1
FOR i=0 TO count-1
DO
item=items(i)
srcPtr=item+1
dstPtr=s+pos
MoveBlock(dstPtr,srcPtr,item(0))
pos==+item(0)
IF i<count-1 THEN
s(pos)='.
pos==+1
FI
OD
s(0)=pos-1
RETURN
PROC Clear(PTR ARRAY items BYTE POINTER count)
BYTE i
CHAR ARRAY item
IF count^=0 THEN RETURN FI
FOR i=0 TO count^-1
DO
item=items(i)
Free(item,item(0)+1)
OD
count^=0
RETURN
PROC Main()
CHAR ARRAY s="Hello,How,Are,You,Today"
CHAR ARRAY r(256)
PTR ARRAY items(100)
BYTE i,count
Put(125) PutE() ;clear screen
AllocInit(0)
count=Split(s,',,items)
Join(items,count,'.,r)
PrintF("Input:%E""%S""%E%E",s)
PrintE("Split:")
FOR i=0 TO count-1
DO
PrintF("""%S""",items(i))
IF i<count-1 THEN
Print(", ")
ELSE
PutE() PutE()
FI
OD
PrintF("Join:%E""%S""%E",r)
Clear(items,@count)
RETURN