Skip to content

Commit b270922

Browse files
committed
core\unicode\tools -> os2
1 parent d075dc1 commit b270922

File tree

1 file changed

+42
-63
lines changed

1 file changed

+42
-63
lines changed

core/unicode/tools/generate_entity_table.odin

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package xml_example
22

3-
import "core:encoding/xml"
4-
import "core:os"
3+
import "core:encoding/xml"
4+
import os "core:os/os2"
55
import path "core:path/filepath"
6-
import "core:mem"
7-
import "core:strings"
8-
import "core:strconv"
9-
import "core:slice"
10-
import "core:fmt"
6+
import "core:strings"
7+
import "core:strconv"
8+
import "core:slice"
9+
import "core:fmt"
1110

12-
/*
13-
Silent error handler for the parser.
14-
*/
11+
// Silent error handler for the parser.
1512
Error_Handler :: proc(pos: xml.Pos, fmt: string, args: ..any) {}
1613

1714
OPTIONS :: xml.Options{ flags = { .Ignore_Unsupported, }, expected_doctype = "unicode", }
@@ -22,7 +19,7 @@ Entity :: struct {
2219
description: string,
2320
}
2421

25-
generate_encoding_entity_table :: proc() {
22+
main :: proc() {
2623
filename := path.join({ODIN_ROOT, "tests", "core", "assets", "XML", "unicode.xml"})
2724
defer delete(filename)
2825

@@ -33,14 +30,14 @@ generate_encoding_entity_table :: proc() {
3330
defer xml.destroy(doc)
3431

3532
if err != .None {
36-
fmt.printf("Load/Parse error: %v\n", err)
33+
fmt.printfln("Load/Parse error: %v", err)
3734
if err == .File_Error {
38-
fmt.printf("\"%v\" not found. Did you run \"tests\\download_assets.py\"?", filename)
35+
fmt.eprintfln("%q not found. Did you run \"tests\\download_assets.py\"?", filename)
3936
}
4037
os.exit(1)
4138
}
4239

43-
fmt.printf("\"%v\" loaded and parsed.\n", filename)
40+
fmt.printfln("%q loaded and parsed.", filename)
4441

4542
generated_buf: strings.Builder
4643
defer strings.builder_destroy(&generated_buf)
@@ -54,7 +51,7 @@ generate_encoding_entity_table :: proc() {
5451

5552
charlist := doc.elements[charlist_id]
5653

57-
fmt.printf("Found `<charlist>` with %v children.\n", len(charlist.value))
54+
fmt.printfln("Found `<charlist>` with %v children.", len(charlist.value))
5855

5956
entity_map: map[string]Entity
6057
names: [dynamic]string
@@ -70,26 +67,32 @@ generate_encoding_entity_table :: proc() {
7067
char := doc.elements[id]
7168

7269
if char.ident != "character" {
73-
fmt.eprintf("Expected `<character>`, got `<%v>`\n", char.ident)
70+
fmt.eprintfln("Expected `<character>`, got `<%v>`", char.ident)
7471
os.exit(1)
7572
}
7673

7774
if codepoint_string, ok := xml.find_attribute_val_by_key(doc, id, "dec"); !ok {
7875
fmt.eprintln("`<character id=\"...\">` attribute not found.")
7976
os.exit(1)
8077
} else {
81-
codepoint := strconv.atoi(codepoint_string)
78+
dash := strings.index(codepoint_string, "-")
79+
if dash > -1 {
80+
codepoint_string = codepoint_string[:dash]
81+
}
82+
codepoint, code_ok := strconv.parse_int(codepoint_string)
83+
if !code_ok {
84+
fmt.eprintfln("codepoint_string: %q, codepoint: %v", codepoint_string, codepoint)
85+
continue
86+
}
8287

8388
desc, desc_ok := xml.find_child_by_ident(doc, id, "description")
89+
assert(desc_ok)
8490
description := ""
8591
if len(doc.elements[desc].value) == 1 {
8692
description = doc.elements[desc].value[0].(string)
8793
}
8894

89-
/*
90-
For us to be interested in this codepoint, it has to have at least one entity.
91-
*/
92-
95+
// For us to be interested in this codepoint, it has to have at least one entity.
9396
nth := 0
9497
for {
9598
character_entity := xml.find_child_by_ident(doc, id, "entity", nth) or_break
@@ -103,8 +106,8 @@ generate_encoding_entity_table :: proc() {
103106
}
104107

105108
if name == "\"\"" {
106-
fmt.printf("%#v\n", char)
107-
fmt.printf("%#v\n", character_entity)
109+
fmt.printfln("%#v", char)
110+
fmt.printfln("%#v", character_entity)
108111
}
109112

110113
if len(name) > max_name_length { longest_name = name }
@@ -130,29 +133,25 @@ generate_encoding_entity_table :: proc() {
130133
}
131134
}
132135

133-
/*
134-
Sort by name.
135-
*/
136+
// Sort by name.
136137
slice.sort(names[:])
137138

138-
fmt.printf("Found %v unique `&name;` -> rune mappings.\n", count)
139-
fmt.printf("Shortest name: %v (%v)\n", shortest_name, min_name_length)
140-
fmt.printf("Longest name: %v (%v)\n", longest_name, max_name_length)
139+
fmt.printfln("Found %v unique `&name;` -> rune mappings.", count)
140+
fmt.printfln("Shortest name: %v (%v)", shortest_name, min_name_length)
141+
fmt.printfln("Longest name: %v (%v)", longest_name, max_name_length)
141142

142-
/*
143-
Generate table.
144-
*/
143+
// Generate table.
145144
fmt.wprintln(w, "package encoding_unicode_entity")
146145
fmt.wprintln(w, "")
147146
fmt.wprintln(w, GENERATED)
148147
fmt.wprintln(w, "")
149148
fmt.wprintf (w, TABLE_FILE_PROLOG)
150149
fmt.wprintln(w, "")
151150

152-
fmt.wprintf (w, "// `&%v;`\n", shortest_name)
153-
fmt.wprintf (w, "XML_NAME_TO_RUNE_MIN_LENGTH :: %v\n", min_name_length)
154-
fmt.wprintf (w, "// `&%v;`\n", longest_name)
155-
fmt.wprintf (w, "XML_NAME_TO_RUNE_MAX_LENGTH :: %v\n", max_name_length)
151+
fmt.wprintfln(w, "// `&%v;`", shortest_name)
152+
fmt.wprintfln(w, "XML_NAME_TO_RUNE_MIN_LENGTH :: %v", min_name_length)
153+
fmt.wprintfln(w, "// `&%v;`", longest_name)
154+
fmt.wprintfln(w, "XML_NAME_TO_RUNE_MAX_LENGTH :: %v", max_name_length)
156155
fmt.wprintln(w, "")
157156

158157
fmt.wprintln(w,
@@ -189,7 +188,7 @@ named_xml_entity_to_rune :: proc(name: string) -> (decoded: rune, ok: bool) {
189188
}
190189

191190
prefix = rune(v[0])
192-
fmt.wprintf (w, "\tcase '%v':\n", prefix)
191+
fmt.wprintfln(w, "\tcase '%v':", prefix)
193192
fmt.wprintln(w, "\t\tswitch name {")
194193
}
195194

@@ -199,8 +198,8 @@ named_xml_entity_to_rune :: proc(name: string) -> (decoded: rune, ok: bool) {
199198
for i := len(e.name); i < max_name_length; i += 1 {
200199
fmt.wprintf(w, " ")
201200
}
202-
fmt.wprintf(w, " // %v\n", e.description)
203-
fmt.wprintf(w, "\t\t\treturn %v, true\n", rune_to_string(e.codepoint))
201+
fmt.wprintfln(w, " // %v", e.description)
202+
fmt.wprintfln(w, "\t\t\treturn %v, true", rune_to_string(e.codepoint))
204203

205204
should_close = true
206205
}
@@ -216,17 +215,13 @@ named_xml_entity_to_rune :: proc(name: string) -> (decoded: rune, ok: bool) {
216215

217216
written := os.write_entire_file(generated_filename, transmute([]byte)strings.to_string(generated_buf))
218217

219-
if written {
220-
fmt.printf("Successfully written generated \"%v\".\n", generated_filename)
218+
if written == nil {
219+
fmt.printfln("Successfully written generated \"%v\".", generated_filename)
221220
} else {
222-
fmt.printf("Failed to write generated \"%v\".\n", generated_filename)
221+
fmt.printfln("Failed to write generated \"%v\".", generated_filename)
223222
}
224223

225-
delete(entity_map)
226-
delete(names)
227-
for &name in names {
228-
free(&name)
229-
}
224+
// Not a library, no need to clean up.
230225
}
231226

232227
GENERATED :: `/*
@@ -275,20 +270,4 @@ is_dotted_name :: proc(name: string) -> (dotted: bool) {
275270
if r == '.' { return true}
276271
}
277272
return false
278-
}
279-
280-
main :: proc() {
281-
track: mem.Tracking_Allocator
282-
mem.tracking_allocator_init(&track, context.allocator)
283-
context.allocator = mem.tracking_allocator(&track)
284-
285-
generate_encoding_entity_table()
286-
287-
if len(track.allocation_map) > 0 {
288-
fmt.println()
289-
for _, v in track.allocation_map {
290-
fmt.printf("%v Leaked %v bytes.\n", v.location, v.size)
291-
}
292-
}
293-
fmt.println("Done and cleaned up!")
294273
}

0 commit comments

Comments
 (0)