entity_example.odin 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. options := xml.Options{ flags = { .Ignore_Unsupported, .Intern_Comments, .Unbox_CDATA, .Decode_SGML_Entities }}
  38. doc, _ := xml.parse(#load("test.html"), options)
  39. defer xml.destroy(doc)
  40. doc_print(doc)
  41. }
  42. main :: proc() {
  43. track: mem.Tracking_Allocator
  44. mem.tracking_allocator_init(&track, context.allocator)
  45. context.allocator = mem.tracking_allocator(&track)
  46. // _main()
  47. _entities()
  48. if len(track.allocation_map) > 0 {
  49. fmt.println()
  50. for _, v in track.allocation_map {
  51. fmt.printf("%v Leaked %v bytes.\n", v.location, v.size)
  52. }
  53. }
  54. }