1 # read up to 'len' graphemes after skipping the first 'start' ones
2 fn substring in: (addr array byte), start: int, len: int, out-ah: (addr handle array byte) {
3 var in-stream: (stream byte 0x100)
4 var in-stream-addr/esi: (addr stream byte) <- address in-stream
5 write in-stream-addr, in
6 var out-stream: (stream byte 0x100)
7 var out-stream-addr/edi: (addr stream byte) <- address out-stream
9 # skip 'start' graphemes
10 var i/eax: int <- copy 0
15 var dummy/eax: grapheme <- read-grapheme in-stream-addr
16 compare dummy, 0xffffffff/end-of-file
17 break-if-= $substring:core
22 # copy 'len' graphemes
28 var g/eax: grapheme <- read-grapheme in-stream-addr
29 compare g, 0xffffffff/end-of-file
30 break-if-= $substring:core
31 write-grapheme out-stream-addr, g
37 stream-to-array out-stream-addr, out-ah
41 var out-h: (handle array byte)
42 var out-ah/edi: (addr handle array byte) <- address out-h
44 substring 0, 0, 3, out-ah
45 var out/eax: (addr array byte) <- lookup *out-ah
46 check-strings-equal out, "", "F - test-substring/null"
47 substring "", 0, 3, out-ah
48 var out/eax: (addr array byte) <- lookup *out-ah
49 #? print-string-to-real-screen out
50 #? print-string-to-real-screen "\n"
51 check-strings-equal out, "", "F - test-substring/empty"
53 substring "abcde", 0, 3, out-ah
54 var out/eax: (addr array byte) <- lookup *out-ah
55 #? print-string-to-real-screen out
56 #? print-string-to-real-screen "\n"
57 check-strings-equal out, "abc", "F - test-substring/truncate"
59 substring "abcde", 0, 5, out-ah
60 var out/eax: (addr array byte) <- lookup *out-ah
61 check-strings-equal out, "abcde", "F - test-substring/all"
63 substring "abcde", 0, 7, out-ah
64 var out/eax: (addr array byte) <- lookup *out-ah
65 check-strings-equal out, "abcde", "F - test-substring/too-small"
66 # substrings outside string
67 substring "abcde", 6, 1, out-ah
68 var out/eax: (addr array byte) <- lookup *out-ah
69 check-strings-equal out, "", "F - test-substring/start-too-large"
71 substring "", 2, 3, out-ah
72 var out/eax: (addr array byte) <- lookup *out-ah
73 check-strings-equal out, "", "F - test-substring/middle-empty"
75 substring "abcde", 1, 2, out-ah
76 var out/eax: (addr array byte) <- lookup *out-ah
77 check-strings-equal out, "bc", "F - test-substring/middle-truncate"
79 substring "abcde", 1, 4, out-ah
80 var out/eax: (addr array byte) <- lookup *out-ah
81 check-strings-equal out, "bcde", "F - test-substring/middle-all"
83 substring "abcde", 1, 5, out-ah
84 var out/eax: (addr array byte) <- lookup *out-ah
85 check-strings-equal out, "bcde", "F - test-substring/middle-too-small"
88 fn split-string in: (addr array byte), delim: grapheme, out: (addr handle array (handle array byte)) {
89 var in-stream: (stream byte 0x100)
90 var in-stream-addr/esi: (addr stream byte) <- address in-stream
91 write in-stream-addr, in
92 var tokens-stream: (stream (handle array byte) 0x100)
93 var tokens-stream-addr/edi: (addr stream (handle array byte)) <- address tokens-stream
94 var curr-stream: (stream byte 0x100)
95 var curr-stream-addr/ecx: (addr stream byte) <- address curr-stream
97 var g/eax: grapheme <- read-grapheme in-stream-addr
100 #? print-grapheme-to-real-screen g
101 #? print-string-to-real-screen "\n"
105 # token complete; flush
106 var token: (handle array byte)
107 var token-ah/eax: (addr handle array byte) <- address token
108 stream-to-array curr-stream-addr, token-ah
109 write-to-stream tokens-stream-addr, token-ah
110 clear-stream curr-stream-addr
111 loop $split-string:core
113 write-grapheme curr-stream-addr, g
116 stream-to-array tokens-stream-addr, out
119 fn test-split-string {
120 var out-h: (handle array (handle array byte))
121 var out-ah/edi: (addr handle array (handle array byte)) <- address out-h
123 split-string "bab", 0x61, out-ah