From 6e3e3809231c71fc30b6d0cdcb1f60c5e6e816ef Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 22 Nov 2011 12:30:02 -0500 Subject: [PATCH] allow direct conversion between string and named []byte, []rune The allowed conversions before and after are: type Tstring string type Tbyte []byte type Trune []rune string <-> string // ok string <-> []byte // ok string <-> []rune // ok string <-> Tstring // ok string <-> Tbyte // was illegal, now ok string <-> Trune // was illegal, now ok Tstring <-> string // ok Tstring <-> []byte // ok Tstring <-> []rune // ok Tstring <-> Tstring // ok Tstring <-> Tbyte // was illegal, now ok Tstring <-> Trune // was illegal, now ok Update spec, compiler, tests. Use in a few packages. We agreed on this a few months ago but never implemented it. Fixes #1707. R=golang-dev, gri, r CC=golang-dev https://golang.org/cl/5421057 --- doc/go_spec.html | 24 +++++--- src/cmd/gc/subr.c | 12 ++-- src/pkg/encoding/xml/xml_test.go | 102 +++++++++++++++---------------- src/pkg/net/http/sniff.go | 34 +++++------ src/pkg/net/mail/message.go | 2 +- test/convert1.go | 96 +++++++++++++++++++++++++++++ test/convlit.go | 6 +- test/named1.go | 4 +- 8 files changed, 191 insertions(+), 89 deletions(-) create mode 100644 test/convert1.go diff --git a/doc/go_spec.html b/doc/go_spec.html index 0e52d4d230..43281c9953 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,5 +1,5 @@ - + ` var rawTokens = []Token{ - CharData([]byte("\n")), + CharData("\n"), ProcInst{"xml", []byte(`version="1.0" encoding="UTF-8"`)}, - CharData([]byte("\n")), - Directive([]byte(`DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + CharData("\n"), + Directive(`DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"`), - ), - CharData([]byte("\n")), + CharData("\n"), StartElement{Name{"", "body"}, []Attr{{Name{"xmlns", "foo"}, "ns1"}, {Name{"", "xmlns"}, "ns2"}, {Name{"xmlns", "tag"}, "ns3"}}}, - CharData([]byte("\n ")), + CharData("\n "), StartElement{Name{"", "hello"}, []Attr{{Name{"", "lang"}, "en"}}}, - CharData([]byte("World <>'\" 白鵬翔")), + CharData("World <>'\" 白鵬翔"), EndElement{Name{"", "hello"}}, - CharData([]byte("\n ")), + CharData("\n "), StartElement{Name{"", "goodbye"}, []Attr{}}, EndElement{Name{"", "goodbye"}}, - CharData([]byte("\n ")), + CharData("\n "), StartElement{Name{"", "outer"}, []Attr{{Name{"foo", "attr"}, "value"}, {Name{"xmlns", "tag"}, "ns4"}}}, - CharData([]byte("\n ")), + CharData("\n "), StartElement{Name{"", "inner"}, []Attr{}}, EndElement{Name{"", "inner"}}, - CharData([]byte("\n ")), + CharData("\n "), EndElement{Name{"", "outer"}}, - CharData([]byte("\n ")), + CharData("\n "), StartElement{Name{"tag", "name"}, []Attr{}}, - CharData([]byte("\n ")), - CharData([]byte("Some text here.")), - CharData([]byte("\n ")), + CharData("\n "), + CharData("Some text here."), + CharData("\n "), EndElement{Name{"tag", "name"}}, - CharData([]byte("\n")), + CharData("\n"), EndElement{Name{"", "body"}}, - Comment([]byte(" missing final newline ")), + Comment(" missing final newline "), } var cookedTokens = []Token{ - CharData([]byte("\n")), + CharData("\n"), ProcInst{"xml", []byte(`version="1.0" encoding="UTF-8"`)}, - CharData([]byte("\n")), - Directive([]byte(`DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + CharData("\n"), + Directive(`DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"`), - ), - CharData([]byte("\n")), + CharData("\n"), StartElement{Name{"ns2", "body"}, []Attr{{Name{"xmlns", "foo"}, "ns1"}, {Name{"", "xmlns"}, "ns2"}, {Name{"xmlns", "tag"}, "ns3"}}}, - CharData([]byte("\n ")), + CharData("\n "), StartElement{Name{"ns2", "hello"}, []Attr{{Name{"", "lang"}, "en"}}}, - CharData([]byte("World <>'\" 白鵬翔")), + CharData("World <>'\" 白鵬翔"), EndElement{Name{"ns2", "hello"}}, - CharData([]byte("\n ")), + CharData("\n "), StartElement{Name{"ns2", "goodbye"}, []Attr{}}, EndElement{Name{"ns2", "goodbye"}}, - CharData([]byte("\n ")), + CharData("\n "), StartElement{Name{"ns2", "outer"}, []Attr{{Name{"ns1", "attr"}, "value"}, {Name{"xmlns", "tag"}, "ns4"}}}, - CharData([]byte("\n ")), + CharData("\n "), StartElement{Name{"ns2", "inner"}, []Attr{}}, EndElement{Name{"ns2", "inner"}}, - CharData([]byte("\n ")), + CharData("\n "), EndElement{Name{"ns2", "outer"}}, - CharData([]byte("\n ")), + CharData("\n "), StartElement{Name{"ns3", "name"}, []Attr{}}, - CharData([]byte("\n ")), - CharData([]byte("Some text here.")), - CharData([]byte("\n ")), + CharData("\n "), + CharData("Some text here."), + CharData("\n "), EndElement{Name{"ns3", "name"}}, - CharData([]byte("\n")), + CharData("\n"), EndElement{Name{"ns2", "body"}}, - Comment([]byte(" missing final newline ")), + Comment(" missing final newline "), } const testInputAltEncoding = ` @@ -101,11 +99,11 @@ const testInputAltEncoding = ` VALUE` var rawTokensAltEncoding = []Token{ - CharData([]byte("\n")), + CharData("\n"), ProcInst{"xml", []byte(`version="1.0" encoding="x-testing-uppercase"`)}, - CharData([]byte("\n")), + CharData("\n"), StartElement{Name{"", "tag"}, []Attr{}}, - CharData([]byte("value")), + CharData("value"), EndElement{Name{"", "tag"}}, } @@ -270,21 +268,21 @@ var nestedDirectivesInput = ` ` var nestedDirectivesTokens = []Token{ - CharData([]byte("\n")), - Directive([]byte(`DOCTYPE []`)), - CharData([]byte("\n")), - Directive([]byte(`DOCTYPE [">]`)), - CharData([]byte("\n")), - Directive([]byte(`DOCTYPE []`)), - CharData([]byte("\n")), - Directive([]byte(`DOCTYPE ['>]`)), - CharData([]byte("\n")), - Directive([]byte(`DOCTYPE []`)), - CharData([]byte("\n")), - Directive([]byte(`DOCTYPE ['>]`)), - CharData([]byte("\n")), - Directive([]byte(`DOCTYPE []`)), - CharData([]byte("\n")), + CharData("\n"), + Directive(`DOCTYPE []`), + CharData("\n"), + Directive(`DOCTYPE [">]`), + CharData("\n"), + Directive(`DOCTYPE []`), + CharData("\n"), + Directive(`DOCTYPE ['>]`), + CharData("\n"), + Directive(`DOCTYPE []`), + CharData("\n"), + Directive(`DOCTYPE ['>]`), + CharData("\n"), + Directive(`DOCTYPE []`), + CharData("\n"), } func TestNestedDirectives(t *testing.T) { diff --git a/src/pkg/net/http/sniff.go b/src/pkg/net/http/sniff.go index 5707c7f057..c1c78e2417 100644 --- a/src/pkg/net/http/sniff.go +++ b/src/pkg/net/http/sniff.go @@ -48,23 +48,23 @@ type sniffSig interface { // Data matching the table in section 6. var sniffSignatures = []sniffSig{ - htmlSig([]byte("