-
Notifications
You must be signed in to change notification settings - Fork 21
/
util.odin
220 lines (185 loc) · 4.26 KB
/
util.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package odin_html_docs
import "base:intrinsics"
import "core:fmt"
import "core:os"
import "core:slice"
import "core:strings"
import doc "core:odin/doc-format"
array :: proc(a: $A/doc.Array($T)) -> []T {
return doc.from_array(cfg.header, a)
}
str :: proc(s: $A/doc.String) -> string {
return doc.from_string(cfg.header, s)
}
base_type :: proc(t: doc.Type) -> doc.Type {
t := t
for {
if t.kind != .Named {
break
}
t = cfg.types[array(t.types)[0]]
}
return t
}
type_deref :: proc(t: doc.Type) -> doc.Type {
bt := base_type(t)
#partial switch bt.kind {
case .Pointer:
return cfg.types[array(bt.types)[0]]
}
return t
}
is_type_untyped :: proc(type: doc.Type) -> bool {
if type.kind == .Basic {
flags := transmute(doc.Type_Flags_Basic)type.flags
return .Untyped in flags
}
return false
}
is_type_string_or_rune :: proc(type: doc.Type) -> bool {
type := type
type = base_type(type)
if type.kind == .Basic {
switch str(type.name) {
case "string", "cstring", "untyped string":
return true
case "rune", "untyped rune":
return true
}
}
return false
}
is_entity_blank :: proc(e: doc.Entity_Index) -> bool {
name := str(cfg.entities[e].name)
return name == ""
}
Dir_Node :: struct {
dir: string,
path: string,
name: string,
pkg: ^doc.Pkg,
children: [dynamic]^Dir_Node,
}
sort_directory_tree :: proc(node: ^Dir_Node) {
slice.sort_by_key(node.children[:], proc(node: ^Dir_Node) -> string {
return strings.to_lower(node.name, context.temp_allocator)
})
// Remove duplicates
for i := 1; i < len(node.children); /**/ {
if node.children[i-1].name == node.children[i].name {
ordered_remove(&node.children, i)
} else {
i += 1
}
}
for child in node.children {
sort_directory_tree(child)
}
}
insert_into_directory_tree :: proc(root: ^Dir_Node, pkgs_to_use: map[string]^doc.Pkg) {
children := make([dynamic]^Dir_Node)
pkgs_to_use_loop: for path, pkg in pkgs_to_use {
dir, _, inner := strings.partition(path, "/")
if inner == "" {
for child in root.children {
if child.name == dir {
continue pkgs_to_use_loop
}
}
node := new_clone(Dir_Node{
dir = dir,
name = dir,
path = path,
pkg = pkg,
})
append(&root.children, node)
} else {
node := new_clone(Dir_Node{
dir = dir,
name = inner,
path = path,
pkg = pkg,
})
append(&children, node)
}
}
child_loop: for child in children {
dir, _, _ := strings.partition(child.path, "/")
for node in root.children {
if node.dir == dir {
for c in node.children {
if c.name == child.name {
continue child_loop
}
}
append(&node.children, child)
continue child_loop
}
}
for other_parent in root.children {
if other_parent.name == dir {
continue child_loop
}
}
parent := new_clone(Dir_Node{
dir = dir,
name = dir,
path = dir,
pkg = nil,
})
append(&root.children, parent)
append(&parent.children, child)
}
sort_directory_tree(root)
return
}
//
// General
//
errorf :: proc(format: string, args: ..any) -> ! {
fmt.eprintf("%s ", os.args[0])
fmt.eprintf(format, ..args)
fmt.eprintln()
os.exit(1)
}
HTML_LESS_THAN :: "<"
HTML_GREATER_THAN :: ">"
// On Unix systems we need to set the directory mode so that we
// can read/write from them
DIRECTORY_MODE :: 0o775 when os.OS == .Darwin || os.OS == .Linux || os.OS == .FreeBSD else 0
common_prefix :: proc(strs: []string) -> string {
if len(strs) == 0 {
return ""
}
n := max(int)
for str in strs {
n = min(n, len(str))
}
prefix := strs[0][:n]
for str in strs[1:] {
for len(prefix) != 0 && str[:len(prefix)] != prefix {
prefix = prefix[:len(prefix)-1]
}
if len(prefix) == 0 {
break
}
}
return prefix
}
recursive_make_directory :: proc(path: string, prefix := "") {
head, _, tail := strings.partition(path, "/")
path_to_make := head
if prefix != "" {
path_to_make = fmt.tprintf("%s/%s", prefix, head)
}
os.make_directory(path_to_make, DIRECTORY_MODE)
if tail != "" {
recursive_make_directory(tail, path_to_make)
}
}
escape_html_string :: proc(s: string, allocator := context.allocator) -> string {
context.allocator = allocator
escaped, _ := strings.replace_all(s, "<", HTML_LESS_THAN)
escaped, _ = strings.replace_all(escaped, ">", HTML_GREATER_THAN)
return escaped
}