go_subnetter/netsplit/funcs_ianastring.go

52 lines
889 B
Go

package netsplit
import (
`encoding/xml`
`errors`
`fmt`
`io`
`strings`
)
func (i *IANAString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
var tok xml.Token
var nextRefIdx int
var ref *IANARef
var sb *strings.Builder = new(strings.Builder)
for {
if tok, err = d.Token(); err != nil {
if errors.Is(err, io.EOF) {
err = nil
break
}
return
}
switch t := tok.(type) {
case xml.CharData:
sb.Write(t)
case xml.StartElement:
switch t.Name.Local {
case "xref":
ref = new(IANARef)
if err = d.DecodeElement(ref, &t); err != nil {
return
}
if i.References == nil {
i.References = make([]*IANARef, 0)
}
i.References = append(i.References, ref)
fmt.Fprintf(sb, "[REF %d]", nextRefIdx)
nextRefIdx++
default:
continue
}
}
}
i.Text = strings.TrimSpace(sb.String())
return
}