Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Tokenize-a-string/Cowgol/tokenize-a-string.cowgol
Normal file
40
Task/Tokenize-a-string/Cowgol/tokenize-a-string.cowgol
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
include "cowgol.coh";
|
||||
include "strings.coh";
|
||||
|
||||
# Tokenize a string. Note: the string is modified in place.
|
||||
sub tokenize(sep: uint8, str: [uint8], out: [[uint8]]): (length: intptr) is
|
||||
length := 0;
|
||||
loop
|
||||
[out] := str;
|
||||
out := @next out;
|
||||
length := length + 1;
|
||||
while [str] != 0 and [str] != sep loop
|
||||
str := @next str;
|
||||
end loop;
|
||||
if [str] == sep then
|
||||
[str] := 0;
|
||||
str := @next str;
|
||||
else
|
||||
break;
|
||||
end if;
|
||||
end loop;
|
||||
end sub;
|
||||
|
||||
# The string
|
||||
var string: [uint8] := "Hello,How,Are,You,Today";
|
||||
|
||||
# Make a mutable copy
|
||||
var buf: uint8[64];
|
||||
CopyString(string, &buf[0]);
|
||||
|
||||
# Tokenize the copy
|
||||
var parts: [uint8][64];
|
||||
var length := tokenize(',', &buf[0], &parts[0]) as @indexof parts;
|
||||
|
||||
# Print each string
|
||||
var i: @indexof parts := 0;
|
||||
while i < length loop
|
||||
print(parts[i]);
|
||||
print(".\n");
|
||||
i := i + 1;
|
||||
end loop;
|
||||
Loading…
Add table
Add a link
Reference in a new issue