Skip to content

Commit f89831c

Browse files
A string builder
*It's actually slower than FastCode's automatic naive array copy-concatenation even though it's a linked list because of garbage collection.
1 parent c742dc3 commit f89831c

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

examples/string.txt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
struct _char_node {
2+
char
3+
next
4+
}
5+
6+
struct str_builder {
7+
_head
8+
size
9+
}
10+
11+
struct str_lexer {
12+
_str
13+
_index
14+
_last_char
15+
size
16+
}
17+
18+
proc str_builder() {
19+
builder = new str_builder
20+
builder.size = 0
21+
return builder
22+
}
23+
24+
proc append_char(builder, char) {
25+
new_node = new _char_node
26+
new_node.char = char
27+
new_node.next = builder._head
28+
builder._head = new_node
29+
builder.size++
30+
}
31+
32+
proc append_str(builder, str) {
33+
i = 0
34+
len = len(str)
35+
while i < len => append_char(builder, str[i++])
36+
}
37+
38+
proc build_str(builder) {
39+
new_str = array(builder.size)
40+
i = builder.size - 1
41+
current = builder._head
42+
while current != null {
43+
new_str[i--] = current.char
44+
current = ref current.next
45+
}
46+
return new_str
47+
}
48+
49+
proc str_lexer(str) {
50+
lexer = new str_lexer
51+
lexer._str = str
52+
lexer._index = 0
53+
lexer.size = len(str)
54+
read_char(lexer)
55+
return lexer
56+
}
57+
58+
proc eos(lexer) => return lexer._last_char == 0
59+
60+
proc read_char(lexer) {
61+
if lexer._index == lexer.size {
62+
return lexer._last_char = 0
63+
}
64+
return lexer._last_char = lexer._str[lexer._index++]
65+
}
66+
67+
proc read_tok(lexer) {
68+
builder = str_builder()
69+
if eos(lexer) => return null
70+
while lexer._last_char == ' ' or lexer._last_char == ',' or lexer._last_char == '\n' {
71+
if eos(lexer) => return null
72+
read_char(lexer)
73+
}
74+
while lexer._last_char != 0 and lexer._last_char != ' ' and lexer._last_char != ',' and lexer._last_char != '\n' {
75+
append_char(builder, lexer._last_char)
76+
read_char(lexer)
77+
}
78+
return build_str(builder)
79+
}

0 commit comments

Comments
 (0)