Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
66caa38d
Commit
66caa38d
authored
Mar 23, 2010
by
Kyle Consalus
Committed by
Russ Cox
Mar 23, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
xml: add CopyToken
R=rsc CC=golang-dev
https://golang.org/cl/634042
parent
9e0ae94e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
2 deletions
+52
-2
src/pkg/xml/xml.go
src/pkg/xml/xml.go
+26
-2
src/pkg/xml/xml_test.go
src/pkg/xml/xml_test.go
+26
-0
No files found.
src/pkg/xml/xml.go
View file @
66caa38d
...
...
@@ -55,6 +55,13 @@ type StartElement struct {
Attr
[]
Attr
}
func
(
e
StartElement
)
Copy
()
StartElement
{
attrs
:=
make
([]
Attr
,
len
(
e
.
Attr
))
copy
(
e
.
Attr
,
attrs
)
e
.
Attr
=
attrs
return
e
}
// An EndElement represents an XML end element.
type
EndElement
struct
{
Name
Name
...
...
@@ -100,6 +107,23 @@ type readByter interface {
ReadByte
()
(
b
byte
,
err
os
.
Error
)
}
// CopyToken returns a copy of a Token.
func
CopyToken
(
t
Token
)
Token
{
switch
v
:=
t
.
(
type
)
{
case
CharData
:
return
v
.
Copy
()
case
Comment
:
return
v
.
Copy
()
case
Directive
:
return
v
.
Copy
()
case
ProcInst
:
return
v
.
Copy
()
case
StartElement
:
return
v
.
Copy
()
}
return
t
}
// A Parser represents an XML parser reading a particular input stream.
// The parser assumes that its input is encoded in UTF-8.
type
Parser
struct
{
...
...
@@ -180,8 +204,8 @@ func NewParser(r io.Reader) *Parser {
//
// Slices of bytes in the returned token data refer to the
// parser's internal buffer and remain valid only until the next
// call to Token. To acquire a copy of the bytes, call
the token's
// Copy method.
// call to Token. To acquire a copy of the bytes, call
CopyToken
//
or the token's
Copy method.
//
// Token expands self-closing elements such as <br/>
// into separate start and end elements returned by successive calls.
...
...
src/pkg/xml/xml_test.go
View file @
66caa38d
...
...
@@ -328,3 +328,29 @@ func TestUnquotedAttrs(t *testing.T) {
t
.
Errorf
(
"Unexpected attribute name: %v"
,
attr
.
Name
.
Local
)
}
}
func
TestCopyTokenCharData
(
t
*
testing
.
T
)
{
data
:=
[]
byte
(
"same data"
)
var
tok1
Token
=
CharData
(
data
)
tok2
:=
CopyToken
(
tok1
)
if
!
reflect
.
DeepEqual
(
tok1
,
tok2
)
{
t
.
Error
(
"CopyToken(CharData) != CharData"
)
}
data
[
1
]
=
'o'
if
reflect
.
DeepEqual
(
tok1
,
tok2
)
{
t
.
Error
(
"CopyToken(CharData) uses same buffer."
)
}
}
func
TestCopyTokenStartElement
(
t
*
testing
.
T
)
{
elt
:=
StartElement
{
Name
{
""
,
"hello"
},
[]
Attr
{
Attr
{
Name
{
""
,
"lang"
},
"en"
}}}
var
tok1
Token
=
elt
tok2
:=
CopyToken
(
tok1
)
if
!
reflect
.
DeepEqual
(
tok1
,
tok2
)
{
t
.
Error
(
"CopyToken(StartElement) != StartElement"
)
}
elt
.
Attr
[
0
]
=
Attr
{
Name
{
""
,
"lang"
},
"de"
}
if
reflect
.
DeepEqual
(
tok1
,
tok2
)
{
t
.
Error
(
"CopyToken(CharData) uses same buffer."
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment