entity_example.odin 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package unicode_entity_example
  2. import "core:encoding/xml"
  3. import "core:encoding/entity"
  4. import "core:strings"
  5. import "core:mem"
  6. import "core:fmt"
  7. import "core:time"
  8. OPTIONS :: xml.Options{
  9. flags = {
  10. .Ignore_Unsupported, .Intern_Comments,
  11. },
  12. expected_doctype = "",
  13. }
  14. doc_print :: proc(doc: ^xml.Document) {
  15. buf: strings.Builder
  16. defer strings.destroy_builder(&buf)
  17. w := strings.to_writer(&buf)
  18. xml.print(w, doc)
  19. fmt.println(strings.to_string(buf))
  20. }
  21. _entities :: proc() {
  22. doc: ^xml.Document
  23. err: xml.Error
  24. DOC :: #load("../../../../tests/core/assets/XML/unicode.xml")
  25. parse_duration: time.Duration
  26. {
  27. time.SCOPED_TICK_DURATION(&parse_duration)
  28. doc, err = xml.parse(DOC, OPTIONS)
  29. }
  30. defer xml.destroy(doc)
  31. doc_print(doc)
  32. ms := time.duration_milliseconds(parse_duration)
  33. speed := (f64(1000.0) / ms) * f64(len(DOC)) / 1_024.0 / 1_024.0
  34. fmt.printf("Parse time: %.2f ms (%.2f MiB/s).\n", ms, speed)
  35. fmt.printf("Error: %v\n", err)
  36. }
  37. _main :: proc() {
  38. using fmt
  39. doc, err := xml.parse(#load("test.html"))
  40. defer xml.destroy(doc)
  41. doc_print(doc)
  42. if false {
  43. val := doc.root.children[1].children[2].value
  44. println()
  45. replaced, ok := entity.decode_xml(val)
  46. defer delete(replaced)
  47. printf("Before: '%v', Err: %v\n", val, err)
  48. printf("Passthrough: '%v'\nOK: %v\n", replaced, ok)
  49. println()
  50. }
  51. if false {
  52. val := doc.root.children[1].children[2].value
  53. println()
  54. replaced, ok := entity.decode_xml(val, { .CDATA_Unbox })
  55. defer delete(replaced)
  56. printf("Before: '%v', Err: %v\n", val, err)
  57. printf("CDATA_Unbox: '%v'\nOK: %v\n", replaced, ok)
  58. println()
  59. }
  60. if true {
  61. val := doc.root.children[1].children[2].value
  62. println()
  63. replaced, ok := entity.decode_xml(val, { .CDATA_Unbox, .CDATA_Decode })
  64. defer delete(replaced)
  65. printf("Before: '%v', Err: %v\n", val, err)
  66. printf("CDATA_Decode: '%v'\nOK: %v\n", replaced, ok)
  67. println()
  68. }
  69. if true {
  70. val := doc.root.children[1].children[1].value
  71. println()
  72. replaced, ok := entity.decode_xml(val, { .Comment_Strip })
  73. defer delete(replaced)
  74. printf("Before: '%v', Err: %v\n", val, err)
  75. printf("Comment_Strip: '%v'\nOK: %v\n", replaced, ok)
  76. println()
  77. }
  78. }
  79. main :: proc() {
  80. using fmt
  81. track: mem.Tracking_Allocator
  82. mem.tracking_allocator_init(&track, context.allocator)
  83. context.allocator = mem.tracking_allocator(&track)
  84. _main()
  85. //_entities()
  86. if len(track.allocation_map) > 0 {
  87. println()
  88. for _, v in track.allocation_map {
  89. printf("%v Leaked %v bytes.\n", v.location, v.size)
  90. }
  91. }
  92. }