https://github.com/akkartik/mu/blob/main/411string.mu
1
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
8 $substring:core: {
9
10 var i/eax: int <- copy 0
11 {
12 compare i, start
13 break-if->=
14 {
15 var dummy/eax: grapheme <- read-grapheme in-stream-addr
16 compare dummy, 0xffffffff/end-of-file
17 break-if-= $substring:core
18 }
19 i <- increment
20 loop
21 }
22
23 i <- copy 0
24 {
25 compare i, len
26 break-if->=
27 {
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
32 }
33 i <- increment
34 loop
35 }
36 }
37 stream-to-array out-stream-addr, out-ah
38 }
39
40 fn test-substring {
41 var out-h: (handle array byte)
42 var out-ah/edi: (addr handle array byte) <- address out-h
43
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
50
51 check-strings-equal out, "", "F - test-substring/empty"
52
53 substring "abcde", 0, 3, out-ah
54 var out/eax: (addr array byte) <- lookup *out-ah
55
56