src/encoding/xml/xml.go GO 2,084 lines View on github.com → Search inside
File is large — showing lines 1–2,000 of 2,084.
1// Copyright 2009 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.45// Package xml implements a simple XML 1.0 parser that6// understands XML name spaces.7package xml89// References:10//    Annotated XML spec: https://www.xml.com/axml/testaxml.htm11//    XML name spaces: https://www.w3.org/TR/REC-xml-names/1213import (14	"bufio"15	"bytes"16	"errors"17	"fmt"18	"io"19	"strconv"20	"strings"21	"unicode"22	"unicode/utf8"23)2425// A SyntaxError represents a syntax error in the XML input stream.26type SyntaxError struct {27	Msg  string28	Line int29}3031func (e *SyntaxError) Error() string {32	return "XML syntax error on line " + strconv.Itoa(e.Line) + ": " + e.Msg33}3435// A Name represents an XML name (Local) annotated36// with a name space identifier (Space).37// In tokens returned by [Decoder.Token], the Space identifier38// is given as a canonical URL, not the short prefix used39// in the document being parsed.40type Name struct {41	Space, Local string42}4344// An Attr represents an attribute in an XML element (Name=Value).45type Attr struct {46	Name  Name47	Value string48}4950// A Token is an interface holding one of the token types:51// [StartElement], [EndElement], [CharData], [Comment], [ProcInst], or [Directive].52type Token any5354// A StartElement represents an XML start element.55type StartElement struct {56	Name Name57	Attr []Attr58}5960// Copy creates a new copy of StartElement.61func (e StartElement) Copy() StartElement {62	attrs := make([]Attr, len(e.Attr))63	copy(attrs, e.Attr)64	e.Attr = attrs65	return e66}6768// End returns the corresponding XML end element.69func (e StartElement) End() EndElement {70	return EndElement{e.Name}71}7273// An EndElement represents an XML end element.74type EndElement struct {75	Name Name76}7778// A CharData represents XML character data (raw text),79// in which XML escape sequences have been replaced by80// the characters they represent.81type CharData []byte8283// Copy creates a new copy of CharData.84func (c CharData) Copy() CharData { return CharData(bytes.Clone(c)) }8586// A Comment represents an XML comment of the form <!--comment-->.87// The bytes do not include the <!-- and --> comment markers.88type Comment []byte8990// Copy creates a new copy of Comment.91func (c Comment) Copy() Comment { return Comment(bytes.Clone(c)) }9293// A ProcInst represents an XML processing instruction of the form <?target inst?>94type ProcInst struct {95	Target string96	Inst   []byte97}9899// Copy creates a new copy of ProcInst.100func (p ProcInst) Copy() ProcInst {101	p.Inst = bytes.Clone(p.Inst)102	return p103}104105// A Directive represents an XML directive of the form <!text>.106// The bytes do not include the <! and > markers.107type Directive []byte108109// Copy creates a new copy of Directive.110func (d Directive) Copy() Directive { return Directive(bytes.Clone(d)) }111112// CopyToken returns a copy of a Token.113func CopyToken(t Token) Token {114	switch v := t.(type) {115	case CharData:116		return v.Copy()117	case Comment:118		return v.Copy()119	case Directive:120		return v.Copy()121	case ProcInst:122		return v.Copy()123	case StartElement:124		return v.Copy()125	}126	return t127}128129// A TokenReader is anything that can decode a stream of XML tokens, including a130// [Decoder].131//132// When Token encounters an error or end-of-file condition after successfully133// reading a token, it returns the token. It may return the (non-nil) error from134// the same call or return the error (and a nil token) from a subsequent call.135// An instance of this general case is that a TokenReader returning a non-nil136// token at the end of the token stream may return either io.EOF or a nil error.137// The next Read should return nil, [io.EOF].138//139// Implementations of Token are discouraged from returning a nil token with a140// nil error. Callers should treat a return of nil, nil as indicating that141// nothing happened; in particular it does not indicate EOF.142type TokenReader interface {143	Token() (Token, error)144}145146// A Decoder represents an XML parser reading a particular input stream.147// The parser assumes that its input is encoded in UTF-8.148type Decoder struct {149	// Strict defaults to true, enforcing the requirements150	// of the XML specification.151	// If set to false, the parser allows input containing common152	// mistakes:153	//	* If an element is missing an end tag, the parser invents154	//	  end tags as necessary to keep the return values from Token155	//	  properly balanced.156	//	* In attribute values and character data, unknown or malformed157	//	  character entities (sequences beginning with &) are left alone.158	//159	// Setting:160	//161	//	d.Strict = false162	//	d.AutoClose = xml.HTMLAutoClose163	//	d.Entity = xml.HTMLEntity164	//165	// creates a parser that can handle typical HTML.166	//167	// Strict mode does not enforce the requirements of the XML name spaces TR.168	// In particular it does not reject name space tags using undefined prefixes.169	// Such tags are recorded with the unknown prefix as the name space URL.170	Strict bool171172	// When Strict == false, AutoClose indicates a set of elements to173	// consider closed immediately after they are opened, regardless174	// of whether an end element is present.175	AutoClose []string176177	// Entity can be used to map non-standard entity names to string replacements.178	// The parser behaves as if these standard mappings are present in the map,179	// regardless of the actual map content:180	//181	//	"lt": "<",182	//	"gt": ">",183	//	"amp": "&",184	//	"apos": "'",185	//	"quot": `"`,186	Entity map[string]string187188	// CharsetReader, if non-nil, defines a function to generate189	// charset-conversion readers, converting from the provided190	// non-UTF-8 charset into UTF-8. If CharsetReader is nil or191	// returns an error, parsing stops with an error. One of the192	// CharsetReader's result values must be non-nil.193	CharsetReader func(charset string, input io.Reader) (io.Reader, error)194195	// DefaultSpace sets the default name space used for unadorned tags,196	// as if the entire XML stream were wrapped in an element containing197	// the attribute xmlns="DefaultSpace".198	DefaultSpace string199200	r              io.ByteReader201	t              TokenReader202	buf            bytes.Buffer203	saved          *bytes.Buffer204	stk            *stack205	stkDepth       int206	free           *stack207	needClose      bool208	toClose        Name209	nextToken      Token210	nextByte       int211	ns             map[string]string212	err            error213	line           int214	linestart      int64215	offset         int64216	inUnmarshalXML bool217}218219// NewDecoder creates a new XML parser reading from r.220// If r does not implement [io.ByteReader], NewDecoder will221// do its own buffering.222func NewDecoder(r io.Reader) *Decoder {223	d := &Decoder{224		ns:       make(map[string]string),225		nextByte: -1,226		line:     1,227		Strict:   true,228	}229	d.switchToReader(r)230	return d231}232233// NewTokenDecoder creates a new XML parser using an underlying token stream.234func NewTokenDecoder(t TokenReader) *Decoder {235	// Is it already a Decoder?236	if d, ok := t.(*Decoder); ok {237		return d238	}239	d := &Decoder{240		ns:       make(map[string]string),241		t:        t,242		nextByte: -1,243		line:     1,244		Strict:   true,245	}246	return d247}248249// Token returns the next XML token in the input stream.250// At the end of the input stream, Token returns nil, [io.EOF].251//252// Slices of bytes in the returned token data refer to the253// parser's internal buffer and remain valid only until the next254// call to Token. To acquire a copy of the bytes, call [CopyToken]255// or the token's Copy method.256//257// Token expands self-closing elements such as <br>258// into separate start and end elements returned by successive calls.259//260// Token guarantees that the [StartElement] and [EndElement]261// tokens it returns are properly nested and matched:262// if Token encounters an unexpected end element263// or EOF before all expected end elements,264// it will return an error.265//266// If [Decoder.CharsetReader] is called and returns an error,267// the error is wrapped and returned.268//269// Token implements XML name spaces as described by270// https://www.w3.org/TR/REC-xml-names/. Each of the271// [Name] structures contained in the Token has the Space272// set to the URL identifying its name space when known.273// If Token encounters an unrecognized name space prefix,274// it uses the prefix as the Space rather than report an error.275func (d *Decoder) Token() (Token, error) {276	var t Token277	var err error278	if d.stk != nil && d.stk.kind == stkEOF {279		return nil, io.EOF280	}281	if d.nextToken != nil {282		t = d.nextToken283		d.nextToken = nil284	} else {285		if t, err = d.rawToken(); t == nil && err != nil {286			if err == io.EOF && d.stk != nil && d.stk.kind != stkEOF {287				err = d.syntaxError("unexpected EOF")288			}289			return nil, err290		}291		// We still have a token to process, so clear any292		// errors (e.g. EOF) and proceed.293		err = nil294	}295	if !d.Strict {296		if t1, ok := d.autoClose(t); ok {297			d.nextToken = t298			t = t1299		}300	}301	switch t1 := t.(type) {302	case StartElement:303		// In XML name spaces, the translations listed in the304		// attributes apply to the element name and305		// to the other attribute names, so process306		// the translations first.307		for _, a := range t1.Attr {308			if a.Name.Space == xmlnsPrefix {309				v, ok := d.ns[a.Name.Local]310				d.pushNs(a.Name.Local, v, ok)311				d.ns[a.Name.Local] = a.Value312			}313			if a.Name.Space == "" && a.Name.Local == xmlnsPrefix {314				// Default space for untagged names315				v, ok := d.ns[""]316				d.pushNs("", v, ok)317				d.ns[""] = a.Value318			}319		}320321		d.pushElement(t1.Name)322		d.translate(&t1.Name, true)323		for i := range t1.Attr {324			d.translate(&t1.Attr[i].Name, false)325		}326		t = t1327328	case EndElement:329		if !d.popElement(&t1) {330			return nil, d.err331		}332		t = t1333	}334	return t, err335}336337const (338	xmlURL      = "http://www.w3.org/XML/1998/namespace"339	xmlnsPrefix = "xmlns"340	xmlPrefix   = "xml"341)342343// Apply name space translation to name n.344// The default name space (for Space=="")345// applies only to element names, not to attribute names.346func (d *Decoder) translate(n *Name, isElementName bool) {347	switch {348	case n.Space == xmlnsPrefix:349		return350	case n.Space == "" && !isElementName:351		return352	case n.Space == xmlPrefix:353		n.Space = xmlURL354	case n.Space == "" && n.Local == xmlnsPrefix:355		return356	}357	if v, ok := d.ns[n.Space]; ok {358		n.Space = v359	} else if n.Space == "" {360		n.Space = d.DefaultSpace361	}362}363364func (d *Decoder) switchToReader(r io.Reader) {365	// Get efficient byte at a time reader.366	// Assume that if reader has its own367	// ReadByte, it's efficient enough.368	// Otherwise, use bufio.369	if rb, ok := r.(io.ByteReader); ok {370		d.r = rb371	} else {372		d.r = bufio.NewReader(r)373	}374}375376// Parsing state - stack holds old name space translations377// and the current set of open elements. The translations to pop when378// ending a given tag are *below* it on the stack, which is379// more work but forced on us by XML.380type stack struct {381	next *stack382	kind int383	name Name384	ok   bool385}386387const (388	stkStart = iota389	stkNs390	stkEOF391)392393func (d *Decoder) push(kind int) *stack {394	s := d.free395	if s != nil {396		d.free = s.next397	} else {398		s = new(stack)399	}400	s.next = d.stk401	s.kind = kind402	if kind == stkStart {403		d.stkDepth++404	}405	d.stk = s406	return s407}408409func (d *Decoder) pop() *stack {410	s := d.stk411	if s != nil {412		if s.kind == stkStart {413			d.stkDepth--414		}415		d.stk = s.next416		s.next = d.free417		d.free = s418	}419	return s420}421422// Record that after the current element is finished423// (that element is already pushed on the stack)424// Token should return EOF until popEOF is called.425func (d *Decoder) pushEOF() {426	// Walk down stack to find Start.427	// It might not be the top, because there might be stkNs428	// entries above it.429	start := d.stk430	for start.kind != stkStart {431		start = start.next432	}433	// The stkNs entries below a start are associated with that434	// element too; skip over them.435	for start.next != nil && start.next.kind == stkNs {436		start = start.next437	}438	s := d.free439	if s != nil {440		d.free = s.next441	} else {442		s = new(stack)443	}444	s.kind = stkEOF445	s.next = start.next446	start.next = s447}448449// Undo a pushEOF.450// The element must have been finished, so the EOF should be at the top of the stack.451func (d *Decoder) popEOF() bool {452	if d.stk == nil || d.stk.kind != stkEOF {453		return false454	}455	d.pop()456	return true457}458459// Record that we are starting an element with the given name.460func (d *Decoder) pushElement(name Name) {461	s := d.push(stkStart)462	s.name = name463}464465// Record that we are changing the value of ns[local].466// The old value is url, ok.467func (d *Decoder) pushNs(local string, url string, ok bool) {468	s := d.push(stkNs)469	s.name.Local = local470	s.name.Space = url471	s.ok = ok472}473474// Creates a SyntaxError with the current line number.475func (d *Decoder) syntaxError(msg string) error {476	return &SyntaxError{Msg: msg, Line: d.line}477}478479// Record that we are ending an element with the given name.480// The name must match the record at the top of the stack,481// which must be a pushElement record.482// After popping the element, apply any undo records from483// the stack to restore the name translations that existed484// before we saw this element.485func (d *Decoder) popElement(t *EndElement) bool {486	s := d.pop()487	name := t.Name488	switch {489	case s == nil || s.kind != stkStart:490		d.err = d.syntaxError("unexpected end element </" + name.Local + ">")491		return false492	case s.name.Local != name.Local:493		if !d.Strict {494			d.needClose = true495			d.toClose = t.Name496			t.Name = s.name497			return true498		}499		d.err = d.syntaxError("element <" + s.name.Local + "> closed by </" + name.Local + ">")500		return false501	case s.name.Space != name.Space:502		ns := name.Space503		if name.Space == "" {504			ns = `""`505		}506		d.err = d.syntaxError("element <" + s.name.Local + "> in space " + s.name.Space +507			" closed by </" + name.Local + "> in space " + ns)508		return false509	}510511	d.translate(&t.Name, true)512513	// Pop stack until a Start or EOF is on the top, undoing the514	// translations that were associated with the element we just closed.515	for d.stk != nil && d.stk.kind != stkStart && d.stk.kind != stkEOF {516		s := d.pop()517		if s.ok {518			d.ns[s.name.Local] = s.name.Space519		} else {520			delete(d.ns, s.name.Local)521		}522	}523524	return true525}526527// If the top element on the stack is autoclosing and528// t is not the end tag, invent the end tag.529func (d *Decoder) autoClose(t Token) (Token, bool) {530	if d.stk == nil || d.stk.kind != stkStart {531		return nil, false532	}533	for _, s := range d.AutoClose {534		if strings.EqualFold(s, d.stk.name.Local) {535			// This one should be auto closed if t doesn't close it.536			et, ok := t.(EndElement)537			if !ok || !strings.EqualFold(et.Name.Local, d.stk.name.Local) {538				return EndElement{d.stk.name}, true539			}540			break541		}542	}543	return nil, false544}545546var errRawToken = errors.New("xml: cannot use RawToken from UnmarshalXML method")547548// RawToken is like [Decoder.Token] but does not verify that549// start and end elements match and does not translate550// name space prefixes to their corresponding URLs.551func (d *Decoder) RawToken() (Token, error) {552	if d.inUnmarshalXML {553		return nil, errRawToken554	}555	return d.rawToken()556}557558func (d *Decoder) rawToken() (Token, error) {559	if d.t != nil {560		return d.t.Token()561	}562	if d.err != nil {563		return nil, d.err564	}565	if d.needClose {566		// The last element we read was self-closing and567		// we returned just the StartElement half.568		// Return the EndElement half now.569		d.needClose = false570		return EndElement{d.toClose}, nil571	}572573	b, ok := d.getc()574	if !ok {575		return nil, d.err576	}577578	if b != '<' {579		// Text section.580		d.ungetc(b)581		data := d.text(-1, false)582		if data == nil {583			return nil, d.err584		}585		return CharData(data), nil586	}587588	if b, ok = d.mustgetc(); !ok {589		return nil, d.err590	}591	switch b {592	case '/':593		// </: End element594		var name Name595		if name, ok = d.nsname(); !ok {596			if d.err == nil {597				d.err = d.syntaxError("expected element name after </")598			}599			return nil, d.err600		}601		d.space()602		if b, ok = d.mustgetc(); !ok {603			return nil, d.err604		}605		if b != '>' {606			d.err = d.syntaxError("invalid characters between </" + name.Local + " and >")607			return nil, d.err608		}609		return EndElement{name}, nil610611	case '?':612		// <?: Processing instruction.613		var target string614		if target, ok = d.name(); !ok {615			if d.err == nil {616				d.err = d.syntaxError("expected target name after <?")617			}618			return nil, d.err619		}620		d.space()621		d.buf.Reset()622		var b0 byte623		for {624			if b, ok = d.mustgetc(); !ok {625				return nil, d.err626			}627			d.buf.WriteByte(b)628			if b0 == '?' && b == '>' {629				break630			}631			b0 = b632		}633		data := d.buf.Bytes()634		data = data[0 : len(data)-2] // chop ?>635636		if target == "xml" {637			content := string(data)638			ver := procInst("version", content)639			if ver != "" && ver != "1.0" {640				d.err = fmt.Errorf("xml: unsupported version %q; only version 1.0 is supported", ver)641				return nil, d.err642			}643			enc := procInst("encoding", content)644			if enc != "" && enc != "utf-8" && enc != "UTF-8" && !strings.EqualFold(enc, "utf-8") {645				if d.CharsetReader == nil {646					d.err = fmt.Errorf("xml: encoding %q declared but Decoder.CharsetReader is nil", enc)647					return nil, d.err648				}649				newr, err := d.CharsetReader(enc, d.r.(io.Reader))650				if err != nil {651					d.err = fmt.Errorf("xml: opening charset %q: %w", enc, err)652					return nil, d.err653				}654				if newr == nil {655					panic("CharsetReader returned a nil Reader for charset " + enc)656				}657				d.switchToReader(newr)658			}659		}660		return ProcInst{target, data}, nil661662	case '!':663		// <!: Maybe comment, maybe CDATA.664		if b, ok = d.mustgetc(); !ok {665			return nil, d.err666		}667		switch b {668		case '-': // <!-669			// Probably <!-- for a comment.670			if b, ok = d.mustgetc(); !ok {671				return nil, d.err672			}673			if b != '-' {674				d.err = d.syntaxError("invalid sequence <!- not part of <!--")675				return nil, d.err676			}677			// Look for terminator.678			d.buf.Reset()679			var b0, b1 byte680			for {681				if b, ok = d.mustgetc(); !ok {682					return nil, d.err683				}684				d.buf.WriteByte(b)685				if b0 == '-' && b1 == '-' {686					if b != '>' {687						d.err = d.syntaxError(688							`invalid sequence "--" not allowed in comments`)689						return nil, d.err690					}691					break692				}693				b0, b1 = b1, b694			}695			data := d.buf.Bytes()696			data = data[0 : len(data)-3] // chop -->697			return Comment(data), nil698699		case '[': // <![700			// Probably <![CDATA[.701			for i := 0; i < 6; i++ {702				if b, ok = d.mustgetc(); !ok {703					return nil, d.err704				}705				if b != "CDATA["[i] {706					d.err = d.syntaxError("invalid <![ sequence")707					return nil, d.err708				}709			}710			// Have <![CDATA[.  Read text until ]]>.711			data := d.text(-1, true)712			if data == nil {713				return nil, d.err714			}715			return CharData(data), nil716		}717718		// Probably a directive: <!DOCTYPE ...>, <!ENTITY ...>, etc.719		// We don't care, but accumulate for caller. Quoted angle720		// brackets do not count for nesting.721		d.buf.Reset()722		d.buf.WriteByte(b)723		inquote := uint8(0)724		depth := 0725		for {726			if b, ok = d.mustgetc(); !ok {727				return nil, d.err728			}729			if inquote == 0 && b == '>' && depth == 0 {730				break731			}732		HandleB:733			d.buf.WriteByte(b)734			switch {735			case b == inquote:736				inquote = 0737738			case inquote != 0:739				// in quotes, no special action740741			case b == '\'' || b == '"':742				inquote = b743744			case b == '>' && inquote == 0:745				depth--746747			case b == '<' && inquote == 0:748				// Look for <!-- to begin comment.749				s := "!--"750				for i := 0; i < len(s); i++ {751					if b, ok = d.mustgetc(); !ok {752						return nil, d.err753					}754					if b != s[i] {755						for j := 0; j < i; j++ {756							d.buf.WriteByte(s[j])757						}758						depth++759						goto HandleB760					}761				}762763				// Remove < that was written above.764				d.buf.Truncate(d.buf.Len() - 1)765766				// Look for terminator.767				var b0, b1 byte768				for {769					if b, ok = d.mustgetc(); !ok {770						return nil, d.err771					}772					if b0 == '-' && b1 == '-' && b == '>' {773						break774					}775					b0, b1 = b1, b776				}777778				// Replace the comment with a space in the returned Directive779				// body, so that markup parts that were separated by the comment780				// (like a "<" and a "!") don't get joined when re-encoding the781				// Directive, taking new semantic meaning.782				d.buf.WriteByte(' ')783			}784		}785		return Directive(d.buf.Bytes()), nil786	}787788	// Must be an open element like <a href="foo">789	d.ungetc(b)790791	var (792		name  Name793		empty bool794		attr  []Attr795	)796	if name, ok = d.nsname(); !ok {797		if d.err == nil {798			d.err = d.syntaxError("expected element name after <")799		}800		return nil, d.err801	}802803	attr = []Attr{}804	for {805		d.space()806		if b, ok = d.mustgetc(); !ok {807			return nil, d.err808		}809		if b == '/' {810			empty = true811			if b, ok = d.mustgetc(); !ok {812				return nil, d.err813			}814			if b != '>' {815				d.err = d.syntaxError("expected /> in element")816				return nil, d.err817			}818			break819		}820		if b == '>' {821			break822		}823		d.ungetc(b)824825		a := Attr{}826		if a.Name, ok = d.nsname(); !ok {827			if d.err == nil {828				d.err = d.syntaxError("expected attribute name in element")829			}830			return nil, d.err831		}832		d.space()833		if b, ok = d.mustgetc(); !ok {834			return nil, d.err835		}836		if b != '=' {837			if d.Strict {838				d.err = d.syntaxError("attribute name without = in element")839				return nil, d.err840			}841			d.ungetc(b)842			a.Value = a.Name.Local843		} else {844			d.space()845			data := d.attrval()846			if data == nil {847				return nil, d.err848			}849			a.Value = string(data)850		}851		attr = append(attr, a)852	}853	if empty {854		d.needClose = true855		d.toClose = name856	}857	return StartElement{name, attr}, nil858}859860func (d *Decoder) attrval() []byte {861	b, ok := d.mustgetc()862	if !ok {863		return nil864	}865	// Handle quoted attribute values866	if b == '"' || b == '\'' {867		return d.text(int(b), false)868	}869	// Handle unquoted attribute values for strict parsers870	if d.Strict {871		d.err = d.syntaxError("unquoted or missing attribute value in element")872		return nil873	}874	// Handle unquoted attribute values for unstrict parsers875	d.ungetc(b)876	d.buf.Reset()877	for {878		b, ok = d.mustgetc()879		if !ok {880			return nil881		}882		// https://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.2883		if 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z' ||884			'0' <= b && b <= '9' || b == '_' || b == ':' || b == '-' {885			d.buf.WriteByte(b)886		} else {887			d.ungetc(b)888			break889		}890	}891	return d.buf.Bytes()892}893894// Skip spaces if any895func (d *Decoder) space() {896	for {897		b, ok := d.getc()898		if !ok {899			return900		}901		switch b {902		case ' ', '\r', '\n', '\t':903		default:904			d.ungetc(b)905			return906		}907	}908}909910// Read a single byte.911// If there is no byte to read, return ok==false912// and leave the error in d.err.913// Maintain line number.914func (d *Decoder) getc() (b byte, ok bool) {915	if d.err != nil {916		return 0, false917	}918	if d.nextByte >= 0 {919		b = byte(d.nextByte)920		d.nextByte = -1921	} else {922		b, d.err = d.r.ReadByte()923		if d.err != nil {924			return 0, false925		}926		if d.saved != nil {927			d.saved.WriteByte(b)928		}929	}930	if b == '\n' {931		d.line++932		d.linestart = d.offset + 1933	}934	d.offset++935	return b, true936}937938// InputOffset returns the input stream byte offset of the current decoder position.939// The offset gives the location of the end of the most recently returned token940// and the beginning of the next token.941func (d *Decoder) InputOffset() int64 {942	return d.offset943}944945// InputPos returns the line of the current decoder position and the 1 based946// input position of the line. The position gives the location of the end of the947// most recently returned token.948func (d *Decoder) InputPos() (line, column int) {949	return d.line, int(d.offset-d.linestart) + 1950}951952// Return saved offset.953// If we did ungetc (nextByte >= 0), have to back up one.954func (d *Decoder) savedOffset() int {955	n := d.saved.Len()956	if d.nextByte >= 0 {957		n--958	}959	return n960}961962// Must read a single byte.963// If there is no byte to read,964// set d.err to SyntaxError("unexpected EOF")965// and return ok==false966func (d *Decoder) mustgetc() (b byte, ok bool) {967	if b, ok = d.getc(); !ok {968		if d.err == io.EOF {969			d.err = d.syntaxError("unexpected EOF")970		}971	}972	return973}974975// Unread a single byte.976func (d *Decoder) ungetc(b byte) {977	if b == '\n' {978		d.line--979	}980	d.nextByte = int(b)981	d.offset--982}983984var entity = map[string]rune{985	"lt":   '<',986	"gt":   '>',987	"amp":  '&',988	"apos": '\'',989	"quot": '"',990}991992// Read plain text section (XML calls it character data).993// If quote >= 0, we are in a quoted string and need to find the matching quote.994// If cdata == true, we are in a <![CDATA[ section and need to find ]]>.995// On failure return nil and leave the error in d.err.996func (d *Decoder) text(quote int, cdata bool) []byte {997	var b0, b1 byte998	var trunc int999	d.buf.Reset()1000Input:1001	for {1002		b, ok := d.getc()1003		if !ok {1004			if cdata {1005				if d.err == io.EOF {1006					d.err = d.syntaxError("unexpected EOF in CDATA section")1007				}1008				return nil1009			}1010			break Input1011		}10121013		// <![CDATA[ section ends with ]]>.1014		// It is an error for ]]> to appear in ordinary text,1015		// but it is allowed in quoted strings.1016		if quote < 0 && b0 == ']' && b1 == ']' && b == '>' {1017			if cdata {1018				trunc = 21019				break Input1020			}1021			d.err = d.syntaxError("unescaped ]]> not in CDATA section")1022			return nil1023		}10241025		// Stop reading text if we see a <.1026		if b == '<' && !cdata {1027			if quote >= 0 {1028				d.err = d.syntaxError("unescaped < inside quoted string")1029				return nil1030			}1031			d.ungetc('<')1032			break Input1033		}1034		if quote >= 0 && b == byte(quote) {1035			break Input1036		}1037		if b == '&' && !cdata {1038			// Read escaped character expression up to semicolon.1039			// XML in all its glory allows a document to define and use1040			// its own character names with <!ENTITY ...> directives.1041			// Parsers are required to recognize lt, gt, amp, apos, and quot1042			// even if they have not been declared.1043			before := d.buf.Len()1044			d.buf.WriteByte('&')1045			var ok bool1046			var text string1047			var haveText bool1048			if b, ok = d.mustgetc(); !ok {1049				return nil1050			}1051			if b == '#' {1052				d.buf.WriteByte(b)1053				if b, ok = d.mustgetc(); !ok {1054					return nil1055				}1056				base := 101057				if b == 'x' {1058					base = 161059					d.buf.WriteByte(b)1060					if b, ok = d.mustgetc(); !ok {1061						return nil1062					}1063				}1064				start := d.buf.Len()1065				for '0' <= b && b <= '9' ||1066					base == 16 && 'a' <= b && b <= 'f' ||1067					base == 16 && 'A' <= b && b <= 'F' {1068					d.buf.WriteByte(b)1069					if b, ok = d.mustgetc(); !ok {1070						return nil1071					}1072				}1073				if b != ';' {1074					d.ungetc(b)1075				} else {1076					s := string(d.buf.Bytes()[start:])1077					d.buf.WriteByte(';')1078					n, err := strconv.ParseUint(s, base, 64)1079					if err == nil && n <= unicode.MaxRune {1080						text = string(rune(n))1081						haveText = true1082					}1083				}1084			} else {1085				d.ungetc(b)1086				if !d.readName() {1087					if d.err != nil {1088						return nil1089					}1090				}1091				if b, ok = d.mustgetc(); !ok {1092					return nil1093				}1094				if b != ';' {1095					d.ungetc(b)1096				} else {1097					name := d.buf.Bytes()[before+1:]1098					d.buf.WriteByte(';')1099					if isName(name) {1100						s := string(name)1101						if r, ok := entity[s]; ok {1102							text = string(r)1103							haveText = true1104						} else if d.Entity != nil {1105							text, haveText = d.Entity[s]1106						}1107					}1108				}1109			}11101111			if haveText {1112				d.buf.Truncate(before)1113				d.buf.WriteString(text)1114				b0, b1 = 0, 01115				continue Input1116			}1117			if !d.Strict {1118				b0, b1 = 0, 01119				continue Input1120			}1121			ent := string(d.buf.Bytes()[before:])1122			if ent[len(ent)-1] != ';' {1123				ent += " (no semicolon)"1124			}1125			d.err = d.syntaxError("invalid character entity " + ent)1126			return nil1127		}11281129		// We must rewrite unescaped \r and \r\n into \n.1130		if b == '\r' {1131			d.buf.WriteByte('\n')1132		} else if b1 == '\r' && b == '\n' {1133			// Skip \r\n--we already wrote \n.1134		} else {1135			d.buf.WriteByte(b)1136		}11371138		b0, b1 = b1, b1139	}1140	data := d.buf.Bytes()1141	data = data[0 : len(data)-trunc]11421143	// Inspect each rune for being a disallowed character.1144	buf := data1145	for len(buf) > 0 {1146		r, size := utf8.DecodeRune(buf)1147		if r == utf8.RuneError && size == 1 {1148			d.err = d.syntaxError("invalid UTF-8")1149			return nil1150		}1151		buf = buf[size:]1152		if !isInCharacterRange(r) {1153			d.err = d.syntaxError(fmt.Sprintf("illegal character code %U", r))1154			return nil1155		}1156	}11571158	return data1159}11601161// Decide whether the given rune is in the XML Character Range, per1162// the Char production of https://www.xml.com/axml/testaxml.htm,1163// Section 2.2 Characters.1164func isInCharacterRange(r rune) (inrange bool) {1165	return r == 0x09 ||1166		r == 0x0A ||1167		r == 0x0D ||1168		r >= 0x20 && r <= 0xD7FF ||1169		r >= 0xE000 && r <= 0xFFFD ||1170		r >= 0x10000 && r <= 0x10FFFF1171}11721173// Get name space name: name with a : stuck in the middle.1174// The part before the : is the name space identifier.1175func (d *Decoder) nsname() (name Name, ok bool) {1176	s, ok := d.name()1177	if !ok {1178		return1179	}1180	if strings.Count(s, ":") > 1 {1181		return name, false1182	} else if space, local, ok := strings.Cut(s, ":"); !ok || space == "" || local == "" {1183		name.Local = s1184	} else {1185		name.Space = space1186		name.Local = local1187	}1188	return name, true1189}11901191// Get name: /first(first|second)*/1192// Do not set d.err if the name is missing (unless unexpected EOF is received):1193// let the caller provide better context.1194func (d *Decoder) name() (s string, ok bool) {1195	d.buf.Reset()1196	if !d.readName() {1197		return "", false1198	}11991200	// Now we check the characters.1201	b := d.buf.Bytes()1202	if !isName(b) {1203		d.err = d.syntaxError("invalid XML name: " + string(b))1204		return "", false1205	}1206	return string(b), true1207}12081209// Read a name and append its bytes to d.buf.1210// The name is delimited by any single-byte character not valid in names.1211// All multi-byte characters are accepted; the caller must check their validity.1212func (d *Decoder) readName() (ok bool) {1213	var b byte1214	if b, ok = d.mustgetc(); !ok {1215		return1216	}1217	if b < utf8.RuneSelf && !isNameByte(b) {1218		d.ungetc(b)1219		return false1220	}1221	d.buf.WriteByte(b)12221223	for {1224		if b, ok = d.mustgetc(); !ok {1225			return1226		}1227		if b < utf8.RuneSelf && !isNameByte(b) {1228			d.ungetc(b)1229			break1230		}1231		d.buf.WriteByte(b)1232	}1233	return true1234}12351236func isNameByte(c byte) bool {1237	return 'A' <= c && c <= 'Z' ||1238		'a' <= c && c <= 'z' ||1239		'0' <= c && c <= '9' ||1240		c == '_' || c == ':' || c == '.' || c == '-'1241}12421243func isName(s []byte) bool {1244	if len(s) == 0 {1245		return false1246	}1247	c, n := utf8.DecodeRune(s)1248	if c == utf8.RuneError && n == 1 {1249		return false1250	}1251	if !unicode.Is(first, c) {1252		return false1253	}1254	for n < len(s) {1255		s = s[n:]1256		c, n = utf8.DecodeRune(s)1257		if c == utf8.RuneError && n == 1 {1258			return false1259		}1260		if !unicode.Is(first, c) && !unicode.Is(second, c) {1261			return false1262		}1263	}1264	return true1265}12661267func isNameString(s string) bool {1268	if len(s) == 0 {1269		return false1270	}1271	c, n := utf8.DecodeRuneInString(s)1272	if c == utf8.RuneError && n == 1 {1273		return false1274	}1275	if !unicode.Is(first, c) {1276		return false1277	}1278	for n < len(s) {1279		s = s[n:]1280		c, n = utf8.DecodeRuneInString(s)1281		if c == utf8.RuneError && n == 1 {1282			return false1283		}1284		if !unicode.Is(first, c) && !unicode.Is(second, c) {1285			return false1286		}1287	}1288	return true1289}12901291// These tables were generated by cut and paste from Appendix B of1292// the XML spec at https://www.xml.com/axml/testaxml.htm1293// and then reformatting. First corresponds to (Letter | '_' | ':')1294// and second corresponds to NameChar.12951296var first = &unicode.RangeTable{1297	R16: []unicode.Range16{1298		{0x003A, 0x003A, 1},1299		{0x0041, 0x005A, 1},1300		{0x005F, 0x005F, 1},1301		{0x0061, 0x007A, 1},1302		{0x00C0, 0x00D6, 1},1303		{0x00D8, 0x00F6, 1},1304		{0x00F8, 0x00FF, 1},1305		{0x0100, 0x0131, 1},1306		{0x0134, 0x013E, 1},1307		{0x0141, 0x0148, 1},1308		{0x014A, 0x017E, 1},1309		{0x0180, 0x01C3, 1},1310		{0x01CD, 0x01F0, 1},1311		{0x01F4, 0x01F5, 1},1312		{0x01FA, 0x0217, 1},1313		{0x0250, 0x02A8, 1},1314		{0x02BB, 0x02C1, 1},1315		{0x0386, 0x0386, 1},1316		{0x0388, 0x038A, 1},1317		{0x038C, 0x038C, 1},1318		{0x038E, 0x03A1, 1},1319		{0x03A3, 0x03CE, 1},1320		{0x03D0, 0x03D6, 1},1321		{0x03DA, 0x03E0, 2},1322		{0x03E2, 0x03F3, 1},1323		{0x0401, 0x040C, 1},1324		{0x040E, 0x044F, 1},1325		{0x0451, 0x045C, 1},1326		{0x045E, 0x0481, 1},1327		{0x0490, 0x04C4, 1},1328		{0x04C7, 0x04C8, 1},1329		{0x04CB, 0x04CC, 1},1330		{0x04D0, 0x04EB, 1},1331		{0x04EE, 0x04F5, 1},1332		{0x04F8, 0x04F9, 1},1333		{0x0531, 0x0556, 1},1334		{0x0559, 0x0559, 1},1335		{0x0561, 0x0586, 1},1336		{0x05D0, 0x05EA, 1},1337		{0x05F0, 0x05F2, 1},1338		{0x0621, 0x063A, 1},1339		{0x0641, 0x064A, 1},1340		{0x0671, 0x06B7, 1},1341		{0x06BA, 0x06BE, 1},1342		{0x06C0, 0x06CE, 1},1343		{0x06D0, 0x06D3, 1},1344		{0x06D5, 0x06D5, 1},1345		{0x06E5, 0x06E6, 1},1346		{0x0905, 0x0939, 1},1347		{0x093D, 0x093D, 1},1348		{0x0958, 0x0961, 1},1349		{0x0985, 0x098C, 1},1350		{0x098F, 0x0990, 1},1351		{0x0993, 0x09A8, 1},1352		{0x09AA, 0x09B0, 1},1353		{0x09B2, 0x09B2, 1},1354		{0x09B6, 0x09B9, 1},1355		{0x09DC, 0x09DD, 1},1356		{0x09DF, 0x09E1, 1},1357		{0x09F0, 0x09F1, 1},1358		{0x0A05, 0x0A0A, 1},1359		{0x0A0F, 0x0A10, 1},1360		{0x0A13, 0x0A28, 1},1361		{0x0A2A, 0x0A30, 1},1362		{0x0A32, 0x0A33, 1},1363		{0x0A35, 0x0A36, 1},1364		{0x0A38, 0x0A39, 1},1365		{0x0A59, 0x0A5C, 1},1366		{0x0A5E, 0x0A5E, 1},1367		{0x0A72, 0x0A74, 1},1368		{0x0A85, 0x0A8B, 1},1369		{0x0A8D, 0x0A8D, 1},1370		{0x0A8F, 0x0A91, 1},1371		{0x0A93, 0x0AA8, 1},1372		{0x0AAA, 0x0AB0, 1},1373		{0x0AB2, 0x0AB3, 1},1374		{0x0AB5, 0x0AB9, 1},1375		{0x0ABD, 0x0AE0, 0x23},1376		{0x0B05, 0x0B0C, 1},1377		{0x0B0F, 0x0B10, 1},1378		{0x0B13, 0x0B28, 1},1379		{0x0B2A, 0x0B30, 1},1380		{0x0B32, 0x0B33, 1},1381		{0x0B36, 0x0B39, 1},1382		{0x0B3D, 0x0B3D, 1},1383		{0x0B5C, 0x0B5D, 1},1384		{0x0B5F, 0x0B61, 1},1385		{0x0B85, 0x0B8A, 1},1386		{0x0B8E, 0x0B90, 1},1387		{0x0B92, 0x0B95, 1},1388		{0x0B99, 0x0B9A, 1},1389		{0x0B9C, 0x0B9C, 1},1390		{0x0B9E, 0x0B9F, 1},1391		{0x0BA3, 0x0BA4, 1},1392		{0x0BA8, 0x0BAA, 1},1393		{0x0BAE, 0x0BB5, 1},1394		{0x0BB7, 0x0BB9, 1},1395		{0x0C05, 0x0C0C, 1},1396		{0x0C0E, 0x0C10, 1},1397		{0x0C12, 0x0C28, 1},1398		{0x0C2A, 0x0C33, 1},1399		{0x0C35, 0x0C39, 1},1400		{0x0C60, 0x0C61, 1},1401		{0x0C85, 0x0C8C, 1},1402		{0x0C8E, 0x0C90, 1},1403		{0x0C92, 0x0CA8, 1},1404		{0x0CAA, 0x0CB3, 1},1405		{0x0CB5, 0x0CB9, 1},1406		{0x0CDE, 0x0CDE, 1},1407		{0x0CE0, 0x0CE1, 1},1408		{0x0D05, 0x0D0C, 1},1409		{0x0D0E, 0x0D10, 1},1410		{0x0D12, 0x0D28, 1},1411		{0x0D2A, 0x0D39, 1},1412		{0x0D60, 0x0D61, 1},1413		{0x0E01, 0x0E2E, 1},1414		{0x0E30, 0x0E30, 1},1415		{0x0E32, 0x0E33, 1},1416		{0x0E40, 0x0E45, 1},1417		{0x0E81, 0x0E82, 1},1418		{0x0E84, 0x0E84, 1},1419		{0x0E87, 0x0E88, 1},1420		{0x0E8A, 0x0E8D, 3},1421		{0x0E94, 0x0E97, 1},1422		{0x0E99, 0x0E9F, 1},1423		{0x0EA1, 0x0EA3, 1},1424		{0x0EA5, 0x0EA7, 2},1425		{0x0EAA, 0x0EAB, 1},1426		{0x0EAD, 0x0EAE, 1},1427		{0x0EB0, 0x0EB0, 1},1428		{0x0EB2, 0x0EB3, 1},1429		{0x0EBD, 0x0EBD, 1},1430		{0x0EC0, 0x0EC4, 1},1431		{0x0F40, 0x0F47, 1},1432		{0x0F49, 0x0F69, 1},1433		{0x10A0, 0x10C5, 1},1434		{0x10D0, 0x10F6, 1},1435		{0x1100, 0x1100, 1},1436		{0x1102, 0x1103, 1},1437		{0x1105, 0x1107, 1},1438		{0x1109, 0x1109, 1},1439		{0x110B, 0x110C, 1},1440		{0x110E, 0x1112, 1},1441		{0x113C, 0x1140, 2},1442		{0x114C, 0x1150, 2},1443		{0x1154, 0x1155, 1},1444		{0x1159, 0x1159, 1},1445		{0x115F, 0x1161, 1},1446		{0x1163, 0x1169, 2},1447		{0x116D, 0x116E, 1},1448		{0x1172, 0x1173, 1},1449		{0x1175, 0x119E, 0x119E - 0x1175},1450		{0x11A8, 0x11AB, 0x11AB - 0x11A8},1451		{0x11AE, 0x11AF, 1},1452		{0x11B7, 0x11B8, 1},1453		{0x11BA, 0x11BA, 1},1454		{0x11BC, 0x11C2, 1},1455		{0x11EB, 0x11F0, 0x11F0 - 0x11EB},1456		{0x11F9, 0x11F9, 1},1457		{0x1E00, 0x1E9B, 1},1458		{0x1EA0, 0x1EF9, 1},1459		{0x1F00, 0x1F15, 1},1460		{0x1F18, 0x1F1D, 1},1461		{0x1F20, 0x1F45, 1},1462		{0x1F48, 0x1F4D, 1},1463		{0x1F50, 0x1F57, 1},1464		{0x1F59, 0x1F5B, 0x1F5B - 0x1F59},1465		{0x1F5D, 0x1F5D, 1},1466		{0x1F5F, 0x1F7D, 1},1467		{0x1F80, 0x1FB4, 1},1468		{0x1FB6, 0x1FBC, 1},1469		{0x1FBE, 0x1FBE, 1},1470		{0x1FC2, 0x1FC4, 1},1471		{0x1FC6, 0x1FCC, 1},1472		{0x1FD0, 0x1FD3, 1},1473		{0x1FD6, 0x1FDB, 1},1474		{0x1FE0, 0x1FEC, 1},1475		{0x1FF2, 0x1FF4, 1},1476		{0x1FF6, 0x1FFC, 1},1477		{0x2126, 0x2126, 1},1478		{0x212A, 0x212B, 1},1479		{0x212E, 0x212E, 1},1480		{0x2180, 0x2182, 1},1481		{0x3007, 0x3007, 1},1482		{0x3021, 0x3029, 1},1483		{0x3041, 0x3094, 1},1484		{0x30A1, 0x30FA, 1},1485		{0x3105, 0x312C, 1},1486		{0x4E00, 0x9FA5, 1},1487		{0xAC00, 0xD7A3, 1},1488	},1489}14901491var second = &unicode.RangeTable{1492	R16: []unicode.Range16{1493		{0x002D, 0x002E, 1},1494		{0x0030, 0x0039, 1},1495		{0x00B7, 0x00B7, 1},1496		{0x02D0, 0x02D1, 1},1497		{0x0300, 0x0345, 1},1498		{0x0360, 0x0361, 1},1499		{0x0387, 0x0387, 1},1500		{0x0483, 0x0486, 1},1501		{0x0591, 0x05A1, 1},1502		{0x05A3, 0x05B9, 1},1503		{0x05BB, 0x05BD, 1},1504		{0x05BF, 0x05BF, 1},1505		{0x05C1, 0x05C2, 1},1506		{0x05C4, 0x0640, 0x0640 - 0x05C4},1507		{0x064B, 0x0652, 1},1508		{0x0660, 0x0669, 1},1509		{0x0670, 0x0670, 1},1510		{0x06D6, 0x06DC, 1},1511		{0x06DD, 0x06DF, 1},1512		{0x06E0, 0x06E4, 1},1513		{0x06E7, 0x06E8, 1},1514		{0x06EA, 0x06ED, 1},1515		{0x06F0, 0x06F9, 1},1516		{0x0901, 0x0903, 1},1517		{0x093C, 0x093C, 1},1518		{0x093E, 0x094C, 1},1519		{0x094D, 0x094D, 1},1520		{0x0951, 0x0954, 1},1521		{0x0962, 0x0963, 1},1522		{0x0966, 0x096F, 1},1523		{0x0981, 0x0983, 1},1524		{0x09BC, 0x09BC, 1},1525		{0x09BE, 0x09BF, 1},1526		{0x09C0, 0x09C4, 1},1527		{0x09C7, 0x09C8, 1},1528		{0x09CB, 0x09CD, 1},1529		{0x09D7, 0x09D7, 1},1530		{0x09E2, 0x09E3, 1},1531		{0x09E6, 0x09EF, 1},1532		{0x0A02, 0x0A3C, 0x3A},1533		{0x0A3E, 0x0A3F, 1},1534		{0x0A40, 0x0A42, 1},1535		{0x0A47, 0x0A48, 1},1536		{0x0A4B, 0x0A4D, 1},1537		{0x0A66, 0x0A6F, 1},1538		{0x0A70, 0x0A71, 1},1539		{0x0A81, 0x0A83, 1},1540		{0x0ABC, 0x0ABC, 1},1541		{0x0ABE, 0x0AC5, 1},1542		{0x0AC7, 0x0AC9, 1},1543		{0x0ACB, 0x0ACD, 1},1544		{0x0AE6, 0x0AEF, 1},1545		{0x0B01, 0x0B03, 1},1546		{0x0B3C, 0x0B3C, 1},1547		{0x0B3E, 0x0B43, 1},1548		{0x0B47, 0x0B48, 1},1549		{0x0B4B, 0x0B4D, 1},1550		{0x0B56, 0x0B57, 1},1551		{0x0B66, 0x0B6F, 1},1552		{0x0B82, 0x0B83, 1},1553		{0x0BBE, 0x0BC2, 1},1554		{0x0BC6, 0x0BC8, 1},1555		{0x0BCA, 0x0BCD, 1},1556		{0x0BD7, 0x0BD7, 1},1557		{0x0BE7, 0x0BEF, 1},1558		{0x0C01, 0x0C03, 1},1559		{0x0C3E, 0x0C44, 1},1560		{0x0C46, 0x0C48, 1},1561		{0x0C4A, 0x0C4D, 1},1562		{0x0C55, 0x0C56, 1},1563		{0x0C66, 0x0C6F, 1},1564		{0x0C82, 0x0C83, 1},1565		{0x0CBE, 0x0CC4, 1},1566		{0x0CC6, 0x0CC8, 1},1567		{0x0CCA, 0x0CCD, 1},1568		{0x0CD5, 0x0CD6, 1},1569		{0x0CE6, 0x0CEF, 1},1570		{0x0D02, 0x0D03, 1},1571		{0x0D3E, 0x0D43, 1},1572		{0x0D46, 0x0D48, 1},1573		{0x0D4A, 0x0D4D, 1},1574		{0x0D57, 0x0D57, 1},1575		{0x0D66, 0x0D6F, 1},1576		{0x0E31, 0x0E31, 1},1577		{0x0E34, 0x0E3A, 1},1578		{0x0E46, 0x0E46, 1},1579		{0x0E47, 0x0E4E, 1},1580		{0x0E50, 0x0E59, 1},1581		{0x0EB1, 0x0EB1, 1},1582		{0x0EB4, 0x0EB9, 1},1583		{0x0EBB, 0x0EBC, 1},1584		{0x0EC6, 0x0EC6, 1},1585		{0x0EC8, 0x0ECD, 1},1586		{0x0ED0, 0x0ED9, 1},1587		{0x0F18, 0x0F19, 1},1588		{0x0F20, 0x0F29, 1},1589		{0x0F35, 0x0F39, 2},1590		{0x0F3E, 0x0F3F, 1},1591		{0x0F71, 0x0F84, 1},1592		{0x0F86, 0x0F8B, 1},1593		{0x0F90, 0x0F95, 1},1594		{0x0F97, 0x0F97, 1},1595		{0x0F99, 0x0FAD, 1},1596		{0x0FB1, 0x0FB7, 1},1597		{0x0FB9, 0x0FB9, 1},1598		{0x20D0, 0x20DC, 1},1599		{0x20E1, 0x3005, 0x3005 - 0x20E1},1600		{0x302A, 0x302F, 1},1601		{0x3031, 0x3035, 1},1602		{0x3099, 0x309A, 1},1603		{0x309D, 0x309E, 1},1604		{0x30FC, 0x30FE, 1},1605	},1606}16071608// HTMLEntity is an entity map containing translations for the1609// standard HTML entity characters.1610//1611// See the [Decoder.Strict] and [Decoder.Entity] fields' documentation.1612var HTMLEntity map[string]string = htmlEntity16131614var htmlEntity = map[string]string{1615	/*1616		hget http://www.w3.org/TR/html4/sgml/entities.html |1617		ssam '1618			,y /\&gt;/ x/\&lt;(.|\n)+/ s/\n/ /g1619			,x v/^\&lt;!ENTITY/d1620			,s/\&lt;!ENTITY ([^ ]+) .*U\+([0-9A-F][0-9A-F][0-9A-F][0-9A-F]) .+/	"\1": "\\u\2",/g1621		'1622	*/1623	"nbsp":     "\u00A0",1624	"iexcl":    "\u00A1",1625	"cent":     "\u00A2",1626	"pound":    "\u00A3",1627	"curren":   "\u00A4",1628	"yen":      "\u00A5",1629	"brvbar":   "\u00A6",1630	"sect":     "\u00A7",1631	"uml":      "\u00A8",1632	"copy":     "\u00A9",1633	"ordf":     "\u00AA",1634	"laquo":    "\u00AB",1635	"not":      "\u00AC",1636	"shy":      "\u00AD",1637	"reg":      "\u00AE",1638	"macr":     "\u00AF",1639	"deg":      "\u00B0",1640	"plusmn":   "\u00B1",1641	"sup2":     "\u00B2",1642	"sup3":     "\u00B3",1643	"acute":    "\u00B4",1644	"micro":    "\u00B5",1645	"para":     "\u00B6",1646	"middot":   "\u00B7",1647	"cedil":    "\u00B8",1648	"sup1":     "\u00B9",1649	"ordm":     "\u00BA",1650	"raquo":    "\u00BB",1651	"frac14":   "\u00BC",1652	"frac12":   "\u00BD",1653	"frac34":   "\u00BE",1654	"iquest":   "\u00BF",1655	"Agrave":   "\u00C0",1656	"Aacute":   "\u00C1",1657	"Acirc":    "\u00C2",1658	"Atilde":   "\u00C3",1659	"Auml":     "\u00C4",1660	"Aring":    "\u00C5",1661	"AElig":    "\u00C6",1662	"Ccedil":   "\u00C7",1663	"Egrave":   "\u00C8",1664	"Eacute":   "\u00C9",1665	"Ecirc":    "\u00CA",1666	"Euml":     "\u00CB",1667	"Igrave":   "\u00CC",1668	"Iacute":   "\u00CD",1669	"Icirc":    "\u00CE",1670	"Iuml":     "\u00CF",1671	"ETH":      "\u00D0",1672	"Ntilde":   "\u00D1",1673	"Ograve":   "\u00D2",1674	"Oacute":   "\u00D3",1675	"Ocirc":    "\u00D4",1676	"Otilde":   "\u00D5",1677	"Ouml":     "\u00D6",1678	"times":    "\u00D7",1679	"Oslash":   "\u00D8",1680	"Ugrave":   "\u00D9",1681	"Uacute":   "\u00DA",1682	"Ucirc":    "\u00DB",1683	"Uuml":     "\u00DC",1684	"Yacute":   "\u00DD",1685	"THORN":    "\u00DE",1686	"szlig":    "\u00DF",1687	"agrave":   "\u00E0",1688	"aacute":   "\u00E1",1689	"acirc":    "\u00E2",1690	"atilde":   "\u00E3",1691	"auml":     "\u00E4",1692	"aring":    "\u00E5",1693	"aelig":    "\u00E6",1694	"ccedil":   "\u00E7",1695	"egrave":   "\u00E8",1696	"eacute":   "\u00E9",1697	"ecirc":    "\u00EA",1698	"euml":     "\u00EB",1699	"igrave":   "\u00EC",1700	"iacute":   "\u00ED",1701	"icirc":    "\u00EE",1702	"iuml":     "\u00EF",1703	"eth":      "\u00F0",1704	"ntilde":   "\u00F1",1705	"ograve":   "\u00F2",1706	"oacute":   "\u00F3",1707	"ocirc":    "\u00F4",1708	"otilde":   "\u00F5",1709	"ouml":     "\u00F6",1710	"divide":   "\u00F7",1711	"oslash":   "\u00F8",1712	"ugrave":   "\u00F9",1713	"uacute":   "\u00FA",1714	"ucirc":    "\u00FB",1715	"uuml":     "\u00FC",1716	"yacute":   "\u00FD",1717	"thorn":    "\u00FE",1718	"yuml":     "\u00FF",1719	"fnof":     "\u0192",1720	"Alpha":    "\u0391",1721	"Beta":     "\u0392",1722	"Gamma":    "\u0393",1723	"Delta":    "\u0394",1724	"Epsilon":  "\u0395",1725	"Zeta":     "\u0396",1726	"Eta":      "\u0397",1727	"Theta":    "\u0398",1728	"Iota":     "\u0399",1729	"Kappa":    "\u039A",1730	"Lambda":   "\u039B",1731	"Mu":       "\u039C",1732	"Nu":       "\u039D",1733	"Xi":       "\u039E",1734	"Omicron":  "\u039F",1735	"Pi":       "\u03A0",1736	"Rho":      "\u03A1",1737	"Sigma":    "\u03A3",1738	"Tau":      "\u03A4",1739	"Upsilon":  "\u03A5",1740	"Phi":      "\u03A6",1741	"Chi":      "\u03A7",1742	"Psi":      "\u03A8",1743	"Omega":    "\u03A9",1744	"alpha":    "\u03B1",1745	"beta":     "\u03B2",1746	"gamma":    "\u03B3",1747	"delta":    "\u03B4",1748	"epsilon":  "\u03B5",1749	"zeta":     "\u03B6",1750	"eta":      "\u03B7",1751	"theta":    "\u03B8",1752	"iota":     "\u03B9",1753	"kappa":    "\u03BA",1754	"lambda":   "\u03BB",1755	"mu":       "\u03BC",1756	"nu":       "\u03BD",1757	"xi":       "\u03BE",1758	"omicron":  "\u03BF",1759	"pi":       "\u03C0",1760	"rho":      "\u03C1",1761	"sigmaf":   "\u03C2",1762	"sigma":    "\u03C3",1763	"tau":      "\u03C4",1764	"upsilon":  "\u03C5",1765	"phi":      "\u03C6",1766	"chi":      "\u03C7",1767	"psi":      "\u03C8",1768	"omega":    "\u03C9",1769	"thetasym": "\u03D1",1770	"upsih":    "\u03D2",1771	"piv":      "\u03D6",1772	"bull":     "\u2022",1773	"hellip":   "\u2026",1774	"prime":    "\u2032",1775	"Prime":    "\u2033",1776	"oline":    "\u203E",1777	"frasl":    "\u2044",1778	"weierp":   "\u2118",1779	"image":    "\u2111",1780	"real":     "\u211C",1781	"trade":    "\u2122",1782	"alefsym":  "\u2135",1783	"larr":     "\u2190",1784	"uarr":     "\u2191",1785	"rarr":     "\u2192",1786	"darr":     "\u2193",1787	"harr":     "\u2194",1788	"crarr":    "\u21B5",1789	"lArr":     "\u21D0",1790	"uArr":     "\u21D1",1791	"rArr":     "\u21D2",1792	"dArr":     "\u21D3",1793	"hArr":     "\u21D4",1794	"forall":   "\u2200",1795	"part":     "\u2202",1796	"exist":    "\u2203",1797	"empty":    "\u2205",1798	"nabla":    "\u2207",1799	"isin":     "\u2208",1800	"notin":    "\u2209",1801	"ni":       "\u220B",1802	"prod":     "\u220F",1803	"sum":      "\u2211",1804	"minus":    "\u2212",1805	"lowast":   "\u2217",1806	"radic":    "\u221A",1807	"prop":     "\u221D",1808	"infin":    "\u221E",1809	"ang":      "\u2220",1810	"and":      "\u2227",1811	"or":       "\u2228",1812	"cap":      "\u2229",1813	"cup":      "\u222A",1814	"int":      "\u222B",1815	"there4":   "\u2234",1816	"sim":      "\u223C",1817	"cong":     "\u2245",1818	"asymp":    "\u2248",1819	"ne":       "\u2260",1820	"equiv":    "\u2261",1821	"le":       "\u2264",1822	"ge":       "\u2265",1823	"sub":      "\u2282",1824	"sup":      "\u2283",1825	"nsub":     "\u2284",1826	"sube":     "\u2286",1827	"supe":     "\u2287",1828	"oplus":    "\u2295",1829	"otimes":   "\u2297",1830	"perp":     "\u22A5",1831	"sdot":     "\u22C5",1832	"lceil":    "\u2308",1833	"rceil":    "\u2309",1834	"lfloor":   "\u230A",1835	"rfloor":   "\u230B",1836	"lang":     "\u2329",1837	"rang":     "\u232A",1838	"loz":      "\u25CA",1839	"spades":   "\u2660",1840	"clubs":    "\u2663",1841	"hearts":   "\u2665",1842	"diams":    "\u2666",1843	"quot":     "\u0022",1844	"amp":      "\u0026",1845	"lt":       "\u003C",1846	"gt":       "\u003E",1847	"OElig":    "\u0152",1848	"oelig":    "\u0153",1849	"Scaron":   "\u0160",1850	"scaron":   "\u0161",1851	"Yuml":     "\u0178",1852	"circ":     "\u02C6",1853	"tilde":    "\u02DC",1854	"ensp":     "\u2002",1855	"emsp":     "\u2003",1856	"thinsp":   "\u2009",1857	"zwnj":     "\u200C",1858	"zwj":      "\u200D",1859	"lrm":      "\u200E",1860	"rlm":      "\u200F",1861	"ndash":    "\u2013",1862	"mdash":    "\u2014",1863	"lsquo":    "\u2018",1864	"rsquo":    "\u2019",1865	"sbquo":    "\u201A",1866	"ldquo":    "\u201C",1867	"rdquo":    "\u201D",1868	"bdquo":    "\u201E",1869	"dagger":   "\u2020",1870	"Dagger":   "\u2021",1871	"permil":   "\u2030",1872	"lsaquo":   "\u2039",1873	"rsaquo":   "\u203A",1874	"euro":     "\u20AC",1875}18761877// HTMLAutoClose is the set of HTML elements that1878// should be considered to close automatically.1879//1880// See the [Decoder.Strict] and [Decoder.Entity] fields' documentation.1881var HTMLAutoClose []string = htmlAutoClose18821883var htmlAutoClose = []string{1884	/*1885		hget http://www.w3.org/TR/html4/loose.dtd |1886		9 sed -n 's/<!ELEMENT ([^ ]*) +- O EMPTY.+/	"\1",/p' | tr A-Z a-z1887	*/1888	"basefont",1889	"br",1890	"area",1891	"link",1892	"img",1893	"param",1894	"hr",1895	"input",1896	"col",1897	"frame",1898	"isindex",1899	"base",1900	"meta",1901}19021903var (1904	escQuot = []byte("&#34;") // shorter than "&quot;"1905	escApos = []byte("&#39;") // shorter than "&apos;"1906	escAmp  = []byte("&amp;")1907	escLT   = []byte("&lt;")1908	escGT   = []byte("&gt;")1909	escTab  = []byte("&#x9;")1910	escNL   = []byte("&#xA;")1911	escCR   = []byte("&#xD;")1912	escFFFD = []byte("\uFFFD") // Unicode replacement character1913)19141915// EscapeText writes to w the properly escaped XML equivalent1916// of the plain text data s.1917func EscapeText(w io.Writer, s []byte) error {1918	return escapeText(w, s, true)1919}19201921// escapeText writes to w the properly escaped XML equivalent1922// of the plain text data s. If escapeNewline is true, newline1923// characters will be escaped.1924func escapeText(w io.Writer, s []byte, escapeNewline bool) error {1925	var esc []byte1926	last := 01927	for i := 0; i < len(s); {1928		r, width := utf8.DecodeRune(s[i:])1929		i += width1930		switch r {1931		case '"':1932			esc = escQuot1933		case '\'':1934			esc = escApos1935		case '&':1936			esc = escAmp1937		case '<':1938			esc = escLT1939		case '>':1940			esc = escGT1941		case '\t':1942			esc = escTab1943		case '\n':1944			if !escapeNewline {1945				continue1946			}1947			esc = escNL1948		case '\r':1949			esc = escCR1950		default:1951			if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {1952				esc = escFFFD1953				break1954			}1955			continue1956		}1957		if _, err := w.Write(s[last : i-width]); err != nil {1958			return err1959		}1960		if _, err := w.Write(esc); err != nil {1961			return err1962		}1963		last = i1964	}1965	_, err := w.Write(s[last:])1966	return err1967}19681969// EscapeString writes to p the properly escaped XML equivalent1970// of the plain text data s.1971func (p *printer) EscapeString(s string) {1972	var esc []byte1973	last := 01974	for i := 0; i < len(s); {1975		r, width := utf8.DecodeRuneInString(s[i:])1976		i += width1977		switch r {1978		case '"':1979			esc = escQuot1980		case '\'':1981			esc = escApos1982		case '&':1983			esc = escAmp1984		case '<':1985			esc = escLT1986		case '>':1987			esc = escGT1988		case '\t':1989			esc = escTab1990		case '\n':1991			esc = escNL1992		case '\r':1993			esc = escCR1994		default:1995			if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {1996				esc = escFFFD1997				break1998			}1999			continue2000		}

Code quality findings 10

Declared map variable without initialization; writing to a nil map causes a panic. Use make() to initialize
warning correctness nil-map-write
var entity = map[string]rune{
Declared map variable without initialization; writing to a nil map causes a panic. Use make() to initialize
warning correctness nil-map-write
var HTMLEntity map[string]string = htmlEntity
Declared map variable without initialization; writing to a nil map causes a panic. Use make() to initialize
warning correctness nil-map-write
var htmlEntity = map[string]string{
Type switch without default case; unhandled types will silently do nothing. Add a default case for safety
info correctness unchecked-type-switch
switch v := t.(type) {
Type switch without default case; unhandled types will silently do nothing. Add a default case for safety
info correctness unchecked-type-switch
switch t1 := t.(type) {
Deeply nested control structures reduce readability; consider extracting to functions or using early returns
info maintainability deep-nesting
if !ok || !strings.EqualFold(et.Name.Local, d.stk.name.Local) {
Deeply nested control structures reduce readability; consider extracting to functions or using early returns
info maintainability deep-nesting
for {
Deeply nested control structures reduce readability; consider extracting to functions or using early returns
info maintainability deep-nesting
if !ok {
Deeply nested control structures reduce readability; consider extracting to functions or using early returns
info maintainability deep-nesting
if cdata {
Deeply nested control structures reduce readability; consider extracting to functions or using early returns
info maintainability deep-nesting
if d.err == io.EOF {

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.