diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2021-12-02 20:12:12 +0100 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2021-12-05 02:52:23 +0100 |
| commit | 2dd67dba89732b89adb0199bc0a99de4cbc34e8f (patch) | |
| tree | d5ba3341bdfa31758d59590b0c62d6f3aa8a3cad /core/encoding/entity/example | |
| parent | 580721440657a9fe5334b6bf095fb70b584fa4f6 (diff) | |
[core:encoding/entity] Add new package to decode &<entity>; entities.
Includes generator to generate a lookup for named entitiess.
Diffstat (limited to 'core/encoding/entity/example')
| -rw-r--r-- | core/encoding/entity/example/entity_example.odin | 122 | ||||
| -rw-r--r-- | core/encoding/entity/example/test.html | 26 |
2 files changed, 148 insertions, 0 deletions
diff --git a/core/encoding/entity/example/entity_example.odin b/core/encoding/entity/example/entity_example.odin new file mode 100644 index 000000000..8758d9ad9 --- /dev/null +++ b/core/encoding/entity/example/entity_example.odin @@ -0,0 +1,122 @@ +package unicode_entity_example + +import "core:encoding/xml" +import "core:encoding/entity" +import "core:strings" +import "core:mem" +import "core:fmt" +import "core:time" + +OPTIONS :: xml.Options{ + flags = { + .Ignore_Unsupported, .Intern_Comments, + }, + expected_doctype = "", +} + +doc_print :: proc(doc: ^xml.Document) { + buf: strings.Builder + defer strings.destroy_builder(&buf) + w := strings.to_writer(&buf) + + xml.print(w, doc) + fmt.println(strings.to_string(buf)) +} + +_entities :: proc() { + doc: ^xml.Document + err: xml.Error + + DOC :: #load("../../../../tests/core/assets/XML/unicode.xml") + + parse_duration: time.Duration + + { + time.SCOPED_TICK_DURATION(&parse_duration) + doc, err = xml.parse(DOC, OPTIONS) + } + defer xml.destroy(doc) + + doc_print(doc) + + ms := time.duration_milliseconds(parse_duration) + + speed := (f64(1000.0) / ms) * f64(len(DOC)) / 1_024.0 / 1_024.0 + + fmt.printf("Parse time: %.2f ms (%.2f MiB/s).\n", ms, speed) + fmt.printf("Error: %v\n", err) +} + +_main :: proc() { + using fmt + + doc, err := xml.parse(#load("test.html")) + defer xml.destroy(doc) + doc_print(doc) + + if false { + val := doc.root.children[1].children[2].value + + println() + replaced, ok := entity.decode_xml(val) + defer delete(replaced) + + printf("Before: '%v', Err: %v\n", val, err) + printf("Passthrough: '%v'\nOK: %v\n", replaced, ok) + println() + } + + if false { + val := doc.root.children[1].children[2].value + + println() + replaced, ok := entity.decode_xml(val, { .CDATA_Unbox }) + defer delete(replaced) + + printf("Before: '%v', Err: %v\n", val, err) + printf("CDATA_Unbox: '%v'\nOK: %v\n", replaced, ok) + println() + } + + if true { + val := doc.root.children[1].children[2].value + + println() + replaced, ok := entity.decode_xml(val, { .CDATA_Unbox, .CDATA_Decode }) + defer delete(replaced) + + printf("Before: '%v', Err: %v\n", val, err) + printf("CDATA_Decode: '%v'\nOK: %v\n", replaced, ok) + println() + } + + if true { + val := doc.root.children[1].children[1].value + + println() + replaced, ok := entity.decode_xml(val, { .Comment_Strip }) + defer delete(replaced) + + printf("Before: '%v', Err: %v\n", val, err) + printf("Comment_Strip: '%v'\nOK: %v\n", replaced, ok) + println() + } +} + +main :: proc() { + using fmt + + track: mem.Tracking_Allocator + mem.tracking_allocator_init(&track, context.allocator) + context.allocator = mem.tracking_allocator(&track) + + _main() + //_entities() + + if len(track.allocation_map) > 0 { + println() + for _, v in track.allocation_map { + printf("%v Leaked %v bytes.\n", v.location, v.size) + } + } +}
\ No newline at end of file diff --git a/core/encoding/entity/example/test.html b/core/encoding/entity/example/test.html new file mode 100644 index 000000000..60e32bf03 --- /dev/null +++ b/core/encoding/entity/example/test.html @@ -0,0 +1,26 @@ +<html>
+ <head>
+ <title>Entity Reference Test</title>
+ <style>
+ body {
+ background: #000; color: #eee;
+ width: 40%;
+ margin-left: auto;
+ margin-right: auto;
+ font-size: 14pt;
+ }
+ </style>
+ </head>
+ <body>
+ <h1>Entity Reference Test</h1>
+ <div id="test_cdata_in_comment" foo="">
+ Foozle]! © <!-- <![CDATA[ ® ]]> -->42&;1234&
+ </div>
+ <div id="test_cdata_unwrap_and_passthrough">
+ Foozle]! © <![CDATA[BOX ® /BOX]]>42&;1234&
+ </div>
+ <div>
+ | | | fj ` \ ® ϱ ∳
+ </div>
+ </body>
+</html>
\ No newline at end of file |