entity_example.odin 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package unicode_entity_example
  2. import "core:encoding/xml"
  3. import "core:strings"
  4. import "core:mem"
  5. import "core:fmt"
  6. import "core:time"
  7. doc_print :: proc(doc: ^xml.Document) {
  8. buf: strings.Builder
  9. defer strings.builder_destroy(&buf)
  10. w := strings.to_writer(&buf)
  11. xml.print(w, doc)
  12. fmt.println(strings.to_string(buf))
  13. }
  14. _entities :: proc() {
  15. doc: ^xml.Document
  16. err: xml.Error
  17. DOC :: #load("../../../../tests/core/assets/XML/unicode.xml")
  18. OPTIONS :: xml.Options{
  19. flags = {
  20. .Ignore_Unsupported, .Intern_Comments,
  21. },
  22. expected_doctype = "",
  23. }
  24. parse_duration: time.Duration
  25. {
  26. time.SCOPED_TICK_DURATION(&parse_duration)
  27. doc, err = xml.parse(DOC, OPTIONS)
  28. }
  29. defer xml.destroy(doc)
  30. doc_print(doc)
  31. ms := time.duration_milliseconds(parse_duration)
  32. speed := (f64(1000.0) / ms) * f64(len(DOC)) / 1_024.0 / 1_024.0
  33. fmt.printf("Parse time: %.2f ms (%.2f MiB/s).\n", ms, speed)
  34. fmt.printf("Error: %v\n", err)
  35. }
  36. _main :: proc() {
  37. using fmt
  38. options := xml.Options{ flags = { .Ignore_Unsupported, .Intern_Comments, .Unbox_CDATA, .Decode_SGML_Entities }}
  39. doc, _ := xml.parse(#load("test.html"), options)
  40. defer xml.destroy(doc)
  41. doc_print(doc)
  42. }
  43. main :: proc() {
  44. using fmt
  45. track: mem.Tracking_Allocator
  46. mem.tracking_allocator_init(&track, context.allocator)
  47. context.allocator = mem.tracking_allocator(&track)
  48. // _main()
  49. _entities()
  50. if len(track.allocation_map) > 0 {
  51. println()
  52. for _, v in track.allocation_map {
  53. printf("%v Leaked %v bytes.\n", v.location, v.size)
  54. }
  55. }
  56. }