helpers.odin 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. An XML 1.0 / 1.1 parser
  3. Copyright 2021-2022 Jeroen van Rijn <[email protected]>.
  4. Made available under Odin's BSD-3 license.
  5. This file contains helper functions.
  6. */
  7. package xml
  8. // Find parent's nth child with a given ident.
  9. find_child_by_ident :: proc(doc: ^Document, parent_id: Element_ID, ident: string, nth := 0) -> (res: Element_ID, found: bool) {
  10. tag := doc.elements[parent_id]
  11. count := 0
  12. for child_id in tag.children {
  13. child := doc.elements[child_id]
  14. /*
  15. Skip commments. They have no name.
  16. */
  17. if child.kind != .Element { continue }
  18. /*
  19. If the ident matches and it's the nth such child, return it.
  20. */
  21. if child.ident == ident {
  22. if count == nth { return child_id, true }
  23. count += 1
  24. }
  25. }
  26. return 0, false
  27. }
  28. // Find an attribute by key.
  29. find_attribute_val_by_key :: proc(doc: ^Document, parent_id: Element_ID, key: string) -> (val: string, found: bool) {
  30. tag := doc.elements[parent_id]
  31. for attr in tag.attribs {
  32. /*
  33. If the ident matches, we're done. There can only ever be one attribute with the same name.
  34. */
  35. if attr.key == key { return attr.val, true }
  36. }
  37. return "", false
  38. }