Skip to content

Commit

Permalink
refactor: rename scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
Youen Péron committed Nov 14, 2023
1 parent 98c0b15 commit 9f72027
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions pkg/xixo/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ type XMLParser struct {
skipOuterElements bool
xpathEnabled bool
scratch *scratch
scratch2 *scratch
scratchInnerText *scratch
scratchWriter *scratch
scratchOuterText *scratch
deffer bool
TotalReadSize uint64
nextWrite *byte
Expand All @@ -36,9 +35,8 @@ func NewXMLParser(reader io.Reader, writer io.Writer) *XMLParser {
resultChannel: make(chan *XMLElement, 256),
skipElements: map[string]bool{},
scratch: &scratch{data: make([]byte, 1024)},
scratch2: &scratch{data: make([]byte, 1024)},
scratchInnerText: &scratch{data: make([]byte, 1024)},
scratchWriter: &scratch{data: make([]byte, 1024)},
scratchOuterText: &scratch{data: make([]byte, 1024)},
}
}

Expand Down Expand Up @@ -172,8 +170,7 @@ func (x *XMLParser) parse() error {
return err
}

x.scratch2.reset()
x.scratchOuterText.reset()
x.scratchInnerText.reset()

continue
}
Expand Down Expand Up @@ -235,9 +232,8 @@ func (x *XMLParser) getElementTree(result *XMLElement) *XMLElement {
iscomment bool
)

result.outerTextBefore = string(x.scratch2.bytes())
x.scratchOuterText.reset()
x.scratch2.reset() // this hold the inner text
result.outerTextBefore = string(x.scratchInnerText.bytes())
x.scratchInnerText.reset() // this hold the inner text

for {
cur, err = x.readByte()
Expand All @@ -258,7 +254,7 @@ func (x *XMLParser) getElementTree(result *XMLElement) *XMLElement {

if iscdata {
for _, cd := range cddata {
x.scratch2.add(cd)
x.scratchInnerText.add(cd)
}

continue
Expand Down Expand Up @@ -293,8 +289,8 @@ func (x *XMLParser) getElementTree(result *XMLElement) *XMLElement {
}

if tag == result.Name {
result.InnerText = string(x.scratch2.bytes())
x.scratch2.reset()
result.InnerText = string(x.scratchInnerText.bytes())
x.scratchInnerText.reset()

return result
}
Expand Down Expand Up @@ -351,8 +347,7 @@ func (x *XMLParser) getElementTree(result *XMLElement) *XMLElement {
}
}
} else {
x.scratch2.add(cur)
x.scratchOuterText.add(cur)
x.scratchInnerText.add(cur)
}
}
}
Expand Down Expand Up @@ -438,9 +433,8 @@ func (x *XMLParser) startElement() (*XMLElement, bool, error) {
if prev == '/' {
result.Name = string(x.scratch.bytes()[:len(x.scratch.bytes())-1])
result.autoClosable = true
result.outerTextBefore = string(x.scratch2.bytes())
x.scratchOuterText.reset()
x.scratch2.reset()
result.outerTextBefore = string(x.scratchInnerText.bytes())
x.scratchInnerText.reset()

if x.xpathEnabled {
names := strings.Split(result.Name, ":")
Expand Down Expand Up @@ -578,18 +572,17 @@ func (x *XMLParser) readComment() (bool, error) {
len(x.scratch.bytes()) > 1 &&
x.scratch.bytes()[len(x.scratch.bytes())-1] == '-' &&
x.scratch.bytes()[len(x.scratch.bytes())-2] == '-' {
x.scratch2.add('<')
x.scratch2.add('!')
x.scratch2.add('-')
x.scratch2.add('-')
x.scratchInnerText.add('<')
x.scratchInnerText.add('!')
x.scratchInnerText.add('-')
x.scratchInnerText.add('-')

for _, c := range x.scratch.bytes() {
x.scratch2.add(c)
x.scratchInnerText.add(c)
}

x.scratch2.add('>')
x.scratchInnerText.add('>')

x.scratchOuterText.reset()
// x.scratch2.reset()
x.scratch.reset()

Expand Down Expand Up @@ -845,7 +838,6 @@ skipDecleration:

func (x *XMLParser) closeTagName() (string, error) {
x.scratch.reset()
x.scratchOuterText.reset()

var (
c byte
Expand Down

0 comments on commit 9f72027

Please sign in to comment.