diff options
author | bstarynk <bstarynk@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-01-16 11:17:15 +0000 |
---|---|---|
committer | bstarynk <bstarynk@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-01-16 11:17:15 +0000 |
commit | c2e9c1da57c35f46052398ab31707ceb2dfc4e09 (patch) | |
tree | afd93279258b2ca1d846fdca1df67587efe6faad /libgo | |
parent | 56664e5da23e1ca0072b690b206c297484897bd3 (diff) | |
download | gcc-c2e9c1da57c35f46052398ab31707ceb2dfc4e09.tar.gz |
2012-01-16 Basile Starynkevitch <basile@starynkevitch.net>
MELT branch merged with trunk rev 183204 using svnmerge
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/melt-branch@183208 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo')
320 files changed, 7079 insertions, 2914 deletions
diff --git a/libgo/MERGE b/libgo/MERGE index 0bcb056063c..96fb7f66498 100644 --- a/libgo/MERGE +++ b/libgo/MERGE @@ -1,4 +1,4 @@ -0c39eee85b0d +4a8268927758 The first line of this file holds the Mercurial revision number of the last merge done from the master library sources. diff --git a/libgo/Makefile.am b/libgo/Makefile.am index 4625ce232b6..348a1cae8d2 100644 --- a/libgo/Makefile.am +++ b/libgo/Makefile.am @@ -279,8 +279,7 @@ toolexeclibgoimage_DATA = \ image/gif.gox \ image/jpeg.gox \ image/png.gox \ - image/tiff.gox \ - image/ycbcr.gox + image/tiff.gox toolexeclibgoindexdir = $(toolexeclibgodir)/index @@ -576,6 +575,7 @@ go_html_files = \ go/html/doctype.go \ go/html/entity.go \ go/html/escape.go \ + go/html/foreign.go \ go/html/node.go \ go/html/parse.go \ go/html/render.go \ @@ -585,7 +585,8 @@ go_image_files = \ go/image/format.go \ go/image/geom.go \ go/image/image.go \ - go/image/names.go + go/image/names.go \ + go/image/ycbcr.go go_io_files = \ go/io/multi.go \ @@ -610,14 +611,11 @@ go_math_files = \ go/math/dim.go \ go/math/erf.go \ go/math/exp.go \ - go/math/exp_port.go \ - go/math/exp2.go \ go/math/expm1.go \ go/math/floor.go \ go/math/frexp.go \ go/math/gamma.go \ go/math/hypot.go \ - go/math/hypot_port.go \ go/math/j0.go \ go/math/j1.go \ go/math/jn.go \ @@ -638,7 +636,6 @@ go_math_files = \ go/math/sincos.go \ go/math/sinh.go \ go/math/sqrt.go \ - go/math/sqrt_port.go \ go/math/tan.go \ go/math/tanh.go \ go/math/unsafe.go @@ -657,10 +654,15 @@ if LIBGO_IS_LINUX go_net_fd_os_file = go/net/fd_linux.go go_net_newpollserver_file = go/net/newpollserver.go else # !LIBGO_IS_LINUX && !LIBGO_IS_RTEMS +if LIBGO_IS_NETBSD +go_net_fd_os_file = go/net/fd_netbsd.go +go_net_newpollserver_file = go/net/newpollserver.go +else # !LIBGO_IS_NETBSD && !LIBGO_IS_LINUX && !LIBGO_IS_RTEMS # By default use select with pipes. Most systems should have # something better. go_net_fd_os_file = go/net/fd_select.go go_net_newpollserver_file = go/net/newpollserver.go +endif # !LIBGO_IS_NETBSD endif # !LIBGO_IS_LINUX endif # !LIBGO_IS_RTEMS @@ -691,8 +693,12 @@ endif if LIBGO_IS_LINUX go_net_interface_file = go/net/interface_linux.go else +if LIBGO_IS_NETBSD +go_net_interface_file = go/net/interface_netbsd.go +else go_net_interface_file = go/net/interface_stub.go endif +endif go_net_files = \ go/net/cgo_unix.go \ @@ -848,6 +854,7 @@ go_strconv_files = \ go/strconv/atof.go \ go/strconv/atoi.go \ go/strconv/decimal.go \ + go/strconv/extfloat.go \ go/strconv/ftoa.go \ go/strconv/itoa.go \ go/strconv/quote.go @@ -883,12 +890,12 @@ go_syslog_c_files = \ go_testing_files = \ go/testing/benchmark.go \ go/testing/example.go \ - go/testing/testing.go + go/testing/testing.go \ + go/testing/wrapper.go go_time_files = \ go/time/format.go \ go/time/sleep.go \ - go/time/sys.go \ go/time/sys_unix.go \ go/time/tick.go \ go/time/time.go \ @@ -1201,7 +1208,9 @@ go_go_build_files = \ go_go_doc_files = \ go/go/doc/comment.go \ go/go/doc/doc.go \ - go/go/doc/example.go + go/go/doc/example.go \ + go/go/doc/exports.go \ + go/go/doc/filter.go go_go_parser_files = \ go/go/parser/interface.go \ go/go/parser/parser.go @@ -1245,7 +1254,8 @@ go_image_bmp_files = \ go/image/bmp/reader.go go_image_color_files = \ - go/image/color/color.go + go/image/color/color.go \ + go/image/color/ycbcr.go go_image_draw_files = \ go/image/draw/draw.go @@ -1270,9 +1280,6 @@ go_image_tiff_files = \ go/image/tiff/consts.go \ go/image/tiff/reader.go -go_image_ycbcr_files = \ - go/image/ycbcr/ycbcr.go - go_index_suffixarray_files = \ go/index/suffixarray/qsufsort.go \ go/index/suffixarray/suffixarray.go @@ -1322,6 +1329,7 @@ go_net_http_files = \ go/net/http/filetransport.go \ go/net/http/fs.go \ go/net/http/header.go \ + go/net/http/jar.go \ go/net/http/lex.go \ go/net/http/request.go \ go/net/http/response.go \ @@ -1765,7 +1773,6 @@ libgo_go_objs = \ image/jpeg.lo \ image/png.lo \ image/tiff.lo \ - image/ycbcr.lo \ index/suffixarray.lo \ io/ioutil.lo \ log/syslog.lo \ @@ -3070,16 +3077,6 @@ image/tiff/check: $(CHECK_DEPS) @$(CHECK) .PHONY: image/tiff/check -@go_include@ image/ycbcr.lo.dep -image/ycbcr.lo.dep: $(go_image_ycbcr_files) - $(BUILDDEPS) -image/ycbcr.lo: $(go_image_ycbcr_files) - $(BUILDPACKAGE) -image/ycbcr/check: $(CHECK_DEPS) - @$(MKDIR_P) image/ycbcr - @$(CHECK) -.PHONY: image/ycbcr/check - @go_include@ index/suffixarray.lo.dep index/suffixarray.lo.dep: $(go_index_suffixarray_files) $(BUILDDEPS) @@ -3732,8 +3729,6 @@ image/png.gox: image/png.lo $(BUILDGOX) image/tiff.gox: image/tiff.lo $(BUILDGOX) -image/ycbcr.gox: image/ycbcr.lo - $(BUILDGOX) index/suffixarray.gox: index/suffixarray.lo $(BUILDGOX) @@ -3942,11 +3937,11 @@ TEST_PACKAGES = \ hash/crc32/check \ hash/crc64/check \ hash/fnv/check \ + image/color/check \ image/draw/check \ image/jpeg/check \ image/png/check \ image/tiff/check \ - image/ycbcr/check \ index/suffixarray/check \ io/ioutil/check \ log/syslog/check \ diff --git a/libgo/Makefile.in b/libgo/Makefile.in index 0304f7e8996..6bf18475628 100644 --- a/libgo/Makefile.in +++ b/libgo/Makefile.in @@ -167,20 +167,19 @@ am__DEPENDENCIES_2 = bufio/bufio.lo bytes/bytes.lo bytes/index.lo \ hash/fnv.lo net/http/cgi.lo net/http/fcgi.lo \ net/http/httptest.lo net/http/httputil.lo net/http/pprof.lo \ image/bmp.lo image/color.lo image/draw.lo image/gif.lo \ - image/jpeg.lo image/png.lo image/tiff.lo image/ycbcr.lo \ - index/suffixarray.lo io/ioutil.lo log/syslog.lo \ - log/syslog/syslog_c.lo math/big.lo math/cmplx.lo math/rand.lo \ - mime/mime.lo mime/multipart.lo net/dict.lo net/http.lo \ - net/mail.lo net/rpc.lo net/smtp.lo net/textproto.lo net/url.lo \ - old/netchan.lo old/regexp.lo old/template.lo \ - $(am__DEPENDENCIES_1) os/user.lo os/signal.lo path/filepath.lo \ - regexp/syntax.lo net/rpc/jsonrpc.lo runtime/debug.lo \ - runtime/pprof.lo sync/atomic.lo sync/atomic_c.lo \ - syscall/syscall.lo syscall/errno.lo syscall/wait.lo \ - text/scanner.lo text/tabwriter.lo text/template.lo \ - text/template/parse.lo testing/testing.lo testing/iotest.lo \ - testing/quick.lo testing/script.lo unicode/utf16.lo \ - unicode/utf8.lo + image/jpeg.lo image/png.lo image/tiff.lo index/suffixarray.lo \ + io/ioutil.lo log/syslog.lo log/syslog/syslog_c.lo math/big.lo \ + math/cmplx.lo math/rand.lo mime/mime.lo mime/multipart.lo \ + net/dict.lo net/http.lo net/mail.lo net/rpc.lo net/smtp.lo \ + net/textproto.lo net/url.lo old/netchan.lo old/regexp.lo \ + old/template.lo $(am__DEPENDENCIES_1) os/user.lo os/signal.lo \ + path/filepath.lo regexp/syntax.lo net/rpc/jsonrpc.lo \ + runtime/debug.lo runtime/pprof.lo sync/atomic.lo \ + sync/atomic_c.lo syscall/syscall.lo syscall/errno.lo \ + syscall/wait.lo text/scanner.lo text/tabwriter.lo \ + text/template.lo text/template/parse.lo testing/testing.lo \ + testing/iotest.lo testing/quick.lo testing/script.lo \ + unicode/utf16.lo unicode/utf8.lo libgo_la_DEPENDENCIES = $(am__DEPENDENCIES_2) $(am__DEPENDENCIES_1) \ $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \ $(am__DEPENDENCIES_1) @@ -732,8 +731,7 @@ toolexeclibgoimage_DATA = \ image/gif.gox \ image/jpeg.gox \ image/png.gox \ - image/tiff.gox \ - image/ycbcr.gox + image/tiff.gox toolexeclibgoindexdir = $(toolexeclibgodir)/index toolexeclibgoindex_DATA = \ @@ -962,6 +960,7 @@ go_html_files = \ go/html/doctype.go \ go/html/entity.go \ go/html/escape.go \ + go/html/foreign.go \ go/html/node.go \ go/html/parse.go \ go/html/render.go \ @@ -971,7 +970,8 @@ go_image_files = \ go/image/format.go \ go/image/geom.go \ go/image/image.go \ - go/image/names.go + go/image/names.go \ + go/image/ycbcr.go go_io_files = \ go/io/multi.go \ @@ -996,14 +996,11 @@ go_math_files = \ go/math/dim.go \ go/math/erf.go \ go/math/exp.go \ - go/math/exp_port.go \ - go/math/exp2.go \ go/math/expm1.go \ go/math/floor.go \ go/math/frexp.go \ go/math/gamma.go \ go/math/hypot.go \ - go/math/hypot_port.go \ go/math/j0.go \ go/math/j1.go \ go/math/jn.go \ @@ -1024,7 +1021,6 @@ go_math_files = \ go/math/sincos.go \ go/math/sinh.go \ go/math/sqrt.go \ - go/math/sqrt_port.go \ go/math/tan.go \ go/math/tanh.go \ go/math/unsafe.go @@ -1037,10 +1033,12 @@ go_mime_files = \ # By default use select with pipes. Most systems should have # something better. -@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_RTEMS_FALSE@go_net_fd_os_file = go/net/fd_select.go +@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_NETBSD_FALSE@@LIBGO_IS_RTEMS_FALSE@go_net_fd_os_file = go/net/fd_select.go +@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_NETBSD_TRUE@@LIBGO_IS_RTEMS_FALSE@go_net_fd_os_file = go/net/fd_netbsd.go @LIBGO_IS_LINUX_TRUE@@LIBGO_IS_RTEMS_FALSE@go_net_fd_os_file = go/net/fd_linux.go @LIBGO_IS_RTEMS_TRUE@go_net_fd_os_file = go/net/fd_select.go -@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_RTEMS_FALSE@go_net_newpollserver_file = go/net/newpollserver.go +@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_NETBSD_FALSE@@LIBGO_IS_RTEMS_FALSE@go_net_newpollserver_file = go/net/newpollserver.go +@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_NETBSD_TRUE@@LIBGO_IS_RTEMS_FALSE@go_net_newpollserver_file = go/net/newpollserver.go @LIBGO_IS_LINUX_TRUE@@LIBGO_IS_RTEMS_FALSE@go_net_newpollserver_file = go/net/newpollserver.go @LIBGO_IS_RTEMS_TRUE@go_net_newpollserver_file = go/net/newpollserver_rtems.go @LIBGO_IS_IRIX_FALSE@@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_SOLARIS_FALSE@go_net_cgo_file = go/net/cgo_bsd.go @@ -1053,7 +1051,8 @@ go_mime_files = \ @LIBGO_IS_LINUX_TRUE@go_net_sock_file = go/net/sock_linux.go @LIBGO_IS_LINUX_FALSE@go_net_sendfile_file = go/net/sendfile_stub.go @LIBGO_IS_LINUX_TRUE@go_net_sendfile_file = go/net/sendfile_linux.go -@LIBGO_IS_LINUX_FALSE@go_net_interface_file = go/net/interface_stub.go +@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_NETBSD_FALSE@go_net_interface_file = go/net/interface_stub.go +@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_NETBSD_TRUE@go_net_interface_file = go/net/interface_netbsd.go @LIBGO_IS_LINUX_TRUE@go_net_interface_file = go/net/interface_linux.go go_net_files = \ go/net/cgo_unix.go \ @@ -1168,6 +1167,7 @@ go_strconv_files = \ go/strconv/atof.go \ go/strconv/atoi.go \ go/strconv/decimal.go \ + go/strconv/extfloat.go \ go/strconv/ftoa.go \ go/strconv/itoa.go \ go/strconv/quote.go @@ -1197,12 +1197,12 @@ go_syslog_c_files = \ go_testing_files = \ go/testing/benchmark.go \ go/testing/example.go \ - go/testing/testing.go + go/testing/testing.go \ + go/testing/wrapper.go go_time_files = \ go/time/format.go \ go/time/sleep.go \ - go/time/sys.go \ go/time/sys_unix.go \ go/time/tick.go \ go/time/time.go \ @@ -1567,7 +1567,9 @@ go_go_build_files = \ go_go_doc_files = \ go/go/doc/comment.go \ go/go/doc/doc.go \ - go/go/doc/example.go + go/go/doc/example.go \ + go/go/doc/exports.go \ + go/go/doc/filter.go go_go_parser_files = \ go/go/parser/interface.go \ @@ -1618,7 +1620,8 @@ go_image_bmp_files = \ go/image/bmp/reader.go go_image_color_files = \ - go/image/color/color.go + go/image/color/color.go \ + go/image/color/ycbcr.go go_image_draw_files = \ go/image/draw/draw.go @@ -1643,9 +1646,6 @@ go_image_tiff_files = \ go/image/tiff/consts.go \ go/image/tiff/reader.go -go_image_ycbcr_files = \ - go/image/ycbcr/ycbcr.go - go_index_suffixarray_files = \ go/index/suffixarray/qsufsort.go \ go/index/suffixarray/suffixarray.go @@ -1698,6 +1698,7 @@ go_net_http_files = \ go/net/http/filetransport.go \ go/net/http/fs.go \ go/net/http/header.go \ + go/net/http/jar.go \ go/net/http/lex.go \ go/net/http/request.go \ go/net/http/response.go \ @@ -2047,7 +2048,6 @@ libgo_go_objs = \ image/jpeg.lo \ image/png.lo \ image/tiff.lo \ - image/ycbcr.lo \ index/suffixarray.lo \ io/ioutil.lo \ log/syslog.lo \ @@ -2303,11 +2303,11 @@ TEST_PACKAGES = \ hash/crc32/check \ hash/crc64/check \ hash/fnv/check \ + image/color/check \ image/draw/check \ image/jpeg/check \ image/png/check \ image/tiff/check \ - image/ycbcr/check \ index/suffixarray/check \ io/ioutil/check \ log/syslog/check \ @@ -5661,16 +5661,6 @@ image/tiff/check: $(CHECK_DEPS) @$(CHECK) .PHONY: image/tiff/check -@go_include@ image/ycbcr.lo.dep -image/ycbcr.lo.dep: $(go_image_ycbcr_files) - $(BUILDDEPS) -image/ycbcr.lo: $(go_image_ycbcr_files) - $(BUILDPACKAGE) -image/ycbcr/check: $(CHECK_DEPS) - @$(MKDIR_P) image/ycbcr - @$(CHECK) -.PHONY: image/ycbcr/check - @go_include@ index/suffixarray.lo.dep index/suffixarray.lo.dep: $(go_index_suffixarray_files) $(BUILDDEPS) @@ -6318,8 +6308,6 @@ image/png.gox: image/png.lo $(BUILDGOX) image/tiff.gox: image/tiff.lo $(BUILDGOX) -image/ycbcr.gox: image/ycbcr.lo - $(BUILDGOX) index/suffixarray.gox: index/suffixarray.lo $(BUILDGOX) diff --git a/libgo/configure b/libgo/configure index c1ee7bac082..7e8a38d37b4 100755 --- a/libgo/configure +++ b/libgo/configure @@ -657,6 +657,8 @@ LIBGO_IS_SOLARIS_FALSE LIBGO_IS_SOLARIS_TRUE LIBGO_IS_RTEMS_FALSE LIBGO_IS_RTEMS_TRUE +LIBGO_IS_NETBSD_FALSE +LIBGO_IS_NETBSD_TRUE LIBGO_IS_LINUX_FALSE LIBGO_IS_LINUX_TRUE LIBGO_IS_IRIX_FALSE @@ -11097,7 +11099,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 11100 "configure" +#line 11102 "configure" #include "confdefs.h" #if HAVE_DLFCN_H @@ -11203,7 +11205,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 11206 "configure" +#line 11208 "configure" #include "confdefs.h" #if HAVE_DLFCN_H @@ -13473,6 +13475,7 @@ case ${host} in *-*-freebsd*) is_freebsd=yes; GOOS=freebsd ;; *-*-irix6*) is_irix=yes; GOOS=irix ;; *-*-linux*) is_linux=yes; GOOS=linux ;; + *-*-netbsd*) is_netbsd=yes; GOOS=netbsd ;; *-*-rtems*) is_rtems=yes; GOOS=rtems ;; *-*-solaris2*) is_solaris=yes; GOOS=solaris ;; esac @@ -13508,6 +13511,14 @@ else LIBGO_IS_LINUX_FALSE= fi + if test $is_netbsd = yes; then + LIBGO_IS_NETBSD_TRUE= + LIBGO_IS_NETBSD_FALSE='#' +else + LIBGO_IS_NETBSD_TRUE='#' + LIBGO_IS_NETBSD_FALSE= +fi + if test $is_rtems = yes; then LIBGO_IS_RTEMS_TRUE= LIBGO_IS_RTEMS_FALSE='#' @@ -14938,6 +14949,10 @@ if test -z "${LIBGO_IS_LINUX_TRUE}" && test -z "${LIBGO_IS_LINUX_FALSE}"; then as_fn_error "conditional \"LIBGO_IS_LINUX\" was never defined. Usually this means the macro was only invoked conditionally." "$LINENO" 5 fi +if test -z "${LIBGO_IS_NETBSD_TRUE}" && test -z "${LIBGO_IS_NETBSD_FALSE}"; then + as_fn_error "conditional \"LIBGO_IS_NETBSD\" was never defined. +Usually this means the macro was only invoked conditionally." "$LINENO" 5 +fi if test -z "${LIBGO_IS_RTEMS_TRUE}" && test -z "${LIBGO_IS_RTEMS_FALSE}"; then as_fn_error "conditional \"LIBGO_IS_RTEMS\" was never defined. Usually this means the macro was only invoked conditionally." "$LINENO" 5 diff --git a/libgo/configure.ac b/libgo/configure.ac index a56106eaca9..81326557afa 100644 --- a/libgo/configure.ac +++ b/libgo/configure.ac @@ -134,6 +134,7 @@ case ${host} in *-*-freebsd*) is_freebsd=yes; GOOS=freebsd ;; *-*-irix6*) is_irix=yes; GOOS=irix ;; *-*-linux*) is_linux=yes; GOOS=linux ;; + *-*-netbsd*) is_netbsd=yes; GOOS=netbsd ;; *-*-rtems*) is_rtems=yes; GOOS=rtems ;; *-*-solaris2*) is_solaris=yes; GOOS=solaris ;; esac @@ -141,6 +142,7 @@ AM_CONDITIONAL(LIBGO_IS_DARWIN, test $is_darwin = yes) AM_CONDITIONAL(LIBGO_IS_FREEBSD, test $is_freebsd = yes) AM_CONDITIONAL(LIBGO_IS_IRIX, test $is_irix = yes) AM_CONDITIONAL(LIBGO_IS_LINUX, test $is_linux = yes) +AM_CONDITIONAL(LIBGO_IS_NETBSD, test $is_netbsd = yes) AM_CONDITIONAL(LIBGO_IS_RTEMS, test $is_rtems = yes) AM_CONDITIONAL(LIBGO_IS_SOLARIS, test $is_solaris = yes) AC_SUBST(GOOS) diff --git a/libgo/go/archive/tar/reader_test.go b/libgo/go/archive/tar/reader_test.go index 5829d03faa6..0a6513d0cac 100644 --- a/libgo/go/archive/tar/reader_test.go +++ b/libgo/go/archive/tar/reader_test.go @@ -10,7 +10,6 @@ import ( "fmt" "io" "os" - "reflect" "testing" "time" ) @@ -127,7 +126,7 @@ testLoop: f.Close() continue testLoop } - if !reflect.DeepEqual(hdr, header) { + if *hdr != *header { t.Errorf("test %d, entry %d: Incorrect header:\nhave %+v\nwant %+v", i, j, *hdr, *header) } @@ -201,7 +200,7 @@ func TestIncrementalRead(t *testing.T) { } // check the header - if !reflect.DeepEqual(hdr, headers[nread]) { + if *hdr != *headers[nread] { t.Errorf("Incorrect header:\nhave %+v\nwant %+v", *hdr, headers[nread]) } diff --git a/libgo/go/archive/zip/reader_test.go b/libgo/go/archive/zip/reader_test.go index 8c0ecaa4386..0e40268c2f8 100644 --- a/libgo/go/archive/zip/reader_test.go +++ b/libgo/go/archive/zip/reader_test.go @@ -9,6 +9,7 @@ import ( "encoding/binary" "io" "io/ioutil" + "os" "testing" "time" ) @@ -25,7 +26,7 @@ type ZipTestFile struct { Content []byte // if blank, will attempt to compare against File File string // name of file to compare to (relative to testdata/) Mtime string // modified time in format "mm-dd-yy hh:mm:ss" - Mode uint32 + Mode os.FileMode } // Caution: The Mtime values found for the test files should correspond to @@ -47,13 +48,13 @@ var tests = []ZipTest{ Name: "test.txt", Content: []byte("This is a test text file.\n"), Mtime: "09-05-10 12:12:02", - Mode: 0x81a4, + Mode: 0644, }, { Name: "gophercolor16x16.png", File: "gophercolor16x16.png", Mtime: "09-05-10 15:52:58", - Mode: 0x81a4, + Mode: 0644, }, }, }, @@ -64,6 +65,7 @@ var tests = []ZipTest{ Name: "r/r.zip", File: "r.zip", Mtime: "03-04-10 00:24:16", + Mode: 0666, }, }, }, @@ -76,9 +78,43 @@ var tests = []ZipTest{ Name: "filename", Content: []byte("This is a test textfile.\n"), Mtime: "02-02-11 13:06:20", + Mode: 0666, }, }, }, + { + // created in windows XP file manager. + Name: "winxp.zip", + File: crossPlatform, + }, + { + // created by Zip 3.0 under Linux + Name: "unix.zip", + File: crossPlatform, + }, +} + +var crossPlatform = []ZipTestFile{ + { + Name: "hello", + Content: []byte("world \r\n"), + Mode: 0666, + }, + { + Name: "dir/bar", + Content: []byte("foo \r\n"), + Mode: 0666, + }, + { + Name: "dir/empty/", + Content: []byte{}, + Mode: os.ModeDir | 0777, + }, + { + Name: "readonly", + Content: []byte("important \r\n"), + Mode: 0444, + }, } func TestReader(t *testing.T) { @@ -127,10 +163,10 @@ func readTestZip(t *testing.T, zt ZipTest) { done := make(chan bool) for i := 0; i < 5; i++ { for j, ft := range zt.File { - go func() { + go func(j int, ft ZipTestFile) { readTestFile(t, ft, z.File[j]) done <- true - }() + }(j, ft) n++ } } @@ -159,13 +195,15 @@ func readTestFile(t *testing.T, ft ZipTestFile, f *File) { t.Errorf("name=%q, want %q", f.Name, ft.Name) } - mtime, err := time.Parse("01-02-06 15:04:05", ft.Mtime) - if err != nil { - t.Error(err) - return - } - if ft := f.ModTime(); !ft.Equal(mtime) { - t.Errorf("%s: mtime=%s, want %s", f.Name, ft, mtime) + if ft.Mtime != "" { + mtime, err := time.Parse("01-02-06 15:04:05", ft.Mtime) + if err != nil { + t.Error(err) + return + } + if ft := f.ModTime(); !ft.Equal(mtime) { + t.Errorf("%s: mtime=%s, want %s", f.Name, ft, mtime) + } } testFileMode(t, f, ft.Mode) @@ -191,7 +229,7 @@ func readTestFile(t *testing.T, ft ZipTestFile, f *File) { r.Close() var c []byte - if len(ft.Content) != 0 { + if ft.Content != nil { c = ft.Content } else if c, err = ioutil.ReadFile("testdata/" + ft.File); err != nil { t.Error(err) @@ -211,7 +249,7 @@ func readTestFile(t *testing.T, ft ZipTestFile, f *File) { } } -func testFileMode(t *testing.T, f *File, want uint32) { +func testFileMode(t *testing.T, f *File, want os.FileMode) { mode, err := f.Mode() if want == 0 { if err == nil { @@ -220,7 +258,7 @@ func testFileMode(t *testing.T, f *File, want uint32) { } else if err != nil { t.Errorf("%s mode: %s", f.Name, err) } else if mode != want { - t.Errorf("%s mode: want 0x%x, got 0x%x", f.Name, want, mode) + t.Errorf("%s mode: want %v, got %v", f.Name, want, mode) } } diff --git a/libgo/go/archive/zip/struct.go b/libgo/go/archive/zip/struct.go index 43c04bb27b2..34a87fae5b3 100644 --- a/libgo/go/archive/zip/struct.go +++ b/libgo/go/archive/zip/struct.go @@ -12,7 +12,7 @@ This package does not support ZIP64 or disk spanning. package zip import ( - "errors" + "os" "time" ) @@ -32,7 +32,11 @@ const ( dataDescriptorLen = 12 // Constants for the first byte in CreatorVersion - creatorUnix = 3 + creatorFAT = 0 + creatorUnix = 3 + creatorNTFS = 11 + creatorVFAT = 14 + creatorMacOSX = 19 ) type FileHeader struct { @@ -92,23 +96,107 @@ func msDosTimeToTime(dosDate, dosTime uint16) time.Time { ) } +// timeToMsDosTime converts a time.Time to an MS-DOS date and time. +// The resolution is 2s. +// See: http://msdn.microsoft.com/en-us/library/ms724274(v=VS.85).aspx +func timeToMsDosTime(t time.Time) (fDate uint16, fTime uint16) { + t = t.In(time.UTC) + fDate = uint16(t.Day() + int(t.Month())<<5 + (t.Year()-1980)<<9) + fTime = uint16(t.Second()/2 + t.Minute()<<5 + t.Hour()<<11) + return +} + // ModTime returns the modification time. // The resolution is 2s. func (h *FileHeader) ModTime() time.Time { return msDosTimeToTime(h.ModifiedDate, h.ModifiedTime) } +// SetModTime sets the ModifiedTime and ModifiedDate fields to the given time. +// The resolution is 2s. +func (h *FileHeader) SetModTime(t time.Time) { + h.ModifiedDate, h.ModifiedTime = timeToMsDosTime(t) +} + +// traditional names for Unix constants +const ( + s_IFMT = 0xf000 + s_IFDIR = 0x4000 + s_IFREG = 0x8000 + s_ISUID = 0x800 + s_ISGID = 0x400 + + msdosDir = 0x10 + msdosReadOnly = 0x01 +) + // Mode returns the permission and mode bits for the FileHeader. // An error is returned in case the information is not available. -func (h *FileHeader) Mode() (mode uint32, err error) { - if h.CreatorVersion>>8 == creatorUnix { - return h.ExternalAttrs >> 16, nil +func (h *FileHeader) Mode() (mode os.FileMode, err error) { + switch h.CreatorVersion >> 8 { + case creatorUnix, creatorMacOSX: + mode = unixModeToFileMode(h.ExternalAttrs >> 16) + case creatorNTFS, creatorVFAT, creatorFAT: + mode = msdosModeToFileMode(h.ExternalAttrs) } - return 0, errors.New("file mode not available") + if len(h.Name) > 0 && h.Name[len(h.Name)-1] == '/' { + mode |= os.ModeDir + } + return mode, nil } // SetMode changes the permission and mode bits for the FileHeader. -func (h *FileHeader) SetMode(mode uint32) { +func (h *FileHeader) SetMode(mode os.FileMode) { h.CreatorVersion = h.CreatorVersion&0xff | creatorUnix<<8 - h.ExternalAttrs = mode << 16 + h.ExternalAttrs = fileModeToUnixMode(mode) << 16 + + // set MSDOS attributes too, as the original zip does. + if mode&os.ModeDir != 0 { + h.ExternalAttrs |= msdosDir + } + if mode&0200 == 0 { + h.ExternalAttrs |= msdosReadOnly + } +} + +func msdosModeToFileMode(m uint32) (mode os.FileMode) { + if m&msdosDir != 0 { + mode = os.ModeDir | 0777 + } else { + mode = 0666 + } + if m&msdosReadOnly != 0 { + mode &^= 0222 + } + return mode +} + +func fileModeToUnixMode(mode os.FileMode) uint32 { + var m uint32 + if mode&os.ModeDir != 0 { + m = s_IFDIR + } else { + m = s_IFREG + } + if mode&os.ModeSetuid != 0 { + m |= s_ISUID + } + if mode&os.ModeSetgid != 0 { + m |= s_ISGID + } + return m | uint32(mode&0777) +} + +func unixModeToFileMode(m uint32) os.FileMode { + var mode os.FileMode + if m&s_IFMT == s_IFDIR { + mode |= os.ModeDir + } + if m&s_ISGID != 0 { + mode |= os.ModeSetgid + } + if m&s_ISUID != 0 { + mode |= os.ModeSetuid + } + return mode | os.FileMode(m&0777) } diff --git a/libgo/go/archive/zip/testdata/unix.zip b/libgo/go/archive/zip/testdata/unix.zip Binary files differnew file mode 100644 index 00000000000..ce1a981b280 --- /dev/null +++ b/libgo/go/archive/zip/testdata/unix.zip diff --git a/libgo/go/archive/zip/testdata/winxp.zip b/libgo/go/archive/zip/testdata/winxp.zip Binary files differnew file mode 100644 index 00000000000..3919322f0c5 --- /dev/null +++ b/libgo/go/archive/zip/testdata/winxp.zip diff --git a/libgo/go/archive/zip/writer_test.go b/libgo/go/archive/zip/writer_test.go index 1188103568e..5a576b1c32c 100644 --- a/libgo/go/archive/zip/writer_test.go +++ b/libgo/go/archive/zip/writer_test.go @@ -8,6 +8,7 @@ import ( "bytes" "io/ioutil" "math/rand" + "os" "testing" ) @@ -17,7 +18,7 @@ type WriteTest struct { Name string Data []byte Method uint16 - Mode uint32 + Mode os.FileMode } var writeTests = []WriteTest{ @@ -25,12 +26,31 @@ var writeTests = []WriteTest{ Name: "foo", Data: []byte("Rabbits, guinea pigs, gophers, marsupial rats, and quolls."), Method: Store, + Mode: 0666, }, { Name: "bar", Data: nil, // large data set in the test Method: Deflate, - Mode: 0x81ed, + Mode: 0644, + }, + { + Name: "setuid", + Data: []byte("setuid file"), + Method: Deflate, + Mode: 0755 | os.ModeSetuid, + }, + { + Name: "setgid", + Data: []byte("setgid file"), + Method: Deflate, + Mode: 0755 | os.ModeSetgid, + }, + { + Name: "setgid", + Data: []byte("setgid file"), + Method: Deflate, + Mode: 0755 | os.ModeSetgid, }, } diff --git a/libgo/go/archive/zip/zip_test.go b/libgo/go/archive/zip/zip_test.go index 2075715f3e0..8aab2b68123 100644 --- a/libgo/go/archive/zip/zip_test.go +++ b/libgo/go/archive/zip/zip_test.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "testing" + "time" ) type stringReaderAt string @@ -55,3 +56,13 @@ func TestOver65kFiles(t *testing.T) { } } } + +func TestModTime(t *testing.T) { + var testTime = time.Date(2009, time.November, 10, 23, 45, 58, 0, time.UTC) + fh := new(FileHeader) + fh.SetModTime(testTime) + outTime := fh.ModTime() + if !outTime.Equal(testTime) { + t.Errorf("times don't match: got %s, want %s", outTime, testTime) + } +} diff --git a/libgo/go/bufio/bufio.go b/libgo/go/bufio/bufio.go index 61e338b6165..907a9dcb719 100644 --- a/libgo/go/bufio/bufio.go +++ b/libgo/go/bufio/bufio.go @@ -52,12 +52,14 @@ type Reader struct { lastRuneSize int } +const minReadBufferSize = 16 + // NewReaderSize creates a new Reader whose buffer has the specified size, -// which must be greater than one. If the argument io.Reader is already a +// which must be at least 16 bytes. If the argument io.Reader is already a // Reader with large enough size, it returns the underlying Reader. // It returns the Reader and any error. func NewReaderSize(rd io.Reader, size int) (*Reader, error) { - if size <= 1 { + if size < minReadBufferSize { return nil, BufSizeError(size) } // Is it already a Reader? diff --git a/libgo/go/bufio/bufio_test.go b/libgo/go/bufio/bufio_test.go index 54029cd40fd..6463bbb9cb3 100644 --- a/libgo/go/bufio/bufio_test.go +++ b/libgo/go/bufio/bufio_test.go @@ -135,9 +135,10 @@ var bufreaders = []bufReader{ {"lines", readLines}, } +const minReadBufferSize = 16 + var bufsizes = []int{ - 2, 3, 4, 5, 6, 7, 8, 9, 10, - 23, 32, 46, 64, 93, 128, 1024, 4096, + minReadBufferSize, 23, 32, 46, 64, 93, 128, 1024, 4096, } func TestReader(t *testing.T) { @@ -514,27 +515,32 @@ func TestWriteString(t *testing.T) { } func TestBufferFull(t *testing.T) { - buf, _ := NewReaderSize(strings.NewReader("hello, world"), 5) - line, err := buf.ReadSlice(',') - if string(line) != "hello" || err != ErrBufferFull { + const longString = "And now, hello, world! It is the time for all good men to come to the aid of their party" + buf, err := NewReaderSize(strings.NewReader(longString), minReadBufferSize) + if err != nil { + t.Fatal("NewReaderSize:", err) + } + line, err := buf.ReadSlice('!') + if string(line) != "And now, hello, " || err != ErrBufferFull { t.Errorf("first ReadSlice(,) = %q, %v", line, err) } - line, err = buf.ReadSlice(',') - if string(line) != "," || err != nil { + line, err = buf.ReadSlice('!') + if string(line) != "world!" || err != nil { t.Errorf("second ReadSlice(,) = %q, %v", line, err) } } func TestPeek(t *testing.T) { p := make([]byte, 10) - buf, _ := NewReaderSize(strings.NewReader("abcdefghij"), 4) + // string is 16 (minReadBufferSize) long. + buf, _ := NewReaderSize(strings.NewReader("abcdefghijklmnop"), minReadBufferSize) if s, err := buf.Peek(1); string(s) != "a" || err != nil { t.Fatalf("want %q got %q, err=%v", "a", string(s), err) } if s, err := buf.Peek(4); string(s) != "abcd" || err != nil { t.Fatalf("want %q got %q, err=%v", "abcd", string(s), err) } - if _, err := buf.Peek(5); err != ErrBufferFull { + if _, err := buf.Peek(32); err != ErrBufferFull { t.Fatalf("want ErrBufFull got %v", err) } if _, err := buf.Read(p[0:3]); string(p[0:3]) != "abc" || err != nil { @@ -552,8 +558,8 @@ func TestPeek(t *testing.T) { if s, err := buf.Peek(4); string(s) != "ghij" || err != nil { t.Fatalf("want %q got %q, err=%v", "ghij", string(s), err) } - if _, err := buf.Read(p[0:4]); string(p[0:4]) != "ghij" || err != nil { - t.Fatalf("want %q got %q, err=%v", "ghij", string(p[0:3]), err) + if _, err := buf.Read(p[0:]); string(p[0:]) != "ghijklmnop" || err != nil { + t.Fatalf("want %q got %q, err=%v", "ghijklmnop", string(p[0:minReadBufferSize]), err) } if s, err := buf.Peek(0); string(s) != "" || err != nil { t.Fatalf("want %q got %q, err=%v", "", string(s), err) @@ -635,19 +641,25 @@ func TestReadLine(t *testing.T) { } func TestLineTooLong(t *testing.T) { - buf := bytes.NewBuffer([]byte("aaabbbcc\n")) - l, _ := NewReaderSize(buf, 3) + data := make([]byte, 0) + for i := 0; i < minReadBufferSize*5/2; i++ { + data = append(data, '0'+byte(i%10)) + } + buf := bytes.NewBuffer(data) + l, _ := NewReaderSize(buf, minReadBufferSize) line, isPrefix, err := l.ReadLine() - if !isPrefix || !bytes.Equal(line, []byte("aaa")) || err != nil { - t.Errorf("bad result for first line: %x %s", line, err) + if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil { + t.Errorf("bad result for first line: got %q want %q %v", line, data[:minReadBufferSize], err) } + data = data[len(line):] line, isPrefix, err = l.ReadLine() - if !isPrefix || !bytes.Equal(line, []byte("bbb")) || err != nil { - t.Errorf("bad result for second line: %x", line) + if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil { + t.Errorf("bad result for second line: got %q want %q %v", line, data[:minReadBufferSize], err) } + data = data[len(line):] line, isPrefix, err = l.ReadLine() - if isPrefix || !bytes.Equal(line, []byte("cc")) || err != nil { - t.Errorf("bad result for third line: %x", line) + if isPrefix || !bytes.Equal(line, data[:minReadBufferSize/2]) || err != nil { + t.Errorf("bad result for third line: got %q want %q %v", line, data[:minReadBufferSize/2], err) } line, isPrefix, err = l.ReadLine() if isPrefix || err == nil { @@ -656,8 +668,8 @@ func TestLineTooLong(t *testing.T) { } func TestReadAfterLines(t *testing.T) { - line1 := "line1" - restData := "line2\nline 3\n" + line1 := "this is line1" + restData := "this is line2\nthis is line 3\n" inbuf := bytes.NewBuffer([]byte(line1 + "\n" + restData)) outbuf := new(bytes.Buffer) maxLineLength := len(line1) + len(restData)/2 @@ -676,7 +688,7 @@ func TestReadAfterLines(t *testing.T) { } func TestReadEmptyBuffer(t *testing.T) { - l, _ := NewReaderSize(bytes.NewBuffer(nil), 10) + l, _ := NewReaderSize(bytes.NewBuffer(nil), minReadBufferSize) line, isPrefix, err := l.ReadLine() if err != io.EOF { t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err) @@ -684,7 +696,7 @@ func TestReadEmptyBuffer(t *testing.T) { } func TestLinesAfterRead(t *testing.T) { - l, _ := NewReaderSize(bytes.NewBuffer([]byte("foo")), 10) + l, _ := NewReaderSize(bytes.NewBuffer([]byte("foo")), minReadBufferSize) _, err := ioutil.ReadAll(l) if err != nil { t.Error(err) @@ -715,34 +727,19 @@ type readLineResult struct { } var readLineNewlinesTests = []struct { - input string - bufSize int - expect []readLineResult + input string + expect []readLineResult }{ - {"h\r\nb\r\n", 2, []readLineResult{ - {[]byte("h"), true, nil}, - {nil, false, nil}, - {[]byte("b"), true, nil}, - {nil, false, nil}, - {nil, false, io.EOF}, - }}, - {"hello\r\nworld\r\n", 6, []readLineResult{ - {[]byte("hello"), true, nil}, + {"012345678901234\r\n012345678901234\r\n", []readLineResult{ + {[]byte("012345678901234"), true, nil}, {nil, false, nil}, - {[]byte("world"), true, nil}, + {[]byte("012345678901234"), true, nil}, {nil, false, nil}, {nil, false, io.EOF}, }}, - {"hello\rworld\r", 6, []readLineResult{ - {[]byte("hello"), true, nil}, - {[]byte("\rworld"), true, nil}, - {[]byte("\r"), false, nil}, - {nil, false, io.EOF}, - }}, - {"h\ri\r\n\r", 2, []readLineResult{ - {[]byte("h"), true, nil}, - {[]byte("\ri"), true, nil}, - {nil, false, nil}, + {"0123456789012345\r012345678901234\r", []readLineResult{ + {[]byte("0123456789012345"), true, nil}, + {[]byte("\r012345678901234"), true, nil}, {[]byte("\r"), false, nil}, {nil, false, io.EOF}, }}, @@ -750,12 +747,12 @@ var readLineNewlinesTests = []struct { func TestReadLineNewlines(t *testing.T) { for _, e := range readLineNewlinesTests { - testReadLineNewlines(t, e.input, e.bufSize, e.expect) + testReadLineNewlines(t, e.input, e.expect) } } -func testReadLineNewlines(t *testing.T, input string, bufSize int, expect []readLineResult) { - b, err := NewReaderSize(strings.NewReader(input), bufSize) +func testReadLineNewlines(t *testing.T, input string, expect []readLineResult) { + b, err := NewReaderSize(strings.NewReader(input), minReadBufferSize) if err != nil { t.Fatal(err) } diff --git a/libgo/go/bytes/buffer_test.go b/libgo/go/bytes/buffer_test.go index 52359700322..adb93302a54 100644 --- a/libgo/go/bytes/buffer_test.go +++ b/libgo/go/bytes/buffer_test.go @@ -16,7 +16,6 @@ const N = 10000 // make this bigger for a larger (and slower) test var data string // test data for write tests var bytes []byte // test data; same as data but as a slice. - func init() { bytes = make([]byte, N) for i := 0; i < N; i++ { diff --git a/libgo/go/bytes/bytes.go b/libgo/go/bytes/bytes.go index 9bfd88fa398..e94a0ec5c4f 100644 --- a/libgo/go/bytes/bytes.go +++ b/libgo/go/bytes/bytes.go @@ -37,7 +37,9 @@ func Compare(a, b []byte) int { } // Equal returns a boolean reporting whether a == b. -func Equal(a, b []byte) bool { +func Equal(a, b []byte) bool + +func equalPortable(a, b []byte) bool { if len(a) != len(b) { return false } @@ -74,18 +76,33 @@ func explode(s []byte, n int) [][]byte { // Count counts the number of non-overlapping instances of sep in s. func Count(s, sep []byte) int { - if len(sep) == 0 { + n := len(sep) + if n == 0 { return utf8.RuneCount(s) + 1 } + if n > len(s) { + return 0 + } + count := 0 c := sep[0] - n := 0 - for i := 0; i+len(sep) <= len(s); i++ { - if s[i] == c && (len(sep) == 1 || Equal(s[i:i+len(sep)], sep)) { - n++ - i += len(sep) - 1 + i := 0 + t := s[:len(s)-n+1] + for i < len(t) { + if t[i] != c { + o := IndexByte(t[i:], c) + if o < 0 { + break + } + i += o } + if n == 1 || Equal(s[i:i+n], sep) { + count++ + i += n + continue + } + i++ } - return n + return count } // Contains returns whether subslice is within b. @@ -99,11 +116,27 @@ func Index(s, sep []byte) int { if n == 0 { return 0 } + if n > len(s) { + return -1 + } c := sep[0] - for i := 0; i+n <= len(s); i++ { - if s[i] == c && (n == 1 || Equal(s[i:i+n], sep)) { + if n == 1 { + return IndexByte(s, c) + } + i := 0 + t := s[:len(s)-n+1] + for i < len(t) { + if t[i] != c { + o := IndexByte(t[i:], c) + if o < 0 { + break + } + i += o + } + if Equal(s[i:i+n], sep) { return i } + i++ } return -1 } @@ -437,7 +470,7 @@ func Title(s []byte) []byte { // Use a closure here to remember state. // Hackish but effective. Depends on Map scanning in order and calling // the closure once per rune. - prev := rune(' ') + prev := ' ' return Map( func(r rune) rune { if isSeparator(prev) { diff --git a/libgo/go/bytes/bytes_test.go b/libgo/go/bytes/bytes_test.go index 829ef05319c..2a1d41b910e 100644 --- a/libgo/go/bytes/bytes_test.go +++ b/libgo/go/bytes/bytes_test.go @@ -64,13 +64,17 @@ func TestCompare(t *testing.T) { a := []byte(tt.a) b := []byte(tt.b) cmp := Compare(a, b) - eql := Equal(a, b) if cmp != tt.i { t.Errorf(`Compare(%q, %q) = %v`, tt.a, tt.b, cmp) } + eql := Equal(a, b) if eql != (tt.i == 0) { t.Errorf(`Equal(%q, %q) = %v`, tt.a, tt.b, eql) } + eql = EqualPortable(a, b) + if eql != (tt.i == 0) { + t.Errorf(`EqualPortable(%q, %q) = %v`, tt.a, tt.b, eql) + } } } @@ -264,41 +268,147 @@ func TestIndexRune(t *testing.T) { } } -func BenchmarkIndexByte4K(b *testing.B) { bmIndex(b, IndexByte, 4<<10) } +var bmbuf []byte -func BenchmarkIndexByte4M(b *testing.B) { bmIndex(b, IndexByte, 4<<20) } +func BenchmarkIndexByte32(b *testing.B) { bmIndexByte(b, IndexByte, 32) } +func BenchmarkIndexByte4K(b *testing.B) { bmIndexByte(b, IndexByte, 4<<10) } +func BenchmarkIndexByte4M(b *testing.B) { bmIndexByte(b, IndexByte, 4<<20) } +func BenchmarkIndexByte64M(b *testing.B) { bmIndexByte(b, IndexByte, 64<<20) } +func BenchmarkIndexBytePortable32(b *testing.B) { bmIndexByte(b, IndexBytePortable, 32) } +func BenchmarkIndexBytePortable4K(b *testing.B) { bmIndexByte(b, IndexBytePortable, 4<<10) } +func BenchmarkIndexBytePortable4M(b *testing.B) { bmIndexByte(b, IndexBytePortable, 4<<20) } +func BenchmarkIndexBytePortable64M(b *testing.B) { bmIndexByte(b, IndexBytePortable, 64<<20) } -func BenchmarkIndexByte64M(b *testing.B) { bmIndex(b, IndexByte, 64<<20) } +func bmIndexByte(b *testing.B, index func([]byte, byte) int, n int) { + if len(bmbuf) < n { + bmbuf = make([]byte, n) + } + b.SetBytes(int64(n)) + buf := bmbuf[0:n] + buf[n-1] = 'x' + for i := 0; i < b.N; i++ { + j := index(buf, 'x') + if j != n-1 { + b.Fatal("bad index", j) + } + } + buf[n-1] = '\x00' +} + +func BenchmarkEqual32(b *testing.B) { bmEqual(b, Equal, 32) } +func BenchmarkEqual4K(b *testing.B) { bmEqual(b, Equal, 4<<10) } +func BenchmarkEqual4M(b *testing.B) { bmEqual(b, Equal, 4<<20) } +func BenchmarkEqual64M(b *testing.B) { bmEqual(b, Equal, 64<<20) } +func BenchmarkEqualPort32(b *testing.B) { bmEqual(b, EqualPortable, 32) } +func BenchmarkEqualPort4K(b *testing.B) { bmEqual(b, EqualPortable, 4<<10) } +func BenchmarkEqualPortable4M(b *testing.B) { bmEqual(b, EqualPortable, 4<<20) } +func BenchmarkEqualPortable64M(b *testing.B) { bmEqual(b, EqualPortable, 64<<20) } + +func bmEqual(b *testing.B, equal func([]byte, []byte) bool, n int) { + if len(bmbuf) < 2*n { + bmbuf = make([]byte, 2*n) + } + b.SetBytes(int64(n)) + buf1 := bmbuf[0:n] + buf2 := bmbuf[n : 2*n] + buf1[n-1] = 'x' + buf2[n-1] = 'x' + for i := 0; i < b.N; i++ { + eq := equal(buf1, buf2) + if !eq { + b.Fatal("bad equal") + } + } + buf1[n-1] = '\x00' + buf2[n-1] = '\x00' +} + +func BenchmarkIndex32(b *testing.B) { bmIndex(b, Index, 32) } +func BenchmarkIndex4K(b *testing.B) { bmIndex(b, Index, 4<<10) } +func BenchmarkIndex4M(b *testing.B) { bmIndex(b, Index, 4<<20) } +func BenchmarkIndex64M(b *testing.B) { bmIndex(b, Index, 64<<20) } -func BenchmarkIndexBytePortable4K(b *testing.B) { - bmIndex(b, IndexBytePortable, 4<<10) +func bmIndex(b *testing.B, index func([]byte, []byte) int, n int) { + if len(bmbuf) < n { + bmbuf = make([]byte, n) + } + b.SetBytes(int64(n)) + buf := bmbuf[0:n] + buf[n-1] = 'x' + for i := 0; i < b.N; i++ { + j := index(buf, buf[n-7:]) + if j != n-7 { + b.Fatal("bad index", j) + } + } + buf[n-1] = '\x00' } -func BenchmarkIndexBytePortable4M(b *testing.B) { - bmIndex(b, IndexBytePortable, 4<<20) +func BenchmarkIndexEasy32(b *testing.B) { bmIndexEasy(b, Index, 32) } +func BenchmarkIndexEasy4K(b *testing.B) { bmIndexEasy(b, Index, 4<<10) } +func BenchmarkIndexEasy4M(b *testing.B) { bmIndexEasy(b, Index, 4<<20) } +func BenchmarkIndexEasy64M(b *testing.B) { bmIndexEasy(b, Index, 64<<20) } + +func bmIndexEasy(b *testing.B, index func([]byte, []byte) int, n int) { + if len(bmbuf) < n { + bmbuf = make([]byte, n) + } + b.SetBytes(int64(n)) + buf := bmbuf[0:n] + buf[n-1] = 'x' + buf[n-7] = 'x' + for i := 0; i < b.N; i++ { + j := index(buf, buf[n-7:]) + if j != n-7 { + b.Fatal("bad index", j) + } + } + buf[n-1] = '\x00' + buf[n-7] = '\x00' } -func BenchmarkIndexBytePortable64M(b *testing.B) { - bmIndex(b, IndexBytePortable, 64<<20) +func BenchmarkCount32(b *testing.B) { bmCount(b, Count, 32) } +func BenchmarkCount4K(b *testing.B) { bmCount(b, Count, 4<<10) } +func BenchmarkCount4M(b *testing.B) { bmCount(b, Count, 4<<20) } +func BenchmarkCount64M(b *testing.B) { bmCount(b, Count, 64<<20) } + +func bmCount(b *testing.B, count func([]byte, []byte) int, n int) { + if len(bmbuf) < n { + bmbuf = make([]byte, n) + } + b.SetBytes(int64(n)) + buf := bmbuf[0:n] + buf[n-1] = 'x' + for i := 0; i < b.N; i++ { + j := count(buf, buf[n-7:]) + if j != 1 { + b.Fatal("bad count", j) + } + } + buf[n-1] = '\x00' } -var bmbuf []byte +func BenchmarkCountEasy32(b *testing.B) { bmCountEasy(b, Count, 32) } +func BenchmarkCountEasy4K(b *testing.B) { bmCountEasy(b, Count, 4<<10) } +func BenchmarkCountEasy4M(b *testing.B) { bmCountEasy(b, Count, 4<<20) } +func BenchmarkCountEasy64M(b *testing.B) { bmCountEasy(b, Count, 64<<20) } -func bmIndex(b *testing.B, index func([]byte, byte) int, n int) { +func bmCountEasy(b *testing.B, count func([]byte, []byte) int, n int) { if len(bmbuf) < n { bmbuf = make([]byte, n) } b.SetBytes(int64(n)) buf := bmbuf[0:n] buf[n-1] = 'x' + buf[n-7] = 'x' for i := 0; i < b.N; i++ { - j := index(buf, 'x') - if j != n-1 { - println("bad index", j) - panic("bad index") + j := count(buf, buf[n-7:]) + if j != 1 { + b.Fatal("bad count", j) } } - buf[n-1] = '0' + buf[n-1] = '\x00' + buf[n-7] = '\x00' } type ExplodeTest struct { diff --git a/libgo/go/bytes/example_test.go b/libgo/go/bytes/example_test.go new file mode 100644 index 00000000000..02da1ac082b --- /dev/null +++ b/libgo/go/bytes/example_test.go @@ -0,0 +1,24 @@ +package bytes_test + +import ( + . "bytes" + "encoding/base64" + "io" + "os" +) + +// Hello world! +func ExampleBuffer() { + var b Buffer // A Buffer needs no initialization. + b.Write([]byte("Hello ")) + b.Write([]byte("world!")) + b.WriteTo(os.Stdout) +} + +// Gophers rule! +func ExampleBuffer_reader() { + // A Buffer can turn a string or a []byte into an io.Reader. + buf := NewBufferString("R29waGVycyBydWxlIQ==") + dec := base64.NewDecoder(base64.StdEncoding, buf) + io.Copy(os.Stdout, dec) +} diff --git a/libgo/go/bytes/export_test.go b/libgo/go/bytes/export_test.go index b65428d9ce8..f61523e60bb 100644 --- a/libgo/go/bytes/export_test.go +++ b/libgo/go/bytes/export_test.go @@ -6,3 +6,4 @@ package bytes // Export func for testing var IndexBytePortable = indexBytePortable +var EqualPortable = equalPortable diff --git a/libgo/go/bytes/indexbyte.c b/libgo/go/bytes/indexbyte.c index a0a963e93f6..8213b3e3442 100644 --- a/libgo/go/bytes/indexbyte.c +++ b/libgo/go/bytes/indexbyte.c @@ -26,3 +26,17 @@ IndexByte (struct __go_open_array s, char b) return -1; return p - (char *) s.__values; } + +/* Comparison. */ + +_Bool Equal (struct __go_open_array a, struct __go_open_array b) + asm ("libgo_bytes.bytes.Equal") + __attribute__ ((no_split_stack)); + +_Bool +Equal (struct __go_open_array a, struct __go_open_array b) +{ + if (a.__count != b.__count) + return 0; + return __builtin_memcmp (a.__values, b.__values, a.__count) == 0; +} diff --git a/libgo/go/compress/flate/deflate.go b/libgo/go/compress/flate/deflate.go index 1f659bab713..4f744457dd7 100644 --- a/libgo/go/compress/flate/deflate.go +++ b/libgo/go/compress/flate/deflate.go @@ -319,7 +319,9 @@ Loop: // For matches this long, we don't bother inserting each individual // item into the table. d.index += d.length - d.hash = (int(d.window[d.index])<<hashShift + int(d.window[d.index+1])) + if d.index < d.maxInsertIndex { + d.hash = (int(d.window[d.index])<<hashShift + int(d.window[d.index+1])) + } } if d.ti == maxFlateBlockTokens { // The block includes the current character diff --git a/libgo/go/compress/flate/deflate_test.go b/libgo/go/compress/flate/deflate_test.go index b4876b0f8b3..bae5c82305a 100644 --- a/libgo/go/compress/flate/deflate_test.go +++ b/libgo/go/compress/flate/deflate_test.go @@ -318,3 +318,15 @@ func TestWriterDict(t *testing.T) { t.Fatalf("writer wrote %q want %q", b1.Bytes(), b.Bytes()) } } + +// See http://code.google.com/p/go/issues/detail?id=2508 +func TestRegression2508(t *testing.T) { + w := NewWriter(ioutil.Discard, 1) + buf := make([]byte, 1024) + for i := 0; i < 131072; i++ { + if _, err := w.Write(buf); err != nil { + t.Fatalf("writer failed: %v", err) + } + } + w.Close() +} diff --git a/libgo/go/compress/gzip/gunzip.go b/libgo/go/compress/gzip/gunzip.go index 7c78b9e366d..6d60fdd0ff3 100644 --- a/libgo/go/compress/gzip/gunzip.go +++ b/libgo/go/compress/gzip/gunzip.go @@ -96,6 +96,7 @@ func get4(p []byte) uint32 { func (z *Decompressor) readString() (string, error) { var err error + needconv := false for i := 0; ; i++ { if i >= len(z.buf) { return "", HeaderError @@ -104,9 +105,18 @@ func (z *Decompressor) readString() (string, error) { if err != nil { return "", err } + if z.buf[i] > 0x7f { + needconv = true + } if z.buf[i] == 0 { // GZIP (RFC 1952) specifies that strings are NUL-terminated ISO 8859-1 (Latin-1). - // TODO(nigeltao): Convert from ISO 8859-1 (Latin-1) to UTF-8. + if needconv { + s := make([]rune, 0, i) + for _, v := range z.buf[0:i] { + s = append(s, rune(v)) + } + return string(s), nil + } return string(z.buf[0:i]), nil } } diff --git a/libgo/go/compress/gzip/gzip.go b/libgo/go/compress/gzip/gzip.go index 07b91b66823..f2639a688c1 100644 --- a/libgo/go/compress/gzip/gzip.go +++ b/libgo/go/compress/gzip/gzip.go @@ -86,13 +86,25 @@ func (z *Compressor) writeBytes(b []byte) error { // writeString writes a string (in ISO 8859-1 (Latin-1) format) to z.w. func (z *Compressor) writeString(s string) error { // GZIP (RFC 1952) specifies that strings are NUL-terminated ISO 8859-1 (Latin-1). - // TODO(nigeltao): Convert from UTF-8 to ISO 8859-1 (Latin-1). + var err error + needconv := false for _, v := range s { - if v == 0 || v > 0x7f { - return errors.New("gzip.Write: non-ASCII header string") + if v == 0 || v > 0xff { + return errors.New("gzip.Write: non-Latin-1 header string") } + if v > 0x7f { + needconv = true + } + } + if needconv { + b := make([]byte, 0, len(s)) + for _, v := range s { + b = append(b, byte(v)) + } + _, err = z.w.Write(b) + } else { + _, err = io.WriteString(z.w, s) } - _, err := io.WriteString(z.w, s) if err != nil { return err } diff --git a/libgo/go/compress/gzip/gzip_test.go b/libgo/go/compress/gzip/gzip_test.go index 815825be999..eb7a7ec0892 100644 --- a/libgo/go/compress/gzip/gzip_test.go +++ b/libgo/go/compress/gzip/gzip_test.go @@ -5,6 +5,8 @@ package gzip import ( + "bufio" + "bytes" "io" "io/ioutil" "testing" @@ -52,7 +54,8 @@ func TestEmpty(t *testing.T) { func TestWriter(t *testing.T) { pipe(t, func(compressor *Compressor) { - compressor.Comment = "comment" + compressor.Comment = "Äußerung" + //compressor.Comment = "comment" compressor.Extra = []byte("extra") compressor.ModTime = time.Unix(1e8, 0) compressor.Name = "name" @@ -69,8 +72,8 @@ func TestWriter(t *testing.T) { if string(b) != "payload" { t.Fatalf("payload is %q, want %q", string(b), "payload") } - if decompressor.Comment != "comment" { - t.Fatalf("comment is %q, want %q", decompressor.Comment, "comment") + if decompressor.Comment != "Äußerung" { + t.Fatalf("comment is %q, want %q", decompressor.Comment, "Äußerung") } if string(decompressor.Extra) != "extra" { t.Fatalf("extra is %q, want %q", decompressor.Extra, "extra") @@ -83,3 +86,29 @@ func TestWriter(t *testing.T) { } }) } + +func TestLatin1(t *testing.T) { + latin1 := []byte{0xc4, 'u', 0xdf, 'e', 'r', 'u', 'n', 'g', 0} + utf8 := "Äußerung" + z := Decompressor{r: bufio.NewReader(bytes.NewBuffer(latin1))} + s, err := z.readString() + if err != nil { + t.Fatalf("%v", err) + } + if s != utf8 { + t.Fatalf("string is %q, want %q", s, utf8) + } + + buf := bytes.NewBuffer(make([]byte, 0, len(latin1))) + c := Compressor{w: buf} + if err = c.writeString(utf8); err != nil { + t.Fatalf("%v", err) + } + s = buf.String() + if s != string(latin1) { + t.Fatalf("string is %v, want %v", s, latin1) + } + //if s, err = buf.ReadString(0); err != nil { + //t.Fatalf("%v", err) + //} +} diff --git a/libgo/go/crypto/aes/aes_test.go b/libgo/go/crypto/aes/aes_test.go index 3505d33e960..e500c666d97 100644 --- a/libgo/go/crypto/aes/aes_test.go +++ b/libgo/go/crypto/aes/aes_test.go @@ -91,6 +91,7 @@ func TestTe(t *testing.T) { s2 := mul(s, 2) s3 := mul(s, 3) w := s2<<24 | s<<16 | s<<8 | s3 + te := [][256]uint32{te0, te1, te2, te3} for j := 0; j < 4; j++ { if x := te[j][i]; x != w { t.Fatalf("te[%d][%d] = %#x, want %#x", j, i, x, w) @@ -110,6 +111,7 @@ func TestTd(t *testing.T) { sd := mul(s, 0xd) se := mul(s, 0xe) w := se<<24 | s9<<16 | sd<<8 | sb + td := [][256]uint32{td0, td1, td2, td3} for j := 0; j < 4; j++ { if x := td[j][i]; x != w { t.Fatalf("td[%d][%d] = %#x, want %#x", j, i, x, w) @@ -354,7 +356,7 @@ func BenchmarkEncrypt(b *testing.B) { tt := encryptTests[0] c, err := NewCipher(tt.key) if err != nil { - panic("NewCipher") + b.Fatal("NewCipher:", err) } out := make([]byte, len(tt.in)) b.StartTimer() diff --git a/libgo/go/crypto/aes/block.go b/libgo/go/crypto/aes/block.go index 37b0dd05841..b930787cec7 100644 --- a/libgo/go/crypto/aes/block.go +++ b/libgo/go/crypto/aes/block.go @@ -56,10 +56,10 @@ func encryptBlock(xk []uint32, dst, src []byte) { nr := len(xk)/4 - 2 // - 2: one above, one more below k := 4 for r := 0; r < nr; r++ { - t0 = xk[k+0] ^ te[0][uint8(s0>>24)] ^ te[1][uint8(s1>>16)] ^ te[2][uint8(s2>>8)] ^ te[3][uint8(s3)] - t1 = xk[k+1] ^ te[0][uint8(s1>>24)] ^ te[1][uint8(s2>>16)] ^ te[2][uint8(s3>>8)] ^ te[3][uint8(s0)] - t2 = xk[k+2] ^ te[0][uint8(s2>>24)] ^ te[1][uint8(s3>>16)] ^ te[2][uint8(s0>>8)] ^ te[3][uint8(s1)] - t3 = xk[k+3] ^ te[0][uint8(s3>>24)] ^ te[1][uint8(s0>>16)] ^ te[2][uint8(s1>>8)] ^ te[3][uint8(s2)] + t0 = xk[k+0] ^ te0[uint8(s0>>24)] ^ te1[uint8(s1>>16)] ^ te2[uint8(s2>>8)] ^ te3[uint8(s3)] + t1 = xk[k+1] ^ te0[uint8(s1>>24)] ^ te1[uint8(s2>>16)] ^ te2[uint8(s3>>8)] ^ te3[uint8(s0)] + t2 = xk[k+2] ^ te0[uint8(s2>>24)] ^ te1[uint8(s3>>16)] ^ te2[uint8(s0>>8)] ^ te3[uint8(s1)] + t3 = xk[k+3] ^ te0[uint8(s3>>24)] ^ te1[uint8(s0>>16)] ^ te2[uint8(s1>>8)] ^ te3[uint8(s2)] k += 4 s0, s1, s2, s3 = t0, t1, t2, t3 } @@ -101,10 +101,10 @@ func decryptBlock(xk []uint32, dst, src []byte) { nr := len(xk)/4 - 2 // - 2: one above, one more below k := 4 for r := 0; r < nr; r++ { - t0 = xk[k+0] ^ td[0][uint8(s0>>24)] ^ td[1][uint8(s3>>16)] ^ td[2][uint8(s2>>8)] ^ td[3][uint8(s1)] - t1 = xk[k+1] ^ td[0][uint8(s1>>24)] ^ td[1][uint8(s0>>16)] ^ td[2][uint8(s3>>8)] ^ td[3][uint8(s2)] - t2 = xk[k+2] ^ td[0][uint8(s2>>24)] ^ td[1][uint8(s1>>16)] ^ td[2][uint8(s0>>8)] ^ td[3][uint8(s3)] - t3 = xk[k+3] ^ td[0][uint8(s3>>24)] ^ td[1][uint8(s2>>16)] ^ td[2][uint8(s1>>8)] ^ td[3][uint8(s0)] + t0 = xk[k+0] ^ td0[uint8(s0>>24)] ^ td1[uint8(s3>>16)] ^ td2[uint8(s2>>8)] ^ td3[uint8(s1)] + t1 = xk[k+1] ^ td0[uint8(s1>>24)] ^ td1[uint8(s0>>16)] ^ td2[uint8(s3>>8)] ^ td3[uint8(s2)] + t2 = xk[k+2] ^ td0[uint8(s2>>24)] ^ td1[uint8(s1>>16)] ^ td2[uint8(s0>>8)] ^ td3[uint8(s3)] + t3 = xk[k+3] ^ td0[uint8(s3>>24)] ^ td1[uint8(s2>>16)] ^ td2[uint8(s1>>8)] ^ td3[uint8(s0)] k += 4 s0, s1, s2, s3 = t0, t1, t2, t3 } @@ -168,7 +168,7 @@ func expandKey(key []byte, enc, dec []uint32) { for j := 0; j < 4; j++ { x := enc[ei+j] if i > 0 && i+4 < n { - x = td[0][sbox0[x>>24]] ^ td[1][sbox0[x>>16&0xff]] ^ td[2][sbox0[x>>8&0xff]] ^ td[3][sbox0[x&0xff]] + x = td0[sbox0[x>>24]] ^ td1[sbox0[x>>16&0xff]] ^ td2[sbox0[x>>8&0xff]] ^ td3[sbox0[x&0xff]] } dec[i+j] = x } diff --git a/libgo/go/crypto/aes/const.go b/libgo/go/crypto/aes/const.go index 25acd0d1702..f0b4eabf6ea 100644 --- a/libgo/go/crypto/aes/const.go +++ b/libgo/go/crypto/aes/const.go @@ -80,283 +80,279 @@ var sbox1 = [256]byte{ // Lookup tables for encryption. // These can be recomputed by adapting the tests in aes_test.go. -var te = [4][256]uint32{ - { - 0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554, - 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a, - 0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b, - 0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea, 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b, - 0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a, 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f, - 0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108, 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f, - 0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e, 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5, - 0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d, 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f, - 0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e, 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb, - 0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce, 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497, - 0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c, 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed, - 0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b, 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a, - 0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16, 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594, - 0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81, 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3, - 0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a, 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504, - 0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163, 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d, - 0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f, 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739, - 0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47, 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395, - 0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f, 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883, - 0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c, 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76, - 0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e, 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4, - 0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6, 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b, - 0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7, 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0, - 0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25, 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818, - 0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72, 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651, - 0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21, 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85, - 0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa, 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12, - 0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0, 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9, - 0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133, 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7, - 0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920, 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a, - 0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17, 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8, - 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a, - }, - { - 0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5, - 0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676, - 0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d, 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0, - 0xec41adad, 0x67b3d4d4, 0xfd5fa2a2, 0xea45afaf, 0xbf239c9c, 0xf753a4a4, 0x96e47272, 0x5b9bc0c0, - 0xc275b7b7, 0x1ce1fdfd, 0xae3d9393, 0x6a4c2626, 0x5a6c3636, 0x417e3f3f, 0x02f5f7f7, 0x4f83cccc, - 0x5c683434, 0xf451a5a5, 0x34d1e5e5, 0x08f9f1f1, 0x93e27171, 0x73abd8d8, 0x53623131, 0x3f2a1515, - 0x0c080404, 0x5295c7c7, 0x65462323, 0x5e9dc3c3, 0x28301818, 0xa1379696, 0x0f0a0505, 0xb52f9a9a, - 0x090e0707, 0x36241212, 0x9b1b8080, 0x3ddfe2e2, 0x26cdebeb, 0x694e2727, 0xcd7fb2b2, 0x9fea7575, - 0x1b120909, 0x9e1d8383, 0x74582c2c, 0x2e341a1a, 0x2d361b1b, 0xb2dc6e6e, 0xeeb45a5a, 0xfb5ba0a0, - 0xf6a45252, 0x4d763b3b, 0x61b7d6d6, 0xce7db3b3, 0x7b522929, 0x3edde3e3, 0x715e2f2f, 0x97138484, - 0xf5a65353, 0x68b9d1d1, 0x00000000, 0x2cc1eded, 0x60402020, 0x1fe3fcfc, 0xc879b1b1, 0xedb65b5b, - 0xbed46a6a, 0x468dcbcb, 0xd967bebe, 0x4b723939, 0xde944a4a, 0xd4984c4c, 0xe8b05858, 0x4a85cfcf, - 0x6bbbd0d0, 0x2ac5efef, 0xe54faaaa, 0x16edfbfb, 0xc5864343, 0xd79a4d4d, 0x55663333, 0x94118585, - 0xcf8a4545, 0x10e9f9f9, 0x06040202, 0x81fe7f7f, 0xf0a05050, 0x44783c3c, 0xba259f9f, 0xe34ba8a8, - 0xf3a25151, 0xfe5da3a3, 0xc0804040, 0x8a058f8f, 0xad3f9292, 0xbc219d9d, 0x48703838, 0x04f1f5f5, - 0xdf63bcbc, 0xc177b6b6, 0x75afdada, 0x63422121, 0x30201010, 0x1ae5ffff, 0x0efdf3f3, 0x6dbfd2d2, - 0x4c81cdcd, 0x14180c0c, 0x35261313, 0x2fc3ecec, 0xe1be5f5f, 0xa2359797, 0xcc884444, 0x392e1717, - 0x5793c4c4, 0xf255a7a7, 0x82fc7e7e, 0x477a3d3d, 0xacc86464, 0xe7ba5d5d, 0x2b321919, 0x95e67373, - 0xa0c06060, 0x98198181, 0xd19e4f4f, 0x7fa3dcdc, 0x66442222, 0x7e542a2a, 0xab3b9090, 0x830b8888, - 0xca8c4646, 0x29c7eeee, 0xd36bb8b8, 0x3c281414, 0x79a7dede, 0xe2bc5e5e, 0x1d160b0b, 0x76addbdb, - 0x3bdbe0e0, 0x56643232, 0x4e743a3a, 0x1e140a0a, 0xdb924949, 0x0a0c0606, 0x6c482424, 0xe4b85c5c, - 0x5d9fc2c2, 0x6ebdd3d3, 0xef43acac, 0xa6c46262, 0xa8399191, 0xa4319595, 0x37d3e4e4, 0x8bf27979, - 0x32d5e7e7, 0x438bc8c8, 0x596e3737, 0xb7da6d6d, 0x8c018d8d, 0x64b1d5d5, 0xd29c4e4e, 0xe049a9a9, - 0xb4d86c6c, 0xfaac5656, 0x07f3f4f4, 0x25cfeaea, 0xafca6565, 0x8ef47a7a, 0xe947aeae, 0x18100808, - 0xd56fbaba, 0x88f07878, 0x6f4a2525, 0x725c2e2e, 0x24381c1c, 0xf157a6a6, 0xc773b4b4, 0x5197c6c6, - 0x23cbe8e8, 0x7ca1dddd, 0x9ce87474, 0x213e1f1f, 0xdd964b4b, 0xdc61bdbd, 0x860d8b8b, 0x850f8a8a, - 0x90e07070, 0x427c3e3e, 0xc471b5b5, 0xaacc6666, 0xd8904848, 0x05060303, 0x01f7f6f6, 0x121c0e0e, - 0xa3c26161, 0x5f6a3535, 0xf9ae5757, 0xd069b9b9, 0x91178686, 0x5899c1c1, 0x273a1d1d, 0xb9279e9e, - 0x38d9e1e1, 0x13ebf8f8, 0xb32b9898, 0x33221111, 0xbbd26969, 0x70a9d9d9, 0x89078e8e, 0xa7339494, - 0xb62d9b9b, 0x223c1e1e, 0x92158787, 0x20c9e9e9, 0x4987cece, 0xffaa5555, 0x78502828, 0x7aa5dfdf, - 0x8f038c8c, 0xf859a1a1, 0x80098989, 0x171a0d0d, 0xda65bfbf, 0x31d7e6e6, 0xc6844242, 0xb8d06868, - 0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616, - }, - { - 0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5, - 0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76, - 0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d, 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0, - 0xadec41ad, 0xd467b3d4, 0xa2fd5fa2, 0xafea45af, 0x9cbf239c, 0xa4f753a4, 0x7296e472, 0xc05b9bc0, - 0xb7c275b7, 0xfd1ce1fd, 0x93ae3d93, 0x266a4c26, 0x365a6c36, 0x3f417e3f, 0xf702f5f7, 0xcc4f83cc, - 0x345c6834, 0xa5f451a5, 0xe534d1e5, 0xf108f9f1, 0x7193e271, 0xd873abd8, 0x31536231, 0x153f2a15, - 0x040c0804, 0xc75295c7, 0x23654623, 0xc35e9dc3, 0x18283018, 0x96a13796, 0x050f0a05, 0x9ab52f9a, - 0x07090e07, 0x12362412, 0x809b1b80, 0xe23ddfe2, 0xeb26cdeb, 0x27694e27, 0xb2cd7fb2, 0x759fea75, - 0x091b1209, 0x839e1d83, 0x2c74582c, 0x1a2e341a, 0x1b2d361b, 0x6eb2dc6e, 0x5aeeb45a, 0xa0fb5ba0, - 0x52f6a452, 0x3b4d763b, 0xd661b7d6, 0xb3ce7db3, 0x297b5229, 0xe33edde3, 0x2f715e2f, 0x84971384, - 0x53f5a653, 0xd168b9d1, 0x00000000, 0xed2cc1ed, 0x20604020, 0xfc1fe3fc, 0xb1c879b1, 0x5bedb65b, - 0x6abed46a, 0xcb468dcb, 0xbed967be, 0x394b7239, 0x4ade944a, 0x4cd4984c, 0x58e8b058, 0xcf4a85cf, - 0xd06bbbd0, 0xef2ac5ef, 0xaae54faa, 0xfb16edfb, 0x43c58643, 0x4dd79a4d, 0x33556633, 0x85941185, - 0x45cf8a45, 0xf910e9f9, 0x02060402, 0x7f81fe7f, 0x50f0a050, 0x3c44783c, 0x9fba259f, 0xa8e34ba8, - 0x51f3a251, 0xa3fe5da3, 0x40c08040, 0x8f8a058f, 0x92ad3f92, 0x9dbc219d, 0x38487038, 0xf504f1f5, - 0xbcdf63bc, 0xb6c177b6, 0xda75afda, 0x21634221, 0x10302010, 0xff1ae5ff, 0xf30efdf3, 0xd26dbfd2, - 0xcd4c81cd, 0x0c14180c, 0x13352613, 0xec2fc3ec, 0x5fe1be5f, 0x97a23597, 0x44cc8844, 0x17392e17, - 0xc45793c4, 0xa7f255a7, 0x7e82fc7e, 0x3d477a3d, 0x64acc864, 0x5de7ba5d, 0x192b3219, 0x7395e673, - 0x60a0c060, 0x81981981, 0x4fd19e4f, 0xdc7fa3dc, 0x22664422, 0x2a7e542a, 0x90ab3b90, 0x88830b88, - 0x46ca8c46, 0xee29c7ee, 0xb8d36bb8, 0x143c2814, 0xde79a7de, 0x5ee2bc5e, 0x0b1d160b, 0xdb76addb, - 0xe03bdbe0, 0x32566432, 0x3a4e743a, 0x0a1e140a, 0x49db9249, 0x060a0c06, 0x246c4824, 0x5ce4b85c, - 0xc25d9fc2, 0xd36ebdd3, 0xacef43ac, 0x62a6c462, 0x91a83991, 0x95a43195, 0xe437d3e4, 0x798bf279, - 0xe732d5e7, 0xc8438bc8, 0x37596e37, 0x6db7da6d, 0x8d8c018d, 0xd564b1d5, 0x4ed29c4e, 0xa9e049a9, - 0x6cb4d86c, 0x56faac56, 0xf407f3f4, 0xea25cfea, 0x65afca65, 0x7a8ef47a, 0xaee947ae, 0x08181008, - 0xbad56fba, 0x7888f078, 0x256f4a25, 0x2e725c2e, 0x1c24381c, 0xa6f157a6, 0xb4c773b4, 0xc65197c6, - 0xe823cbe8, 0xdd7ca1dd, 0x749ce874, 0x1f213e1f, 0x4bdd964b, 0xbddc61bd, 0x8b860d8b, 0x8a850f8a, - 0x7090e070, 0x3e427c3e, 0xb5c471b5, 0x66aacc66, 0x48d89048, 0x03050603, 0xf601f7f6, 0x0e121c0e, - 0x61a3c261, 0x355f6a35, 0x57f9ae57, 0xb9d069b9, 0x86911786, 0xc15899c1, 0x1d273a1d, 0x9eb9279e, - 0xe138d9e1, 0xf813ebf8, 0x98b32b98, 0x11332211, 0x69bbd269, 0xd970a9d9, 0x8e89078e, 0x94a73394, - 0x9bb62d9b, 0x1e223c1e, 0x87921587, 0xe920c9e9, 0xce4987ce, 0x55ffaa55, 0x28785028, 0xdf7aa5df, - 0x8c8f038c, 0xa1f859a1, 0x89800989, 0x0d171a0d, 0xbfda65bf, 0xe631d7e6, 0x42c68442, 0x68b8d068, - 0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16, - }, - { - 0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491, - 0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec, - 0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa, 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb, - 0xadadec41, 0xd4d467b3, 0xa2a2fd5f, 0xafafea45, 0x9c9cbf23, 0xa4a4f753, 0x727296e4, 0xc0c05b9b, - 0xb7b7c275, 0xfdfd1ce1, 0x9393ae3d, 0x26266a4c, 0x36365a6c, 0x3f3f417e, 0xf7f702f5, 0xcccc4f83, - 0x34345c68, 0xa5a5f451, 0xe5e534d1, 0xf1f108f9, 0x717193e2, 0xd8d873ab, 0x31315362, 0x15153f2a, - 0x04040c08, 0xc7c75295, 0x23236546, 0xc3c35e9d, 0x18182830, 0x9696a137, 0x05050f0a, 0x9a9ab52f, - 0x0707090e, 0x12123624, 0x80809b1b, 0xe2e23ddf, 0xebeb26cd, 0x2727694e, 0xb2b2cd7f, 0x75759fea, - 0x09091b12, 0x83839e1d, 0x2c2c7458, 0x1a1a2e34, 0x1b1b2d36, 0x6e6eb2dc, 0x5a5aeeb4, 0xa0a0fb5b, - 0x5252f6a4, 0x3b3b4d76, 0xd6d661b7, 0xb3b3ce7d, 0x29297b52, 0xe3e33edd, 0x2f2f715e, 0x84849713, - 0x5353f5a6, 0xd1d168b9, 0x00000000, 0xeded2cc1, 0x20206040, 0xfcfc1fe3, 0xb1b1c879, 0x5b5bedb6, - 0x6a6abed4, 0xcbcb468d, 0xbebed967, 0x39394b72, 0x4a4ade94, 0x4c4cd498, 0x5858e8b0, 0xcfcf4a85, - 0xd0d06bbb, 0xefef2ac5, 0xaaaae54f, 0xfbfb16ed, 0x4343c586, 0x4d4dd79a, 0x33335566, 0x85859411, - 0x4545cf8a, 0xf9f910e9, 0x02020604, 0x7f7f81fe, 0x5050f0a0, 0x3c3c4478, 0x9f9fba25, 0xa8a8e34b, - 0x5151f3a2, 0xa3a3fe5d, 0x4040c080, 0x8f8f8a05, 0x9292ad3f, 0x9d9dbc21, 0x38384870, 0xf5f504f1, - 0xbcbcdf63, 0xb6b6c177, 0xdada75af, 0x21216342, 0x10103020, 0xffff1ae5, 0xf3f30efd, 0xd2d26dbf, - 0xcdcd4c81, 0x0c0c1418, 0x13133526, 0xecec2fc3, 0x5f5fe1be, 0x9797a235, 0x4444cc88, 0x1717392e, - 0xc4c45793, 0xa7a7f255, 0x7e7e82fc, 0x3d3d477a, 0x6464acc8, 0x5d5de7ba, 0x19192b32, 0x737395e6, - 0x6060a0c0, 0x81819819, 0x4f4fd19e, 0xdcdc7fa3, 0x22226644, 0x2a2a7e54, 0x9090ab3b, 0x8888830b, - 0x4646ca8c, 0xeeee29c7, 0xb8b8d36b, 0x14143c28, 0xdede79a7, 0x5e5ee2bc, 0x0b0b1d16, 0xdbdb76ad, - 0xe0e03bdb, 0x32325664, 0x3a3a4e74, 0x0a0a1e14, 0x4949db92, 0x06060a0c, 0x24246c48, 0x5c5ce4b8, - 0xc2c25d9f, 0xd3d36ebd, 0xacacef43, 0x6262a6c4, 0x9191a839, 0x9595a431, 0xe4e437d3, 0x79798bf2, - 0xe7e732d5, 0xc8c8438b, 0x3737596e, 0x6d6db7da, 0x8d8d8c01, 0xd5d564b1, 0x4e4ed29c, 0xa9a9e049, - 0x6c6cb4d8, 0x5656faac, 0xf4f407f3, 0xeaea25cf, 0x6565afca, 0x7a7a8ef4, 0xaeaee947, 0x08081810, - 0xbabad56f, 0x787888f0, 0x25256f4a, 0x2e2e725c, 0x1c1c2438, 0xa6a6f157, 0xb4b4c773, 0xc6c65197, - 0xe8e823cb, 0xdddd7ca1, 0x74749ce8, 0x1f1f213e, 0x4b4bdd96, 0xbdbddc61, 0x8b8b860d, 0x8a8a850f, - 0x707090e0, 0x3e3e427c, 0xb5b5c471, 0x6666aacc, 0x4848d890, 0x03030506, 0xf6f601f7, 0x0e0e121c, - 0x6161a3c2, 0x35355f6a, 0x5757f9ae, 0xb9b9d069, 0x86869117, 0xc1c15899, 0x1d1d273a, 0x9e9eb927, - 0xe1e138d9, 0xf8f813eb, 0x9898b32b, 0x11113322, 0x6969bbd2, 0xd9d970a9, 0x8e8e8907, 0x9494a733, - 0x9b9bb62d, 0x1e1e223c, 0x87879215, 0xe9e920c9, 0xcece4987, 0x5555ffaa, 0x28287850, 0xdfdf7aa5, - 0x8c8c8f03, 0xa1a1f859, 0x89898009, 0x0d0d171a, 0xbfbfda65, 0xe6e631d7, 0x4242c684, 0x6868b8d0, - 0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e, 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c, - }, +var te0 = [256]uint32{ + 0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554, + 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a, + 0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b, + 0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea, 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b, + 0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a, 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f, + 0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108, 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f, + 0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e, 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5, + 0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d, 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f, + 0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e, 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb, + 0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce, 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497, + 0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c, 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed, + 0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b, 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a, + 0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16, 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594, + 0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81, 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3, + 0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a, 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504, + 0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163, 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d, + 0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f, 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739, + 0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47, 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395, + 0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f, 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883, + 0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c, 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76, + 0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e, 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4, + 0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6, 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b, + 0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7, 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0, + 0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25, 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818, + 0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72, 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651, + 0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21, 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85, + 0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa, 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12, + 0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0, 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9, + 0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133, 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7, + 0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920, 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a, + 0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17, 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8, + 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a, +} +var te1 = [256]uint32{ + 0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5, + 0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676, + 0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d, 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0, + 0xec41adad, 0x67b3d4d4, 0xfd5fa2a2, 0xea45afaf, 0xbf239c9c, 0xf753a4a4, 0x96e47272, 0x5b9bc0c0, + 0xc275b7b7, 0x1ce1fdfd, 0xae3d9393, 0x6a4c2626, 0x5a6c3636, 0x417e3f3f, 0x02f5f7f7, 0x4f83cccc, + 0x5c683434, 0xf451a5a5, 0x34d1e5e5, 0x08f9f1f1, 0x93e27171, 0x73abd8d8, 0x53623131, 0x3f2a1515, + 0x0c080404, 0x5295c7c7, 0x65462323, 0x5e9dc3c3, 0x28301818, 0xa1379696, 0x0f0a0505, 0xb52f9a9a, + 0x090e0707, 0x36241212, 0x9b1b8080, 0x3ddfe2e2, 0x26cdebeb, 0x694e2727, 0xcd7fb2b2, 0x9fea7575, + 0x1b120909, 0x9e1d8383, 0x74582c2c, 0x2e341a1a, 0x2d361b1b, 0xb2dc6e6e, 0xeeb45a5a, 0xfb5ba0a0, + 0xf6a45252, 0x4d763b3b, 0x61b7d6d6, 0xce7db3b3, 0x7b522929, 0x3edde3e3, 0x715e2f2f, 0x97138484, + 0xf5a65353, 0x68b9d1d1, 0x00000000, 0x2cc1eded, 0x60402020, 0x1fe3fcfc, 0xc879b1b1, 0xedb65b5b, + 0xbed46a6a, 0x468dcbcb, 0xd967bebe, 0x4b723939, 0xde944a4a, 0xd4984c4c, 0xe8b05858, 0x4a85cfcf, + 0x6bbbd0d0, 0x2ac5efef, 0xe54faaaa, 0x16edfbfb, 0xc5864343, 0xd79a4d4d, 0x55663333, 0x94118585, + 0xcf8a4545, 0x10e9f9f9, 0x06040202, 0x81fe7f7f, 0xf0a05050, 0x44783c3c, 0xba259f9f, 0xe34ba8a8, + 0xf3a25151, 0xfe5da3a3, 0xc0804040, 0x8a058f8f, 0xad3f9292, 0xbc219d9d, 0x48703838, 0x04f1f5f5, + 0xdf63bcbc, 0xc177b6b6, 0x75afdada, 0x63422121, 0x30201010, 0x1ae5ffff, 0x0efdf3f3, 0x6dbfd2d2, + 0x4c81cdcd, 0x14180c0c, 0x35261313, 0x2fc3ecec, 0xe1be5f5f, 0xa2359797, 0xcc884444, 0x392e1717, + 0x5793c4c4, 0xf255a7a7, 0x82fc7e7e, 0x477a3d3d, 0xacc86464, 0xe7ba5d5d, 0x2b321919, 0x95e67373, + 0xa0c06060, 0x98198181, 0xd19e4f4f, 0x7fa3dcdc, 0x66442222, 0x7e542a2a, 0xab3b9090, 0x830b8888, + 0xca8c4646, 0x29c7eeee, 0xd36bb8b8, 0x3c281414, 0x79a7dede, 0xe2bc5e5e, 0x1d160b0b, 0x76addbdb, + 0x3bdbe0e0, 0x56643232, 0x4e743a3a, 0x1e140a0a, 0xdb924949, 0x0a0c0606, 0x6c482424, 0xe4b85c5c, + 0x5d9fc2c2, 0x6ebdd3d3, 0xef43acac, 0xa6c46262, 0xa8399191, 0xa4319595, 0x37d3e4e4, 0x8bf27979, + 0x32d5e7e7, 0x438bc8c8, 0x596e3737, 0xb7da6d6d, 0x8c018d8d, 0x64b1d5d5, 0xd29c4e4e, 0xe049a9a9, + 0xb4d86c6c, 0xfaac5656, 0x07f3f4f4, 0x25cfeaea, 0xafca6565, 0x8ef47a7a, 0xe947aeae, 0x18100808, + 0xd56fbaba, 0x88f07878, 0x6f4a2525, 0x725c2e2e, 0x24381c1c, 0xf157a6a6, 0xc773b4b4, 0x5197c6c6, + 0x23cbe8e8, 0x7ca1dddd, 0x9ce87474, 0x213e1f1f, 0xdd964b4b, 0xdc61bdbd, 0x860d8b8b, 0x850f8a8a, + 0x90e07070, 0x427c3e3e, 0xc471b5b5, 0xaacc6666, 0xd8904848, 0x05060303, 0x01f7f6f6, 0x121c0e0e, + 0xa3c26161, 0x5f6a3535, 0xf9ae5757, 0xd069b9b9, 0x91178686, 0x5899c1c1, 0x273a1d1d, 0xb9279e9e, + 0x38d9e1e1, 0x13ebf8f8, 0xb32b9898, 0x33221111, 0xbbd26969, 0x70a9d9d9, 0x89078e8e, 0xa7339494, + 0xb62d9b9b, 0x223c1e1e, 0x92158787, 0x20c9e9e9, 0x4987cece, 0xffaa5555, 0x78502828, 0x7aa5dfdf, + 0x8f038c8c, 0xf859a1a1, 0x80098989, 0x171a0d0d, 0xda65bfbf, 0x31d7e6e6, 0xc6844242, 0xb8d06868, + 0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616, +} +var te2 = [256]uint32{ + 0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5, + 0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76, + 0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d, 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0, + 0xadec41ad, 0xd467b3d4, 0xa2fd5fa2, 0xafea45af, 0x9cbf239c, 0xa4f753a4, 0x7296e472, 0xc05b9bc0, + 0xb7c275b7, 0xfd1ce1fd, 0x93ae3d93, 0x266a4c26, 0x365a6c36, 0x3f417e3f, 0xf702f5f7, 0xcc4f83cc, + 0x345c6834, 0xa5f451a5, 0xe534d1e5, 0xf108f9f1, 0x7193e271, 0xd873abd8, 0x31536231, 0x153f2a15, + 0x040c0804, 0xc75295c7, 0x23654623, 0xc35e9dc3, 0x18283018, 0x96a13796, 0x050f0a05, 0x9ab52f9a, + 0x07090e07, 0x12362412, 0x809b1b80, 0xe23ddfe2, 0xeb26cdeb, 0x27694e27, 0xb2cd7fb2, 0x759fea75, + 0x091b1209, 0x839e1d83, 0x2c74582c, 0x1a2e341a, 0x1b2d361b, 0x6eb2dc6e, 0x5aeeb45a, 0xa0fb5ba0, + 0x52f6a452, 0x3b4d763b, 0xd661b7d6, 0xb3ce7db3, 0x297b5229, 0xe33edde3, 0x2f715e2f, 0x84971384, + 0x53f5a653, 0xd168b9d1, 0x00000000, 0xed2cc1ed, 0x20604020, 0xfc1fe3fc, 0xb1c879b1, 0x5bedb65b, + 0x6abed46a, 0xcb468dcb, 0xbed967be, 0x394b7239, 0x4ade944a, 0x4cd4984c, 0x58e8b058, 0xcf4a85cf, + 0xd06bbbd0, 0xef2ac5ef, 0xaae54faa, 0xfb16edfb, 0x43c58643, 0x4dd79a4d, 0x33556633, 0x85941185, + 0x45cf8a45, 0xf910e9f9, 0x02060402, 0x7f81fe7f, 0x50f0a050, 0x3c44783c, 0x9fba259f, 0xa8e34ba8, + 0x51f3a251, 0xa3fe5da3, 0x40c08040, 0x8f8a058f, 0x92ad3f92, 0x9dbc219d, 0x38487038, 0xf504f1f5, + 0xbcdf63bc, 0xb6c177b6, 0xda75afda, 0x21634221, 0x10302010, 0xff1ae5ff, 0xf30efdf3, 0xd26dbfd2, + 0xcd4c81cd, 0x0c14180c, 0x13352613, 0xec2fc3ec, 0x5fe1be5f, 0x97a23597, 0x44cc8844, 0x17392e17, + 0xc45793c4, 0xa7f255a7, 0x7e82fc7e, 0x3d477a3d, 0x64acc864, 0x5de7ba5d, 0x192b3219, 0x7395e673, + 0x60a0c060, 0x81981981, 0x4fd19e4f, 0xdc7fa3dc, 0x22664422, 0x2a7e542a, 0x90ab3b90, 0x88830b88, + 0x46ca8c46, 0xee29c7ee, 0xb8d36bb8, 0x143c2814, 0xde79a7de, 0x5ee2bc5e, 0x0b1d160b, 0xdb76addb, + 0xe03bdbe0, 0x32566432, 0x3a4e743a, 0x0a1e140a, 0x49db9249, 0x060a0c06, 0x246c4824, 0x5ce4b85c, + 0xc25d9fc2, 0xd36ebdd3, 0xacef43ac, 0x62a6c462, 0x91a83991, 0x95a43195, 0xe437d3e4, 0x798bf279, + 0xe732d5e7, 0xc8438bc8, 0x37596e37, 0x6db7da6d, 0x8d8c018d, 0xd564b1d5, 0x4ed29c4e, 0xa9e049a9, + 0x6cb4d86c, 0x56faac56, 0xf407f3f4, 0xea25cfea, 0x65afca65, 0x7a8ef47a, 0xaee947ae, 0x08181008, + 0xbad56fba, 0x7888f078, 0x256f4a25, 0x2e725c2e, 0x1c24381c, 0xa6f157a6, 0xb4c773b4, 0xc65197c6, + 0xe823cbe8, 0xdd7ca1dd, 0x749ce874, 0x1f213e1f, 0x4bdd964b, 0xbddc61bd, 0x8b860d8b, 0x8a850f8a, + 0x7090e070, 0x3e427c3e, 0xb5c471b5, 0x66aacc66, 0x48d89048, 0x03050603, 0xf601f7f6, 0x0e121c0e, + 0x61a3c261, 0x355f6a35, 0x57f9ae57, 0xb9d069b9, 0x86911786, 0xc15899c1, 0x1d273a1d, 0x9eb9279e, + 0xe138d9e1, 0xf813ebf8, 0x98b32b98, 0x11332211, 0x69bbd269, 0xd970a9d9, 0x8e89078e, 0x94a73394, + 0x9bb62d9b, 0x1e223c1e, 0x87921587, 0xe920c9e9, 0xce4987ce, 0x55ffaa55, 0x28785028, 0xdf7aa5df, + 0x8c8f038c, 0xa1f859a1, 0x89800989, 0x0d171a0d, 0xbfda65bf, 0xe631d7e6, 0x42c68442, 0x68b8d068, + 0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16, +} +var te3 = [256]uint32{ + 0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491, + 0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec, + 0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa, 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb, + 0xadadec41, 0xd4d467b3, 0xa2a2fd5f, 0xafafea45, 0x9c9cbf23, 0xa4a4f753, 0x727296e4, 0xc0c05b9b, + 0xb7b7c275, 0xfdfd1ce1, 0x9393ae3d, 0x26266a4c, 0x36365a6c, 0x3f3f417e, 0xf7f702f5, 0xcccc4f83, + 0x34345c68, 0xa5a5f451, 0xe5e534d1, 0xf1f108f9, 0x717193e2, 0xd8d873ab, 0x31315362, 0x15153f2a, + 0x04040c08, 0xc7c75295, 0x23236546, 0xc3c35e9d, 0x18182830, 0x9696a137, 0x05050f0a, 0x9a9ab52f, + 0x0707090e, 0x12123624, 0x80809b1b, 0xe2e23ddf, 0xebeb26cd, 0x2727694e, 0xb2b2cd7f, 0x75759fea, + 0x09091b12, 0x83839e1d, 0x2c2c7458, 0x1a1a2e34, 0x1b1b2d36, 0x6e6eb2dc, 0x5a5aeeb4, 0xa0a0fb5b, + 0x5252f6a4, 0x3b3b4d76, 0xd6d661b7, 0xb3b3ce7d, 0x29297b52, 0xe3e33edd, 0x2f2f715e, 0x84849713, + 0x5353f5a6, 0xd1d168b9, 0x00000000, 0xeded2cc1, 0x20206040, 0xfcfc1fe3, 0xb1b1c879, 0x5b5bedb6, + 0x6a6abed4, 0xcbcb468d, 0xbebed967, 0x39394b72, 0x4a4ade94, 0x4c4cd498, 0x5858e8b0, 0xcfcf4a85, + 0xd0d06bbb, 0xefef2ac5, 0xaaaae54f, 0xfbfb16ed, 0x4343c586, 0x4d4dd79a, 0x33335566, 0x85859411, + 0x4545cf8a, 0xf9f910e9, 0x02020604, 0x7f7f81fe, 0x5050f0a0, 0x3c3c4478, 0x9f9fba25, 0xa8a8e34b, + 0x5151f3a2, 0xa3a3fe5d, 0x4040c080, 0x8f8f8a05, 0x9292ad3f, 0x9d9dbc21, 0x38384870, 0xf5f504f1, + 0xbcbcdf63, 0xb6b6c177, 0xdada75af, 0x21216342, 0x10103020, 0xffff1ae5, 0xf3f30efd, 0xd2d26dbf, + 0xcdcd4c81, 0x0c0c1418, 0x13133526, 0xecec2fc3, 0x5f5fe1be, 0x9797a235, 0x4444cc88, 0x1717392e, + 0xc4c45793, 0xa7a7f255, 0x7e7e82fc, 0x3d3d477a, 0x6464acc8, 0x5d5de7ba, 0x19192b32, 0x737395e6, + 0x6060a0c0, 0x81819819, 0x4f4fd19e, 0xdcdc7fa3, 0x22226644, 0x2a2a7e54, 0x9090ab3b, 0x8888830b, + 0x4646ca8c, 0xeeee29c7, 0xb8b8d36b, 0x14143c28, 0xdede79a7, 0x5e5ee2bc, 0x0b0b1d16, 0xdbdb76ad, + 0xe0e03bdb, 0x32325664, 0x3a3a4e74, 0x0a0a1e14, 0x4949db92, 0x06060a0c, 0x24246c48, 0x5c5ce4b8, + 0xc2c25d9f, 0xd3d36ebd, 0xacacef43, 0x6262a6c4, 0x9191a839, 0x9595a431, 0xe4e437d3, 0x79798bf2, + 0xe7e732d5, 0xc8c8438b, 0x3737596e, 0x6d6db7da, 0x8d8d8c01, 0xd5d564b1, 0x4e4ed29c, 0xa9a9e049, + 0x6c6cb4d8, 0x5656faac, 0xf4f407f3, 0xeaea25cf, 0x6565afca, 0x7a7a8ef4, 0xaeaee947, 0x08081810, + 0xbabad56f, 0x787888f0, 0x25256f4a, 0x2e2e725c, 0x1c1c2438, 0xa6a6f157, 0xb4b4c773, 0xc6c65197, + 0xe8e823cb, 0xdddd7ca1, 0x74749ce8, 0x1f1f213e, 0x4b4bdd96, 0xbdbddc61, 0x8b8b860d, 0x8a8a850f, + 0x707090e0, 0x3e3e427c, 0xb5b5c471, 0x6666aacc, 0x4848d890, 0x03030506, 0xf6f601f7, 0x0e0e121c, + 0x6161a3c2, 0x35355f6a, 0x5757f9ae, 0xb9b9d069, 0x86869117, 0xc1c15899, 0x1d1d273a, 0x9e9eb927, + 0xe1e138d9, 0xf8f813eb, 0x9898b32b, 0x11113322, 0x6969bbd2, 0xd9d970a9, 0x8e8e8907, 0x9494a733, + 0x9b9bb62d, 0x1e1e223c, 0x87879215, 0xe9e920c9, 0xcece4987, 0x5555ffaa, 0x28287850, 0xdfdf7aa5, + 0x8c8c8f03, 0xa1a1f859, 0x89898009, 0x0d0d171a, 0xbfbfda65, 0xe6e631d7, 0x4242c684, 0x6868b8d0, + 0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e, 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c, } // Lookup tables for decryption. // These can be recomputed by adapting the tests in aes_test.go. -var td = [4][256]uint32{ - { - 0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, - 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f, - 0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6, - 0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da, 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844, - 0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd, 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4, - 0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45, 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94, - 0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7, 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a, - 0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5, 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c, - 0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1, 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a, - 0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75, 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051, - 0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46, 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff, - 0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77, 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb, - 0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000, 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e, - 0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927, 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a, - 0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e, 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16, - 0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d, 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8, - 0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd, 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34, - 0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163, 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120, - 0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d, 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0, - 0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422, 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef, - 0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36, 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4, - 0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662, 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5, - 0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3, 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b, - 0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8, 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6, - 0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6, 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0, - 0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815, 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f, - 0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df, 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f, - 0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e, 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713, - 0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89, 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c, - 0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf, 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86, - 0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541, - 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742, - }, - { - 0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303, - 0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3, - 0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0, 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9, - 0xe7038f5f, 0x9515929c, 0xebbf6d7a, 0xda955259, 0x2dd4be83, 0xd3587421, 0x2949e069, 0x448ec9c8, - 0x6a75c289, 0x78f48e79, 0x6b99583e, 0xdd27b971, 0xb6bee14f, 0x17f088ad, 0x66c920ac, 0xb47dce3a, - 0x1863df4a, 0x82e51a31, 0x60975133, 0x4562537f, 0xe0b16477, 0x84bb6bae, 0x1cfe81a0, 0x94f9082b, - 0x58704868, 0x198f45fd, 0x8794de6c, 0xb7527bf8, 0x23ab73d3, 0xe2724b02, 0x57e31f8f, 0x2a6655ab, - 0x07b2eb28, 0x032fb5c2, 0x9a86c57b, 0xa5d33708, 0xf2302887, 0xb223bfa5, 0xba02036a, 0x5ced1682, - 0x2b8acf1c, 0x92a779b4, 0xf0f307f2, 0xa14e69e2, 0xcd65daf4, 0xd50605be, 0x1fd13462, 0x8ac4a6fe, - 0x9d342e53, 0xa0a2f355, 0x32058ae1, 0x75a4f6eb, 0x390b83ec, 0xaa4060ef, 0x065e719f, 0x51bd6e10, - 0xf93e218a, 0x3d96dd06, 0xaedd3e05, 0x464de6bd, 0xb591548d, 0x0571c45d, 0x6f0406d4, 0xff605015, - 0x241998fb, 0x97d6bde9, 0xcc894043, 0x7767d99e, 0xbdb0e842, 0x8807898b, 0x38e7195b, 0xdb79c8ee, - 0x47a17c0a, 0xe97c420f, 0xc9f8841e, 0x00000000, 0x83098086, 0x48322bed, 0xac1e1170, 0x4e6c5a72, - 0xfbfd0eff, 0x560f8538, 0x1e3daed5, 0x27362d39, 0x640a0fd9, 0x21685ca6, 0xd19b5b54, 0x3a24362e, - 0xb10c0a67, 0x0f9357e7, 0xd2b4ee96, 0x9e1b9b91, 0x4f80c0c5, 0xa261dc20, 0x695a774b, 0x161c121a, - 0x0ae293ba, 0xe5c0a02a, 0x433c22e0, 0x1d121b17, 0x0b0e090d, 0xadf28bc7, 0xb92db6a8, 0xc8141ea9, - 0x8557f119, 0x4caf7507, 0xbbee99dd, 0xfda37f60, 0x9ff70126, 0xbc5c72f5, 0xc544663b, 0x345bfb7e, - 0x768b4329, 0xdccb23c6, 0x68b6edfc, 0x63b8e4f1, 0xcad731dc, 0x10426385, 0x40139722, 0x2084c611, - 0x7d854a24, 0xf8d2bb3d, 0x11aef932, 0x6dc729a1, 0x4b1d9e2f, 0xf3dcb230, 0xec0d8652, 0xd077c1e3, - 0x6c2bb316, 0x99a970b9, 0xfa119448, 0x2247e964, 0xc4a8fc8c, 0x1aa0f03f, 0xd8567d2c, 0xef223390, - 0xc787494e, 0xc1d938d1, 0xfe8ccaa2, 0x3698d40b, 0xcfa6f581, 0x28a57ade, 0x26dab78e, 0xa43fadbf, - 0xe42c3a9d, 0x0d507892, 0x9b6a5fcc, 0x62547e46, 0xc2f68d13, 0xe890d8b8, 0x5e2e39f7, 0xf582c3af, - 0xbe9f5d80, 0x7c69d093, 0xa96fd52d, 0xb3cf2512, 0x3bc8ac99, 0xa710187d, 0x6ee89c63, 0x7bdb3bbb, - 0x09cd2678, 0xf46e5918, 0x01ec9ab7, 0xa8834f9a, 0x65e6956e, 0x7eaaffe6, 0x0821bccf, 0xe6ef15e8, - 0xd9bae79b, 0xce4a6f36, 0xd4ea9f09, 0xd629b07c, 0xaf31a4b2, 0x312a3f23, 0x30c6a594, 0xc035a266, - 0x37744ebc, 0xa6fc82ca, 0xb0e090d0, 0x1533a7d8, 0x4af10498, 0xf741ecda, 0x0e7fcd50, 0x2f1791f6, - 0x8d764dd6, 0x4d43efb0, 0x54ccaa4d, 0xdfe49604, 0xe39ed1b5, 0x1b4c6a88, 0xb8c12c1f, 0x7f466551, - 0x049d5eea, 0x5d018c35, 0x73fa8774, 0x2efb0b41, 0x5ab3671d, 0x5292dbd2, 0x33e91056, 0x136dd647, - 0x8c9ad761, 0x7a37a10c, 0x8e59f814, 0x89eb133c, 0xeecea927, 0x35b761c9, 0xede11ce5, 0x3c7a47b1, - 0x599cd2df, 0x3f55f273, 0x791814ce, 0xbf73c737, 0xea53f7cd, 0x5b5ffdaa, 0x14df3d6f, 0x867844db, - 0x81caaff3, 0x3eb968c4, 0x2c382434, 0x5fc2a340, 0x72161dc3, 0x0cbce225, 0x8b283c49, 0x41ff0d95, - 0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857, - }, - { - 0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3, - 0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562, - 0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe, 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3, - 0x5fe7038f, 0x9c951592, 0x7aebbf6d, 0x59da9552, 0x832dd4be, 0x21d35874, 0x692949e0, 0xc8448ec9, - 0x896a75c2, 0x7978f48e, 0x3e6b9958, 0x71dd27b9, 0x4fb6bee1, 0xad17f088, 0xac66c920, 0x3ab47dce, - 0x4a1863df, 0x3182e51a, 0x33609751, 0x7f456253, 0x77e0b164, 0xae84bb6b, 0xa01cfe81, 0x2b94f908, - 0x68587048, 0xfd198f45, 0x6c8794de, 0xf8b7527b, 0xd323ab73, 0x02e2724b, 0x8f57e31f, 0xab2a6655, - 0x2807b2eb, 0xc2032fb5, 0x7b9a86c5, 0x08a5d337, 0x87f23028, 0xa5b223bf, 0x6aba0203, 0x825ced16, - 0x1c2b8acf, 0xb492a779, 0xf2f0f307, 0xe2a14e69, 0xf4cd65da, 0xbed50605, 0x621fd134, 0xfe8ac4a6, - 0x539d342e, 0x55a0a2f3, 0xe132058a, 0xeb75a4f6, 0xec390b83, 0xefaa4060, 0x9f065e71, 0x1051bd6e, - 0x8af93e21, 0x063d96dd, 0x05aedd3e, 0xbd464de6, 0x8db59154, 0x5d0571c4, 0xd46f0406, 0x15ff6050, - 0xfb241998, 0xe997d6bd, 0x43cc8940, 0x9e7767d9, 0x42bdb0e8, 0x8b880789, 0x5b38e719, 0xeedb79c8, - 0x0a47a17c, 0x0fe97c42, 0x1ec9f884, 0x00000000, 0x86830980, 0xed48322b, 0x70ac1e11, 0x724e6c5a, - 0xfffbfd0e, 0x38560f85, 0xd51e3dae, 0x3927362d, 0xd9640a0f, 0xa621685c, 0x54d19b5b, 0x2e3a2436, - 0x67b10c0a, 0xe70f9357, 0x96d2b4ee, 0x919e1b9b, 0xc54f80c0, 0x20a261dc, 0x4b695a77, 0x1a161c12, - 0xba0ae293, 0x2ae5c0a0, 0xe0433c22, 0x171d121b, 0x0d0b0e09, 0xc7adf28b, 0xa8b92db6, 0xa9c8141e, - 0x198557f1, 0x074caf75, 0xddbbee99, 0x60fda37f, 0x269ff701, 0xf5bc5c72, 0x3bc54466, 0x7e345bfb, - 0x29768b43, 0xc6dccb23, 0xfc68b6ed, 0xf163b8e4, 0xdccad731, 0x85104263, 0x22401397, 0x112084c6, - 0x247d854a, 0x3df8d2bb, 0x3211aef9, 0xa16dc729, 0x2f4b1d9e, 0x30f3dcb2, 0x52ec0d86, 0xe3d077c1, - 0x166c2bb3, 0xb999a970, 0x48fa1194, 0x642247e9, 0x8cc4a8fc, 0x3f1aa0f0, 0x2cd8567d, 0x90ef2233, - 0x4ec78749, 0xd1c1d938, 0xa2fe8cca, 0x0b3698d4, 0x81cfa6f5, 0xde28a57a, 0x8e26dab7, 0xbfa43fad, - 0x9de42c3a, 0x920d5078, 0xcc9b6a5f, 0x4662547e, 0x13c2f68d, 0xb8e890d8, 0xf75e2e39, 0xaff582c3, - 0x80be9f5d, 0x937c69d0, 0x2da96fd5, 0x12b3cf25, 0x993bc8ac, 0x7da71018, 0x636ee89c, 0xbb7bdb3b, - 0x7809cd26, 0x18f46e59, 0xb701ec9a, 0x9aa8834f, 0x6e65e695, 0xe67eaaff, 0xcf0821bc, 0xe8e6ef15, - 0x9bd9bae7, 0x36ce4a6f, 0x09d4ea9f, 0x7cd629b0, 0xb2af31a4, 0x23312a3f, 0x9430c6a5, 0x66c035a2, - 0xbc37744e, 0xcaa6fc82, 0xd0b0e090, 0xd81533a7, 0x984af104, 0xdaf741ec, 0x500e7fcd, 0xf62f1791, - 0xd68d764d, 0xb04d43ef, 0x4d54ccaa, 0x04dfe496, 0xb5e39ed1, 0x881b4c6a, 0x1fb8c12c, 0x517f4665, - 0xea049d5e, 0x355d018c, 0x7473fa87, 0x412efb0b, 0x1d5ab367, 0xd25292db, 0x5633e910, 0x47136dd6, - 0x618c9ad7, 0x0c7a37a1, 0x148e59f8, 0x3c89eb13, 0x27eecea9, 0xc935b761, 0xe5ede11c, 0xb13c7a47, - 0xdf599cd2, 0x733f55f2, 0xce791814, 0x37bf73c7, 0xcdea53f7, 0xaa5b5ffd, 0x6f14df3d, 0xdb867844, - 0xf381caaf, 0xc43eb968, 0x342c3824, 0x405fc2a3, 0xc372161d, 0x250cbce2, 0x498b283c, 0x9541ff0d, - 0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8, - }, - { - 0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b, - 0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5, - 0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d, 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b, - 0x8f5fe703, 0x929c9515, 0x6d7aebbf, 0x5259da95, 0xbe832dd4, 0x7421d358, 0xe0692949, 0xc9c8448e, - 0xc2896a75, 0x8e7978f4, 0x583e6b99, 0xb971dd27, 0xe14fb6be, 0x88ad17f0, 0x20ac66c9, 0xce3ab47d, - 0xdf4a1863, 0x1a3182e5, 0x51336097, 0x537f4562, 0x6477e0b1, 0x6bae84bb, 0x81a01cfe, 0x082b94f9, - 0x48685870, 0x45fd198f, 0xde6c8794, 0x7bf8b752, 0x73d323ab, 0x4b02e272, 0x1f8f57e3, 0x55ab2a66, - 0xeb2807b2, 0xb5c2032f, 0xc57b9a86, 0x3708a5d3, 0x2887f230, 0xbfa5b223, 0x036aba02, 0x16825ced, - 0xcf1c2b8a, 0x79b492a7, 0x07f2f0f3, 0x69e2a14e, 0xdaf4cd65, 0x05bed506, 0x34621fd1, 0xa6fe8ac4, - 0x2e539d34, 0xf355a0a2, 0x8ae13205, 0xf6eb75a4, 0x83ec390b, 0x60efaa40, 0x719f065e, 0x6e1051bd, - 0x218af93e, 0xdd063d96, 0x3e05aedd, 0xe6bd464d, 0x548db591, 0xc45d0571, 0x06d46f04, 0x5015ff60, - 0x98fb2419, 0xbde997d6, 0x4043cc89, 0xd99e7767, 0xe842bdb0, 0x898b8807, 0x195b38e7, 0xc8eedb79, - 0x7c0a47a1, 0x420fe97c, 0x841ec9f8, 0x00000000, 0x80868309, 0x2bed4832, 0x1170ac1e, 0x5a724e6c, - 0x0efffbfd, 0x8538560f, 0xaed51e3d, 0x2d392736, 0x0fd9640a, 0x5ca62168, 0x5b54d19b, 0x362e3a24, - 0x0a67b10c, 0x57e70f93, 0xee96d2b4, 0x9b919e1b, 0xc0c54f80, 0xdc20a261, 0x774b695a, 0x121a161c, - 0x93ba0ae2, 0xa02ae5c0, 0x22e0433c, 0x1b171d12, 0x090d0b0e, 0x8bc7adf2, 0xb6a8b92d, 0x1ea9c814, - 0xf1198557, 0x75074caf, 0x99ddbbee, 0x7f60fda3, 0x01269ff7, 0x72f5bc5c, 0x663bc544, 0xfb7e345b, - 0x4329768b, 0x23c6dccb, 0xedfc68b6, 0xe4f163b8, 0x31dccad7, 0x63851042, 0x97224013, 0xc6112084, - 0x4a247d85, 0xbb3df8d2, 0xf93211ae, 0x29a16dc7, 0x9e2f4b1d, 0xb230f3dc, 0x8652ec0d, 0xc1e3d077, - 0xb3166c2b, 0x70b999a9, 0x9448fa11, 0xe9642247, 0xfc8cc4a8, 0xf03f1aa0, 0x7d2cd856, 0x3390ef22, - 0x494ec787, 0x38d1c1d9, 0xcaa2fe8c, 0xd40b3698, 0xf581cfa6, 0x7ade28a5, 0xb78e26da, 0xadbfa43f, - 0x3a9de42c, 0x78920d50, 0x5fcc9b6a, 0x7e466254, 0x8d13c2f6, 0xd8b8e890, 0x39f75e2e, 0xc3aff582, - 0x5d80be9f, 0xd0937c69, 0xd52da96f, 0x2512b3cf, 0xac993bc8, 0x187da710, 0x9c636ee8, 0x3bbb7bdb, - 0x267809cd, 0x5918f46e, 0x9ab701ec, 0x4f9aa883, 0x956e65e6, 0xffe67eaa, 0xbccf0821, 0x15e8e6ef, - 0xe79bd9ba, 0x6f36ce4a, 0x9f09d4ea, 0xb07cd629, 0xa4b2af31, 0x3f23312a, 0xa59430c6, 0xa266c035, - 0x4ebc3774, 0x82caa6fc, 0x90d0b0e0, 0xa7d81533, 0x04984af1, 0xecdaf741, 0xcd500e7f, 0x91f62f17, - 0x4dd68d76, 0xefb04d43, 0xaa4d54cc, 0x9604dfe4, 0xd1b5e39e, 0x6a881b4c, 0x2c1fb8c1, 0x65517f46, - 0x5eea049d, 0x8c355d01, 0x877473fa, 0x0b412efb, 0x671d5ab3, 0xdbd25292, 0x105633e9, 0xd647136d, - 0xd7618c9a, 0xa10c7a37, 0xf8148e59, 0x133c89eb, 0xa927eece, 0x61c935b7, 0x1ce5ede1, 0x47b13c7a, - 0xd2df599c, 0xf2733f55, 0x14ce7918, 0xc737bf73, 0xf7cdea53, 0xfdaa5b5f, 0x3d6f14df, 0x44db8678, - 0xaff381ca, 0x68c43eb9, 0x24342c38, 0xa3405fc2, 0x1dc37216, 0xe2250cbc, 0x3c498b28, 0x0d9541ff, - 0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0, - }, +var td0 = [256]uint32{ + 0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, + 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f, + 0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6, + 0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da, 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844, + 0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd, 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4, + 0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45, 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94, + 0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7, 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a, + 0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5, 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c, + 0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1, 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a, + 0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75, 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051, + 0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46, 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff, + 0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77, 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb, + 0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000, 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e, + 0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927, 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a, + 0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e, 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16, + 0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d, 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8, + 0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd, 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34, + 0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163, 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120, + 0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d, 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0, + 0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422, 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef, + 0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36, 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4, + 0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662, 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5, + 0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3, 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b, + 0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8, 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6, + 0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6, 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0, + 0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815, 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f, + 0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df, 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f, + 0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e, 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713, + 0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89, 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c, + 0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf, 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86, + 0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541, + 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742, +} +var td1 = [256]uint32{ + 0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303, + 0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3, + 0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0, 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9, + 0xe7038f5f, 0x9515929c, 0xebbf6d7a, 0xda955259, 0x2dd4be83, 0xd3587421, 0x2949e069, 0x448ec9c8, + 0x6a75c289, 0x78f48e79, 0x6b99583e, 0xdd27b971, 0xb6bee14f, 0x17f088ad, 0x66c920ac, 0xb47dce3a, + 0x1863df4a, 0x82e51a31, 0x60975133, 0x4562537f, 0xe0b16477, 0x84bb6bae, 0x1cfe81a0, 0x94f9082b, + 0x58704868, 0x198f45fd, 0x8794de6c, 0xb7527bf8, 0x23ab73d3, 0xe2724b02, 0x57e31f8f, 0x2a6655ab, + 0x07b2eb28, 0x032fb5c2, 0x9a86c57b, 0xa5d33708, 0xf2302887, 0xb223bfa5, 0xba02036a, 0x5ced1682, + 0x2b8acf1c, 0x92a779b4, 0xf0f307f2, 0xa14e69e2, 0xcd65daf4, 0xd50605be, 0x1fd13462, 0x8ac4a6fe, + 0x9d342e53, 0xa0a2f355, 0x32058ae1, 0x75a4f6eb, 0x390b83ec, 0xaa4060ef, 0x065e719f, 0x51bd6e10, + 0xf93e218a, 0x3d96dd06, 0xaedd3e05, 0x464de6bd, 0xb591548d, 0x0571c45d, 0x6f0406d4, 0xff605015, + 0x241998fb, 0x97d6bde9, 0xcc894043, 0x7767d99e, 0xbdb0e842, 0x8807898b, 0x38e7195b, 0xdb79c8ee, + 0x47a17c0a, 0xe97c420f, 0xc9f8841e, 0x00000000, 0x83098086, 0x48322bed, 0xac1e1170, 0x4e6c5a72, + 0xfbfd0eff, 0x560f8538, 0x1e3daed5, 0x27362d39, 0x640a0fd9, 0x21685ca6, 0xd19b5b54, 0x3a24362e, + 0xb10c0a67, 0x0f9357e7, 0xd2b4ee96, 0x9e1b9b91, 0x4f80c0c5, 0xa261dc20, 0x695a774b, 0x161c121a, + 0x0ae293ba, 0xe5c0a02a, 0x433c22e0, 0x1d121b17, 0x0b0e090d, 0xadf28bc7, 0xb92db6a8, 0xc8141ea9, + 0x8557f119, 0x4caf7507, 0xbbee99dd, 0xfda37f60, 0x9ff70126, 0xbc5c72f5, 0xc544663b, 0x345bfb7e, + 0x768b4329, 0xdccb23c6, 0x68b6edfc, 0x63b8e4f1, 0xcad731dc, 0x10426385, 0x40139722, 0x2084c611, + 0x7d854a24, 0xf8d2bb3d, 0x11aef932, 0x6dc729a1, 0x4b1d9e2f, 0xf3dcb230, 0xec0d8652, 0xd077c1e3, + 0x6c2bb316, 0x99a970b9, 0xfa119448, 0x2247e964, 0xc4a8fc8c, 0x1aa0f03f, 0xd8567d2c, 0xef223390, + 0xc787494e, 0xc1d938d1, 0xfe8ccaa2, 0x3698d40b, 0xcfa6f581, 0x28a57ade, 0x26dab78e, 0xa43fadbf, + 0xe42c3a9d, 0x0d507892, 0x9b6a5fcc, 0x62547e46, 0xc2f68d13, 0xe890d8b8, 0x5e2e39f7, 0xf582c3af, + 0xbe9f5d80, 0x7c69d093, 0xa96fd52d, 0xb3cf2512, 0x3bc8ac99, 0xa710187d, 0x6ee89c63, 0x7bdb3bbb, + 0x09cd2678, 0xf46e5918, 0x01ec9ab7, 0xa8834f9a, 0x65e6956e, 0x7eaaffe6, 0x0821bccf, 0xe6ef15e8, + 0xd9bae79b, 0xce4a6f36, 0xd4ea9f09, 0xd629b07c, 0xaf31a4b2, 0x312a3f23, 0x30c6a594, 0xc035a266, + 0x37744ebc, 0xa6fc82ca, 0xb0e090d0, 0x1533a7d8, 0x4af10498, 0xf741ecda, 0x0e7fcd50, 0x2f1791f6, + 0x8d764dd6, 0x4d43efb0, 0x54ccaa4d, 0xdfe49604, 0xe39ed1b5, 0x1b4c6a88, 0xb8c12c1f, 0x7f466551, + 0x049d5eea, 0x5d018c35, 0x73fa8774, 0x2efb0b41, 0x5ab3671d, 0x5292dbd2, 0x33e91056, 0x136dd647, + 0x8c9ad761, 0x7a37a10c, 0x8e59f814, 0x89eb133c, 0xeecea927, 0x35b761c9, 0xede11ce5, 0x3c7a47b1, + 0x599cd2df, 0x3f55f273, 0x791814ce, 0xbf73c737, 0xea53f7cd, 0x5b5ffdaa, 0x14df3d6f, 0x867844db, + 0x81caaff3, 0x3eb968c4, 0x2c382434, 0x5fc2a340, 0x72161dc3, 0x0cbce225, 0x8b283c49, 0x41ff0d95, + 0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857, +} +var td2 = [256]uint32{ + 0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3, + 0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562, + 0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe, 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3, + 0x5fe7038f, 0x9c951592, 0x7aebbf6d, 0x59da9552, 0x832dd4be, 0x21d35874, 0x692949e0, 0xc8448ec9, + 0x896a75c2, 0x7978f48e, 0x3e6b9958, 0x71dd27b9, 0x4fb6bee1, 0xad17f088, 0xac66c920, 0x3ab47dce, + 0x4a1863df, 0x3182e51a, 0x33609751, 0x7f456253, 0x77e0b164, 0xae84bb6b, 0xa01cfe81, 0x2b94f908, + 0x68587048, 0xfd198f45, 0x6c8794de, 0xf8b7527b, 0xd323ab73, 0x02e2724b, 0x8f57e31f, 0xab2a6655, + 0x2807b2eb, 0xc2032fb5, 0x7b9a86c5, 0x08a5d337, 0x87f23028, 0xa5b223bf, 0x6aba0203, 0x825ced16, + 0x1c2b8acf, 0xb492a779, 0xf2f0f307, 0xe2a14e69, 0xf4cd65da, 0xbed50605, 0x621fd134, 0xfe8ac4a6, + 0x539d342e, 0x55a0a2f3, 0xe132058a, 0xeb75a4f6, 0xec390b83, 0xefaa4060, 0x9f065e71, 0x1051bd6e, + 0x8af93e21, 0x063d96dd, 0x05aedd3e, 0xbd464de6, 0x8db59154, 0x5d0571c4, 0xd46f0406, 0x15ff6050, + 0xfb241998, 0xe997d6bd, 0x43cc8940, 0x9e7767d9, 0x42bdb0e8, 0x8b880789, 0x5b38e719, 0xeedb79c8, + 0x0a47a17c, 0x0fe97c42, 0x1ec9f884, 0x00000000, 0x86830980, 0xed48322b, 0x70ac1e11, 0x724e6c5a, + 0xfffbfd0e, 0x38560f85, 0xd51e3dae, 0x3927362d, 0xd9640a0f, 0xa621685c, 0x54d19b5b, 0x2e3a2436, + 0x67b10c0a, 0xe70f9357, 0x96d2b4ee, 0x919e1b9b, 0xc54f80c0, 0x20a261dc, 0x4b695a77, 0x1a161c12, + 0xba0ae293, 0x2ae5c0a0, 0xe0433c22, 0x171d121b, 0x0d0b0e09, 0xc7adf28b, 0xa8b92db6, 0xa9c8141e, + 0x198557f1, 0x074caf75, 0xddbbee99, 0x60fda37f, 0x269ff701, 0xf5bc5c72, 0x3bc54466, 0x7e345bfb, + 0x29768b43, 0xc6dccb23, 0xfc68b6ed, 0xf163b8e4, 0xdccad731, 0x85104263, 0x22401397, 0x112084c6, + 0x247d854a, 0x3df8d2bb, 0x3211aef9, 0xa16dc729, 0x2f4b1d9e, 0x30f3dcb2, 0x52ec0d86, 0xe3d077c1, + 0x166c2bb3, 0xb999a970, 0x48fa1194, 0x642247e9, 0x8cc4a8fc, 0x3f1aa0f0, 0x2cd8567d, 0x90ef2233, + 0x4ec78749, 0xd1c1d938, 0xa2fe8cca, 0x0b3698d4, 0x81cfa6f5, 0xde28a57a, 0x8e26dab7, 0xbfa43fad, + 0x9de42c3a, 0x920d5078, 0xcc9b6a5f, 0x4662547e, 0x13c2f68d, 0xb8e890d8, 0xf75e2e39, 0xaff582c3, + 0x80be9f5d, 0x937c69d0, 0x2da96fd5, 0x12b3cf25, 0x993bc8ac, 0x7da71018, 0x636ee89c, 0xbb7bdb3b, + 0x7809cd26, 0x18f46e59, 0xb701ec9a, 0x9aa8834f, 0x6e65e695, 0xe67eaaff, 0xcf0821bc, 0xe8e6ef15, + 0x9bd9bae7, 0x36ce4a6f, 0x09d4ea9f, 0x7cd629b0, 0xb2af31a4, 0x23312a3f, 0x9430c6a5, 0x66c035a2, + 0xbc37744e, 0xcaa6fc82, 0xd0b0e090, 0xd81533a7, 0x984af104, 0xdaf741ec, 0x500e7fcd, 0xf62f1791, + 0xd68d764d, 0xb04d43ef, 0x4d54ccaa, 0x04dfe496, 0xb5e39ed1, 0x881b4c6a, 0x1fb8c12c, 0x517f4665, + 0xea049d5e, 0x355d018c, 0x7473fa87, 0x412efb0b, 0x1d5ab367, 0xd25292db, 0x5633e910, 0x47136dd6, + 0x618c9ad7, 0x0c7a37a1, 0x148e59f8, 0x3c89eb13, 0x27eecea9, 0xc935b761, 0xe5ede11c, 0xb13c7a47, + 0xdf599cd2, 0x733f55f2, 0xce791814, 0x37bf73c7, 0xcdea53f7, 0xaa5b5ffd, 0x6f14df3d, 0xdb867844, + 0xf381caaf, 0xc43eb968, 0x342c3824, 0x405fc2a3, 0xc372161d, 0x250cbce2, 0x498b283c, 0x9541ff0d, + 0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8, +} +var td3 = [256]uint32{ + 0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b, + 0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5, + 0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d, 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b, + 0x8f5fe703, 0x929c9515, 0x6d7aebbf, 0x5259da95, 0xbe832dd4, 0x7421d358, 0xe0692949, 0xc9c8448e, + 0xc2896a75, 0x8e7978f4, 0x583e6b99, 0xb971dd27, 0xe14fb6be, 0x88ad17f0, 0x20ac66c9, 0xce3ab47d, + 0xdf4a1863, 0x1a3182e5, 0x51336097, 0x537f4562, 0x6477e0b1, 0x6bae84bb, 0x81a01cfe, 0x082b94f9, + 0x48685870, 0x45fd198f, 0xde6c8794, 0x7bf8b752, 0x73d323ab, 0x4b02e272, 0x1f8f57e3, 0x55ab2a66, + 0xeb2807b2, 0xb5c2032f, 0xc57b9a86, 0x3708a5d3, 0x2887f230, 0xbfa5b223, 0x036aba02, 0x16825ced, + 0xcf1c2b8a, 0x79b492a7, 0x07f2f0f3, 0x69e2a14e, 0xdaf4cd65, 0x05bed506, 0x34621fd1, 0xa6fe8ac4, + 0x2e539d34, 0xf355a0a2, 0x8ae13205, 0xf6eb75a4, 0x83ec390b, 0x60efaa40, 0x719f065e, 0x6e1051bd, + 0x218af93e, 0xdd063d96, 0x3e05aedd, 0xe6bd464d, 0x548db591, 0xc45d0571, 0x06d46f04, 0x5015ff60, + 0x98fb2419, 0xbde997d6, 0x4043cc89, 0xd99e7767, 0xe842bdb0, 0x898b8807, 0x195b38e7, 0xc8eedb79, + 0x7c0a47a1, 0x420fe97c, 0x841ec9f8, 0x00000000, 0x80868309, 0x2bed4832, 0x1170ac1e, 0x5a724e6c, + 0x0efffbfd, 0x8538560f, 0xaed51e3d, 0x2d392736, 0x0fd9640a, 0x5ca62168, 0x5b54d19b, 0x362e3a24, + 0x0a67b10c, 0x57e70f93, 0xee96d2b4, 0x9b919e1b, 0xc0c54f80, 0xdc20a261, 0x774b695a, 0x121a161c, + 0x93ba0ae2, 0xa02ae5c0, 0x22e0433c, 0x1b171d12, 0x090d0b0e, 0x8bc7adf2, 0xb6a8b92d, 0x1ea9c814, + 0xf1198557, 0x75074caf, 0x99ddbbee, 0x7f60fda3, 0x01269ff7, 0x72f5bc5c, 0x663bc544, 0xfb7e345b, + 0x4329768b, 0x23c6dccb, 0xedfc68b6, 0xe4f163b8, 0x31dccad7, 0x63851042, 0x97224013, 0xc6112084, + 0x4a247d85, 0xbb3df8d2, 0xf93211ae, 0x29a16dc7, 0x9e2f4b1d, 0xb230f3dc, 0x8652ec0d, 0xc1e3d077, + 0xb3166c2b, 0x70b999a9, 0x9448fa11, 0xe9642247, 0xfc8cc4a8, 0xf03f1aa0, 0x7d2cd856, 0x3390ef22, + 0x494ec787, 0x38d1c1d9, 0xcaa2fe8c, 0xd40b3698, 0xf581cfa6, 0x7ade28a5, 0xb78e26da, 0xadbfa43f, + 0x3a9de42c, 0x78920d50, 0x5fcc9b6a, 0x7e466254, 0x8d13c2f6, 0xd8b8e890, 0x39f75e2e, 0xc3aff582, + 0x5d80be9f, 0xd0937c69, 0xd52da96f, 0x2512b3cf, 0xac993bc8, 0x187da710, 0x9c636ee8, 0x3bbb7bdb, + 0x267809cd, 0x5918f46e, 0x9ab701ec, 0x4f9aa883, 0x956e65e6, 0xffe67eaa, 0xbccf0821, 0x15e8e6ef, + 0xe79bd9ba, 0x6f36ce4a, 0x9f09d4ea, 0xb07cd629, 0xa4b2af31, 0x3f23312a, 0xa59430c6, 0xa266c035, + 0x4ebc3774, 0x82caa6fc, 0x90d0b0e0, 0xa7d81533, 0x04984af1, 0xecdaf741, 0xcd500e7f, 0x91f62f17, + 0x4dd68d76, 0xefb04d43, 0xaa4d54cc, 0x9604dfe4, 0xd1b5e39e, 0x6a881b4c, 0x2c1fb8c1, 0x65517f46, + 0x5eea049d, 0x8c355d01, 0x877473fa, 0x0b412efb, 0x671d5ab3, 0xdbd25292, 0x105633e9, 0xd647136d, + 0xd7618c9a, 0xa10c7a37, 0xf8148e59, 0x133c89eb, 0xa927eece, 0x61c935b7, 0x1ce5ede1, 0x47b13c7a, + 0xd2df599c, 0xf2733f55, 0x14ce7918, 0xc737bf73, 0xf7cdea53, 0xfdaa5b5f, 0x3d6f14df, 0x44db8678, + 0xaff381ca, 0x68c43eb9, 0x24342c38, 0xa3405fc2, 0x1dc37216, 0xe2250cbc, 0x3c498b28, 0x0d9541ff, + 0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0, } diff --git a/libgo/go/crypto/crypto.go b/libgo/go/crypto/crypto.go index 53672a4da3c..c913494f61b 100644 --- a/libgo/go/crypto/crypto.go +++ b/libgo/go/crypto/crypto.go @@ -71,3 +71,6 @@ func RegisterHash(h Hash, f func() hash.Hash) { } hashes[h] = f } + +// PrivateKey represents a private key using an unspecified algorithm. +type PrivateKey interface{} diff --git a/libgo/go/crypto/dsa/dsa.go b/libgo/go/crypto/dsa/dsa.go index a2adc7eb5c3..be478468455 100644 --- a/libgo/go/crypto/dsa/dsa.go +++ b/libgo/go/crypto/dsa/dsa.go @@ -185,6 +185,10 @@ func GenerateKey(priv *PrivateKey, rand io.Reader) error { // larger message) using the private key, priv. It returns the signature as a // pair of integers. The security of the private key depends on the entropy of // rand. +// +// Note that FIPS 186-3 section 4.6 specifies that the hash should be truncated +// to the byte-length of the subgroup. This function does not perform that +// truncation itself. func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) { // FIPS 186-3, section 4.6 @@ -218,10 +222,7 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err continue } - if n > len(hash) { - n = len(hash) - } - z := k.SetBytes(hash[:n]) + z := k.SetBytes(hash) s = new(big.Int).Mul(priv.X, r) s.Add(s, z) @@ -238,7 +239,11 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err } // Verify verifies the signature in r, s of hash using the public key, pub. It -// returns true iff the signature is valid. +// reports whether the signature is valid. +// +// Note that FIPS 186-3 section 4.6 specifies that the hash should be truncated +// to the byte-length of the subgroup. This function does not perform that +// truncation itself. func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool { // FIPS 186-3, section 4.7 @@ -255,12 +260,7 @@ func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool { if n&7 != 0 { return false } - n >>= 3 - - if n > len(hash) { - n = len(hash) - } - z := new(big.Int).SetBytes(hash[:n]) + z := new(big.Int).SetBytes(hash) u1 := new(big.Int).Mul(z, w) u1.Mod(u1, pub.Q) diff --git a/libgo/go/crypto/ocsp/ocsp_test.go b/libgo/go/crypto/ocsp/ocsp_test.go index bacca558b48..49d8ea29b93 100644 --- a/libgo/go/crypto/ocsp/ocsp_test.go +++ b/libgo/go/crypto/ocsp/ocsp_test.go @@ -23,11 +23,11 @@ func TestOCSPDecode(t *testing.T) { NextUpdate: time.Date(2010, 7, 7, 18, 35, 17, 0, time.UTC), } - if !reflect.DeepEqual(resp.ThisUpdate, resp.ThisUpdate) { + if !reflect.DeepEqual(resp.ThisUpdate, expected.ThisUpdate) { t.Errorf("resp.ThisUpdate: got %d, want %d", resp.ThisUpdate, expected.ThisUpdate) } - if !reflect.DeepEqual(resp.NextUpdate, resp.NextUpdate) { + if !reflect.DeepEqual(resp.NextUpdate, expected.NextUpdate) { t.Errorf("resp.NextUpdate: got %d, want %d", resp.NextUpdate, expected.NextUpdate) } diff --git a/libgo/go/crypto/openpgp/keys.go b/libgo/go/crypto/openpgp/keys.go index df39970c0b6..74e7d239e08 100644 --- a/libgo/go/crypto/openpgp/keys.go +++ b/libgo/go/crypto/openpgp/keys.go @@ -16,6 +16,7 @@ import ( // PublicKeyType is the armor type for a PGP public key. var PublicKeyType = "PGP PUBLIC KEY BLOCK" + // PrivateKeyType is the armor type for a PGP private key. var PrivateKeyType = "PGP PRIVATE KEY BLOCK" diff --git a/libgo/go/crypto/rand/rand_unix.go b/libgo/go/crypto/rand/rand_unix.go index d9cddf6d2ad..5d4fc8198ac 100644 --- a/libgo/go/crypto/rand/rand_unix.go +++ b/libgo/go/crypto/rand/rand_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd // Unix cryptographically secure pseudorandom number // generator. diff --git a/libgo/go/crypto/tls/common.go b/libgo/go/crypto/tls/common.go index f57d932a98f..a461ad951b0 100644 --- a/libgo/go/crypto/tls/common.go +++ b/libgo/go/crypto/tls/common.go @@ -5,8 +5,8 @@ package tls import ( + "crypto" "crypto/rand" - "crypto/rsa" "crypto/x509" "io" "strings" @@ -255,7 +255,7 @@ func (c *Config) BuildNameToCertificate() { // A Certificate is a chain of one or more certificates, leaf first. type Certificate struct { Certificate [][]byte - PrivateKey *rsa.PrivateKey + PrivateKey crypto.PrivateKey // supported types: *rsa.PrivateKey // OCSPStaple contains an optional OCSP response which will be served // to clients that request it. OCSPStaple []byte diff --git a/libgo/go/crypto/tls/handshake_client.go b/libgo/go/crypto/tls/handshake_client.go index e39e59cd5a1..73648002bd5 100644 --- a/libgo/go/crypto/tls/handshake_client.go +++ b/libgo/go/crypto/tls/handshake_client.go @@ -234,7 +234,7 @@ func (c *Conn) clientHandshake() error { digest := make([]byte, 0, 36) digest = finishedHash.serverMD5.Sum(digest) digest = finishedHash.serverSHA1.Sum(digest) - signed, err := rsa.SignPKCS1v15(c.config.rand(), c.config.Certificates[0].PrivateKey, crypto.MD5SHA1, digest) + signed, err := rsa.SignPKCS1v15(c.config.rand(), c.config.Certificates[0].PrivateKey.(*rsa.PrivateKey), crypto.MD5SHA1, digest) if err != nil { return c.sendAlert(alertInternalError) } diff --git a/libgo/go/crypto/tls/key_agreement.go b/libgo/go/crypto/tls/key_agreement.go index b531717d840..c3c16647853 100644 --- a/libgo/go/crypto/tls/key_agreement.go +++ b/libgo/go/crypto/tls/key_agreement.go @@ -44,7 +44,7 @@ func (ka rsaKeyAgreement) processClientKeyExchange(config *Config, ckx *clientKe ciphertext = ckx.ciphertext[2:] } - err = rsa.DecryptPKCS1v15SessionKey(config.rand(), config.Certificates[0].PrivateKey, ciphertext, preMasterSecret) + err = rsa.DecryptPKCS1v15SessionKey(config.rand(), config.Certificates[0].PrivateKey.(*rsa.PrivateKey), ciphertext, preMasterSecret) if err != nil { return nil, err } @@ -147,7 +147,7 @@ Curve: copy(serverECDHParams[4:], ecdhePublic) md5sha1 := md5SHA1Hash(clientHello.random, hello.random, serverECDHParams) - sig, err := rsa.SignPKCS1v15(config.rand(), config.Certificates[0].PrivateKey, crypto.MD5SHA1, md5sha1) + sig, err := rsa.SignPKCS1v15(config.rand(), config.Certificates[0].PrivateKey.(*rsa.PrivateKey), crypto.MD5SHA1, md5sha1) if err != nil { return nil, errors.New("failed to sign ECDHE parameters: " + err.Error()) } diff --git a/libgo/go/crypto/tls/root_darwin.go b/libgo/go/crypto/tls/root_darwin.go index 15122416bd1..db1b18b3c07 100644 --- a/libgo/go/crypto/tls/root_darwin.go +++ b/libgo/go/crypto/tls/root_darwin.go @@ -8,7 +8,7 @@ package tls // Note: We disable -Werror here because the code in this file uses a deprecated API to stay // compatible with both Mac OS X 10.6 and 10.7. Using a deprecated function on Darwin generates // a warning. -#cgo CFLAGS: -Wno-error +#cgo CFLAGS: -Wno-error -Wno-deprecated-declarations #cgo LDFLAGS: -framework CoreFoundation -framework Security #include <CoreFoundation/CoreFoundation.h> #include <Security/Security.h> diff --git a/libgo/go/crypto/tls/root_stub.go b/libgo/go/crypto/tls/root_stub.go index 1903eed8138..d00493a5736 100644 --- a/libgo/go/crypto/tls/root_stub.go +++ b/libgo/go/crypto/tls/root_stub.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build plan9 darwin/nocgo + package tls func initDefaultRoots() { diff --git a/libgo/go/crypto/tls/root_unix.go b/libgo/go/crypto/tls/root_unix.go index 1b9aeb03b5b..5bbd9825dca 100644 --- a/libgo/go/crypto/tls/root_unix.go +++ b/libgo/go/crypto/tls/root_unix.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build freebsd linux openbsd netbsd + package tls import ( diff --git a/libgo/go/crypto/x509/cert_pool.go b/libgo/go/crypto/x509/cert_pool.go index adc7f9bc6d7..5a0a87678e3 100644 --- a/libgo/go/crypto/x509/cert_pool.go +++ b/libgo/go/crypto/x509/cert_pool.go @@ -28,6 +28,9 @@ func NewCertPool() *CertPool { // given certificate. If no such certificate can be found or the signature // doesn't match, it returns nil. func (s *CertPool) findVerifiedParents(cert *Certificate) (parents []int) { + if s == nil { + return + } var candidates []int if len(cert.AuthorityKeyId) > 0 { diff --git a/libgo/go/crypto/x509/verify_test.go b/libgo/go/crypto/x509/verify_test.go index df5443023ff..2016858307e 100644 --- a/libgo/go/crypto/x509/verify_test.go +++ b/libgo/go/crypto/x509/verify_test.go @@ -19,6 +19,7 @@ type verifyTest struct { roots []string currentTime int64 dnsName string + nilRoots bool errorCallback func(*testing.T, int, error) bool expectedChains [][]string @@ -48,6 +49,14 @@ var verifyTests = []verifyTest{ { leaf: googleLeaf, intermediates: []string{thawteIntermediate}, + nilRoots: true, // verifies that we don't crash + currentTime: 1302726541, + dnsName: "www.google.com", + errorCallback: expectAuthorityUnknown, + }, + { + leaf: googleLeaf, + intermediates: []string{thawteIntermediate}, roots: []string{verisignRoot}, currentTime: 1, dnsName: "www.example.com", @@ -136,6 +145,9 @@ func TestVerify(t *testing.T) { DNSName: test.dnsName, CurrentTime: time.Unix(test.currentTime, 0), } + if test.nilRoots { + opts.Roots = nil + } for j, root := range test.roots { ok := opts.Roots.AppendCertsFromPEM([]byte(root)) diff --git a/libgo/go/crypto/x509/x509.go b/libgo/go/crypto/x509/x509.go index 65ca3158003..28c7880e531 100644 --- a/libgo/go/crypto/x509/x509.go +++ b/libgo/go/crypto/x509/x509.go @@ -981,6 +981,7 @@ func CreateCertificate(rand io.Reader, template, parent *Certificate, pub *rsa.P // pemCRLPrefix is the magic string that indicates that we have a PEM encoded // CRL. var pemCRLPrefix = []byte("-----BEGIN X509 CRL") + // pemType is the type of a PEM encoded CRL. var pemType = "X509 CRL" diff --git a/libgo/go/debug/gosym/pclntab_test.go b/libgo/go/debug/gosym/pclntab_test.go index 562e7a0a37f..e5c29889b7f 100644 --- a/libgo/go/debug/gosym/pclntab_test.go +++ b/libgo/go/debug/gosym/pclntab_test.go @@ -13,7 +13,8 @@ import ( func dotest() bool { // For now, only works on ELF platforms. - return syscall.OS == "linux" && os.Getenv("GOARCH") == "amd64" + // TODO: convert to work with new go tool + return false && syscall.OS == "linux" && os.Getenv("GOARCH") == "amd64" } func getTable(t *testing.T) *Table { diff --git a/libgo/go/encoding/binary/binary_test.go b/libgo/go/encoding/binary/binary_test.go index fd4fdb01574..3e7057ea227 100644 --- a/libgo/go/encoding/binary/binary_test.go +++ b/libgo/go/encoding/binary/binary_test.go @@ -171,11 +171,42 @@ func (br *byteSliceReader) Read(p []byte) (int, error) { return n, nil } -func BenchmarkRead(b *testing.B) { +func BenchmarkReadSlice1000Int32s(b *testing.B) { + bsr := &byteSliceReader{} + slice := make([]int32, 1000) + buf := make([]byte, len(slice)*4) + b.SetBytes(int64(len(buf))) + b.ResetTimer() + for i := 0; i < b.N; i++ { + bsr.remain = buf + Read(bsr, BigEndian, slice) + } +} + +func BenchmarkReadStruct(b *testing.B) { + bsr := &byteSliceReader{} + var buf bytes.Buffer + Write(&buf, BigEndian, &s) + n := TotalSize(reflect.ValueOf(s)) + b.SetBytes(int64(n)) + t := s + b.ResetTimer() + for i := 0; i < b.N; i++ { + bsr.remain = buf.Bytes() + Read(bsr, BigEndian, &t) + } + b.StopTimer() + if !reflect.DeepEqual(s, t) { + b.Fatal("no match") + } +} + +func BenchmarkReadInts(b *testing.B) { var ls Struct bsr := &byteSliceReader{} var r io.Reader = bsr - + b.SetBytes(2 * (1 + 2 + 4 + 8)) + b.ResetTimer() for i := 0; i < b.N; i++ { bsr.remain = big Read(r, BigEndian, &ls.Int8) @@ -196,25 +227,19 @@ func BenchmarkRead(b *testing.B) { for i := range want.Array { want.Array[i] = 0 } + b.StopTimer() if !reflect.DeepEqual(ls, want) { panic("no match") } } -func BenchmarkWrite(b *testing.B) { +func BenchmarkWriteInts(b *testing.B) { buf := new(bytes.Buffer) var w io.Writer = buf - + b.SetBytes(2 * (1 + 2 + 4 + 8)) + b.ResetTimer() for i := 0; i < b.N; i++ { buf.Reset() - Write(w, BigEndian, &s.Int8) - Write(w, BigEndian, &s.Int16) - Write(w, BigEndian, &s.Int32) - Write(w, BigEndian, &s.Int64) - Write(w, BigEndian, &s.Uint8) - Write(w, BigEndian, &s.Uint16) - Write(w, BigEndian, &s.Uint32) - Write(w, BigEndian, &s.Uint64) Write(w, BigEndian, s.Int8) Write(w, BigEndian, s.Int16) Write(w, BigEndian, s.Int32) @@ -224,11 +249,8 @@ func BenchmarkWrite(b *testing.B) { Write(w, BigEndian, s.Uint32) Write(w, BigEndian, s.Uint64) } - - if !bytes.Equal(buf.Bytes()[:30], big[:30]) { - panic("first half doesn't match") - } - if !bytes.Equal(buf.Bytes()[30:], big[:30]) { - panic("second half doesn't match") + b.StopTimer() + if !bytes.Equal(buf.Bytes(), big[:30]) { + b.Fatalf("first half doesn't match: %x %x", buf.Bytes(), big[:30]) } } diff --git a/libgo/go/encoding/binary/varint_test.go b/libgo/go/encoding/binary/varint_test.go index b553d6d4eb0..dc550f22f44 100644 --- a/libgo/go/encoding/binary/varint_test.go +++ b/libgo/go/encoding/binary/varint_test.go @@ -165,6 +165,7 @@ func TestNonCanonicalZero(t *testing.T) { func BenchmarkPutUvarint32(b *testing.B) { buf := make([]byte, MaxVarintLen32) + b.SetBytes(4) for i := 0; i < b.N; i++ { for j := uint(0); j < MaxVarintLen32; j++ { PutUvarint(buf, 1<<(j*7)) @@ -174,6 +175,7 @@ func BenchmarkPutUvarint32(b *testing.B) { func BenchmarkPutUvarint64(b *testing.B) { buf := make([]byte, MaxVarintLen64) + b.SetBytes(8) for i := 0; i < b.N; i++ { for j := uint(0); j < MaxVarintLen64; j++ { PutUvarint(buf, 1<<(j*7)) diff --git a/libgo/go/encoding/gob/codec_test.go b/libgo/go/encoding/gob/codec_test.go index dc0e0078e68..73844b920c1 100644 --- a/libgo/go/encoding/gob/codec_test.go +++ b/libgo/go/encoding/gob/codec_test.go @@ -102,12 +102,15 @@ func TestIntCodec(t *testing.T) { // The result of encoding a true boolean with field number 7 var boolResult = []byte{0x07, 0x01} + // The result of encoding a number 17 with field number 7 var signedResult = []byte{0x07, 2 * 17} var unsignedResult = []byte{0x07, 17} var floatResult = []byte{0x07, 0xFE, 0x31, 0x40} + // The result of encoding a number 17+19i with field number 7 var complexResult = []byte{0x07, 0xFE, 0x31, 0x40, 0xFE, 0x33, 0x40} + // The result of encoding "hello" with field number 7 var bytesResult = []byte{0x07, 0x05, 'h', 'e', 'l', 'l', 'o'} diff --git a/libgo/go/encoding/gob/decode.go b/libgo/go/encoding/gob/decode.go index 1515d1286d0..ba1f2eb8130 100644 --- a/libgo/go/encoding/gob/decode.go +++ b/libgo/go/encoding/gob/decode.go @@ -1068,7 +1068,12 @@ func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *de engine.instr = make([]decInstr, 1) // one item name := rt.String() // best we can do if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) { - return nil, errors.New("gob: wrong type received for local value " + name + ": " + dec.typeString(remoteId)) + remoteType := dec.typeString(remoteId) + // Common confusing case: local interface type, remote concrete type. + if ut.base.Kind() == reflect.Interface && remoteId != tInterface { + return nil, errors.New("gob: local interface type " + name + " can only be decoded from remote interface type; received concrete type " + remoteType) + } + return nil, errors.New("gob: decoding into local type " + name + ", received remote type " + remoteType) } op, indir := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp)) ovfl := errors.New(`value for "` + name + `" out of range`) diff --git a/libgo/go/encoding/gob/encode.go b/libgo/go/encoding/gob/encode.go index c7e48230c53..f05b17c3096 100644 --- a/libgo/go/encoding/gob/encode.go +++ b/libgo/go/encoding/gob/encode.go @@ -469,7 +469,14 @@ func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv reflect.Value) { // isZero returns whether the value is the zero of its type. func isZero(val reflect.Value) bool { switch val.Kind() { - case reflect.Array, reflect.Map, reflect.Slice, reflect.String: + case reflect.Array: + for i := 0; i < val.Len(); i++ { + if !isZero(val.Index(i)) { + return false + } + } + return true + case reflect.Map, reflect.Slice, reflect.String: return val.Len() == 0 case reflect.Bool: return !val.Bool() @@ -483,6 +490,13 @@ func isZero(val reflect.Value) bool { return val.Float() == 0 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return val.Uint() == 0 + case reflect.Struct: + for i := 0; i < val.NumField(); i++ { + if !isZero(val.Field(i)) { + return false + } + } + return true } panic("unknown type in isZero " + val.Type().String()) } diff --git a/libgo/go/encoding/gob/encoder_test.go b/libgo/go/encoding/gob/encoder_test.go index 5bc957bb370..cd1500d0772 100644 --- a/libgo/go/encoding/gob/encoder_test.go +++ b/libgo/go/encoding/gob/encoder_test.go @@ -309,7 +309,7 @@ var singleTests = []SingleTest{ {[7]int{4, 55, 1, 44, 22, 66, 1234}, &testArray, ""}, // Decode errors - {172, &testFloat32, "wrong type"}, + {172, &testFloat32, "type"}, } func TestSingletons(t *testing.T) { diff --git a/libgo/go/encoding/gob/gobencdec_test.go b/libgo/go/encoding/gob/gobencdec_test.go index eacfd842db3..b8dfeeb5156 100644 --- a/libgo/go/encoding/gob/gobencdec_test.go +++ b/libgo/go/encoding/gob/gobencdec_test.go @@ -13,6 +13,7 @@ import ( "io" "strings" "testing" + "time" ) // Types that implement the GobEncoder/Decoder interfaces. @@ -526,3 +527,50 @@ func TestGobEncoderExtraIndirect(t *testing.T) { t.Errorf("got = %q, want %q", got, gdb) } } + +// Another bug: this caused a crash with the new Go1 Time type. +// We throw in a gob-encoding array, to test another case of isZero + +type isZeroBug struct { + T time.Time + S string + I int + A isZeroBugArray +} + +type isZeroBugArray [2]uint8 + +// Receiver is value, not pointer, to test isZero of array. +func (a isZeroBugArray) GobEncode() (b []byte, e error) { + b = append(b, a[:]...) + return b, nil +} + +func (a *isZeroBugArray) GobDecode(data []byte) error { + println("DECODE") + if len(data) != len(a) { + return io.EOF + } + a[0] = data[0] + a[1] = data[1] + return nil +} + +func TestGobEncodeIsZero(t *testing.T) { + x := isZeroBug{time.Now(), "hello", -55, isZeroBugArray{1, 2}} + b := new(bytes.Buffer) + enc := NewEncoder(b) + err := enc.Encode(x) + if err != nil { + t.Fatal("encode:", err) + } + var y isZeroBug + dec := NewDecoder(b) + err = dec.Decode(&y) + if err != nil { + t.Fatal("decode:", err) + } + if x != y { + t.Fatalf("%v != %v", x, y) + } +} diff --git a/libgo/go/encoding/gob/timing_test.go b/libgo/go/encoding/gob/timing_test.go index 47437a607f1..1017eb7f51d 100644 --- a/libgo/go/encoding/gob/timing_test.go +++ b/libgo/go/encoding/gob/timing_test.go @@ -39,7 +39,7 @@ func benchmarkEndToEnd(r io.Reader, w io.Writer, b *testing.B) { func BenchmarkEndToEndPipe(b *testing.B) { r, w, err := os.Pipe() if err != nil { - panic("can't get pipe:" + err.Error()) + b.Fatal("can't get pipe:", err) } benchmarkEndToEnd(r, w, b) } diff --git a/libgo/go/encoding/gob/type.go b/libgo/go/encoding/gob/type.go index 1b20843fa25..71a28be7cab 100644 --- a/libgo/go/encoding/gob/type.go +++ b/libgo/go/encoding/gob/type.go @@ -130,6 +130,7 @@ func userType(rt reflect.Type) *userTypeInfo { } return ut } + // A typeId represents a gob Type as an integer that can be passed on the wire. // Internally, typeIds are used as keys to a map to recover the underlying type info. type typeId int32 diff --git a/libgo/go/encoding/json/bench_test.go b/libgo/go/encoding/json/bench_test.go index f0c52011a1d..333c1c0ce9e 100644 --- a/libgo/go/encoding/json/bench_test.go +++ b/libgo/go/encoding/json/bench_test.go @@ -84,7 +84,7 @@ func BenchmarkCodeEncoder(b *testing.B) { enc := NewEncoder(ioutil.Discard) for i := 0; i < b.N; i++ { if err := enc.Encode(&codeStruct); err != nil { - panic(err) + b.Fatal("Encode:", err) } } b.SetBytes(int64(len(codeJSON))) @@ -98,7 +98,7 @@ func BenchmarkCodeMarshal(b *testing.B) { } for i := 0; i < b.N; i++ { if _, err := Marshal(&codeStruct); err != nil { - panic(err) + b.Fatal("Marshal:", err) } } b.SetBytes(int64(len(codeJSON))) @@ -120,7 +120,7 @@ func BenchmarkCodeDecoder(b *testing.B) { buf.WriteByte('\n') buf.WriteByte('\n') if err := dec.Decode(&r); err != nil { - panic(err) + b.Fatal("Decode:", err) } } b.SetBytes(int64(len(codeJSON))) @@ -135,7 +135,7 @@ func BenchmarkCodeUnmarshal(b *testing.B) { for i := 0; i < b.N; i++ { var r codeResponse if err := Unmarshal(codeJSON, &r); err != nil { - panic(err) + b.Fatal("Unmmarshal:", err) } } b.SetBytes(int64(len(codeJSON))) @@ -150,7 +150,7 @@ func BenchmarkCodeUnmarshalReuse(b *testing.B) { var r codeResponse for i := 0; i < b.N; i++ { if err := Unmarshal(codeJSON, &r); err != nil { - panic(err) + b.Fatal("Unmmarshal:", err) } } b.SetBytes(int64(len(codeJSON))) diff --git a/libgo/go/encoding/json/decode.go b/libgo/go/encoding/json/decode.go index 0a700926296..8287b330034 100644 --- a/libgo/go/encoding/json/decode.go +++ b/libgo/go/encoding/json/decode.go @@ -228,7 +228,9 @@ func (d *decodeState) value(v reflect.Value) { // Feed in an empty string - the shortest, simplest value - // so that it knows we got to the end of the value. if d.scan.redo { - panic("redo") + // rewind. + d.scan.redo = false + d.scan.step = stateBeginValue } d.scan.step(&d.scan, '"') d.scan.step(&d.scan, '"') @@ -317,25 +319,22 @@ func (d *decodeState) array(v reflect.Value) { } v = pv - // Decoding into nil interface? Switch to non-reflect code. - iv := v - ok := iv.Kind() == reflect.Interface - if ok { - iv.Set(reflect.ValueOf(d.arrayInterface())) - return - } - // Check type of target. - av := v - if av.Kind() != reflect.Array && av.Kind() != reflect.Slice { + switch v.Kind() { + default: d.saveError(&UnmarshalTypeError{"array", v.Type()}) d.off-- d.next() return + case reflect.Interface: + // Decoding into nil interface? Switch to non-reflect code. + v.Set(reflect.ValueOf(d.arrayInterface())) + return + case reflect.Array: + case reflect.Slice: + break } - sv := v - i := 0 for { // Look ahead for ] - can only happen on first iteration. @@ -349,23 +348,25 @@ func (d *decodeState) array(v reflect.Value) { d.scan.undo(op) // Get element of array, growing if necessary. - if i >= av.Cap() && sv.IsValid() { - newcap := sv.Cap() + sv.Cap()/2 - if newcap < 4 { - newcap = 4 + if v.Kind() == reflect.Slice { + // Grow slice if necessary + if i >= v.Cap() { + newcap := v.Cap() + v.Cap()/2 + if newcap < 4 { + newcap = 4 + } + newv := reflect.MakeSlice(v.Type(), v.Len(), newcap) + reflect.Copy(newv, v) + v.Set(newv) + } + if i >= v.Len() { + v.SetLen(i + 1) } - newv := reflect.MakeSlice(sv.Type(), sv.Len(), newcap) - reflect.Copy(newv, sv) - sv.Set(newv) - } - if i >= av.Len() && sv.IsValid() { - // Must be slice; gave up on array during i >= av.Cap(). - sv.SetLen(i + 1) } - // Decode into element. - if i < av.Len() { - d.value(av.Index(i)) + if i < v.Len() { + // Decode into element. + d.value(v.Index(i)) } else { // Ran out of fixed array: skip. d.value(reflect.Value{}) @@ -382,19 +383,19 @@ func (d *decodeState) array(v reflect.Value) { } } - if i < av.Len() { - if !sv.IsValid() { + if i < v.Len() { + if v.Kind() == reflect.Array { // Array. Zero the rest. - z := reflect.Zero(av.Type().Elem()) - for ; i < av.Len(); i++ { - av.Index(i).Set(z) + z := reflect.Zero(v.Type().Elem()) + for ; i < v.Len(); i++ { + v.Index(i).Set(z) } } else { - sv.SetLen(i) + v.SetLen(i) } } - if i == 0 && av.Kind() == reflect.Slice && sv.IsNil() { - sv.Set(reflect.MakeSlice(sv.Type(), 0, 0)) + if i == 0 && v.Kind() == reflect.Slice { + v.Set(reflect.MakeSlice(v.Type(), 0, 0)) } } diff --git a/libgo/go/encoding/json/decode_test.go b/libgo/go/encoding/json/decode_test.go index bf3953eb051..05c8a064a42 100644 --- a/libgo/go/encoding/json/decode_test.go +++ b/libgo/go/encoding/json/decode_test.go @@ -6,6 +6,7 @@ package json import ( "bytes" + "fmt" "reflect" "strings" "testing" @@ -73,6 +74,12 @@ var unmarshalTests = []unmarshalTest{ // syntax errors {`{"X": "foo", "Y"}`, nil, nil, &SyntaxError{"invalid character '}' after object key", 17}}, + {`[1, 2, 3+]`, nil, nil, &SyntaxError{"invalid character '+' after array element", 9}}, + + // array tests + {`[1, 2, 3]`, new([3]int), [3]int{1, 2, 3}, nil}, + {`[1, 2, 3]`, new([1]int), [1]int{1}, nil}, + {`[1, 2, 3]`, new([5]int), [5]int{1, 2, 3, 0, 0}, nil}, // composite tests {allValueIndent, new(All), allValue, nil}, @@ -242,6 +249,38 @@ func TestHTMLEscape(t *testing.T) { } } +// WrongString is a struct that's misusing the ,string modifier. +type WrongString struct { + Message string `json:"result,string"` +} + +type wrongStringTest struct { + in, err string +} + +// TODO(bradfitz): as part of Issue 2331, fix these tests' expected +// error values to be helpful, rather than the confusing messages they +// are now. +var wrongStringTests = []wrongStringTest{ + {`{"result":"x"}`, "JSON decoder out of sync - data changing underfoot?"}, + {`{"result":"foo"}`, "json: cannot unmarshal bool into Go value of type string"}, + {`{"result":"123"}`, "json: cannot unmarshal number into Go value of type string"}, +} + +// If people misuse the ,string modifier, the error message should be +// helpful, telling the user that they're doing it wrong. +func TestErrorMessageFromMisusedString(t *testing.T) { + for n, tt := range wrongStringTests { + r := strings.NewReader(tt.in) + var s WrongString + err := NewDecoder(r).Decode(&s) + got := fmt.Sprintf("%v", err) + if got != tt.err { + t.Errorf("%d. got err = %q, want %q", n, got, tt.err) + } + } +} + func noSpace(c rune) rune { if isSpace(c) { return -1 diff --git a/libgo/go/encoding/json/encode.go b/libgo/go/encoding/json/encode.go index 69deaf2a40d..3d2f4fc316e 100644 --- a/libgo/go/encoding/json/encode.go +++ b/libgo/go/encoding/json/encode.go @@ -197,6 +197,7 @@ var hex = "0123456789abcdef" // An encodeState encodes JSON into a bytes.Buffer. type encodeState struct { bytes.Buffer // accumulated output + scratch [64]byte } func (e *encodeState) marshal(v interface{}) (err error) { @@ -275,14 +276,26 @@ func (e *encodeState) reflectValueQuoted(v reflect.Value, quoted bool) { } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - writeString(e, strconv.FormatInt(v.Int(), 10)) - + b := strconv.AppendInt(e.scratch[:0], v.Int(), 10) + if quoted { + writeString(e, string(b)) + } else { + e.Write(b) + } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - writeString(e, strconv.FormatUint(v.Uint(), 10)) - + b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10) + if quoted { + writeString(e, string(b)) + } else { + e.Write(b) + } case reflect.Float32, reflect.Float64: - writeString(e, strconv.FormatFloat(v.Float(), 'g', -1, v.Type().Bits())) - + b := strconv.AppendFloat(e.scratch[:0], v.Float(), 'g', -1, v.Type().Bits()) + if quoted { + writeString(e, string(b)) + } else { + e.Write(b) + } case reflect.String: if quoted { sb, err := Marshal(v.String()) @@ -339,13 +352,10 @@ func (e *encodeState) reflectValueQuoted(v reflect.Value, quoted bool) { e.WriteString("null") break } - // Slices can be marshalled as nil, but otherwise are handled - // as arrays. - fallthrough - case reflect.Array: - if v.Type() == byteSliceType { + if v.Type().Elem().Kind() == reflect.Uint8 { + // Byte slices get special treatment; arrays don't. + s := v.Bytes() e.WriteByte('"') - s := v.Interface().([]byte) if len(s) < 1024 { // for small buffers, using Encode directly is much faster. dst := make([]byte, base64.StdEncoding.EncodedLen(len(s))) @@ -361,6 +371,10 @@ func (e *encodeState) reflectValueQuoted(v reflect.Value, quoted bool) { e.WriteByte('"') break } + // Slices can be marshalled as nil, but otherwise are handled + // as arrays. + fallthrough + case reflect.Array: e.WriteByte('[') n := v.Len() for i := 0; i < n; i++ { diff --git a/libgo/go/encoding/json/encode_test.go b/libgo/go/encoding/json/encode_test.go index 92f266aba63..9366589f252 100644 --- a/libgo/go/encoding/json/encode_test.go +++ b/libgo/go/encoding/json/encode_test.go @@ -82,3 +82,28 @@ func TestStringTag(t *testing.T) { t.Fatalf("decode didn't match.\nsource: %#v\nEncoded as:\n%s\ndecode: %#v", s, string(got), s2) } } + +// byte slices are special even if they're renamed types. +type renamedByte byte +type renamedByteSlice []byte +type renamedRenamedByteSlice []renamedByte + +func TestEncodeRenamedByteSlice(t *testing.T) { + s := renamedByteSlice("abc") + result, err := Marshal(s) + if err != nil { + t.Fatal(err) + } + expect := `"YWJj"` + if string(result) != expect { + t.Errorf(" got %s want %s", result, expect) + } + r := renamedRenamedByteSlice("abc") + result, err = Marshal(r) + if err != nil { + t.Fatal(err) + } + if string(result) != expect { + t.Errorf(" got %s want %s", result, expect) + } +} diff --git a/libgo/go/encoding/json/testdata/code.json.gz b/libgo/go/encoding/json/testdata/code.json.gz Binary files differnew file mode 100644 index 00000000000..0e2895b53ac --- /dev/null +++ b/libgo/go/encoding/json/testdata/code.json.gz diff --git a/libgo/go/encoding/xml/marshal_test.go b/libgo/go/encoding/xml/marshal_test.go index 80407658015..6a241694baf 100644 --- a/libgo/go/encoding/xml/marshal_test.go +++ b/libgo/go/encoding/xml/marshal_test.go @@ -394,7 +394,7 @@ func TestUnmarshal(t *testing.T) { if err != nil { t.Errorf("#%d: unexpected error: %#v", i, err) } else if got, want := dest, test.Value; !reflect.DeepEqual(got, want) { - t.Errorf("#%d: unmarshal(%#s) = %#v, want %#v", i, test.ExpectXML, got, want) + t.Errorf("#%d: unmarshal(%q) = %#v, want %#v", i, test.ExpectXML, got, want) } } } diff --git a/libgo/go/exp/inotify/inotify_linux_test.go b/libgo/go/exp/inotify/inotify_linux_test.go index a6bb46fe77d..d035ec1410f 100644 --- a/libgo/go/exp/inotify/inotify_linux_test.go +++ b/libgo/go/exp/inotify/inotify_linux_test.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build linux + package inotify import ( @@ -17,6 +19,9 @@ func TestInotifyEvents(t *testing.T) { t.Fatalf("NewWatcher() failed: %s", err) } + t.Logf("NEEDS TO BE CONVERTED TO NEW GO TOOL") // TODO + return + // Add a watch for "_test" err = watcher.Watch("_test") if err != nil { @@ -57,7 +62,7 @@ func TestInotifyEvents(t *testing.T) { } // We expect this event to be received almost immediately, but let's wait 1 s to be sure - time.Sleep(1000e6) // 1000 ms + time.Sleep(1 * time.Second) if eventsReceived == 0 { t.Fatal("inotify event hasn't been received after 1 second") } @@ -69,7 +74,7 @@ func TestInotifyEvents(t *testing.T) { select { case <-done: t.Log("event channel closed") - case <-time.After(1e9): + case <-time.After(1 * time.Second): t.Fatal("event stream was not closed after 1 second") } } @@ -84,7 +89,7 @@ func TestInotifyClose(t *testing.T) { done = true }() - time.Sleep(50e6) // 50 ms + time.Sleep(50 * time.Millisecond) if !done { t.Fatal("double Close() test failed: second Close() call didn't return") } diff --git a/libgo/go/exp/norm/normregtest.go b/libgo/go/exp/norm/normregtest.go index d214ce11bc2..57ba7032981 100644 --- a/libgo/go/exp/norm/normregtest.go +++ b/libgo/go/exp/norm/normregtest.go @@ -177,7 +177,7 @@ func loadTestData() { } if test.r == 0 { // save for CharacterByCharacterTests - test.r = int(r) + test.r = rune(r) } var buf [utf8.UTFMax]byte sz := utf8.EncodeRune(buf[:], rune(r)) @@ -242,9 +242,9 @@ func doConformanceTests(t *Test, partn int) { func CharacterByCharacterTests() { tests := part[1].tests - last := 0 + var last rune = 0 for i := 0; i <= len(tests); i++ { // last one is special case - var r int + var r rune if i == len(tests) { r = 0x2FA1E // Don't have to go to 0x10FFFF } else { @@ -285,7 +285,7 @@ func PerformanceTest() { norm.NFC.Append(nil, buf...) success <- true }() - timeout := time.After(1e9) + timeout := time.After(1 * time.Second) select { case <-success: // test completed before the timeout diff --git a/libgo/go/exp/sql/convert.go b/libgo/go/exp/sql/convert.go index 24315a0d351..feb79aeafe0 100644 --- a/libgo/go/exp/sql/convert.go +++ b/libgo/go/exp/sql/convert.go @@ -95,35 +95,26 @@ func convertAssign(dest, src interface{}) error { switch dv.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: s := asString(src) - i64, err := strconv.ParseInt(s, 10, 64) + i64, err := strconv.ParseInt(s, 10, dv.Type().Bits()) if err != nil { return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err) } - if dv.OverflowInt(i64) { - return fmt.Errorf("string %q overflows %s", s, dv.Kind()) - } dv.SetInt(i64) return nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: s := asString(src) - u64, err := strconv.ParseUint(s, 10, 64) + u64, err := strconv.ParseUint(s, 10, dv.Type().Bits()) if err != nil { return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err) } - if dv.OverflowUint(u64) { - return fmt.Errorf("string %q overflows %s", s, dv.Kind()) - } dv.SetUint(u64) return nil case reflect.Float32, reflect.Float64: s := asString(src) - f64, err := strconv.ParseFloat(s, 64) + f64, err := strconv.ParseFloat(s, dv.Type().Bits()) if err != nil { return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err) } - if dv.OverflowFloat(f64) { - return fmt.Errorf("value %q overflows %s", s, dv.Kind()) - } dv.SetFloat(f64) return nil } diff --git a/libgo/go/exp/sql/convert_test.go b/libgo/go/exp/sql/convert_test.go index 52cee927241..bed09ffb29d 100644 --- a/libgo/go/exp/sql/convert_test.go +++ b/libgo/go/exp/sql/convert_test.go @@ -55,10 +55,10 @@ var conversionTests = []conversionTest{ // Strings to integers {s: "255", d: &scanuint8, wantuint: 255}, - {s: "256", d: &scanuint8, wanterr: `string "256" overflows uint8`}, + {s: "256", d: &scanuint8, wanterr: `converting string "256" to a uint8: strconv.ParseUint: parsing "256": value out of range`}, {s: "256", d: &scanuint16, wantuint: 256}, {s: "-1", d: &scanint, wantint: -1}, - {s: "foo", d: &scanint, wanterr: `converting string "foo" to a int: parsing "foo": invalid syntax`}, + {s: "foo", d: &scanint, wanterr: `converting string "foo" to a int: strconv.ParseInt: parsing "foo": invalid syntax`}, // True bools {s: true, d: &scanbool, wantbool: true}, diff --git a/libgo/go/exp/sql/sql.go b/libgo/go/exp/sql/sql.go index f17d12eaa13..937982cdbe6 100644 --- a/libgo/go/exp/sql/sql.go +++ b/libgo/go/exp/sql/sql.go @@ -22,10 +22,10 @@ var drivers = make(map[string]driver.Driver) // it panics. func Register(name string, driver driver.Driver) { if driver == nil { - panic("db: Register driver is nil") + panic("sql: Register driver is nil") } if _, dup := drivers[name]; dup { - panic("db: Register called twice for driver " + name) + panic("sql: Register called twice for driver " + name) } drivers[name] = driver } @@ -80,7 +80,7 @@ type ScannerInto interface { // ErrNoRows is returned by Scan when QueryRow doesn't return a // row. In such a case, QueryRow returns a placeholder *Row value that // defers this error until a Scan. -var ErrNoRows = errors.New("db: no rows in result set") +var ErrNoRows = errors.New("sql: no rows in result set") // DB is a database handle. It's safe for concurrent use by multiple // goroutines. @@ -102,7 +102,7 @@ type DB struct { func Open(driverName, dataSourceName string) (*DB, error) { driver, ok := drivers[driverName] if !ok { - return nil, fmt.Errorf("db: unknown driver %q (forgotten import?)", driverName) + return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName) } return &DB{driver: driver, dsn: dataSourceName}, nil } @@ -134,6 +134,7 @@ func (db *DB) maxIdleConns() int { func (db *DB) conn() (driver.Conn, error) { db.mu.Lock() if db.closed { + db.mu.Unlock() return nil, errors.New("sql: database is closed") } if n := len(db.freeConn); n > 0 { @@ -513,7 +514,7 @@ func (s *Stmt) Exec(args ...interface{}) (Result, error) { // placeholders, so we won't sanity check input here and instead let the // driver deal with errors. if want := si.NumInput(); want != -1 && len(args) != want { - return nil, fmt.Errorf("db: expected %d arguments, got %d", want, len(args)) + return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args)) } // Convert args to subset types. @@ -521,10 +522,10 @@ func (s *Stmt) Exec(args ...interface{}) (Result, error) { for n, arg := range args { args[n], err = cc.ColumnConverter(n).ConvertValue(arg) if err != nil { - return nil, fmt.Errorf("db: converting Exec argument #%d's type: %v", n, err) + return nil, fmt.Errorf("sql: converting Exec argument #%d's type: %v", n, err) } if !driver.IsParameterSubsetType(args[n]) { - return nil, fmt.Errorf("db: driver ColumnConverter error converted %T to unsupported type %T", + return nil, fmt.Errorf("sql: driver ColumnConverter error converted %T to unsupported type %T", arg, args[n]) } } @@ -532,7 +533,7 @@ func (s *Stmt) Exec(args ...interface{}) (Result, error) { for n, arg := range args { args[n], err = driver.DefaultParameterConverter.ConvertValue(arg) if err != nil { - return nil, fmt.Errorf("db: converting Exec argument #%d's type: %v", n, err) + return nil, fmt.Errorf("sql: converting Exec argument #%d's type: %v", n, err) } } } @@ -554,7 +555,7 @@ func (s *Stmt) connStmt() (ci driver.Conn, releaseConn func(), si driver.Stmt, e s.mu.Lock() if s.closed { s.mu.Unlock() - err = errors.New("db: statement is closed") + err = errors.New("sql: statement is closed") return } @@ -616,7 +617,7 @@ func (s *Stmt) Query(args ...interface{}) (*Rows, error) { // placeholders, so we won't sanity check input here and instead let the // driver deal with errors. if want := si.NumInput(); want != -1 && len(args) != want { - return nil, fmt.Errorf("db: statement expects %d inputs; got %d", si.NumInput(), len(args)) + return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", si.NumInput(), len(args)) } sargs, err := subsetTypeArgs(args) if err != nil { @@ -736,27 +737,40 @@ func (rs *Rows) Err() error { return rs.lasterr } +// Columns returns the column names. +// Columns returns an error if the rows are closed, or if the rows +// are from QueryRow and there was a deferred error. +func (rs *Rows) Columns() ([]string, error) { + if rs.closed { + return nil, errors.New("sql: Rows are closed") + } + if rs.rowsi == nil { + return nil, errors.New("sql: no Rows available") + } + return rs.rowsi.Columns(), nil +} + // Scan copies the columns in the current row into the values pointed // at by dest. If dest contains pointers to []byte, the slices should // not be modified and should only be considered valid until the next // call to Next or Scan. func (rs *Rows) Scan(dest ...interface{}) error { if rs.closed { - return errors.New("db: Rows closed") + return errors.New("sql: Rows closed") } if rs.lasterr != nil { return rs.lasterr } if rs.lastcols == nil { - return errors.New("db: Scan called without calling Next") + return errors.New("sql: Scan called without calling Next") } if len(dest) != len(rs.lastcols) { - return fmt.Errorf("db: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest)) + return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest)) } for i, sv := range rs.lastcols { err := convertAssign(dest[i], sv) if err != nil { - return fmt.Errorf("db: Scan error on column index %d: %v", i, err) + return fmt.Errorf("sql: Scan error on column index %d: %v", i, err) } } return nil diff --git a/libgo/go/exp/sql/sql_test.go b/libgo/go/exp/sql/sql_test.go index 4f8318d26ef..5307a235ddf 100644 --- a/libgo/go/exp/sql/sql_test.go +++ b/libgo/go/exp/sql/sql_test.go @@ -75,6 +75,23 @@ func TestQuery(t *testing.T) { } } +func TestRowsColumns(t *testing.T) { + db := newTestDB(t, "people") + defer closeDB(t, db) + rows, err := db.Query("SELECT|people|age,name|") + if err != nil { + t.Fatalf("Query: %v", err) + } + cols, err := rows.Columns() + if err != nil { + t.Fatalf("Columns: %v", err) + } + want := []string{"age", "name"} + if !reflect.DeepEqual(cols, want) { + t.Errorf("got %#v; want %#v", cols, want) + } +} + func TestQueryRow(t *testing.T) { db := newTestDB(t, "people") defer closeDB(t, db) @@ -187,12 +204,12 @@ func TestExec(t *testing.T) { {[]interface{}{7, 9}, ""}, // Invalid conversions: - {[]interface{}{"Brad", int64(0xFFFFFFFF)}, "db: converting Exec argument #1's type: sql/driver: value 4294967295 overflows int32"}, - {[]interface{}{"Brad", "strconv fail"}, "db: converting Exec argument #1's type: sql/driver: value \"strconv fail\" can't be converted to int32"}, + {[]interface{}{"Brad", int64(0xFFFFFFFF)}, "sql: converting Exec argument #1's type: sql/driver: value 4294967295 overflows int32"}, + {[]interface{}{"Brad", "strconv fail"}, "sql: converting Exec argument #1's type: sql/driver: value \"strconv fail\" can't be converted to int32"}, // Wrong number of args: - {[]interface{}{}, "db: expected 2 arguments, got 0"}, - {[]interface{}{1, 2, 3}, "db: expected 2 arguments, got 3"}, + {[]interface{}{}, "sql: expected 2 arguments, got 0"}, + {[]interface{}{1, 2, 3}, "sql: expected 2 arguments, got 3"}, } for n, et := range execTests { _, err := stmt.Exec(et.args...) @@ -228,3 +245,16 @@ func TestTxStmt(t *testing.T) { t.Fatalf("Commit = %v", err) } } + +// Tests fix for issue 2542, that we release a lock when querying on +// a closed connection. +func TestIssue2542Deadlock(t *testing.T) { + db := newTestDB(t, "people") + closeDB(t, db) + for i := 0; i < 2; i++ { + _, err := db.Query("SELECT|people|age,name|") + if err == nil { + t.Fatalf("expected error") + } + } +} diff --git a/libgo/go/exp/ssh/client.go b/libgo/go/exp/ssh/client.go index d89b908cdcf..7c862078b7e 100644 --- a/libgo/go/exp/ssh/client.go +++ b/libgo/go/exp/ssh/client.go @@ -187,10 +187,10 @@ func (c *ClientConn) mainLoop() { if err != nil { break } - // TODO(dfc) A note on blocking channel use. - // The msg, win, data and dataExt channels of a clientChan can - // cause this loop to block indefinately if the consumer does - // not service them. + // TODO(dfc) A note on blocking channel use. + // The msg, win, data and dataExt channels of a clientChan can + // cause this loop to block indefinately if the consumer does + // not service them. switch packet[0] { case msgChannelData: if len(packet) < 9 { @@ -200,7 +200,7 @@ func (c *ClientConn) mainLoop() { peersId := uint32(packet[1])<<24 | uint32(packet[2])<<16 | uint32(packet[3])<<8 | uint32(packet[4]) if length := int(packet[5])<<24 | int(packet[6])<<16 | int(packet[7])<<8 | int(packet[8]); length > 0 { packet = packet[9:] - c.getChan(peersId).stdout.data <- packet[:length] + c.getChan(peersId).stdout.handleData(packet[:length]) } case msgChannelExtendedData: if len(packet) < 13 { @@ -211,11 +211,11 @@ func (c *ClientConn) mainLoop() { datatype := uint32(packet[5])<<24 | uint32(packet[6])<<16 | uint32(packet[7])<<8 | uint32(packet[8]) if length := int(packet[9])<<24 | int(packet[10])<<16 | int(packet[11])<<8 | int(packet[12]); length > 0 { packet = packet[13:] - // RFC 4254 5.2 defines data_type_code 1 to be data destined + // RFC 4254 5.2 defines data_type_code 1 to be data destined // for stderr on interactive sessions. Other data types are // silently discarded. if datatype == 1 { - c.getChan(peersId).stderr.data <- packet[:length] + c.getChan(peersId).stderr.handleData(packet[:length]) } } default: @@ -228,12 +228,22 @@ func (c *ClientConn) mainLoop() { c.getChan(msg.PeersId).msg <- msg case *channelCloseMsg: ch := c.getChan(msg.PeersId) + ch.theyClosed = true close(ch.stdin.win) - close(ch.stdout.data) - close(ch.stderr.data) + ch.stdout.eof() + ch.stderr.eof() + close(ch.msg) + if !ch.weClosed { + ch.weClosed = true + ch.sendClose() + } c.chanlist.remove(msg.PeersId) case *channelEOFMsg: - c.getChan(msg.PeersId).msg <- msg + ch := c.getChan(msg.PeersId) + ch.stdout.eof() + // RFC 4254 is mute on how EOF affects dataExt messages but + // it is logical to signal EOF at the same time. + ch.stderr.eof() case *channelRequestSuccessMsg: c.getChan(msg.PeersId).msg <- msg case *channelRequestFailureMsg: @@ -242,6 +252,8 @@ func (c *ClientConn) mainLoop() { c.getChan(msg.PeersId).msg <- msg case *windowAdjustMsg: c.getChan(msg.PeersId).stdin.win <- int(msg.AdditionalBytes) + case *disconnectMsg: + break default: fmt.Printf("mainLoop: unhandled message %T: %v\n", msg, msg) } @@ -249,7 +261,7 @@ func (c *ClientConn) mainLoop() { } } -// Dial connects to the given network address using net.Dial and +// Dial connects to the given network address using net.Dial and // then initiates a SSH handshake, returning the resulting client connection. func Dial(network, addr string, config *ClientConfig) (*ClientConn, error) { conn, err := net.Dial(network, addr) @@ -259,18 +271,18 @@ func Dial(network, addr string, config *ClientConfig) (*ClientConn, error) { return Client(conn, config) } -// A ClientConfig structure is used to configure a ClientConn. After one has +// A ClientConfig structure is used to configure a ClientConn. After one has // been passed to an SSH function it must not be modified. type ClientConfig struct { - // Rand provides the source of entropy for key exchange. If Rand is - // nil, the cryptographic random reader in package crypto/rand will + // Rand provides the source of entropy for key exchange. If Rand is + // nil, the cryptographic random reader in package crypto/rand will // be used. Rand io.Reader // The username to authenticate. User string - // A slice of ClientAuth methods. Only the first instance + // A slice of ClientAuth methods. Only the first instance // of a particular RFC 4252 method will be used during authentication. Auth []ClientAuth @@ -285,7 +297,7 @@ func (c *ClientConfig) rand() io.Reader { return c.Rand } -// A clientChan represents a single RFC 4254 channel that is multiplexed +// A clientChan represents a single RFC 4254 channel that is multiplexed // over a single SSH connection. type clientChan struct { packetWriter @@ -294,10 +306,13 @@ type clientChan struct { stdout *chanReader // receives the payload of channelData messages stderr *chanReader // receives the payload of channelExtendedData messages msg chan interface{} // incoming messages + + theyClosed bool // indicates the close msg has been received from the remote side + weClosed bool // incidates the close msg has been sent from our side } // newClientChan returns a partially constructed *clientChan -// using the local id provided. To be usable clientChan.peersId +// using the local id provided. To be usable clientChan.peersId // needs to be assigned once known. func newClientChan(t *transport, id uint32) *clientChan { c := &clientChan{ @@ -320,8 +335,8 @@ func newClientChan(t *transport, id uint32) *clientChan { return c } -// waitForChannelOpenResponse, if successful, fills out -// the peerId and records any initial window advertisement. +// waitForChannelOpenResponse, if successful, fills out +// the peerId and records any initial window advertisement. func (c *clientChan) waitForChannelOpenResponse() error { switch msg := (<-c.msg).(type) { case *channelOpenConfirmMsg: @@ -335,13 +350,29 @@ func (c *clientChan) waitForChannelOpenResponse() error { return errors.New("unexpected packet") } -// Close closes the channel. This does not close the underlying connection. -func (c *clientChan) Close() error { +// sendEOF sends EOF to the server. RFC 4254 Section 5.3 +func (c *clientChan) sendEOF() error { + return c.writePacket(marshal(msgChannelEOF, channelEOFMsg{ + PeersId: c.peersId, + })) +} + +// sendClose signals the intent to close the channel. +func (c *clientChan) sendClose() error { return c.writePacket(marshal(msgChannelClose, channelCloseMsg{ PeersId: c.peersId, })) } +// Close closes the channel. This does not close the underlying connection. +func (c *clientChan) Close() error { + if !c.weClosed { + c.weClosed = true + return c.sendClose() + } + return nil +} + // Thread safe channel list. type chanlist struct { // protects concurrent access to chans @@ -413,7 +444,7 @@ func (w *chanWriter) Write(data []byte) (n int, err error) { } func (w *chanWriter) Close() error { - return w.clientChan.writePacket(marshal(msgChannelEOF, channelEOFMsg{w.clientChan.peersId})) + return w.clientChan.sendEOF() } // A chanReader represents stdout or stderr of a remote process. @@ -422,10 +453,27 @@ type chanReader struct { // If writes to this channel block, they will block mainLoop, making // it unable to receive new messages from the remote side. data chan []byte // receives data from remote + dataClosed bool // protects data from being closed twice clientChan *clientChan // the channel backing this reader buf []byte } +// eof signals to the consumer that there is no more data to be received. +func (r *chanReader) eof() { + if !r.dataClosed { + r.dataClosed = true + close(r.data) + } +} + +// handleData sends buf to the reader's consumer. If r.data is closed +// the data will be silently discarded +func (r *chanReader) handleData(buf []byte) { + if !r.dataClosed { + r.data <- buf + } +} + // Read reads data from the remote process's stdout or stderr. func (r *chanReader) Read(data []byte) (int, error) { var ok bool diff --git a/libgo/go/exp/ssh/client_auth.go b/libgo/go/exp/ssh/client_auth.go index 1a382357b46..3a7e9fb9801 100644 --- a/libgo/go/exp/ssh/client_auth.go +++ b/libgo/go/exp/ssh/client_auth.go @@ -283,8 +283,8 @@ func (p *publickeyAuth) method() string { return "publickey" } -// ClientAuthPublickey returns a ClientAuth using public key authentication. -func ClientAuthPublickey(impl ClientKeyring) ClientAuth { +// ClientAuthKeyring returns a ClientAuth using public key authentication. +func ClientAuthKeyring(impl ClientKeyring) ClientAuth { return &publickeyAuth{impl} } diff --git a/libgo/go/exp/ssh/client_auth_test.go b/libgo/go/exp/ssh/client_auth_test.go index 2b89e9728c7..c41a93b5c7d 100644 --- a/libgo/go/exp/ssh/client_auth_test.go +++ b/libgo/go/exp/ssh/client_auth_test.go @@ -122,7 +122,7 @@ var ( PasswordCallback: func(user, pass string) bool { return user == "testuser" && pass == string(clientPassword) }, - PubKeyCallback: func(user, algo string, pubkey []byte) bool { + PublicKeyCallback: func(user, algo string, pubkey []byte) bool { key := clientKeychain.keys[0].(*rsa.PrivateKey).PublicKey expected := []byte(serializePublickey(key)) algoname := algoName(key) @@ -179,7 +179,7 @@ func TestClientAuthPublickey(t *testing.T) { config := &ClientConfig{ User: "testuser", Auth: []ClientAuth{ - ClientAuthPublickey(clientKeychain), + ClientAuthKeyring(clientKeychain), }, } c, err := Dial("tcp", newMockAuthServer(t), config) @@ -210,7 +210,7 @@ func TestClientAuthWrongPassword(t *testing.T) { User: "testuser", Auth: []ClientAuth{ ClientAuthPassword(wrongPw), - ClientAuthPublickey(clientKeychain), + ClientAuthKeyring(clientKeychain), }, } @@ -228,7 +228,7 @@ func TestClientAuthInvalidPublickey(t *testing.T) { config := &ClientConfig{ User: "testuser", Auth: []ClientAuth{ - ClientAuthPublickey(kc), + ClientAuthKeyring(kc), }, } @@ -246,7 +246,7 @@ func TestClientAuthRSAandDSA(t *testing.T) { config := &ClientConfig{ User: "testuser", Auth: []ClientAuth{ - ClientAuthPublickey(kc), + ClientAuthKeyring(kc), }, } c, err := Dial("tcp", newMockAuthServer(t), config) diff --git a/libgo/go/exp/ssh/client_func_test.go b/libgo/go/exp/ssh/client_func_test.go index 137456095a0..b4bdba95396 100644 --- a/libgo/go/exp/ssh/client_func_test.go +++ b/libgo/go/exp/ssh/client_func_test.go @@ -6,7 +6,7 @@ package ssh // ClientConn functional tests. // These tests require a running ssh server listening on port 22 -// on the local host. Functional tests will be skipped unless +// on the local host. Functional tests will be skipped unless // -ssh.user and -ssh.pass must be passed to gotest. import ( @@ -50,7 +50,7 @@ func TestFuncPublickeyAuth(t *testing.T) { config := &ClientConfig{ User: *sshuser, Auth: []ClientAuth{ - ClientAuthPublickey(kc), + ClientAuthKeyring(kc), }, } conn, err := Dial("tcp", "localhost:22", config) diff --git a/libgo/go/exp/ssh/server.go b/libgo/go/exp/ssh/server.go index 1eee9a4a977..31011c66176 100644 --- a/libgo/go/exp/ssh/server.go +++ b/libgo/go/exp/ssh/server.go @@ -36,10 +36,10 @@ type ServerConfig struct { // several goroutines. PasswordCallback func(user, password string) bool - // PubKeyCallback, if non-nil, is called when a client attempts public + // PublicKeyCallback, if non-nil, is called when a client attempts public // key authentication. It must return true iff the given public key is // valid for the given user. - PubKeyCallback func(user, algo string, pubkey []byte) bool + PublicKeyCallback func(user, algo string, pubkey []byte) bool // Cryptographic-related configuration. Crypto CryptoConfig @@ -359,7 +359,7 @@ func isAcceptableAlgo(algo string) bool { // testPubKey returns true if the given public key is acceptable for the user. func (s *ServerConn) testPubKey(user, algo string, pubKey []byte) bool { - if s.config.PubKeyCallback == nil || !isAcceptableAlgo(algo) { + if s.config.PublicKeyCallback == nil || !isAcceptableAlgo(algo) { return false } @@ -369,7 +369,7 @@ func (s *ServerConn) testPubKey(user, algo string, pubKey []byte) bool { } } - result := s.config.PubKeyCallback(user, algo, pubKey) + result := s.config.PublicKeyCallback(user, algo, pubKey) if len(s.cachedPubKeys) < maxCachedPubKeys { c := cachedPubKey{ user: user, @@ -425,7 +425,7 @@ userAuthLoop: break userAuthLoop } case "publickey": - if s.config.PubKeyCallback == nil { + if s.config.PublicKeyCallback == nil { break } payload := userAuthReq.Payload @@ -499,7 +499,7 @@ userAuthLoop: if s.config.PasswordCallback != nil { failureMsg.Methods = append(failureMsg.Methods, "password") } - if s.config.PubKeyCallback != nil { + if s.config.PublicKeyCallback != nil { failureMsg.Methods = append(failureMsg.Methods, "publickey") } diff --git a/libgo/go/exp/ssh/session.go b/libgo/go/exp/ssh/session.go index 23ea18c29ae..807dd8740d7 100644 --- a/libgo/go/exp/ssh/session.go +++ b/libgo/go/exp/ssh/session.go @@ -34,6 +34,20 @@ const ( SIGUSR2 Signal = "USR2" ) +var signals = map[Signal]int{ + SIGABRT: 6, + SIGALRM: 14, + SIGFPE: 8, + SIGHUP: 1, + SIGILL: 4, + SIGINT: 2, + SIGKILL: 9, + SIGPIPE: 13, + SIGQUIT: 3, + SIGSEGV: 11, + SIGTERM: 15, +} + // A Session represents a connection to a remote command or shell. type Session struct { // Stdin specifies the remote process's standard input. @@ -54,10 +68,12 @@ type Session struct { *clientChan // the channel backing this session - started bool // true once Start, Run or Shell is invoked. - closeAfterWait []io.Closer - copyFuncs []func() error - errch chan error // one send per copyFunc + started bool // true once Start, Run or Shell is invoked. + copyFuncs []func() error + errch chan error // one send per copyFunc + + // true if pipe method is active + stdinpipe, stdoutpipe, stderrpipe bool } // RFC 4254 Section 6.4. @@ -170,10 +186,17 @@ func (s *Session) Start(cmd string) error { return s.start() } -// Run runs cmd on the remote host and waits for it to terminate. -// Typically, the remote server passes cmd to the shell for -// interpretation. A Session only accepts one call to Run, -// Start or Shell. +// Run runs cmd on the remote host. Typically, the remote +// server passes cmd to the shell for interpretation. +// A Session only accepts one call to Run, Start or Shell. +// +// The returned error is nil if the command runs, has no problems +// copying stdin, stdout, and stderr, and exits with a zero exit +// status. +// +// If the command fails to run or doesn't complete successfully, the +// error is of type *ExitError. Other error types may be +// returned for I/O problems. func (s *Session) Run(cmd string) error { err := s.Start(cmd) if err != nil { @@ -216,11 +239,9 @@ func (s *Session) waitForResponse() error { func (s *Session) start() error { s.started = true - type F func(*Session) error + type F func(*Session) for _, setupFd := range []F{(*Session).stdin, (*Session).stdout, (*Session).stderr} { - if err := setupFd(s); err != nil { - return err - } + setupFd(s) } s.errch = make(chan error, len(s.copyFuncs)) @@ -233,6 +254,14 @@ func (s *Session) start() error { } // Wait waits for the remote command to exit. +// +// The returned error is nil if the command runs, has no problems +// copying stdin, stdout, and stderr, and exits with a zero exit +// status. +// +// If the command fails to run or doesn't complete successfully, the +// error is of type *ExitError. Other error types may be +// returned for I/O problems. func (s *Session) Wait() error { if !s.started { return errors.New("ssh: session not started") @@ -245,9 +274,6 @@ func (s *Session) Wait() error { copyError = err } } - for _, fd := range s.closeAfterWait { - fd.Close() - } if waitErr != nil { return waitErr } @@ -255,21 +281,40 @@ func (s *Session) Wait() error { } func (s *Session) wait() error { - for { - switch msg := (<-s.msg).(type) { + wm := Waitmsg{status: -1} + + // Wait for msg channel to be closed before returning. + for msg := range s.msg { + switch msg := msg.(type) { case *channelRequestMsg: - // TODO(dfc) improve this behavior to match os.Waitmsg switch msg.Request { case "exit-status": d := msg.RequestSpecificData - status := int(d[0])<<24 | int(d[1])<<16 | int(d[2])<<8 | int(d[3]) - if status > 0 { - return fmt.Errorf("remote process exited with %d", status) - } - return nil + wm.status = int(d[0])<<24 | int(d[1])<<16 | int(d[2])<<8 | int(d[3]) case "exit-signal": - // TODO(dfc) make a more readable error message - return fmt.Errorf("%v", msg.RequestSpecificData) + signal, rest, ok := parseString(msg.RequestSpecificData) + if !ok { + return fmt.Errorf("wait: could not parse request data: %v", msg.RequestSpecificData) + } + wm.signal = safeString(string(signal)) + + // skip coreDumped bool + if len(rest) == 0 { + return fmt.Errorf("wait: could not parse request data: %v", msg.RequestSpecificData) + } + rest = rest[1:] + + errmsg, rest, ok := parseString(rest) + if !ok { + return fmt.Errorf("wait: could not parse request data: %v", msg.RequestSpecificData) + } + wm.msg = safeString(string(errmsg)) + + lang, _, ok := parseString(rest) + if !ok { + return fmt.Errorf("wait: could not parse request data: %v", msg.RequestSpecificData) + } + wm.lang = safeString(string(lang)) default: return fmt.Errorf("wait: unexpected channel request: %v", msg) } @@ -277,10 +322,26 @@ func (s *Session) wait() error { return fmt.Errorf("wait: unexpected packet %T received: %v", msg, msg) } } - panic("unreachable") + if wm.status == 0 { + return nil + } + if wm.status == -1 { + // exit-status was never sent from server + if wm.signal == "" { + return errors.New("wait: remote command exited without exit status or exit signal") + } + wm.status = 128 + if _, ok := signals[Signal(wm.signal)]; ok { + wm.status += signals[Signal(wm.signal)] + } + } + return &ExitError{wm} } -func (s *Session) stdin() error { +func (s *Session) stdin() { + if s.stdinpipe { + return + } if s.Stdin == nil { s.Stdin = new(bytes.Buffer) } @@ -291,10 +352,12 @@ func (s *Session) stdin() error { } return err }) - return nil } -func (s *Session) stdout() error { +func (s *Session) stdout() { + if s.stdoutpipe { + return + } if s.Stdout == nil { s.Stdout = ioutil.Discard } @@ -302,10 +365,12 @@ func (s *Session) stdout() error { _, err := io.Copy(s.Stdout, s.clientChan.stdout) return err }) - return nil } -func (s *Session) stderr() error { +func (s *Session) stderr() { + if s.stderrpipe { + return + } if s.Stderr == nil { s.Stderr = ioutil.Discard } @@ -313,7 +378,6 @@ func (s *Session) stderr() error { _, err := io.Copy(s.Stderr, s.clientChan.stderr) return err }) - return nil } // StdinPipe returns a pipe that will be connected to the @@ -325,10 +389,8 @@ func (s *Session) StdinPipe() (io.WriteCloser, error) { if s.started { return nil, errors.New("ssh: StdinPipe after process started") } - pr, pw := io.Pipe() - s.Stdin = pr - s.closeAfterWait = append(s.closeAfterWait, pr) - return pw, nil + s.stdinpipe = true + return s.clientChan.stdin, nil } // StdoutPipe returns a pipe that will be connected to the @@ -337,17 +399,15 @@ func (s *Session) StdinPipe() (io.WriteCloser, error) { // stdout and stderr streams. If the StdoutPipe reader is // not serviced fast enought it may eventually cause the // remote command to block. -func (s *Session) StdoutPipe() (io.ReadCloser, error) { +func (s *Session) StdoutPipe() (io.Reader, error) { if s.Stdout != nil { return nil, errors.New("ssh: Stdout already set") } if s.started { return nil, errors.New("ssh: StdoutPipe after process started") } - pr, pw := io.Pipe() - s.Stdout = pw - s.closeAfterWait = append(s.closeAfterWait, pw) - return pr, nil + s.stdoutpipe = true + return s.clientChan.stdout, nil } // StderrPipe returns a pipe that will be connected to the @@ -356,17 +416,15 @@ func (s *Session) StdoutPipe() (io.ReadCloser, error) { // stdout and stderr streams. If the StderrPipe reader is // not serviced fast enought it may eventually cause the // remote command to block. -func (s *Session) StderrPipe() (io.ReadCloser, error) { +func (s *Session) StderrPipe() (io.Reader, error) { if s.Stderr != nil { return nil, errors.New("ssh: Stderr already set") } if s.started { return nil, errors.New("ssh: StderrPipe after process started") } - pr, pw := io.Pipe() - s.Stderr = pw - s.closeAfterWait = append(s.closeAfterWait, pw) - return pr, nil + s.stderrpipe = true + return s.clientChan.stderr, nil } // TODO(dfc) add Output and CombinedOutput helpers @@ -391,3 +449,46 @@ func (c *ClientConn) NewSession() (*Session, error) { clientChan: ch, }, nil } + +// An ExitError reports unsuccessful completion of a remote command. +type ExitError struct { + Waitmsg +} + +func (e *ExitError) Error() string { + return e.Waitmsg.String() +} + +// Waitmsg stores the information about an exited remote command +// as reported by Wait. +type Waitmsg struct { + status int + signal string + msg string + lang string +} + +// ExitStatus returns the exit status of the remote command. +func (w Waitmsg) ExitStatus() int { + return w.status +} + +// Signal returns the exit signal of the remote command if +// it was terminated violently. +func (w Waitmsg) Signal() string { + return w.signal +} + +// Msg returns the exit message given by the remote command +func (w Waitmsg) Msg() string { + return w.msg +} + +// Lang returns the language tag. See RFC 3066 +func (w Waitmsg) Lang() string { + return w.lang +} + +func (w Waitmsg) String() string { + return fmt.Sprintf("Process exited with: %v. Reason was: %v (%v)", w.status, w.msg, w.signal) +} diff --git a/libgo/go/exp/ssh/session_test.go b/libgo/go/exp/ssh/session_test.go index d4818c29f70..2882620b0ba 100644 --- a/libgo/go/exp/ssh/session_test.go +++ b/libgo/go/exp/ssh/session_test.go @@ -12,13 +12,15 @@ import ( "testing" ) +type serverType func(*channel) + // dial constructs a new test server and returns a *ClientConn. -func dial(t *testing.T) *ClientConn { +func dial(handler serverType, t *testing.T) *ClientConn { pw := password("tiger") serverConfig.PasswordCallback = func(user, pass string) bool { return user == "testuser" && pass == string(pw) } - serverConfig.PubKeyCallback = nil + serverConfig.PublicKeyCallback = nil l, err := Listen("tcp", "127.0.0.1:0", serverConfig) if err != nil { @@ -50,27 +52,7 @@ func dial(t *testing.T) *ClientConn { continue } ch.Accept() - go func() { - defer ch.Close() - // this string is returned to stdout - shell := NewServerShell(ch, "golang") - shell.ReadLine() - type exitMsg struct { - PeersId uint32 - Request string - WantReply bool - Status uint32 - } - // TODO(dfc) converting to the concrete type should not be - // necessary to send a packet. - msg := exitMsg{ - PeersId: ch.(*channel).theirId, - Request: "exit-status", - WantReply: false, - Status: 0, - } - ch.(*channel).serverConn.writePacket(marshal(msgChannelRequest, msg)) - }() + go handler(ch.(*channel)) } t.Log("done") }() @@ -91,7 +73,7 @@ func dial(t *testing.T) *ClientConn { // Test a simple string is returned to session.Stdout. func TestSessionShell(t *testing.T) { - conn := dial(t) + conn := dial(shellHandler, t) defer conn.Close() session, err := conn.NewSession() if err != nil { @@ -116,7 +98,7 @@ func TestSessionShell(t *testing.T) { // Test a simple string is returned via StdoutPipe. func TestSessionStdoutPipe(t *testing.T) { - conn := dial(t) + conn := dial(shellHandler, t) defer conn.Close() session, err := conn.NewSession() if err != nil { @@ -147,3 +129,237 @@ func TestSessionStdoutPipe(t *testing.T) { t.Fatalf("Remote shell did not return expected string: expected=golang, actual=%s", actual) } } + +// Test non-0 exit status is returned correctly. +func TestExitStatusNonZero(t *testing.T) { + conn := dial(exitStatusNonZeroHandler, t) + defer conn.Close() + session, err := conn.NewSession() + if err != nil { + t.Fatalf("Unable to request new session: %s", err) + } + defer session.Close() + if err := session.Shell(); err != nil { + t.Fatalf("Unable to execute command: %s", err) + } + err = session.Wait() + if err == nil { + t.Fatalf("expected command to fail but it didn't") + } + e, ok := err.(*ExitError) + if !ok { + t.Fatalf("expected *ExitError but got %T", err) + } + if e.ExitStatus() != 15 { + t.Fatalf("expected command to exit with 15 but got %s", e.ExitStatus()) + } +} + +// Test 0 exit status is returned correctly. +func TestExitStatusZero(t *testing.T) { + conn := dial(exitStatusZeroHandler, t) + defer conn.Close() + session, err := conn.NewSession() + if err != nil { + t.Fatalf("Unable to request new session: %s", err) + } + defer session.Close() + + if err := session.Shell(); err != nil { + t.Fatalf("Unable to execute command: %s", err) + } + err = session.Wait() + if err != nil { + t.Fatalf("expected nil but got %s", err) + } +} + +// Test exit signal and status are both returned correctly. +func TestExitSignalAndStatus(t *testing.T) { + conn := dial(exitSignalAndStatusHandler, t) + defer conn.Close() + session, err := conn.NewSession() + if err != nil { + t.Fatalf("Unable to request new session: %s", err) + } + defer session.Close() + if err := session.Shell(); err != nil { + t.Fatalf("Unable to execute command: %s", err) + } + err = session.Wait() + if err == nil { + t.Fatalf("expected command to fail but it didn't") + } + e, ok := err.(*ExitError) + if !ok { + t.Fatalf("expected *ExitError but got %T", err) + } + if e.Signal() != "TERM" || e.ExitStatus() != 15 { + t.Fatalf("expected command to exit with signal TERM and status 15 but got signal %s and status %v", e.Signal(), e.ExitStatus()) + } +} + +// Test exit signal and status are both returned correctly. +func TestKnownExitSignalOnly(t *testing.T) { + conn := dial(exitSignalHandler, t) + defer conn.Close() + session, err := conn.NewSession() + if err != nil { + t.Fatalf("Unable to request new session: %s", err) + } + defer session.Close() + if err := session.Shell(); err != nil { + t.Fatalf("Unable to execute command: %s", err) + } + err = session.Wait() + if err == nil { + t.Fatalf("expected command to fail but it didn't") + } + e, ok := err.(*ExitError) + if !ok { + t.Fatalf("expected *ExitError but got %T", err) + } + if e.Signal() != "TERM" || e.ExitStatus() != 143 { + t.Fatalf("expected command to exit with signal TERM and status 143 but got signal %s and status %v", e.Signal(), e.ExitStatus()) + } +} + +// Test exit signal and status are both returned correctly. +func TestUnknownExitSignal(t *testing.T) { + conn := dial(exitSignalUnknownHandler, t) + defer conn.Close() + session, err := conn.NewSession() + if err != nil { + t.Fatalf("Unable to request new session: %s", err) + } + defer session.Close() + if err := session.Shell(); err != nil { + t.Fatalf("Unable to execute command: %s", err) + } + err = session.Wait() + if err == nil { + t.Fatalf("expected command to fail but it didn't") + } + e, ok := err.(*ExitError) + if !ok { + t.Fatalf("expected *ExitError but got %T", err) + } + if e.Signal() != "SYS" || e.ExitStatus() != 128 { + t.Fatalf("expected command to exit with signal SYS and status 128 but got signal %s and status %v", e.Signal(), e.ExitStatus()) + } +} + +// Test WaitMsg is not returned if the channel closes abruptly. +func TestExitWithoutStatusOrSignal(t *testing.T) { + conn := dial(exitWithoutSignalOrStatus, t) + defer conn.Close() + session, err := conn.NewSession() + if err != nil { + t.Fatalf("Unable to request new session: %s", err) + } + defer session.Close() + if err := session.Shell(); err != nil { + t.Fatalf("Unable to execute command: %s", err) + } + err = session.Wait() + if err == nil { + t.Fatalf("expected command to fail but it didn't") + } + _, ok := err.(*ExitError) + if ok { + // you can't actually test for errors.errorString + // because it's not exported. + t.Fatalf("expected *errorString but got %T", err) + } +} + +type exitStatusMsg struct { + PeersId uint32 + Request string + WantReply bool + Status uint32 +} + +type exitSignalMsg struct { + PeersId uint32 + Request string + WantReply bool + Signal string + CoreDumped bool + Errmsg string + Lang string +} + +func exitStatusZeroHandler(ch *channel) { + defer ch.Close() + // this string is returned to stdout + shell := NewServerShell(ch, "> ") + shell.ReadLine() + sendStatus(0, ch) +} + +func exitStatusNonZeroHandler(ch *channel) { + defer ch.Close() + shell := NewServerShell(ch, "> ") + shell.ReadLine() + sendStatus(15, ch) +} + +func exitSignalAndStatusHandler(ch *channel) { + defer ch.Close() + shell := NewServerShell(ch, "> ") + shell.ReadLine() + sendStatus(15, ch) + sendSignal("TERM", ch) +} + +func exitSignalHandler(ch *channel) { + defer ch.Close() + shell := NewServerShell(ch, "> ") + shell.ReadLine() + sendSignal("TERM", ch) +} + +func exitSignalUnknownHandler(ch *channel) { + defer ch.Close() + shell := NewServerShell(ch, "> ") + shell.ReadLine() + sendSignal("SYS", ch) +} + +func exitWithoutSignalOrStatus(ch *channel) { + defer ch.Close() + shell := NewServerShell(ch, "> ") + shell.ReadLine() +} + +func shellHandler(ch *channel) { + defer ch.Close() + // this string is returned to stdout + shell := NewServerShell(ch, "golang") + shell.ReadLine() + sendStatus(0, ch) +} + +func sendStatus(status uint32, ch *channel) { + msg := exitStatusMsg{ + PeersId: ch.theirId, + Request: "exit-status", + WantReply: false, + Status: status, + } + ch.serverConn.writePacket(marshal(msgChannelRequest, msg)) +} + +func sendSignal(signal string, ch *channel) { + sig := exitSignalMsg{ + PeersId: ch.theirId, + Request: "exit-signal", + WantReply: false, + Signal: signal, + CoreDumped: false, + Errmsg: "Process terminated", + Lang: "en-GB-oed", + } + ch.serverConn.writePacket(marshal(msgChannelRequest, sig)) +} diff --git a/libgo/go/exp/ssh/tcpip.go b/libgo/go/exp/ssh/tcpip.go index a85044ace9c..bee41eeb0db 100644 --- a/libgo/go/exp/ssh/tcpip.go +++ b/libgo/go/exp/ssh/tcpip.go @@ -10,6 +10,7 @@ import ( "io" "net" ) + // Dial initiates a connection to the addr from the remote host. // addr is resolved using net.ResolveTCPAddr before connection. // This could allow an observer to observe the DNS name of the diff --git a/libgo/go/exp/terminal/terminal.go b/libgo/go/exp/terminal/terminal.go index 18d76cd6b90..809e88cacfa 100644 --- a/libgo/go/exp/terminal/terminal.go +++ b/libgo/go/exp/terminal/terminal.go @@ -2,13 +2,56 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build linux + package terminal -import "io" +import ( + "io" + "sync" +) + +// EscapeCodes contains escape sequences that can be written to the terminal in +// order to achieve different styles of text. +type EscapeCodes struct { + // Foreground colors + Black, Red, Green, Yellow, Blue, Magenta, Cyan, White []byte + + // Reset all attributes + Reset []byte +} + +var vt100EscapeCodes = EscapeCodes{ + Black: []byte{keyEscape, '[', '3', '0', 'm'}, + Red: []byte{keyEscape, '[', '3', '1', 'm'}, + Green: []byte{keyEscape, '[', '3', '2', 'm'}, + Yellow: []byte{keyEscape, '[', '3', '3', 'm'}, + Blue: []byte{keyEscape, '[', '3', '4', 'm'}, + Magenta: []byte{keyEscape, '[', '3', '5', 'm'}, + Cyan: []byte{keyEscape, '[', '3', '6', 'm'}, + White: []byte{keyEscape, '[', '3', '7', 'm'}, + + Reset: []byte{keyEscape, '[', '0', 'm'}, +} // Terminal contains the state for running a VT100 terminal that is capable of // reading lines of input. type Terminal struct { + // AutoCompleteCallback, if non-null, is called for each keypress + // with the full input line and the current position of the cursor. + // If it returns a nil newLine, the key press is processed normally. + // Otherwise it returns a replacement line and the new cursor position. + AutoCompleteCallback func(line []byte, pos, key int) (newLine []byte, newPos int) + + // Escape contains a pointer to the escape codes for this terminal. + // It's always a valid pointer, although the escape codes themselves + // may be empty if the terminal doesn't support them. + Escape *EscapeCodes + + // lock protects the terminal and the state in this object from + // concurrent processing of a key press and a Write() call. + lock sync.Mutex + c io.ReadWriter prompt string @@ -16,6 +59,8 @@ type Terminal struct { line []byte // pos is the logical position of the cursor in line pos int + // echo is true if local echo is enabled + echo bool // cursorX contains the current X value of the cursor where the left // edge is 0. cursorY contains the row number where the first row of @@ -40,10 +85,12 @@ type Terminal struct { // "> "). func NewTerminal(c io.ReadWriter, prompt string) *Terminal { return &Terminal{ + Escape: &vt100EscapeCodes, c: c, prompt: prompt, termWidth: 80, termHeight: 24, + echo: true, } } @@ -109,18 +156,11 @@ func bytesToKey(b []byte) (int, []byte) { // queue appends data to the end of t.outBuf func (t *Terminal) queue(data []byte) { - if len(t.outBuf)+len(data) > cap(t.outBuf) { - newOutBuf := make([]byte, len(t.outBuf), 2*(len(t.outBuf)+len(data))) - copy(newOutBuf, t.outBuf) - t.outBuf = newOutBuf - } - - oldLen := len(t.outBuf) - t.outBuf = t.outBuf[:len(t.outBuf)+len(data)] - copy(t.outBuf[oldLen:], data) + t.outBuf = append(t.outBuf, data...) } var eraseUnderCursor = []byte{' ', keyEscape, '[', 'D'} +var space = []byte{' '} func isPrintable(key int) bool { return key >= 32 && key < 127 @@ -129,6 +169,10 @@ func isPrintable(key int) bool { // moveCursorToPos appends data to t.outBuf which will move the cursor to the // given, logical position in the text. func (t *Terminal) moveCursorToPos(pos int) { + if !t.echo { + return + } + x := len(t.prompt) + pos y := x / t.termWidth x = x % t.termWidth @@ -153,6 +197,12 @@ func (t *Terminal) moveCursorToPos(pos int) { right = x - t.cursorX } + t.cursorX = x + t.cursorY = y + t.move(up, down, left, right) +} + +func (t *Terminal) move(up, down, left, right int) { movement := make([]byte, 3*(up+down+left+right)) m := movement for i := 0; i < up; i++ { @@ -180,11 +230,14 @@ func (t *Terminal) moveCursorToPos(pos int) { m = m[3:] } - t.cursorX = x - t.cursorY = y t.queue(movement) } +func (t *Terminal) clearLineToRight() { + op := []byte{keyEscape, '[', 'K'} + t.queue(op) +} + const maxLineLength = 4096 // handleKey processes the given key and, optionally, returns a line of text @@ -196,12 +249,15 @@ func (t *Terminal) handleKey(key int) (line string, ok bool) { return } t.pos-- + t.moveCursorToPos(t.pos) copy(t.line[t.pos:], t.line[1+t.pos:]) t.line = t.line[:len(t.line)-1] - t.writeLine(t.line[t.pos:]) - t.moveCursorToPos(t.pos) + if t.echo { + t.writeLine(t.line[t.pos:]) + } t.queue(eraseUnderCursor) + t.moveCursorToPos(t.pos) case keyAltLeft: // move left by a word. if t.pos == 0 { @@ -260,6 +316,25 @@ func (t *Terminal) handleKey(key int) (line string, ok bool) { t.cursorY = 0 t.maxLine = 0 default: + if t.AutoCompleteCallback != nil { + t.lock.Unlock() + newLine, newPos := t.AutoCompleteCallback(t.line, t.pos, key) + t.lock.Lock() + + if newLine != nil { + if t.echo { + t.moveCursorToPos(0) + t.writeLine(newLine) + for i := len(newLine); i < len(t.line); i++ { + t.writeLine(space) + } + t.moveCursorToPos(newPos) + } + t.line = newLine + t.pos = newPos + return + } + } if !isPrintable(key) { return } @@ -274,7 +349,9 @@ func (t *Terminal) handleKey(key int) (line string, ok bool) { t.line = t.line[:len(t.line)+1] copy(t.line[t.pos+1:], t.line[t.pos:]) t.line[t.pos] = byte(key) - t.writeLine(t.line[t.pos:]) + if t.echo { + t.writeLine(t.line[t.pos:]) + } t.pos++ t.moveCursorToPos(t.pos) } @@ -283,15 +360,6 @@ func (t *Terminal) handleKey(key int) (line string, ok bool) { func (t *Terminal) writeLine(line []byte) { for len(line) != 0 { - if t.cursorX == t.termWidth { - t.queue([]byte("\r\n")) - t.cursorX = 0 - t.cursorY++ - if t.cursorY > t.maxLine { - t.maxLine = t.cursorY - } - } - remainingOnLine := t.termWidth - t.cursorX todo := len(line) if todo > remainingOnLine { @@ -300,16 +368,95 @@ func (t *Terminal) writeLine(line []byte) { t.queue(line[:todo]) t.cursorX += todo line = line[todo:] + + if t.cursorX == t.termWidth { + t.cursorX = 0 + t.cursorY++ + if t.cursorY > t.maxLine { + t.maxLine = t.cursorY + } + } } } func (t *Terminal) Write(buf []byte) (n int, err error) { - return t.c.Write(buf) + t.lock.Lock() + defer t.lock.Unlock() + + if t.cursorX == 0 && t.cursorY == 0 { + // This is the easy case: there's nothing on the screen that we + // have to move out of the way. + return t.c.Write(buf) + } + + // We have a prompt and possibly user input on the screen. We + // have to clear it first. + t.move(0, /* up */ 0, /* down */ t.cursorX, /* left */ 0 /* right */ ) + t.cursorX = 0 + t.clearLineToRight() + + for t.cursorY > 0 { + t.move(1, /* up */ 0, 0, 0) + t.cursorY-- + t.clearLineToRight() + } + + if _, err = t.c.Write(t.outBuf); err != nil { + return + } + t.outBuf = t.outBuf[:0] + + if n, err = t.c.Write(buf); err != nil { + return + } + + t.queue([]byte(t.prompt)) + chars := len(t.prompt) + if t.echo { + t.queue(t.line) + chars += len(t.line) + } + t.cursorX = chars % t.termWidth + t.cursorY = chars / t.termWidth + t.moveCursorToPos(t.pos) + + if _, err = t.c.Write(t.outBuf); err != nil { + return + } + t.outBuf = t.outBuf[:0] + return +} + +// ReadPassword temporarily changes the prompt and reads a password, without +// echo, from the terminal. +func (t *Terminal) ReadPassword(prompt string) (line string, err error) { + t.lock.Lock() + defer t.lock.Unlock() + + oldPrompt := t.prompt + t.prompt = prompt + t.echo = false + + line, err = t.readLine() + + t.prompt = oldPrompt + t.echo = true + + return } // ReadLine returns a line of input from the terminal. func (t *Terminal) ReadLine() (line string, err error) { - if t.cursorX == 0 { + t.lock.Lock() + defer t.lock.Unlock() + + return t.readLine() +} + +func (t *Terminal) readLine() (line string, err error) { + // t.lock must be held at this point + + if t.cursorX == 0 && t.cursorY == 0 { t.writeLine([]byte(t.prompt)) t.c.Write(t.outBuf) t.outBuf = t.outBuf[:0] @@ -320,7 +467,11 @@ func (t *Terminal) ReadLine() (line string, err error) { // containing a partial key sequence readBuf := t.inBuf[len(t.remainder):] var n int + + t.lock.Unlock() n, err = t.c.Read(readBuf) + t.lock.Lock() + if err != nil { return } @@ -358,5 +509,8 @@ func (t *Terminal) ReadLine() (line string, err error) { } func (t *Terminal) SetSize(width, height int) { + t.lock.Lock() + defer t.lock.Unlock() + t.termWidth, t.termHeight = width, height } diff --git a/libgo/go/exp/terminal/terminal_test.go b/libgo/go/exp/terminal/terminal_test.go index a2197210e2a..75628f695e9 100644 --- a/libgo/go/exp/terminal/terminal_test.go +++ b/libgo/go/exp/terminal/terminal_test.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build linux + package terminal import ( diff --git a/libgo/go/exp/terminal/util.go b/libgo/go/exp/terminal/util.go index 03035673869..a5bbfca3b46 100644 --- a/libgo/go/exp/terminal/util.go +++ b/libgo/go/exp/terminal/util.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build linux + // Package terminal provides support functions for dealing with terminals, as // commonly found on UNIX systems. // @@ -9,7 +11,7 @@ // // oldState, err := terminal.MakeRaw(0) // if err != nil { -// panic(err.String()) +// panic(err) // } // defer terminal.Restore(0, oldState) package terminal @@ -17,6 +19,7 @@ package terminal import ( "io" "syscall" + "unsafe" ) // State contains the state of a terminal. @@ -57,6 +60,18 @@ func Restore(fd int, state *State) error { return err } +func ioctl(int, int, unsafe.Pointer) int __asm__("ioctl") + +// GetSize returns the dimensions of the given terminal. +func GetSize(fd int) (width, height int, err error) { + var dimensions [4]uint16 + + if ioctl(fd, syscall.TIOCGWINSZ, unsafe.Pointer(&dimensions)) < 0 { + return -1, -1, syscall.GetErrno() + } + return int(dimensions[1]), int(dimensions[0]), nil +} + // ReadPassword reads a line of input from a terminal without local echo. This // is commonly used for inputting passwords and other sensitive data. The slice // returned does not include the \n. diff --git a/libgo/go/exp/types/gcimporter.go b/libgo/go/exp/types/gcimporter.go index 6adcc2a9ad2..10c56db21ff 100644 --- a/libgo/go/exp/types/gcimporter.go +++ b/libgo/go/exp/types/gcimporter.go @@ -81,7 +81,7 @@ type gcParser struct { func (p *gcParser) init(filename, id string, src io.Reader, imports map[string]*ast.Object) { p.scanner.Init(src) p.scanner.Error = func(_ *scanner.Scanner, msg string) { p.error(msg) } - p.scanner.Mode = scanner.ScanIdents | scanner.ScanInts | scanner.ScanStrings | scanner.ScanComments | scanner.SkipComments + p.scanner.Mode = scanner.ScanIdents | scanner.ScanInts | scanner.ScanChars | scanner.ScanStrings | scanner.ScanComments | scanner.SkipComments p.scanner.Whitespace = 1<<'\t' | 1<<' ' p.scanner.Filename = filename // for good error messages p.next() @@ -145,18 +145,14 @@ func GcImporter(imports map[string]*ast.Object, path string) (pkg *ast.Object, e // Declare inserts a named object of the given kind in scope. func (p *gcParser) declare(scope *ast.Scope, kind ast.ObjKind, name string) *ast.Object { - // a type may have been declared before - if it exists - // already in the respective package scope, return that - // type - if kind == ast.Typ { - if obj := scope.Lookup(name); obj != nil { - assert(obj.Kind == ast.Typ) - return obj - } + // the object may have been imported before - if it exists + // already in the respective package scope, return that object + if obj := scope.Lookup(name); obj != nil { + assert(obj.Kind == kind) + return obj } - // any other object must be a newly declared object - - // create it and insert it into the package scope + // otherwise create a new object and insert it into the package scope obj := ast.NewObj(kind, name) if scope.Insert(obj) != nil { p.errorf("already declared: %v %s", kind, obj.Name) @@ -199,14 +195,15 @@ func (p *gcParser) errorf(format string, args ...interface{}) { func (p *gcParser) expect(tok rune) string { lit := p.lit if p.tok != tok { - p.errorf("expected %q, got %q (%q)", scanner.TokenString(tok), scanner.TokenString(p.tok), lit) + panic(1) + p.errorf("expected %s, got %s (%s)", scanner.TokenString(tok), scanner.TokenString(p.tok), lit) } p.next() return lit } func (p *gcParser) expectSpecial(tok string) { - sep := rune('x') // not white space + sep := 'x' // not white space i := 0 for i < len(tok) && p.tok == rune(tok[i]) && sep > ' ' { sep = p.scanner.Peek() // if sep <= ' ', there is white space before the next token @@ -261,7 +258,7 @@ func (p *gcParser) parsePkgId() *ast.Object { func (p *gcParser) parseDotIdent() string { ident := "" if p.tok != scanner.Int { - sep := rune('x') // not white space + sep := 'x' // not white space for (p.tok == scanner.Ident || p.tok == scanner.Int || p.tok == '·') && sep > ' ' { ident += p.lit sep = p.scanner.Peek() // if sep <= ' ', there is white space before the next token @@ -645,6 +642,7 @@ func (p *gcParser) parseNumber() Const { // Literal = bool_lit | int_lit | float_lit | complex_lit | string_lit . // bool_lit = "true" | "false" . // complex_lit = "(" float_lit "+" float_lit ")" . +// rune_lit = "(" int_lit "+" int_lit ")" . // string_lit = `"` { unicode_char } `"` . // func (p *gcParser) parseConstDecl() { @@ -674,21 +672,33 @@ func (p *gcParser) parseConstDecl() { typ = Float64.Underlying } case '(': - // complex_lit + // complex_lit or rune_lit p.next() + if p.tok == scanner.Char { + p.next() + p.expect('+') + p.parseNumber() + p.expect(')') + // TODO: x = ... + break + } re := p.parseNumber() p.expect('+') im := p.parseNumber() p.expect(')') x = Const{cmplx{re.val.(*big.Rat), im.val.(*big.Rat)}} typ = Complex128.Underlying + case scanner.Char: + // TODO: x = ... + p.next() case scanner.String: // string_lit x = MakeConst(token.STRING, p.lit) p.next() typ = String.Underlying default: - p.error("expected literal") + println(p.tok) + p.errorf("expected literal got %s", scanner.TokenString(p.tok)) } if obj.Type == nil { obj.Type = typ diff --git a/libgo/go/exp/winfsnotify/winfsnotify.go b/libgo/go/exp/winfsnotify/winfsnotify.go index d133740304b..a6e3a6a8fb7 100644 --- a/libgo/go/exp/winfsnotify/winfsnotify.go +++ b/libgo/go/exp/winfsnotify/winfsnotify.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build windows + // Package winfsnotify allows the user to receive // file system event notifications on Windows. package winfsnotify @@ -75,7 +77,7 @@ type Watcher struct { // NewWatcher creates and returns a Watcher. func NewWatcher() (*Watcher, error) { port, e := syscall.CreateIoCompletionPort(syscall.InvalidHandle, 0, 0, 0) - if e != 0 { + if e != nil { return nil, os.NewSyscallError("CreateIoCompletionPort", e) } w := &Watcher{ @@ -147,7 +149,7 @@ func (w *Watcher) RemoveWatch(path string) error { func (w *Watcher) wakeupReader() error { e := syscall.PostQueuedCompletionStatus(w.port, 0, 0, nil) - if e != 0 { + if e != nil { return os.NewSyscallError("PostQueuedCompletionStatus", e) } return nil @@ -155,7 +157,7 @@ func (w *Watcher) wakeupReader() error { func getDir(pathname string) (dir string, err error) { attr, e := syscall.GetFileAttributes(syscall.StringToUTF16Ptr(pathname)) - if e != 0 { + if e != nil { return "", os.NewSyscallError("GetFileAttributes", e) } if attr&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 { @@ -173,11 +175,11 @@ func getIno(path string) (ino *inode, err error) { syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS|syscall.FILE_FLAG_OVERLAPPED, 0) - if e != 0 { + if e != nil { return nil, os.NewSyscallError("CreateFile", e) } var fi syscall.ByHandleFileInformation - if e = syscall.GetFileInformationByHandle(h, &fi); e != 0 { + if e = syscall.GetFileInformationByHandle(h, &fi); e != nil { syscall.CloseHandle(h) return nil, os.NewSyscallError("GetFileInformationByHandle", e) } @@ -222,7 +224,7 @@ func (w *Watcher) addWatch(pathname string, flags uint64) error { } watchEntry := w.watches.get(ino) if watchEntry == nil { - if _, e := syscall.CreateIoCompletionPort(ino.handle, w.port, 0, 0); e != 0 { + if _, e := syscall.CreateIoCompletionPort(ino.handle, w.port, 0, 0); e != nil { syscall.CloseHandle(ino.handle) return os.NewSyscallError("CreateIoCompletionPort", e) } @@ -295,7 +297,7 @@ func (w *Watcher) deleteWatch(watch *watch) { // Must run within the I/O thread. func (w *Watcher) startRead(watch *watch) error { - if e := syscall.CancelIo(watch.ino.handle); e != 0 { + if e := syscall.CancelIo(watch.ino.handle); e != nil { w.Error <- os.NewSyscallError("CancelIo", e) w.deleteWatch(watch) } @@ -304,7 +306,7 @@ func (w *Watcher) startRead(watch *watch) error { mask |= toWindowsFlags(m) } if mask == 0 { - if e := syscall.CloseHandle(watch.ino.handle); e != 0 { + if e := syscall.CloseHandle(watch.ino.handle); e != nil { w.Error <- os.NewSyscallError("CloseHandle", e) } delete(w.watches[watch.ino.volume], watch.ino.index) @@ -312,7 +314,7 @@ func (w *Watcher) startRead(watch *watch) error { } e := syscall.ReadDirectoryChanges(watch.ino.handle, &watch.buf[0], uint32(unsafe.Sizeof(watch.buf)), false, mask, nil, &watch.ov, 0) - if e != 0 { + if e != nil { err := os.NewSyscallError("ReadDirectoryChanges", e) if e == syscall.ERROR_ACCESS_DENIED && watch.mask&provisional == 0 { // Watched directory was probably removed @@ -354,7 +356,7 @@ func (w *Watcher) readEvents() { } } var err error - if e := syscall.CloseHandle(w.port); e != 0 { + if e := syscall.CloseHandle(w.port); e != nil { err = os.NewSyscallError("CloseHandle", e) } close(w.Event) @@ -386,7 +388,7 @@ func (w *Watcher) readEvents() { default: w.Error <- os.NewSyscallError("GetQueuedCompletionPort", e) continue - case 0: + case nil: } var offset uint32 diff --git a/libgo/go/exp/winfsnotify/winfsnotify_test.go b/libgo/go/exp/winfsnotify/winfsnotify_test.go index fb2b825e68e..59ac1624a26 100644 --- a/libgo/go/exp/winfsnotify/winfsnotify_test.go +++ b/libgo/go/exp/winfsnotify/winfsnotify_test.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build windows + package winfsnotify import ( @@ -21,7 +23,7 @@ func expect(t *testing.T, eventstream <-chan *Event, name string, mask uint32) { if event.Name != name || event.Mask != mask { t.Fatal("did not receive expected event") } - case <-time.After(1e9): + case <-time.After(1 * time.Second): t.Fatal("timed out waiting for event") } } @@ -108,7 +110,7 @@ func TestNotifyClose(t *testing.T) { done = true }() - time.Sleep(50e6) // 50 ms + time.Sleep(50 * time.Millisecond) if !done { t.Fatal("double Close() test failed: second Close() call didn't return") } diff --git a/libgo/go/exp/wingui/gui.go b/libgo/go/exp/wingui/gui.go index 5df2ee0faa1..3b79873fa2b 100644 --- a/libgo/go/exp/wingui/gui.go +++ b/libgo/go/exp/wingui/gui.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build windows + package main import ( @@ -18,8 +20,9 @@ func abortf(format string, a ...interface{}) { os.Exit(1) } -func abortErrNo(funcname string, err int) { - abortf("%s failed: %d %s\n", funcname, err, syscall.Errstr(err)) +func abortErrNo(funcname string, err error) { + errno, _ := err.(syscall.Errno) + abortf("%s failed: %d %s\n", funcname, uint32(errno), err) } // global vars @@ -33,7 +36,7 @@ var ( func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintptr) { switch msg { case WM_CREATE: - var e int + var e error // CreateWindowEx bh, e = CreateWindowEx( 0, @@ -42,7 +45,7 @@ func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintpt WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON, 75, 70, 140, 25, hwnd, 1, mh, 0) - if e != 0 { + if e != nil { abortErrNo("CreateWindowEx", e) } fmt.Printf("button handle is %x\n", bh) @@ -51,7 +54,7 @@ func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintpt switch syscall.Handle(lparam) { case bh: e := PostMessage(hwnd, WM_CLOSE, 0, 0) - if e != 0 { + if e != nil { abortErrNo("PostMessage", e) } default: @@ -69,23 +72,23 @@ func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintpt } func rungui() int { - var e int + var e error // GetModuleHandle mh, e = GetModuleHandle(nil) - if e != 0 { + if e != nil { abortErrNo("GetModuleHandle", e) } // Get icon we're going to use. myicon, e := LoadIcon(0, IDI_APPLICATION) - if e != 0 { + if e != nil { abortErrNo("LoadIcon", e) } // Get cursor we're going to use. mycursor, e := LoadCursor(0, IDC_ARROW) - if e != 0 { + if e != nil { abortErrNo("LoadCursor", e) } @@ -104,7 +107,7 @@ func rungui() int { wc.MenuName = nil wc.ClassName = wcname wc.IconSm = myicon - if _, e := RegisterClassEx(&wc); e != 0 { + if _, e := RegisterClassEx(&wc); e != nil { abortErrNo("RegisterClassEx", e) } @@ -116,7 +119,7 @@ func rungui() int { WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, 0, 0, mh, 0) - if e != 0 { + if e != nil { abortErrNo("CreateWindowEx", e) } fmt.Printf("main window handle is %x\n", wh) @@ -125,7 +128,7 @@ func rungui() int { ShowWindow(wh, SW_SHOWDEFAULT) // UpdateWindow - if e := UpdateWindow(wh); e != 0 { + if e := UpdateWindow(wh); e != nil { abortErrNo("UpdateWindow", e) } @@ -133,7 +136,7 @@ func rungui() int { var m Msg for { r, e := GetMessage(&m, 0, 0, 0) - if e != 0 { + if e != nil { abortErrNo("GetMessage", e) } if r == 0 { diff --git a/libgo/go/exp/wingui/winapi.go b/libgo/go/exp/wingui/winapi.go index 08059df2b9d..f876088e91f 100644 --- a/libgo/go/exp/wingui/winapi.go +++ b/libgo/go/exp/wingui/winapi.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build windows + package main import ( @@ -110,22 +112,22 @@ var ( IDI_INFORMATION = IDI_ASTERISK ) -//sys GetModuleHandle(modname *uint16) (handle syscall.Handle, errno int) = GetModuleHandleW -//sys RegisterClassEx(wndclass *Wndclassex) (atom uint16, errno int) = user32.RegisterClassExW -//sys CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent syscall.Handle, menu syscall.Handle, instance syscall.Handle, param uintptr) (hwnd syscall.Handle, errno int) = user32.CreateWindowExW +//sys GetModuleHandle(modname *uint16) (handle syscall.Handle, err error) = GetModuleHandleW +//sys RegisterClassEx(wndclass *Wndclassex) (atom uint16, err error) = user32.RegisterClassExW +//sys CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent syscall.Handle, menu syscall.Handle, instance syscall.Handle, param uintptr) (hwnd syscall.Handle, err error) = user32.CreateWindowExW //sys DefWindowProc(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (lresult uintptr) = user32.DefWindowProcW -//sys DestroyWindow(hwnd syscall.Handle) (errno int) = user32.DestroyWindow +//sys DestroyWindow(hwnd syscall.Handle) (err error) = user32.DestroyWindow //sys PostQuitMessage(exitcode int32) = user32.PostQuitMessage //sys ShowWindow(hwnd syscall.Handle, cmdshow int32) (wasvisible bool) = user32.ShowWindow -//sys UpdateWindow(hwnd syscall.Handle) (errno int) = user32.UpdateWindow -//sys GetMessage(msg *Msg, hwnd syscall.Handle, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, errno int) [failretval==-1] = user32.GetMessageW +//sys UpdateWindow(hwnd syscall.Handle) (err error) = user32.UpdateWindow +//sys GetMessage(msg *Msg, hwnd syscall.Handle, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, err error) [failretval==-1] = user32.GetMessageW //sys TranslateMessage(msg *Msg) (done bool) = user32.TranslateMessage //sys DispatchMessage(msg *Msg) (ret int32) = user32.DispatchMessageW -//sys LoadIcon(instance syscall.Handle, iconname *uint16) (icon syscall.Handle, errno int) = user32.LoadIconW -//sys LoadCursor(instance syscall.Handle, cursorname *uint16) (cursor syscall.Handle, errno int) = user32.LoadCursorW -//sys SetCursor(cursor syscall.Handle) (precursor syscall.Handle, errno int) = user32.SetCursor +//sys LoadIcon(instance syscall.Handle, iconname *uint16) (icon syscall.Handle, err error) = user32.LoadIconW +//sys LoadCursor(instance syscall.Handle, cursorname *uint16) (cursor syscall.Handle, err error) = user32.LoadCursorW +//sys SetCursor(cursor syscall.Handle) (precursor syscall.Handle, err error) = user32.SetCursor //sys SendMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (lresult uintptr) = user32.SendMessageW -//sys PostMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (errno int) = user32.PostMessageW +//sys PostMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (err error) = user32.PostMessageW func MakeIntResource(id uint16) *uint16 { return (*uint16)(unsafe.Pointer(uintptr(id))) diff --git a/libgo/go/exp/wingui/zwinapi.go b/libgo/go/exp/wingui/zwinapi.go index 38e93eea717..5666c6de530 100644 --- a/libgo/go/exp/wingui/zwinapi.go +++ b/libgo/go/exp/wingui/zwinapi.go @@ -1,3 +1,4 @@ +// +build windows // mksyscall_windows.pl winapi.go // MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT @@ -28,47 +29,41 @@ var ( procPostMessageW = moduser32.NewProc("PostMessageW") ) -func GetModuleHandle(modname *uint16) (handle syscall.Handle, errno int) { +func GetModuleHandle(modname *uint16) (handle syscall.Handle, err error) { r0, _, e1 := syscall.Syscall(procGetModuleHandleW.Addr(), 1, uintptr(unsafe.Pointer(modname)), 0, 0) handle = syscall.Handle(r0) if handle == 0 { if e1 != 0 { - errno = int(e1) + err = error(e1) } else { - errno = syscall.EINVAL + err = syscall.EINVAL } - } else { - errno = 0 } return } -func RegisterClassEx(wndclass *Wndclassex) (atom uint16, errno int) { +func RegisterClassEx(wndclass *Wndclassex) (atom uint16, err error) { r0, _, e1 := syscall.Syscall(procRegisterClassExW.Addr(), 1, uintptr(unsafe.Pointer(wndclass)), 0, 0) atom = uint16(r0) if atom == 0 { if e1 != 0 { - errno = int(e1) + err = error(e1) } else { - errno = syscall.EINVAL + err = syscall.EINVAL } - } else { - errno = 0 } return } -func CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent syscall.Handle, menu syscall.Handle, instance syscall.Handle, param uintptr) (hwnd syscall.Handle, errno int) { +func CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent syscall.Handle, menu syscall.Handle, instance syscall.Handle, param uintptr) (hwnd syscall.Handle, err error) { r0, _, e1 := syscall.Syscall12(procCreateWindowExW.Addr(), 12, uintptr(exstyle), uintptr(unsafe.Pointer(classname)), uintptr(unsafe.Pointer(windowname)), uintptr(style), uintptr(x), uintptr(y), uintptr(width), uintptr(height), uintptr(wndparent), uintptr(menu), uintptr(instance), uintptr(param)) hwnd = syscall.Handle(r0) if hwnd == 0 { if e1 != 0 { - errno = int(e1) + err = error(e1) } else { - errno = syscall.EINVAL + err = syscall.EINVAL } - } else { - errno = 0 } return } @@ -79,16 +74,14 @@ func DefWindowProc(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintp return } -func DestroyWindow(hwnd syscall.Handle) (errno int) { +func DestroyWindow(hwnd syscall.Handle) (err error) { r1, _, e1 := syscall.Syscall(procDestroyWindow.Addr(), 1, uintptr(hwnd), 0, 0) if int(r1) == 0 { if e1 != 0 { - errno = int(e1) + err = error(e1) } else { - errno = syscall.EINVAL + err = syscall.EINVAL } - } else { - errno = 0 } return } @@ -104,31 +97,27 @@ func ShowWindow(hwnd syscall.Handle, cmdshow int32) (wasvisible bool) { return } -func UpdateWindow(hwnd syscall.Handle) (errno int) { +func UpdateWindow(hwnd syscall.Handle) (err error) { r1, _, e1 := syscall.Syscall(procUpdateWindow.Addr(), 1, uintptr(hwnd), 0, 0) if int(r1) == 0 { if e1 != 0 { - errno = int(e1) + err = error(e1) } else { - errno = syscall.EINVAL + err = syscall.EINVAL } - } else { - errno = 0 } return } -func GetMessage(msg *Msg, hwnd syscall.Handle, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, errno int) { +func GetMessage(msg *Msg, hwnd syscall.Handle, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, err error) { r0, _, e1 := syscall.Syscall6(procGetMessageW.Addr(), 4, uintptr(unsafe.Pointer(msg)), uintptr(hwnd), uintptr(MsgFilterMin), uintptr(MsgFilterMax), 0, 0) ret = int32(r0) if ret == -1 { if e1 != 0 { - errno = int(e1) + err = error(e1) } else { - errno = syscall.EINVAL + err = syscall.EINVAL } - } else { - errno = 0 } return } @@ -145,47 +134,41 @@ func DispatchMessage(msg *Msg) (ret int32) { return } -func LoadIcon(instance syscall.Handle, iconname *uint16) (icon syscall.Handle, errno int) { +func LoadIcon(instance syscall.Handle, iconname *uint16) (icon syscall.Handle, err error) { r0, _, e1 := syscall.Syscall(procLoadIconW.Addr(), 2, uintptr(instance), uintptr(unsafe.Pointer(iconname)), 0) icon = syscall.Handle(r0) if icon == 0 { if e1 != 0 { - errno = int(e1) + err = error(e1) } else { - errno = syscall.EINVAL + err = syscall.EINVAL } - } else { - errno = 0 } return } -func LoadCursor(instance syscall.Handle, cursorname *uint16) (cursor syscall.Handle, errno int) { +func LoadCursor(instance syscall.Handle, cursorname *uint16) (cursor syscall.Handle, err error) { r0, _, e1 := syscall.Syscall(procLoadCursorW.Addr(), 2, uintptr(instance), uintptr(unsafe.Pointer(cursorname)), 0) cursor = syscall.Handle(r0) if cursor == 0 { if e1 != 0 { - errno = int(e1) + err = error(e1) } else { - errno = syscall.EINVAL + err = syscall.EINVAL } - } else { - errno = 0 } return } -func SetCursor(cursor syscall.Handle) (precursor syscall.Handle, errno int) { +func SetCursor(cursor syscall.Handle) (precursor syscall.Handle, err error) { r0, _, e1 := syscall.Syscall(procSetCursor.Addr(), 1, uintptr(cursor), 0, 0) precursor = syscall.Handle(r0) if precursor == 0 { if e1 != 0 { - errno = int(e1) + err = error(e1) } else { - errno = syscall.EINVAL + err = syscall.EINVAL } - } else { - errno = 0 } return } @@ -196,16 +179,14 @@ func SendMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr return } -func PostMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (errno int) { +func PostMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (err error) { r1, _, e1 := syscall.Syscall6(procPostMessageW.Addr(), 4, uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam), 0, 0) if int(r1) == 0 { if e1 != 0 { - errno = int(e1) + err = error(e1) } else { - errno = syscall.EINVAL + err = syscall.EINVAL } - } else { - errno = 0 } return } diff --git a/libgo/go/expvar/expvar.go b/libgo/go/expvar/expvar.go index 40f5441ddcb..5ced285804c 100644 --- a/libgo/go/expvar/expvar.go +++ b/libgo/go/expvar/expvar.go @@ -193,7 +193,7 @@ func (f Func) String() string { var vars map[string]Var = make(map[string]Var) var mutex sync.Mutex -// Publish declares an named exported variable. This should be called from a +// Publish declares a named exported variable. This should be called from a // package's init function when it creates its Vars. If the name is already // registered then this will log.Panic. func Publish(name string, v Var) { diff --git a/libgo/go/fmt/fmt_test.go b/libgo/go/fmt/fmt_test.go index 63c33380a25..d34a4f8fd2d 100644 --- a/libgo/go/fmt/fmt_test.go +++ b/libgo/go/fmt/fmt_test.go @@ -506,78 +506,42 @@ func BenchmarkSprintfFloat(b *testing.B) { } } +var mallocBuf bytes.Buffer + +var mallocTest = []struct { + count int + desc string + fn func() +}{ + {0, `Sprintf("")`, func() { Sprintf("") }}, + {1, `Sprintf("xxx")`, func() { Sprintf("xxx") }}, + {1, `Sprintf("%x")`, func() { Sprintf("%x", 7) }}, + {2, `Sprintf("%s")`, func() { Sprintf("%s", "hello") }}, + {1, `Sprintf("%x %x")`, func() { Sprintf("%x", 7, 112) }}, + {1, `Sprintf("%g")`, func() { Sprintf("%g", 3.14159) }}, + {0, `Fprintf(buf, "%x %x %x")`, func() { mallocBuf.Reset(); Fprintf(&mallocBuf, "%x %x %x", 7, 8, 9) }}, + {1, `Fprintf(buf, "%s")`, func() { mallocBuf.Reset(); Fprintf(&mallocBuf, "%s", "hello") }}, +} + +var _ bytes.Buffer + func TestCountMallocs(t *testing.T) { if testing.Short() { return } - const N = 100 - runtime.UpdateMemStats() - mallocs := 0 - runtime.MemStats.Mallocs - for i := 0; i < N; i++ { - Sprintf("") - } - runtime.UpdateMemStats() - mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"\"): %d\n", mallocs/N) - runtime.UpdateMemStats() - mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < N; i++ { - Sprintf("xxx") - } - runtime.UpdateMemStats() - mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"xxx\"): %d\n", mallocs/N) - runtime.UpdateMemStats() - mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < N; i++ { - Sprintf("%x", i) - } - runtime.UpdateMemStats() - mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/N) - runtime.UpdateMemStats() - mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < N; i++ { - Sprintf("%s", "hello") - } - runtime.UpdateMemStats() - mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"%%s\"): %d\n", mallocs/N) - runtime.UpdateMemStats() - mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < N; i++ { - Sprintf("%x %x", i, i) - } - runtime.UpdateMemStats() - mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"%%x %%x\"): %d\n", mallocs/N) - runtime.UpdateMemStats() - mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < N; i++ { - Sprintf("%g", 3.14159) - } - runtime.UpdateMemStats() - mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"%%g\"): %d\n", mallocs/N) - buf := new(bytes.Buffer) - runtime.UpdateMemStats() - mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < N; i++ { - buf.Reset() - Fprintf(buf, "%x %x %x", i, i, i) - } - runtime.UpdateMemStats() - mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Fprintf(buf, \"%%x %%x %%x\"): %d\n", mallocs/N) - runtime.UpdateMemStats() - mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < N; i++ { - buf.Reset() - Fprintf(buf, "%s", "hello") + for _, mt := range mallocTest { + const N = 100 + runtime.UpdateMemStats() + mallocs := 0 - runtime.MemStats.Mallocs + for i := 0; i < N; i++ { + mt.fn() + } + runtime.UpdateMemStats() + mallocs += runtime.MemStats.Mallocs + if mallocs/N != uint64(mt.count) { + t.Errorf("%s: expected %d mallocs, got %d", mt.desc, mt.count, mallocs/N) + } } - runtime.UpdateMemStats() - mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Fprintf(buf, \"%%s\"): %d\n", mallocs/N) } type flagPrinter struct{} @@ -813,3 +777,37 @@ func TestPanics(t *testing.T) { } } } + +// Test that erroneous String routine doesn't cause fatal recursion. +var recurCount = 0 + +type Recur struct { + i int + failed *bool +} + +func (r Recur) String() string { + if recurCount++; recurCount > 10 { + *r.failed = true + return "FAIL" + } + // This will call badVerb. Before the fix, that would cause us to recur into + // this routine to print %!p(value). Now we don't call the user's method + // during an error. + return Sprintf("recur@%p value: %d", r, r.i) +} + +func TestBadVerbRecursion(t *testing.T) { + failed := false + r := Recur{3, &failed} + Sprintf("recur@%p value: %d\n", &r, r.i) + if failed { + t.Error("fail with pointer") + } + failed = false + r = Recur{4, &failed} + Sprintf("recur@%p, value: %d\n", r, r.i) + if failed { + t.Error("fail with value") + } +} diff --git a/libgo/go/fmt/format.go b/libgo/go/fmt/format.go index fbafa9d9ad9..78d9e998b1f 100644 --- a/libgo/go/fmt/format.go +++ b/libgo/go/fmt/format.go @@ -154,12 +154,17 @@ func putint(buf []byte, base, val uint64, digits string) int { return i - 1 } +var ( + trueBytes = []byte("true") + falseBytes = []byte("false") +) + // fmt_boolean formats a boolean. func (f *fmt) fmt_boolean(v bool) { if v { - f.padString("true") + f.pad(trueBytes) } else { - f.padString("false") + f.pad(falseBytes) } } @@ -283,31 +288,18 @@ func (f *fmt) fmt_s(s string) { } // fmt_sx formats a string as a hexadecimal encoding of its bytes. -func (f *fmt) fmt_sx(s string) { - t := "" +func (f *fmt) fmt_sx(s, digits string) { + // TODO: Avoid buffer by pre-padding. + var b bytes.Buffer for i := 0; i < len(s); i++ { if i > 0 && f.space { - t += " " + b.WriteByte(' ') } v := s[i] - t += string(ldigits[v>>4]) - t += string(ldigits[v&0xF]) + b.WriteByte(digits[v>>4]) + b.WriteByte(digits[v&0xF]) } - f.padString(t) -} - -// fmt_sX formats a string as an uppercase hexadecimal encoding of its bytes. -func (f *fmt) fmt_sX(s string) { - t := "" - for i := 0; i < len(s); i++ { - if i > 0 && f.space { - t += " " - } - v := s[i] - t += string(udigits[v>>4]) - t += string(udigits[v&0xF]) - } - f.padString(t) + f.pad(b.Bytes()) } // fmt_q formats a string as a double-quoted, escaped Go string constant. @@ -329,13 +321,13 @@ func (f *fmt) fmt_q(s string) { // fmt_qc formats the integer as a single-quoted, escaped Go character constant. // If the character is not valid Unicode, it will print '\ufffd'. func (f *fmt) fmt_qc(c int64) { - var quoted string + var quoted []byte if f.plus { - quoted = strconv.QuoteRuneToASCII(int(c)) + quoted = strconv.AppendQuoteRuneToASCII(f.intbuf[0:0], rune(c)) } else { - quoted = strconv.QuoteRune(int(c)) + quoted = strconv.AppendQuoteRune(f.intbuf[0:0], rune(c)) } - f.padString(quoted) + f.pad(quoted) } // floating-point @@ -347,57 +339,70 @@ func doPrec(f *fmt, def int) int { return def } -// Add a plus sign or space to the floating-point string representation if missing and required. -func (f *fmt) plusSpace(s string) { - if s[0] != '-' { +// formatFloat formats a float64; it is an efficient equivalent to f.pad(strconv.FormatFloat()...). +func (f *fmt) formatFloat(v float64, verb byte, prec, n int) { + // We leave one byte at the beginning of f.intbuf for a sign if needed, + // and make it a space, which we might be able to use. + f.intbuf[0] = ' ' + slice := strconv.AppendFloat(f.intbuf[0:1], v, verb, prec, n) + // Add a plus sign or space to the floating-point string representation if missing and required. + // The formatted number starts at slice[1]. + switch slice[1] { + case '-', '+': + // We're set; drop the leading space. + slice = slice[1:] + default: + // There's no sign, but we might need one. if f.plus { - s = "+" + s + slice[0] = '+' } else if f.space { - s = " " + s + // space is already there + } else { + slice = slice[1:] } } - f.padString(s) + f.pad(slice) } // fmt_e64 formats a float64 in the form -1.23e+12. -func (f *fmt) fmt_e64(v float64) { f.plusSpace(strconv.FormatFloat(v, 'e', doPrec(f, 6), 64)) } +func (f *fmt) fmt_e64(v float64) { f.formatFloat(v, 'e', doPrec(f, 6), 64) } // fmt_E64 formats a float64 in the form -1.23E+12. -func (f *fmt) fmt_E64(v float64) { f.plusSpace(strconv.FormatFloat(v, 'E', doPrec(f, 6), 64)) } +func (f *fmt) fmt_E64(v float64) { f.formatFloat(v, 'E', doPrec(f, 6), 64) } // fmt_f64 formats a float64 in the form -1.23. -func (f *fmt) fmt_f64(v float64) { f.plusSpace(strconv.FormatFloat(v, 'f', doPrec(f, 6), 64)) } +func (f *fmt) fmt_f64(v float64) { f.formatFloat(v, 'f', doPrec(f, 6), 64) } // fmt_g64 formats a float64 in the 'f' or 'e' form according to size. -func (f *fmt) fmt_g64(v float64) { f.plusSpace(strconv.FormatFloat(v, 'g', doPrec(f, -1), 64)) } +func (f *fmt) fmt_g64(v float64) { f.formatFloat(v, 'g', doPrec(f, -1), 64) } // fmt_g64 formats a float64 in the 'f' or 'E' form according to size. -func (f *fmt) fmt_G64(v float64) { f.plusSpace(strconv.FormatFloat(v, 'G', doPrec(f, -1), 64)) } +func (f *fmt) fmt_G64(v float64) { f.formatFloat(v, 'G', doPrec(f, -1), 64) } // fmt_fb64 formats a float64 in the form -123p3 (exponent is power of 2). -func (f *fmt) fmt_fb64(v float64) { f.plusSpace(strconv.FormatFloat(v, 'b', 0, 64)) } +func (f *fmt) fmt_fb64(v float64) { f.formatFloat(v, 'b', 0, 64) } // float32 // cannot defer to float64 versions // because it will get rounding wrong in corner cases. // fmt_e32 formats a float32 in the form -1.23e+12. -func (f *fmt) fmt_e32(v float32) { f.plusSpace(strconv.FormatFloat(float64(v), 'e', doPrec(f, 6), 32)) } +func (f *fmt) fmt_e32(v float32) { f.formatFloat(float64(v), 'e', doPrec(f, 6), 32) } // fmt_E32 formats a float32 in the form -1.23E+12. -func (f *fmt) fmt_E32(v float32) { f.plusSpace(strconv.FormatFloat(float64(v), 'E', doPrec(f, 6), 32)) } +func (f *fmt) fmt_E32(v float32) { f.formatFloat(float64(v), 'E', doPrec(f, 6), 32) } // fmt_f32 formats a float32 in the form -1.23. -func (f *fmt) fmt_f32(v float32) { f.plusSpace(strconv.FormatFloat(float64(v), 'f', doPrec(f, 6), 32)) } +func (f *fmt) fmt_f32(v float32) { f.formatFloat(float64(v), 'f', doPrec(f, 6), 32) } // fmt_g32 formats a float32 in the 'f' or 'e' form according to size. -func (f *fmt) fmt_g32(v float32) { f.plusSpace(strconv.FormatFloat(float64(v), 'g', doPrec(f, -1), 32)) } +func (f *fmt) fmt_g32(v float32) { f.formatFloat(float64(v), 'g', doPrec(f, -1), 32) } // fmt_G32 formats a float32 in the 'f' or 'E' form according to size. -func (f *fmt) fmt_G32(v float32) { f.plusSpace(strconv.FormatFloat(float64(v), 'G', doPrec(f, -1), 32)) } +func (f *fmt) fmt_G32(v float32) { f.formatFloat(float64(v), 'G', doPrec(f, -1), 32) } // fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2). -func (f *fmt) fmt_fb32(v float32) { f.padString(strconv.FormatFloat(float64(v), 'b', 0, 32)) } +func (f *fmt) fmt_fb32(v float32) { f.formatFloat(float64(v), 'b', 0, 32) } // fmt_c64 formats a complex64 according to the verb. func (f *fmt) fmt_c64(v complex64, verb rune) { diff --git a/libgo/go/fmt/print.go b/libgo/go/fmt/print.go index 8b15a82e773..3b7d3464e20 100644 --- a/libgo/go/fmt/print.go +++ b/libgo/go/fmt/print.go @@ -74,6 +74,7 @@ type GoStringer interface { type pp struct { n int panicking bool + erroring bool // printing an error condition buf bytes.Buffer // field holds the current item, as an interface{}. field interface{} @@ -124,6 +125,7 @@ var ppFree = newCache(func() interface{} { return new(pp) }) func newPrinter() *pp { p := ppFree.get().(*pp) p.panicking = false + p.erroring = false p.fmt.init(&p.buf) return p } @@ -299,6 +301,7 @@ func (p *pp) unknownType(v interface{}) { } func (p *pp) badVerb(verb rune) { + p.erroring = true p.add('%') p.add('!') p.add(verb) @@ -316,6 +319,7 @@ func (p *pp) badVerb(verb rune) { p.buf.Write(nilAngleBytes) } p.add(')') + p.erroring = false } func (p *pp) fmtBool(v bool, verb rune) { @@ -499,9 +503,9 @@ func (p *pp) fmtString(v string, verb rune, goSyntax bool) { case 's': p.fmt.fmt_s(v) case 'x': - p.fmt.fmt_sx(v) + p.fmt.fmt_sx(v, ldigits) case 'X': - p.fmt.fmt_sX(v) + p.fmt.fmt_sx(v, udigits) case 'q': p.fmt.fmt_q(v) default: @@ -538,9 +542,9 @@ func (p *pp) fmtBytes(v []byte, verb rune, goSyntax bool, depth int) { case 's': p.fmt.fmt_s(s) case 'x': - p.fmt.fmt_sx(s) + p.fmt.fmt_sx(s, ldigits) case 'X': - p.fmt.fmt_sX(s) + p.fmt.fmt_sx(s, udigits) case 'q': p.fmt.fmt_q(s) default: @@ -606,6 +610,9 @@ func (p *pp) catchPanic(field interface{}, verb rune) { } func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString, handled bool) { + if p.erroring { + return + } // Is it a Formatter? if formatter, ok := p.field.(Formatter); ok { handled = true diff --git a/libgo/go/fmt/scan_test.go b/libgo/go/fmt/scan_test.go index 0689bf3b6e4..b26c828cbfc 100644 --- a/libgo/go/fmt/scan_test.go +++ b/libgo/go/fmt/scan_test.go @@ -56,6 +56,7 @@ var ( stringVal string stringVal1 string bytesVal []byte + runeVal rune complex64Val complex64 complex128Val complex128 renamedBoolVal renamedBool @@ -225,9 +226,9 @@ var scanfTests = []ScanfTest{ {"%v", "0377\n", &intVal, 0377}, {"%v", "0x44\n", &intVal, 0x44}, {"%d", "72\n", &intVal, 72}, - {"%c", "a\n", &intVal, 'a'}, - {"%c", "\u5072\n", &intVal, 0x5072}, - {"%c", "\u1234\n", &intVal, '\u1234'}, + {"%c", "a\n", &runeVal, 'a'}, + {"%c", "\u5072\n", &runeVal, '\u5072'}, + {"%c", "\u1234\n", &runeVal, '\u1234'}, {"%d", "73\n", &int8Val, int8(73)}, {"%d", "+74\n", &int16Val, int16(74)}, {"%d", "75\n", &int32Val, int32(75)}, @@ -322,6 +323,7 @@ var s, t string var c complex128 var x, y Xs var z IntString +var r1, r2, r3 rune var multiTests = []ScanfMultiTest{ {"", "", []interface{}{}, []interface{}{}, ""}, @@ -333,7 +335,7 @@ var multiTests = []ScanfMultiTest{ {"%3d22%3d", "33322333", args(&i, &j), args(333, 333), ""}, {"%6vX=%3fY", "3+2iX=2.5Y", args(&c, &f), args((3 + 2i), 2.5), ""}, {"%d%s", "123abc", args(&i, &s), args(123, "abc"), ""}, - {"%c%c%c", "2\u50c2X", args(&i, &j, &k), args('2', '\u50c2', 'X'), ""}, + {"%c%c%c", "2\u50c2X", args(&r1, &r2, &r3), args('2', '\u50c2', 'X'), ""}, // Custom scanners. {"%e%f", "eefffff", args(&x, &y), args(Xs("ee"), Xs("fffff")), ""}, @@ -347,7 +349,7 @@ var multiTests = []ScanfMultiTest{ {"X%d", "10X", args(&intVal), nil, "input does not match format"}, // Bad UTF-8: should see every byte. - {"%c%c%c", "\xc2X\xc2", args(&i, &j, &k), args(utf8.RuneError, 'X', utf8.RuneError), ""}, + {"%c%c%c", "\xc2X\xc2", args(&r1, &r2, &r3), args(utf8.RuneError, 'X', utf8.RuneError), ""}, } func testScan(name string, t *testing.T, scan func(r io.Reader, a ...interface{}) (int, error)) { diff --git a/libgo/go/go/ast/scope.go b/libgo/go/go/ast/scope.go index 92e36698081..fbe4779671e 100644 --- a/libgo/go/go/ast/scope.go +++ b/libgo/go/go/ast/scope.go @@ -80,7 +80,7 @@ func (s *Scope) String() string { type Object struct { Kind ObjKind Name string // declared name - Decl interface{} // corresponding Field, XxxSpec, FuncDecl, or LabeledStmt; or nil + Decl interface{} // corresponding Field, XxxSpec, FuncDecl, LabeledStmt, or AssignStmt; or nil Data interface{} // object-specific data; or nil Type interface{} // place holder for type information; may be nil } @@ -125,6 +125,12 @@ func (obj *Object) Pos() token.Pos { if d.Label.Name == name { return d.Label.Pos() } + case *AssignStmt: + for _, x := range d.Lhs { + if ident, isIdent := x.(*Ident); isIdent && ident.Name == name { + return ident.Pos() + } + } } return token.NoPos } diff --git a/libgo/go/go/build/build_test.go b/libgo/go/go/build/build_test.go index e22a49aa3d6..fd4030632a9 100644 --- a/libgo/go/go/build/build_test.go +++ b/libgo/go/go/build/build_test.go @@ -46,8 +46,9 @@ var buildPkgs = []struct { { "go/build/cgotest", &DirInfo{ - CgoFiles: []string{"cgotest.go"}, + CgoFiles: ifCgo([]string{"cgotest.go"}), CFiles: []string{"cgotest.c"}, + HFiles: []string{"cgotest.h"}, Imports: []string{"C", "unsafe"}, TestImports: []string{}, Package: "cgotest", @@ -55,6 +56,13 @@ var buildPkgs = []struct { }, } +func ifCgo(x []string) []string { + if DefaultContext.CgoEnabled { + return x + } + return nil +} + const cmdtestOutput = "3" func TestBuild(t *testing.T) { @@ -71,6 +79,10 @@ func TestBuild(t *testing.T) { continue } + if tt.dir == "go/build/cgotest" && len(info.CgoFiles) == 0 { + continue + } + s, err := Build(tree, tt.dir, info) if err != nil { t.Errorf("Build(%#q): %v", tt.dir, err) diff --git a/libgo/go/go/build/dir.go b/libgo/go/go/build/dir.go index 12dc99942a7..265261f22ea 100644 --- a/libgo/go/go/build/dir.go +++ b/libgo/go/go/build/dir.go @@ -26,9 +26,9 @@ import ( // A Context specifies the supporting context for a build. type Context struct { - GOARCH string // target architecture - GOOS string // target operating system - // TODO(rsc,adg): GOPATH + GOARCH string // target architecture + GOOS string // target operating system + CgoEnabled bool // whether cgo can be used // By default, ScanDir uses the operating system's // file system calls to read directories and files. @@ -75,9 +75,36 @@ func (ctxt *Context) readFile(dir, file string) (string, []byte, error) { // The DefaultContext is the default Context for builds. // It uses the GOARCH and GOOS environment variables // if set, or else the compiled code's GOARCH and GOOS. -var DefaultContext = Context{ - GOARCH: envOr("GOARCH", runtime.GOARCH), - GOOS: envOr("GOOS", runtime.GOOS), +var DefaultContext = defaultContext() + +var cgoEnabled = map[string]bool{ + "darwin/386": true, + "darwin/amd64": true, + "linux/386": true, + "linux/amd64": true, + "freebsd/386": true, + "freebsd/amd64": true, + "windows/386": true, + "windows/amd64": true, +} + +func defaultContext() Context { + var c Context + + c.GOARCH = envOr("GOARCH", runtime.GOARCH) + c.GOOS = envOr("GOOS", runtime.GOOS) + + s := os.Getenv("CGO_ENABLED") + switch s { + case "1": + c.CgoEnabled = true + case "0": + c.CgoEnabled = false + default: + c.CgoEnabled = cgoEnabled[c.GOOS+"/"+c.GOARCH] + } + + return c } func envOr(name, def string) string { @@ -96,8 +123,9 @@ type DirInfo struct { // Source files GoFiles []string // .go files in dir (excluding CgoFiles) + HFiles []string // .h files in dir CFiles []string // .c files in dir - SFiles []string // .s files in dir + SFiles []string // .s (and, when using cgo, .S files in dir) CgoFiles []string // .go files that import "C" // Cgo directives @@ -135,6 +163,7 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) { return nil, err } + var Sfiles []string // files with ".S" (capital S) var di DirInfo imported := make(map[string]bool) testImported := make(map[string]bool) @@ -154,7 +183,7 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) { ext := path.Ext(name) switch ext { - case ".go", ".c", ".s": + case ".go", ".c", ".s", ".h", ".S": // tentatively okay default: // skip @@ -175,9 +204,15 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) { case ".c": di.CFiles = append(di.CFiles, name) continue + case ".h": + di.HFiles = append(di.HFiles, name) + continue case ".s": di.SFiles = append(di.SFiles, name) continue + case ".S": + Sfiles = append(Sfiles, name) + continue } pf, err := parser.ParseFile(fset, filename, data, parser.ImportsOnly|parser.ParseComments) @@ -256,7 +291,9 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) { } } if isCgo { - di.CgoFiles = append(di.CgoFiles, name) + if ctxt.CgoEnabled { + di.CgoFiles = append(di.CgoFiles, name) + } } else if isTest { if pkg == string(pf.Name.Name) { di.TestGoFiles = append(di.TestGoFiles, name) @@ -282,6 +319,15 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) { di.TestImports[i] = p i++ } + + // add the .S files only if we are using cgo + // (which means gcc will compile them). + // The standard assemblers expect .s files. + if len(di.CgoFiles) > 0 { + di.SFiles = append(di.SFiles, Sfiles...) + sort.Strings(di.SFiles) + } + // File name lists are sorted because ReadDir sorts. sort.Strings(di.Imports) sort.Strings(di.TestImports) @@ -289,7 +335,6 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) { } var slashslash = []byte("//") -var plusBuild = []byte("+build") // shouldBuild reports whether it is okay to use this file, // The rule is that in the file's leading run of // comments @@ -466,7 +511,7 @@ func splitQuoted(s string) (r []string, err error) { arg := make([]rune, len(s)) escaped := false quoted := false - quote := rune(0) + quote := '\x00' i := 0 for _, rune := range s { switch { @@ -475,9 +520,9 @@ func splitQuoted(s string) (r []string, err error) { case rune == '\\': escaped = true continue - case quote != 0: + case quote != '\x00': if rune == quote { - quote = 0 + quote = '\x00' continue } case rune == '"' || rune == '\'': @@ -510,14 +555,22 @@ func splitQuoted(s string) (r []string, err error) { // // $GOOS // $GOARCH -// $GOOS/$GOARCH +// cgo (if cgo is enabled) +// nocgo (if cgo is disabled) +// a slash-separated list of any of these // func (ctxt *Context) matchOSArch(name string) bool { + if ctxt.CgoEnabled && name == "cgo" { + return true + } + if !ctxt.CgoEnabled && name == "nocgo" { + return true + } if name == ctxt.GOOS || name == ctxt.GOARCH { return true } i := strings.Index(name, "/") - return i >= 0 && name[:i] == ctxt.GOOS && name[i+1:] == ctxt.GOARCH + return i >= 0 && ctxt.matchOSArch(name[:i]) && ctxt.matchOSArch(name[i+1:]) } // goodOSArchFile returns false if the name contains a $GOOS or $GOARCH diff --git a/libgo/go/go/build/path.go b/libgo/go/go/build/path.go index 91d6c430a9d..7a281800c28 100644 --- a/libgo/go/go/build/path.go +++ b/libgo/go/go/build/path.go @@ -57,7 +57,7 @@ func (t *Tree) PkgDir() string { func (t *Tree) BinDir() string { if t.Goroot { if gobin := os.Getenv("GOBIN"); gobin != "" { - return gobin + return filepath.Clean(gobin) } } return filepath.Join(t.Path, "bin") @@ -85,8 +85,8 @@ func (t *Tree) HasPkg(pkg string) bool { } var ( - ErrNotFound = errors.New("go/build: package could not be found locally") - ErrTreeNotFound = errors.New("go/build: no valid GOROOT or GOPATH could be found") + ErrNotFound = errors.New("package could not be found locally") + ErrTreeNotFound = errors.New("no valid GOROOT or GOPATH could be found") ) // FindTree takes an import or filesystem path and returns the @@ -151,7 +151,7 @@ func init() { root := runtime.GOROOT() t, err := newTree(root) if err != nil { - log.Printf("go/build: invalid GOROOT %q: %v", root, err) + log.Printf("invalid GOROOT %q: %v", root, err) } else { t.Goroot = true Path = []*Tree{t} @@ -163,7 +163,7 @@ func init() { } t, err := newTree(p) if err != nil { - log.Printf("go/build: invalid GOPATH %q: %v", p, err) + log.Printf("invalid GOPATH %q: %v", p, err) continue } Path = append(Path, t) diff --git a/libgo/go/go/doc/comment.go b/libgo/go/go/doc/comment.go index c9fb55bd54e..39f34afa10c 100644 --- a/libgo/go/go/doc/comment.go +++ b/libgo/go/go/doc/comment.go @@ -281,7 +281,20 @@ func heading(line string) string { return line } -// Convert comment text to formatted HTML. +type op int + +const ( + opPara op = iota + opHead + opPre +) + +type block struct { + op op + lines []string +} + +// ToHTML converts comment text to formatted HTML. // The comment was prepared by DocReader, // so it is known not to have leading, trailing blank lines // nor to have trailing spaces at the end of lines. @@ -299,20 +312,43 @@ func heading(line string) string { // map value is not the empty string, it is considered a URL and the word is converted // into a link. func ToHTML(w io.Writer, text string, words map[string]string) { - inpara := false - lastWasBlank := false - lastWasHeading := false - - close := func() { - if inpara { + for _, b := range blocks(text) { + switch b.op { + case opPara: + w.Write(html_p) + for _, line := range b.lines { + emphasize(w, line, words, true) + } w.Write(html_endp) - inpara = false + case opHead: + w.Write(html_h) + for _, line := range b.lines { + commentEscape(w, line, true) + } + w.Write(html_endh) + case opPre: + w.Write(html_pre) + for _, line := range b.lines { + emphasize(w, line, nil, false) + } + w.Write(html_endpre) } } - open := func() { - if !inpara { - w.Write(html_p) - inpara = true +} + +func blocks(text string) []block { + var ( + out []block + para []string + + lastWasBlank = false + lastWasHeading = false + ) + + close := func() { + if para != nil { + out = append(out, block{opPara, para}) + para = nil } } @@ -340,17 +376,13 @@ func ToHTML(w io.Writer, text string, words map[string]string) { for j > i && isBlank(lines[j-1]) { j-- } - block := lines[i:j] + pre := lines[i:j] i = j - unindent(block) + unindent(pre) // put those lines in a pre block - w.Write(html_pre) - for _, line := range block { - emphasize(w, line, nil, false) // no nice text formatting - } - w.Write(html_endpre) + out = append(out, block{opPre, pre}) lastWasHeading = false continue } @@ -362,9 +394,7 @@ func ToHTML(w io.Writer, text string, words map[string]string) { // might be a heading. if head := heading(line); head != "" { close() - w.Write(html_h) - commentEscape(w, head, true) // nice text formatting - w.Write(html_endh) + out = append(out, block{opHead, []string{head}}) i += 2 lastWasHeading = true continue @@ -372,11 +402,95 @@ func ToHTML(w io.Writer, text string, words map[string]string) { } // open paragraph - open() lastWasBlank = false lastWasHeading = false - emphasize(w, lines[i], words, true) // nice text formatting + para = append(para, lines[i]) i++ } close() + + return out +} + +// ToText prepares comment text for presentation in textual output. +// It wraps paragraphs of text to width or fewer Unicode code points +// and then prefixes each line with the indent. In preformatted sections +// (such as program text), it prefixes each non-blank line with preIndent. +func ToText(w io.Writer, text string, indent, preIndent string, width int) { + l := lineWrapper{ + out: w, + width: width, + indent: indent, + } + for i, b := range blocks(text) { + switch b.op { + case opPara: + if i > 0 { + w.Write(nl) + } + for _, line := range b.lines { + l.write(line) + } + l.flush() + case opHead: + w.Write(nl) + for _, line := range b.lines { + l.write(line + "\n") + } + l.flush() + case opPre: + w.Write(nl) + for _, line := range b.lines { + if !isBlank(line) { + w.Write([]byte(preIndent)) + w.Write([]byte(line)) + } + } + } + } +} + +type lineWrapper struct { + out io.Writer + printed bool + width int + indent string + n int + pendSpace int +} + +var nl = []byte("\n") +var space = []byte(" ") + +func (l *lineWrapper) write(text string) { + if l.n == 0 && l.printed { + l.out.Write(nl) // blank line before new paragraph + } + l.printed = true + + for _, f := range strings.Fields(text) { + w := utf8.RuneCountInString(f) + // wrap if line is too long + if l.n > 0 && l.n+l.pendSpace+w > l.width { + l.out.Write(nl) + l.n = 0 + l.pendSpace = 0 + } + if l.n == 0 { + l.out.Write([]byte(l.indent)) + } + l.out.Write(space[:l.pendSpace]) + l.out.Write([]byte(f)) + l.n += l.pendSpace + w + l.pendSpace = 1 + } +} + +func (l *lineWrapper) flush() { + if l.n == 0 { + return + } + l.out.Write(nl) + l.pendSpace = 0 + l.n = 0 } diff --git a/libgo/go/go/doc/comment_test.go b/libgo/go/go/doc/comment_test.go index 6424053ac9d..e8d7f2e4b0f 100644 --- a/libgo/go/go/doc/comment_test.go +++ b/libgo/go/go/doc/comment_test.go @@ -5,6 +5,7 @@ package doc import ( + "reflect" "testing" ) @@ -38,3 +39,45 @@ func TestIsHeading(t *testing.T) { } } } + +var blocksTests = []struct { + in string + out []block +}{ + { + in: `Para 1. +Para 1 line 2. + +Para 2. + +Section + +Para 3. + + pre + pre1 + +Para 4. + pre + pre2 +`, + out: []block{ + {opPara, []string{"Para 1.\n", "Para 1 line 2.\n"}}, + {opPara, []string{"Para 2.\n"}}, + {opHead, []string{"Section"}}, + {opPara, []string{"Para 3.\n"}}, + {opPre, []string{"pre\n", "pre1\n"}}, + {opPara, []string{"Para 4.\n"}}, + {opPre, []string{"pre\n", "pre2\n"}}, + }, + }, +} + +func TestBlocks(t *testing.T) { + for i, tt := range blocksTests { + b := blocks(tt.in) + if !reflect.DeepEqual(b, tt.out) { + t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, b, tt.out) + } + } +} diff --git a/libgo/go/go/doc/doc.go b/libgo/go/go/doc/doc.go index 91748643391..1bb22416c78 100644 --- a/libgo/go/go/doc/doc.go +++ b/libgo/go/go/doc/doc.go @@ -13,17 +13,32 @@ import ( ) // ---------------------------------------------------------------------------- +// Collection of documentation info -type typeDoc struct { +// embeddedType describes the type of an anonymous field. +// +type embeddedType struct { + typ *typeInfo // the corresponding base type + ptr bool // if set, the anonymous field type is a pointer +} + +type typeInfo struct { // len(decl.Specs) == 1, and the element type is *ast.TypeSpec // if the type declaration hasn't been seen yet, decl is nil - decl *ast.GenDecl - // values, factory functions, and methods associated with the type + decl *ast.GenDecl + embedded []embeddedType + forward *TypeDoc // forward link to processed type documentation + + // declarations associated with the type values []*ast.GenDecl // consts and vars factories map[string]*ast.FuncDecl methods map[string]*ast.FuncDecl } +func (info *typeInfo) addEmbeddedType(embedded *typeInfo, isPtr bool) { + info.embedded = append(info.embedded, embeddedType{embedded, isPtr}) +} + // docReader accumulates documentation for a single package. // It modifies the AST: Comments (declaration documentation) // that have been collected by the DocReader are set to nil @@ -32,17 +47,19 @@ type typeDoc struct { // printing the corresponding AST node). // type docReader struct { - doc *ast.CommentGroup // package documentation, if any - pkgName string - values []*ast.GenDecl // consts and vars - types map[string]*typeDoc - funcs map[string]*ast.FuncDecl - bugs []*ast.CommentGroup + doc *ast.CommentGroup // package documentation, if any + pkgName string + values []*ast.GenDecl // consts and vars + types map[string]*typeInfo + embedded map[string]*typeInfo // embedded types, possibly not exported + funcs map[string]*ast.FuncDecl + bugs []*ast.CommentGroup } func (doc *docReader) init(pkgName string) { doc.pkgName = pkgName - doc.types = make(map[string]*typeDoc) + doc.types = make(map[string]*typeInfo) + doc.embedded = make(map[string]*typeInfo) doc.funcs = make(map[string]*ast.FuncDecl) } @@ -52,56 +69,40 @@ func (doc *docReader) addDoc(comments *ast.CommentGroup) { doc.doc = comments return } - // More than one package comment: Usually there will be only // one file with a package comment, but it's better to collect // all comments than drop them on the floor. - // (This code isn't particularly clever - no amortized doubling is - // used - but this situation occurs rarely and is not time-critical.) - n1 := len(doc.doc.List) - n2 := len(comments.List) - list := make([]*ast.Comment, n1+1+n2) // + 1 for separator line - copy(list, doc.doc.List) - list[n1] = &ast.Comment{token.NoPos, "//"} // separator line - copy(list[n1+1:], comments.List) - doc.doc = &ast.CommentGroup{list} -} - -func (doc *docReader) addType(decl *ast.GenDecl) { - spec := decl.Specs[0].(*ast.TypeSpec) - typ := doc.lookupTypeDoc(spec.Name.Name) - // typ should always be != nil since declared types - // are always named - be conservative and check - if typ != nil { - // a type should be added at most once, so typ.decl - // should be nil - if it isn't, simply overwrite it - typ.decl = decl - } + blankComment := &ast.Comment{token.NoPos, "//"} + list := append(doc.doc.List, blankComment) + doc.doc.List = append(list, comments.List...) } -func (doc *docReader) lookupTypeDoc(name string) *typeDoc { - if name == "" { +func (doc *docReader) lookupTypeInfo(name string) *typeInfo { + if name == "" || name == "_" { return nil // no type docs for anonymous types } - if tdoc, found := doc.types[name]; found { - return tdoc + if info, found := doc.types[name]; found { + return info } // type wasn't found - add one without declaration - tdoc := &typeDoc{nil, nil, make(map[string]*ast.FuncDecl), make(map[string]*ast.FuncDecl)} - doc.types[name] = tdoc - return tdoc + info := &typeInfo{ + factories: make(map[string]*ast.FuncDecl), + methods: make(map[string]*ast.FuncDecl), + } + doc.types[name] = info + return info } -func baseTypeName(typ ast.Expr) string { +func baseTypeName(typ ast.Expr, allTypes bool) string { switch t := typ.(type) { case *ast.Ident: // if the type is not exported, the effect to // a client is as if there were no type name - if t.IsExported() { + if t.IsExported() || allTypes { return t.Name } case *ast.StarExpr: - return baseTypeName(t.X) + return baseTypeName(t.X, allTypes) } return "" } @@ -120,7 +121,7 @@ func (doc *docReader) addValue(decl *ast.GenDecl) { switch { case v.Type != nil: // a type is present; determine its name - name = baseTypeName(v.Type) + name = baseTypeName(v.Type, false) case decl.Tok == token.CONST: // no type is present but we have a constant declaration; // use the previous type name (w/o more type information @@ -148,7 +149,7 @@ func (doc *docReader) addValue(decl *ast.GenDecl) { values := &doc.values if domName != "" && domFreq >= int(float64(len(decl.Specs))*threshold) { // typed entries are sufficiently frequent - typ := doc.lookupTypeDoc(domName) + typ := doc.lookupTypeInfo(domName) if typ != nil { values = &typ.values // associate with that type } @@ -175,10 +176,13 @@ func setFunc(table map[string]*ast.FuncDecl, f *ast.FuncDecl) { } func (doc *docReader) addFunc(fun *ast.FuncDecl) { + // strip function body + fun.Body = nil + // determine if it should be associated with a type if fun.Recv != nil { // method - typ := doc.lookupTypeDoc(baseTypeName(fun.Recv.List[0].Type)) + typ := doc.lookupTypeInfo(baseTypeName(fun.Recv.List[0].Type, false)) if typ != nil { // exported receiver type setFunc(typ.methods, fun) @@ -199,8 +203,8 @@ func (doc *docReader) addFunc(fun *ast.FuncDecl) { // exactly one (named or anonymous) result associated // with the first type in result signature (there may // be more than one result) - tname := baseTypeName(res.Type) - typ := doc.lookupTypeDoc(tname) + tname := baseTypeName(res.Type, false) + typ := doc.lookupTypeInfo(tname) if typ != nil { // named and exported result type setFunc(typ.factories, fun) @@ -224,10 +228,17 @@ func (doc *docReader) addDecl(decl ast.Decl) { case token.TYPE: // types are handled individually for _, spec := range d.Specs { - // make a (fake) GenDecl node for this TypeSpec + tspec := spec.(*ast.TypeSpec) + // add the type to the documentation + info := doc.lookupTypeInfo(tspec.Name.Name) + if info == nil { + continue // no name - ignore the type + } + // Make a (fake) GenDecl node for this TypeSpec // (we need to do this here - as opposed to just // for printing - so we don't lose the GenDecl - // documentation) + // documentation). Since a new GenDecl node is + // created, there's no need to nil out d.Doc. // // TODO(gri): Consider just collecting the TypeSpec // node (and copy in the GenDecl.doc if there is no @@ -235,8 +246,32 @@ func (doc *docReader) addDecl(decl ast.Decl) { // makeTypeDocs below). Simpler data structures, but // would lose GenDecl documentation if the TypeSpec // has documentation as well. - doc.addType(&ast.GenDecl{d.Doc, d.Pos(), token.TYPE, token.NoPos, []ast.Spec{spec}, token.NoPos}) - // A new GenDecl node is created, no need to nil out d.Doc. + fake := &ast.GenDecl{d.Doc, d.Pos(), token.TYPE, token.NoPos, + []ast.Spec{tspec}, token.NoPos} + // A type should be added at most once, so info.decl + // should be nil - if it isn't, simply overwrite it. + info.decl = fake + // Look for anonymous fields that might contribute methods. + var fields *ast.FieldList + switch typ := spec.(*ast.TypeSpec).Type.(type) { + case *ast.StructType: + fields = typ.Fields + case *ast.InterfaceType: + fields = typ.Methods + } + if fields != nil { + for _, field := range fields.List { + if len(field.Names) == 0 { + // anonymous field - add corresponding type + // to the info and collect it in doc + name := baseTypeName(field.Type, true) + if embedded := doc.lookupTypeInfo(name); embedded != nil { + _, ptr := field.Type.(*ast.StarExpr) + info.addEmbeddedType(embedded, ptr) + } + } + } + } } } } @@ -285,19 +320,15 @@ func (doc *docReader) addFile(src *ast.File) { src.Comments = nil // consumed unassociated comments - remove from ast.File node } -func NewFileDoc(file *ast.File) *PackageDoc { - var r docReader - r.init(file.Name.Name) - r.addFile(file) - return r.newDoc("", nil) -} - -func NewPackageDoc(pkg *ast.Package, importpath string) *PackageDoc { +func NewPackageDoc(pkg *ast.Package, importpath string, exportsOnly bool) *PackageDoc { var r docReader r.init(pkg.Name) filenames := make([]string, len(pkg.Files)) i := 0 for filename, f := range pkg.Files { + if exportsOnly { + r.fileExports(f) + } r.addFile(f) filenames[i] = filename i++ @@ -397,6 +428,25 @@ func makeFuncDocs(m map[string]*ast.FuncDecl) []*FuncDoc { return d } +type methodSet map[string]*FuncDoc + +func (mset methodSet) add(m *FuncDoc) { + if mset[m.Name] == nil { + mset[m.Name] = m + } +} + +func (mset methodSet) sortedList() []*FuncDoc { + list := make([]*FuncDoc, len(mset)) + i := 0 + for _, m := range mset { + list[i] = m + i++ + } + sort.Sort(sortFuncDoc(list)) + return list +} + // TypeDoc is the documentation for a declared type. // Consts and Vars are sorted lists of constants and variables of (mostly) that type. // Factories is a sorted list of factory functions that return that type. @@ -407,7 +457,9 @@ type TypeDoc struct { Consts []*ValueDoc Vars []*ValueDoc Factories []*FuncDoc - Methods []*FuncDoc + methods []*FuncDoc // top-level methods only + embedded methodSet // embedded methods only + Methods []*FuncDoc // all methods including embedded ones Decl *ast.GenDecl order int } @@ -429,11 +481,17 @@ func (p sortTypeDoc) Less(i, j int) bool { // NOTE(rsc): This would appear not to be correct for type ( ) // blocks, but the doc extractor above has split them into // individual declarations. -func (doc *docReader) makeTypeDocs(m map[string]*typeDoc) []*TypeDoc { - d := make([]*TypeDoc, len(m)) +func (doc *docReader) makeTypeDocs(m map[string]*typeInfo) []*TypeDoc { + // TODO(gri) Consider computing the embedded method information + // before calling makeTypeDocs. Then this function can + // be single-phased again. Also, it might simplify some + // of the logic. + // + // phase 1: associate collected declarations with TypeDocs + list := make([]*TypeDoc, len(m)) i := 0 for _, old := range m { - // all typeDocs should have a declaration associated with + // all typeInfos should have a declaration associated with // them after processing an entire package - be conservative // and check if decl := old.decl; decl != nil { @@ -451,10 +509,16 @@ func (doc *docReader) makeTypeDocs(m map[string]*typeDoc) []*TypeDoc { t.Consts = makeValueDocs(old.values, token.CONST) t.Vars = makeValueDocs(old.values, token.VAR) t.Factories = makeFuncDocs(old.factories) - t.Methods = makeFuncDocs(old.methods) + t.methods = makeFuncDocs(old.methods) + // The list of embedded types' methods is computed from the list + // of embedded types, some of which may not have been processed + // yet (i.e., their forward link is nil) - do this in a 2nd phase. + // The final list of methods can only be computed after that - + // do this in a 3rd phase. t.Decl = old.decl t.order = i - d[i] = t + old.forward = t // old has been processed + list[i] = t i++ } else { // no corresponding type declaration found - move any associated @@ -477,9 +541,99 @@ func (doc *docReader) makeTypeDocs(m map[string]*typeDoc) []*TypeDoc { } } } - d = d[0:i] // some types may have been ignored - sort.Sort(sortTypeDoc(d)) - return d + list = list[0:i] // some types may have been ignored + + // phase 2: collect embedded methods for each processed typeInfo + for _, old := range m { + if t := old.forward; t != nil { + // old has been processed into t; collect embedded + // methods for t from the list of processed embedded + // types in old (and thus for which the methods are known) + typ := t.Type + if _, ok := typ.Type.(*ast.StructType); ok { + // struct + t.embedded = make(methodSet) + collectEmbeddedMethods(t.embedded, old, typ.Name.Name) + } else { + // interface + // TODO(gri) fix this + } + } + } + + // phase 3: compute final method set for each TypeDoc + for _, d := range list { + if len(d.embedded) > 0 { + // there are embedded methods - exclude + // the ones with names conflicting with + // non-embedded methods + mset := make(methodSet) + // top-level methods have priority + for _, m := range d.methods { + mset.add(m) + } + // add non-conflicting embedded methods + for _, m := range d.embedded { + mset.add(m) + } + d.Methods = mset.sortedList() + } else { + // no embedded methods + d.Methods = d.methods + } + } + + sort.Sort(sortTypeDoc(list)) + return list +} + +// collectEmbeddedMethods collects the embedded methods from all +// processed embedded types found in info in mset. It considers +// embedded types at the most shallow level first so that more +// deeply nested embedded methods with conflicting names are +// excluded. +// +func collectEmbeddedMethods(mset methodSet, info *typeInfo, recvTypeName string) { + for _, e := range info.embedded { + if e.typ.forward != nil { // == e was processed + for _, m := range e.typ.forward.methods { + mset.add(customizeRecv(m, e.ptr, recvTypeName)) + } + collectEmbeddedMethods(mset, e.typ, recvTypeName) + } + } +} + +func customizeRecv(m *FuncDoc, embeddedIsPtr bool, recvTypeName string) *FuncDoc { + if m == nil || m.Decl == nil || m.Decl.Recv == nil || len(m.Decl.Recv.List) != 1 { + return m // shouldn't happen, but be safe + } + + // copy existing receiver field and set new type + // TODO(gri) is receiver type computation correct? + // what about deeply nested embeddings? + newField := *m.Decl.Recv.List[0] + _, origRecvIsPtr := newField.Type.(*ast.StarExpr) + var typ ast.Expr = ast.NewIdent(recvTypeName) + if embeddedIsPtr || origRecvIsPtr { + typ = &ast.StarExpr{token.NoPos, typ} + } + newField.Type = typ + + // copy existing receiver field list and set new receiver field + newFieldList := *m.Decl.Recv + newFieldList.List = []*ast.Field{&newField} + + // copy existing function declaration and set new receiver field list + newFuncDecl := *m.Decl + newFuncDecl.Recv = &newFieldList + + // copy existing function documentation and set new declaration + newM := *m + newM.Decl = &newFuncDecl + newM.Recv = typ + + return &newM } func makeBugDocs(list []*ast.CommentGroup) []string { @@ -523,104 +677,3 @@ func (doc *docReader) newDoc(importpath string, filenames []string) *PackageDoc p.Bugs = makeBugDocs(doc.bugs) return p } - -// ---------------------------------------------------------------------------- -// Filtering by name - -type Filter func(string) bool - -func matchFields(fields *ast.FieldList, f Filter) bool { - if fields != nil { - for _, field := range fields.List { - for _, name := range field.Names { - if f(name.Name) { - return true - } - } - } - } - return false -} - -func matchDecl(d *ast.GenDecl, f Filter) bool { - for _, d := range d.Specs { - switch v := d.(type) { - case *ast.ValueSpec: - for _, name := range v.Names { - if f(name.Name) { - return true - } - } - case *ast.TypeSpec: - if f(v.Name.Name) { - return true - } - switch t := v.Type.(type) { - case *ast.StructType: - if matchFields(t.Fields, f) { - return true - } - case *ast.InterfaceType: - if matchFields(t.Methods, f) { - return true - } - } - } - } - return false -} - -func filterValueDocs(a []*ValueDoc, f Filter) []*ValueDoc { - w := 0 - for _, vd := range a { - if matchDecl(vd.Decl, f) { - a[w] = vd - w++ - } - } - return a[0:w] -} - -func filterFuncDocs(a []*FuncDoc, f Filter) []*FuncDoc { - w := 0 - for _, fd := range a { - if f(fd.Name) { - a[w] = fd - w++ - } - } - return a[0:w] -} - -func filterTypeDocs(a []*TypeDoc, f Filter) []*TypeDoc { - w := 0 - for _, td := range a { - n := 0 // number of matches - if matchDecl(td.Decl, f) { - n = 1 - } else { - // type name doesn't match, but we may have matching consts, vars, factories or methods - td.Consts = filterValueDocs(td.Consts, f) - td.Vars = filterValueDocs(td.Vars, f) - td.Factories = filterFuncDocs(td.Factories, f) - td.Methods = filterFuncDocs(td.Methods, f) - n += len(td.Consts) + len(td.Vars) + len(td.Factories) + len(td.Methods) - } - if n > 0 { - a[w] = td - w++ - } - } - return a[0:w] -} - -// Filter eliminates documentation for names that don't pass through the filter f. -// TODO: Recognize "Type.Method" as a name. -// -func (p *PackageDoc) Filter(f Filter) { - p.Consts = filterValueDocs(p.Consts, f) - p.Vars = filterValueDocs(p.Vars, f) - p.Types = filterTypeDocs(p.Types, f) - p.Funcs = filterFuncDocs(p.Funcs, f) - p.Doc = "" // don't show top-level package doc -} diff --git a/libgo/go/go/doc/example.go b/libgo/go/go/doc/example.go index 196c957544a..1bdf4e27e17 100644 --- a/libgo/go/go/doc/example.go +++ b/libgo/go/go/doc/example.go @@ -8,15 +8,16 @@ package doc import ( "go/ast" + "go/printer" "strings" "unicode" "unicode/utf8" ) type Example struct { - Name string // name of the item being demonstrated - Body *ast.BlockStmt // code - Output string // expected output + Name string // name of the item being demonstrated + Body *printer.CommentedNode // code + Output string // expected output } func Examples(pkg *ast.Package) []*Example { @@ -33,7 +34,7 @@ func Examples(pkg *ast.Package) []*Example { } examples = append(examples, &Example{ Name: name[len("Example"):], - Body: f.Body, + Body: &printer.CommentedNode{f.Body, src.Comments}, Output: CommentText(f.Doc), }) } diff --git a/libgo/go/go/doc/exports.go b/libgo/go/go/doc/exports.go new file mode 100644 index 00000000000..9cd186a9c7a --- /dev/null +++ b/libgo/go/go/doc/exports.go @@ -0,0 +1,167 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file implements export filtering of an AST. + +package doc + +import "go/ast" + +func filterIdentList(list []*ast.Ident) []*ast.Ident { + j := 0 + for _, x := range list { + if ast.IsExported(x.Name) { + list[j] = x + j++ + } + } + return list[0:j] +} + +func baseName(x ast.Expr) *ast.Ident { + switch t := x.(type) { + case *ast.Ident: + return t + case *ast.SelectorExpr: + if _, ok := t.X.(*ast.Ident); ok { + return t.Sel + } + case *ast.StarExpr: + return baseName(t.X) + } + return nil +} + +func (doc *docReader) filterFieldList(fields *ast.FieldList) (removedFields bool) { + if fields == nil { + return false + } + list := fields.List + j := 0 + for _, f := range list { + keepField := false + if len(f.Names) == 0 { + // anonymous field + name := baseName(f.Type) + keepField = name != nil && name.IsExported() + } else { + n := len(f.Names) + f.Names = filterIdentList(f.Names) + if len(f.Names) < n { + removedFields = true + } + keepField = len(f.Names) > 0 + } + if keepField { + doc.filterType(f.Type) + list[j] = f + j++ + } + } + if j < len(list) { + removedFields = true + } + fields.List = list[0:j] + return +} + +func (doc *docReader) filterParamList(fields *ast.FieldList) bool { + if fields == nil { + return false + } + var b bool + for _, f := range fields.List { + if doc.filterType(f.Type) { + b = true + } + } + return b +} + +func (doc *docReader) filterType(typ ast.Expr) bool { + switch t := typ.(type) { + case *ast.Ident: + return ast.IsExported(t.Name) + case *ast.ParenExpr: + return doc.filterType(t.X) + case *ast.ArrayType: + return doc.filterType(t.Elt) + case *ast.StructType: + if doc.filterFieldList(t.Fields) { + t.Incomplete = true + } + return len(t.Fields.List) > 0 + case *ast.FuncType: + b1 := doc.filterParamList(t.Params) + b2 := doc.filterParamList(t.Results) + return b1 || b2 + case *ast.InterfaceType: + if doc.filterFieldList(t.Methods) { + t.Incomplete = true + } + return len(t.Methods.List) > 0 + case *ast.MapType: + b1 := doc.filterType(t.Key) + b2 := doc.filterType(t.Value) + return b1 || b2 + case *ast.ChanType: + return doc.filterType(t.Value) + } + return false +} + +func (doc *docReader) filterSpec(spec ast.Spec) bool { + switch s := spec.(type) { + case *ast.ValueSpec: + s.Names = filterIdentList(s.Names) + if len(s.Names) > 0 { + doc.filterType(s.Type) + return true + } + case *ast.TypeSpec: + if ast.IsExported(s.Name.Name) { + doc.filterType(s.Type) + return true + } + } + return false +} + +func (doc *docReader) filterSpecList(list []ast.Spec) []ast.Spec { + j := 0 + for _, s := range list { + if doc.filterSpec(s) { + list[j] = s + j++ + } + } + return list[0:j] +} + +func (doc *docReader) filterDecl(decl ast.Decl) bool { + switch d := decl.(type) { + case *ast.GenDecl: + d.Specs = doc.filterSpecList(d.Specs) + return len(d.Specs) > 0 + case *ast.FuncDecl: + return ast.IsExported(d.Name.Name) + } + return false +} + +// fileExports trims the AST for a Go file in place such that +// only exported nodes remain. fileExports returns true if +// there are exported declarations; otherwise it returns false. +// +func (doc *docReader) fileExports(src *ast.File) bool { + j := 0 + for _, d := range src.Decls { + if doc.filterDecl(d) { + src.Decls[j] = d + j++ + } + } + src.Decls = src.Decls[0:j] + return j > 0 +} diff --git a/libgo/go/go/doc/filter.go b/libgo/go/go/doc/filter.go new file mode 100644 index 00000000000..71c2ebb68bd --- /dev/null +++ b/libgo/go/go/doc/filter.go @@ -0,0 +1,105 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package doc + +import "go/ast" + +type Filter func(string) bool + +func matchFields(fields *ast.FieldList, f Filter) bool { + if fields != nil { + for _, field := range fields.List { + for _, name := range field.Names { + if f(name.Name) { + return true + } + } + } + } + return false +} + +func matchDecl(d *ast.GenDecl, f Filter) bool { + for _, d := range d.Specs { + switch v := d.(type) { + case *ast.ValueSpec: + for _, name := range v.Names { + if f(name.Name) { + return true + } + } + case *ast.TypeSpec: + if f(v.Name.Name) { + return true + } + switch t := v.Type.(type) { + case *ast.StructType: + if matchFields(t.Fields, f) { + return true + } + case *ast.InterfaceType: + if matchFields(t.Methods, f) { + return true + } + } + } + } + return false +} + +func filterValueDocs(a []*ValueDoc, f Filter) []*ValueDoc { + w := 0 + for _, vd := range a { + if matchDecl(vd.Decl, f) { + a[w] = vd + w++ + } + } + return a[0:w] +} + +func filterFuncDocs(a []*FuncDoc, f Filter) []*FuncDoc { + w := 0 + for _, fd := range a { + if f(fd.Name) { + a[w] = fd + w++ + } + } + return a[0:w] +} + +func filterTypeDocs(a []*TypeDoc, f Filter) []*TypeDoc { + w := 0 + for _, td := range a { + n := 0 // number of matches + if matchDecl(td.Decl, f) { + n = 1 + } else { + // type name doesn't match, but we may have matching consts, vars, factories or methods + td.Consts = filterValueDocs(td.Consts, f) + td.Vars = filterValueDocs(td.Vars, f) + td.Factories = filterFuncDocs(td.Factories, f) + td.Methods = filterFuncDocs(td.Methods, f) + n += len(td.Consts) + len(td.Vars) + len(td.Factories) + len(td.Methods) + } + if n > 0 { + a[w] = td + w++ + } + } + return a[0:w] +} + +// Filter eliminates documentation for names that don't pass through the filter f. +// TODO: Recognize "Type.Method" as a name. +// +func (p *PackageDoc) Filter(f Filter) { + p.Consts = filterValueDocs(p.Consts, f) + p.Vars = filterValueDocs(p.Vars, f) + p.Types = filterTypeDocs(p.Types, f) + p.Funcs = filterFuncDocs(p.Funcs, f) + p.Doc = "" // don't show top-level package doc +} diff --git a/libgo/go/go/parser/parser.go b/libgo/go/go/parser/parser.go index f0a8055f4c1..9fbed2d2ca3 100644 --- a/libgo/go/go/parser/parser.go +++ b/libgo/go/go/parser/parser.go @@ -144,28 +144,31 @@ func (p *parser) declare(decl, data interface{}, scope *ast.Scope, kind ast.ObjK } } -func (p *parser) shortVarDecl(idents []*ast.Ident) { +func (p *parser) shortVarDecl(decl *ast.AssignStmt, list []ast.Expr) { // Go spec: A short variable declaration may redeclare variables // provided they were originally declared in the same block with // the same type, and at least one of the non-blank variables is new. n := 0 // number of new variables - for _, ident := range idents { - assert(ident.Obj == nil, "identifier already declared or resolved") - obj := ast.NewObj(ast.Var, ident.Name) - // short var declarations cannot have redeclaration errors - // and are not global => no need to remember the respective - // declaration - ident.Obj = obj - if ident.Name != "_" { - if alt := p.topScope.Insert(obj); alt != nil { - ident.Obj = alt // redeclaration - } else { - n++ // new declaration + for _, x := range list { + if ident, isIdent := x.(*ast.Ident); isIdent { + assert(ident.Obj == nil, "identifier already declared or resolved") + obj := ast.NewObj(ast.Var, ident.Name) + // remember corresponding assignment for other tools + obj.Decl = decl + ident.Obj = obj + if ident.Name != "_" { + if alt := p.topScope.Insert(obj); alt != nil { + ident.Obj = alt // redeclaration + } else { + n++ // new declaration + } } + } else { + p.errorExpected(x.Pos(), "identifier") } } if n == 0 && p.mode&DeclarationErrors != 0 { - p.error(idents[0].Pos(), "no new variables on left side of :=") + p.error(list[0].Pos(), "no new variables on left side of :=") } } @@ -522,7 +525,7 @@ func (p *parser) makeIdentList(list []ast.Expr) []*ast.Ident { for i, x := range list { ident, isIdent := x.(*ast.Ident) if !isIdent { - pos := x.(ast.Expr).Pos() + pos := x.Pos() p.errorExpected(pos, "identifier") ident = &ast.Ident{pos, "_", nil} } @@ -1400,10 +1403,11 @@ func (p *parser) parseSimpleStmt(mode int) (ast.Stmt, bool) { } else { y = p.parseRhsList() } + as := &ast.AssignStmt{x, pos, tok, y} if tok == token.DEFINE { - p.shortVarDecl(p.makeIdentList(x)) + p.shortVarDecl(as, x) } - return &ast.AssignStmt{x, pos, tok, y}, isRange + return as, isRange } if len(x) > 1 { @@ -1715,34 +1719,28 @@ func (p *parser) parseCommClause() *ast.CommClause { comm = &ast.SendStmt{lhs[0], arrow, rhs} } else { // RecvStmt - pos := p.pos - tok := p.tok - var rhs ast.Expr - if tok == token.ASSIGN || tok == token.DEFINE { + if tok := p.tok; tok == token.ASSIGN || tok == token.DEFINE { // RecvStmt with assignment if len(lhs) > 2 { p.errorExpected(lhs[0].Pos(), "1 or 2 expressions") // continue with first two expressions lhs = lhs[0:2] } + pos := p.pos p.next() - rhs = p.parseRhs() - if tok == token.DEFINE && lhs != nil { - p.shortVarDecl(p.makeIdentList(lhs)) + rhs := p.parseRhs() + as := &ast.AssignStmt{lhs, pos, tok, []ast.Expr{rhs}} + if tok == token.DEFINE { + p.shortVarDecl(as, lhs) } + comm = as } else { - // rhs must be single receive operation + // lhs must be single receive operation if len(lhs) > 1 { p.errorExpected(lhs[0].Pos(), "1 expression") // continue with first expression } - rhs = lhs[0] - lhs = nil // there is no lhs - } - if lhs != nil { - comm = &ast.AssignStmt{lhs, pos, tok, []ast.Expr{rhs}} - } else { - comm = &ast.ExprStmt{rhs} + comm = &ast.ExprStmt{lhs[0]} } } } else { diff --git a/libgo/go/go/printer/nodes.go b/libgo/go/go/printer/nodes.go index b2a48c28a73..6817cc42add 100644 --- a/libgo/go/go/printer/nodes.go +++ b/libgo/go/go/printer/nodes.go @@ -39,7 +39,10 @@ import ( // future (not yet interspersed) comments in this function. // func (p *printer) linebreak(line, min int, ws whiteSpace, newSection bool) (printedBreak bool) { - n := p.nlines(line-p.pos.Line, min) + n := nlimit(line - p.pos.Line) + if n < min { + n = min + } if n > 0 { p.print(ws) if newSection { @@ -361,9 +364,10 @@ func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool) lbrace := fields.Opening list := fields.List rbrace := fields.Closing + hasComments := isIncomplete || p.commentBefore(p.fset.Position(rbrace)) srcIsOneLine := lbrace.IsValid() && rbrace.IsValid() && p.fset.Position(lbrace).Line == p.fset.Position(rbrace).Line - if !isIncomplete && !p.commentBefore(p.fset.Position(rbrace)) && srcIsOneLine { + if !hasComments && srcIsOneLine { // possibly a one-line struct/interface if len(list) == 0 { // no blank between keyword and {} in this case @@ -388,9 +392,13 @@ func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool) return } } + // hasComments || !srcIsOneLine + + p.print(blank, lbrace, token.LBRACE, indent) + if hasComments || len(list) > 0 { + p.print(formfeed) + } - // at least one entry or incomplete - p.print(blank, lbrace, token.LBRACE, indent, formfeed) if isStruct { sep := vtab @@ -1509,9 +1517,14 @@ func (p *printer) file(src *ast.File) { prev := tok tok = declToken(d) // if the declaration token changed (e.g., from CONST to TYPE) + // or the next declaration has documentation associated with it, // print an empty line between top-level declarations + // (because p.linebreak is called with the position of d, which + // is past any documentation, the minimum requirement is satisfied + // even w/o the extra getDoc(d) nil-check - leave it in case the + // linebreak logic improves - there's already a TODO). min := 1 - if prev != tok { + if prev != tok || getDoc(d) != nil { min = 2 } p.linebreak(p.fset.Position(d.Pos()).Line, min, ignore, false) diff --git a/libgo/go/go/printer/printer.go b/libgo/go/go/printer/printer.go index f8c22f1419d..a78cfc65fcc 100644 --- a/libgo/go/go/printer/printer.go +++ b/libgo/go/go/printer/printer.go @@ -18,8 +18,11 @@ import ( "text/tabwriter" ) -const debug = false // enable for debugging -const infinity = 1 << 30 +const ( + maxNewlines = 2 // max. number of newlines between source text + debug = false // enable for debugging + infinity = 1 << 30 +) type whiteSpace byte @@ -89,21 +92,7 @@ func (p *printer) internalError(msg ...interface{}) { } } -// nlines returns the adjusted number of linebreaks given the desired number -// of breaks n such that min <= result <= max. -// -func (p *printer) nlines(n, min int) int { - const max = 2 // max. number of newlines - switch { - case n < min: - return min - case n > max: - return max - } - return n -} - -// writeByte writes a single byte to p.output and updates p.pos. +// writeByte writes ch to p.output and updates p.pos. func (p *printer) writeByte(ch byte) { p.output.WriteByte(ch) p.pos.Offset++ @@ -128,13 +117,11 @@ func (p *printer) writeByte(ch byte) { } } -// writeNewlines writes up to n newlines to p.output and updates p.pos. -// The actual number of newlines written is limited by nlines. -// nl must be one of '\n' or '\f'. -// -func (p *printer) writeNewlines(n int, nl byte) { - for n = p.nlines(n, 0); n > 0; n-- { - p.writeByte(nl) +// writeByteN writes ch n times to p.output and updates p.pos. +func (p *printer) writeByteN(ch byte, n int) { + for n > 0 { + p.writeByte(ch) + n-- } } @@ -223,8 +210,8 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *as } if pos.IsValid() && pos.Filename != p.last.Filename { - // comment in a different file - separate with newlines (writeNewlines will limit the number) - p.writeNewlines(10, '\f') + // comment in a different file - separate with newlines + p.writeByteN('\f', maxNewlines) return } @@ -270,6 +257,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *as } else { // comment on a different line: // separate with at least one line break + droppedLinebreak := false if prev == nil { // first comment of a comment group j := 0 @@ -295,6 +283,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *as case newline, formfeed: // TODO(gri): may want to keep formfeed info in some cases p.wsbuf[i] = ignore + droppedLinebreak = true } j = i break @@ -302,25 +291,41 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *as p.writeWhitespace(j) } - // turn off indent if we're about to print a line directive. - indent := p.indent - if strings.HasPrefix(comment.Text, linePrefix) { - p.indent = 0 + // determine number of linebreaks before the comment + n := 0 + if pos.IsValid() && p.last.IsValid() { + n = pos.Line - p.last.Line + if n < 0 { // should never happen + n = 0 + } } - // use formfeeds to break columns before a comment; - // this is analogous to using formfeeds to separate - // individual lines of /*-style comments - but make - // sure there is at least one line break if the previous - // comment was a line comment - n := pos.Line - p.last.Line // if !pos.IsValid(), pos.Line == 0, and n will be 0 - if n <= 0 && prev != nil && prev.Text[1] == '/' { + // at the package scope level only (p.indent == 0), + // add an extra newline if we dropped one before: + // this preserves a blank line before documentation + // comments at the package scope level (issue 2570) + if p.indent == 0 && droppedLinebreak { + n++ + } + + // make sure there is at least one line break + // if the previous comment was a line comment + if n == 0 && prev != nil && prev.Text[1] == '/' { n = 1 } + if n > 0 { - p.writeNewlines(n, '\f') + // turn off indent if we're about to print a line directive + indent := p.indent + if strings.HasPrefix(comment.Text, linePrefix) { + p.indent = 0 + } + // use formfeeds to break columns before a comment; + // this is analogous to using formfeeds to separate + // individual lines of /*-style comments + p.writeByteN('\f', nlimit(n)) + p.indent = indent // restore indent } - p.indent = indent } } @@ -550,10 +555,11 @@ func (p *printer) writeComment(comment *ast.Comment) { // writeCommentSuffix writes a line break after a comment if indicated // and processes any leftover indentation information. If a line break // is needed, the kind of break (newline vs formfeed) depends on the -// pending whitespace. writeCommentSuffix returns true if a pending -// formfeed was dropped from the whitespace buffer. +// pending whitespace. The writeCommentSuffix result indicates if a +// newline was written or if a formfeed was dropped from the whitespace +// buffer. // -func (p *printer) writeCommentSuffix(needsLinebreak bool) (droppedFF bool) { +func (p *printer) writeCommentSuffix(needsLinebreak bool) (wroteNewline, droppedFF bool) { for i, ch := range p.wsbuf { switch ch { case blank, vtab: @@ -566,6 +572,7 @@ func (p *printer) writeCommentSuffix(needsLinebreak bool) (droppedFF bool) { // but remember if we dropped any formfeeds if needsLinebreak { needsLinebreak = false + wroteNewline = true } else { if ch == formfeed { droppedFF = true @@ -579,6 +586,7 @@ func (p *printer) writeCommentSuffix(needsLinebreak bool) (droppedFF bool) { // make sure we have a line break if needsLinebreak { p.writeByte('\n') + wroteNewline = true } return @@ -587,10 +595,10 @@ func (p *printer) writeCommentSuffix(needsLinebreak bool) (droppedFF bool) { // intersperseComments consumes all comments that appear before the next token // tok and prints it together with the buffered whitespace (i.e., the whitespace // that needs to be written before the next token). A heuristic is used to mix -// the comments and whitespace. intersperseComments returns true if a pending -// formfeed was dropped from the whitespace buffer. +// the comments and whitespace. The intersperseComments result indicates if a +// newline was written or if a formfeed was dropped from the whitespace buffer. // -func (p *printer) intersperseComments(next token.Position, tok token.Token) (droppedFF bool) { +func (p *printer) intersperseComments(next token.Position, tok token.Token) (wroteNewline, droppedFF bool) { var last *ast.Comment for ; p.commentBefore(next); p.cindex++ { for _, c := range p.comments[p.cindex].List { @@ -618,7 +626,7 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (dro // no comment was written - we should never reach here since // intersperseComments should not be called in that case p.internalError("intersperseComments called without pending comments") - return false + return } // whiteWhitespace writes the first n whitespace entries. @@ -671,6 +679,14 @@ func (p *printer) writeWhitespace(n int) { // ---------------------------------------------------------------------------- // Printing interface +// nlines limits n to maxNewlines. +func nlimit(n int) int { + if n > maxNewlines { + n = maxNewlines + } + return n +} + func mayCombine(prev token.Token, next byte) (b bool) { switch prev { case token.INT: @@ -765,17 +781,22 @@ func (p *printer) print(args ...interface{}) { p.pos = next if data != "" { - nl := byte('\n') - if p.flush(next, tok) { - nl = '\f' // dropped formfeed before - } + wroteNewline, droppedFF := p.flush(next, tok) // intersperse extra newlines if present in the source // (don't do this in flush as it will cause extra newlines - // at the end of a file) - use formfeeds if we dropped one - // before - if n := next.Line - p.pos.Line; n > 0 { - p.writeNewlines(n, nl) + // at the end of a file) + n := nlimit(next.Line - p.pos.Line) + // don't exceed maxNewlines if we already wrote one + if wroteNewline && n == maxNewlines { + n = maxNewlines - 1 + } + if n > 0 { + ch := byte('\n') + if droppedFF { + ch = '\f' // use formfeed since we dropped one before + } + p.writeByteN(ch, n) } p.writeItem(next, data, isLit) @@ -790,16 +811,15 @@ func (p *printer) commentBefore(next token.Position) bool { return p.cindex < len(p.comments) && p.fset.Position(p.comments[p.cindex].List[0].Pos()).Offset < next.Offset } -// Flush prints any pending comments and whitespace occurring -// textually before the position of the next token tok. Flush -// returns true if a pending formfeed character was dropped -// from the whitespace buffer as a result of interspersing -// comments. +// Flush prints any pending comments and whitespace occurring textually +// before the position of the next token tok. The Flush result indicates +// if a newline was written or if a formfeed was dropped from the whitespace +// buffer. // -func (p *printer) flush(next token.Position, tok token.Token) (droppedFF bool) { +func (p *printer) flush(next token.Position, tok token.Token) (wroteNewline, droppedFF bool) { if p.commentBefore(next) { // if there are comments before the next item, intersperse them - droppedFF = p.intersperseComments(next, tok) + wroteNewline, droppedFF = p.intersperseComments(next, tok) } else { // otherwise, write any leftover whitespace p.writeWhitespace(len(p.wsbuf)) @@ -807,13 +827,76 @@ func (p *printer) flush(next token.Position, tok token.Token) (droppedFF bool) { return } +// getNode returns the ast.CommentGroup associated with n, if any. +func getDoc(n ast.Node) *ast.CommentGroup { + switch n := n.(type) { + case *ast.Field: + return n.Doc + case *ast.ImportSpec: + return n.Doc + case *ast.ValueSpec: + return n.Doc + case *ast.TypeSpec: + return n.Doc + case *ast.GenDecl: + return n.Doc + case *ast.FuncDecl: + return n.Doc + case *ast.File: + return n.Doc + } + return nil +} + func (p *printer) printNode(node interface{}) error { + // unpack *CommentedNode, if any + var comments []*ast.CommentGroup + if cnode, ok := node.(*CommentedNode); ok { + node = cnode.Node + comments = cnode.Comments + } + + if comments != nil { + // commented node - restrict comment list to relevant range + n, ok := node.(ast.Node) + if !ok { + goto unsupported + } + beg := n.Pos() + end := n.End() + // if the node has associated documentation, + // include that commentgroup in the range + // (the comment list is sorted in the order + // of the comment appearance in the source code) + if doc := getDoc(n); doc != nil { + beg = doc.Pos() + } + // token.Pos values are global offsets, we can + // compare them directly + i := 0 + for i < len(comments) && comments[i].End() < beg { + i++ + } + j := i + for j < len(comments) && comments[j].Pos() < end { + j++ + } + if i < j { + p.comments = comments[i:j] + } + } else if n, ok := node.(*ast.File); ok { + // use ast.File comments, if any + p.comments = n.Comments + } + + // if there are no comments, use node comments + p.useNodeComments = p.comments == nil + + // format node switch n := node.(type) { case ast.Expr: - p.useNodeComments = true p.expr(n, ignoreMultiLine) case ast.Stmt: - p.useNodeComments = true // A labeled statement will un-indent to position the // label. Set indent to 1 so we don't get indent "underflow". if _, labeledStmt := n.(*ast.LabeledStmt); labeledStmt { @@ -821,19 +904,19 @@ func (p *printer) printNode(node interface{}) error { } p.stmt(n, false, ignoreMultiLine) case ast.Decl: - p.useNodeComments = true p.decl(n, ignoreMultiLine) case ast.Spec: - p.useNodeComments = true p.spec(n, 1, false, ignoreMultiLine) case *ast.File: - p.comments = n.Comments - p.useNodeComments = n.Comments == nil p.file(n) default: - return fmt.Errorf("go/printer: unsupported node type %T", n) + goto unsupported } + return nil + +unsupported: + return fmt.Errorf("go/printer: unsupported node type %T", node) } // ---------------------------------------------------------------------------- @@ -1001,10 +1084,18 @@ func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{ return } +// A CommentedNode bundles an AST node and corresponding comments. +// It may be provided as argument to any of the FPrint functions. +// +type CommentedNode struct { + Node interface{} // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt + Comments []*ast.CommentGroup +} + // Fprint "pretty-prints" an AST node to output for a given configuration cfg. // Position information is interpreted relative to the file set fset. -// The node type must be *ast.File, or assignment-compatible to ast.Expr, -// ast.Decl, ast.Spec, or ast.Stmt. +// The node type must be *ast.File, *CommentedNode, or assignment-compatible +// to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt. // func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{}) error { return cfg.fprint(output, fset, node, make(map[ast.Node]int)) diff --git a/libgo/go/go/printer/printer_test.go b/libgo/go/go/printer/printer_test.go index 924d4dfdb29..45477d40f6e 100644 --- a/libgo/go/go/printer/printer_test.go +++ b/libgo/go/go/printer/printer_test.go @@ -107,7 +107,7 @@ func check(t *testing.T, source, golden string, mode checkMode) { // start a timer to produce a time-out signal tc := make(chan int) go func() { - time.Sleep(10e9) // plenty of a safety margin, even for very slow machines + time.Sleep(10 * time.Second) // plenty of a safety margin, even for very slow machines tc <- 0 }() diff --git a/libgo/go/go/printer/testdata/comments.golden b/libgo/go/go/printer/testdata/comments.golden index 7b332252c4e..d2ad9e3a2ff 100644 --- a/libgo/go/go/printer/testdata/comments.golden +++ b/libgo/go/go/printer/testdata/comments.golden @@ -106,7 +106,7 @@ type S3 struct { var x int // x var () -// This comment SHOULD be associated with the next declaration. +// This comment SHOULD be associated with f0. func f0() { const pi = 3.14 // pi var s1 struct{} /* an empty struct */ /* foo */ @@ -115,8 +115,9 @@ func f0() { var s2 struct{} = struct{}{} x := pi } + // -// NO SPACE HERE +// This comment should be associated with f1, with one blank line before the comment. // func f1() { f0() diff --git a/libgo/go/go/printer/testdata/comments.input b/libgo/go/go/printer/testdata/comments.input index 2a9a86b6815..222e0a713d4 100644 --- a/libgo/go/go/printer/testdata/comments.input +++ b/libgo/go/go/printer/testdata/comments.input @@ -107,7 +107,7 @@ var x int // x var () -// This comment SHOULD be associated with the next declaration. +// This comment SHOULD be associated with f0. func f0() { const pi = 3.14 // pi var s1 struct {} /* an empty struct */ /* foo */ @@ -117,7 +117,7 @@ func f0() { x := pi } // -// NO SPACE HERE +// This comment should be associated with f1, with one blank line before the comment. // func f1() { f0() @@ -130,7 +130,7 @@ func f1() { func _() { - // this comment should be properly indented +// this comment should be properly indented } diff --git a/libgo/go/go/printer/testdata/declarations.golden b/libgo/go/go/printer/testdata/declarations.golden index bfa2568c21f..239ba890304 100644 --- a/libgo/go/go/printer/testdata/declarations.golden +++ b/libgo/go/go/printer/testdata/declarations.golden @@ -115,6 +115,18 @@ import _ "io" var _ int +// at least one empty line between declarations of the same kind +// if there is associated documentation (was issue 2570) +type T1 struct{} + +// T2 comment +type T2 struct { +} // should be a two-line struct + +// T3 comment +type T2 struct { +} // should be a two-line struct + // printing of constant literals const ( _ = "foobar" @@ -286,6 +298,15 @@ type _ struct { } } +// no blank lines in empty structs and interfaces, but leave 1- or 2-line layout alone +type _ struct{} +type _ struct { +} + +type _ interface{} +type _ interface { +} + // no tabs for single or ungrouped decls func _() { const xxxxxx = 0 diff --git a/libgo/go/go/printer/testdata/declarations.input b/libgo/go/go/printer/testdata/declarations.input index 1d69c57b517..68f90308a36 100644 --- a/libgo/go/go/printer/testdata/declarations.input +++ b/libgo/go/go/printer/testdata/declarations.input @@ -115,6 +115,20 @@ import ( import _ "io" var _ int +// at least one empty line between declarations of the same kind +// if there is associated documentation (was issue 2570) +type T1 struct{} +// T2 comment +type T2 struct { +} // should be a two-line struct + + +// T3 comment +type T2 struct { + + +} // should be a two-line struct + // printing of constant literals const ( @@ -293,6 +307,18 @@ type _ struct { } +// no blank lines in empty structs and interfaces, but leave 1- or 2-line layout alone +type _ struct{ } +type _ struct { + +} + +type _ interface{ } +type _ interface { + +} + + // no tabs for single or ungrouped decls func _() { const xxxxxx = 0 diff --git a/libgo/go/go/printer/testdata/statements.golden b/libgo/go/go/printer/testdata/statements.golden index a6d85107f0b..90e1743557d 100644 --- a/libgo/go/go/printer/testdata/statements.golden +++ b/libgo/go/go/printer/testdata/statements.golden @@ -271,7 +271,6 @@ func _() { // Known bug: The first use call may have more than one empty line before // (see go/printer/nodes.go, func linebreak). - use(x) if x < x { @@ -386,7 +385,6 @@ L: // A comment on the same line as the label, followed by a single empty line. // Known bug: There may be more than one empty line before MoreCode() // (see go/printer/nodes.go, func linebreak). - MoreCode() } diff --git a/libgo/go/go/scanner/scanner.go b/libgo/go/go/scanner/scanner.go index cef9c486508..7fb0104e450 100644 --- a/libgo/go/go/scanner/scanner.go +++ b/libgo/go/go/scanner/scanner.go @@ -426,13 +426,16 @@ func (S *Scanner) scanString() { S.next() } -func (S *Scanner) scanRawString() { +func (S *Scanner) scanRawString() (hasCR bool) { // '`' opening already consumed offs := S.offset - 1 for S.ch != '`' { ch := S.ch S.next() + if ch == '\r' { + hasCR = true + } if ch < 0 { S.error(offs, "string not terminated") break @@ -440,6 +443,7 @@ func (S *Scanner) scanRawString() { } S.next() + return } func (S *Scanner) skipWhitespace() { @@ -490,6 +494,18 @@ func (S *Scanner) switch4(tok0, tok1 token.Token, ch2 rune, tok2, tok3 token.Tok return tok0 } +func stripCR(b []byte) []byte { + c := make([]byte, len(b)) + i := 0 + for _, ch := range b { + if ch != '\r' { + c[i] = ch + i++ + } + } + return c[:i] +} + // Scan scans the next token and returns the token position, // the token, and the literal string corresponding to the // token. The source end is indicated by token.EOF. @@ -518,6 +534,7 @@ scanAgain: insertSemi := false offs := S.offset tok := token.ILLEGAL + hasCR := false // determine token value switch ch := S.ch; { @@ -556,7 +573,7 @@ scanAgain: case '`': insertSemi = true tok = token.STRING - S.scanRawString() + hasCR = S.scanRawString() case ':': tok = S.switch2(token.COLON, token.DEFINE) case '.': @@ -663,5 +680,9 @@ scanAgain: // TODO(gri): The scanner API should change such that the literal string // is only valid if an actual literal was scanned. This will // permit a more efficient implementation. - return S.file.Pos(offs), tok, string(S.src[offs:S.offset]) + lit := S.src[offs:S.offset] + if hasCR { + lit = stripCR(lit) + } + return S.file.Pos(offs), tok, string(lit) } diff --git a/libgo/go/go/scanner/scanner_test.go b/libgo/go/go/scanner/scanner_test.go index 7ed927a49fa..dc8ab2a748a 100644 --- a/libgo/go/go/scanner/scanner_test.go +++ b/libgo/go/go/scanner/scanner_test.go @@ -83,6 +83,8 @@ var tokens = [...]elt{ "`", literal, }, + {token.STRING, "`\r`", literal}, + {token.STRING, "`foo\r\nbar`", literal}, // Operators and delimiters {token.ADD, "+", operator}, @@ -239,8 +241,16 @@ func TestScan(t *testing.T) { if tok != e.tok { t.Errorf("bad token for %q: got %s, expected %s", lit, tok, e.tok) } - if e.tok.IsLiteral() && lit != e.lit { - t.Errorf("bad literal for %q: got %q, expected %q", lit, lit, e.lit) + if e.tok.IsLiteral() { + // no CRs in raw string literals + elit := e.lit + if elit[0] == '`' { + elit = string(stripCR([]byte(elit))) + epos.Offset += len(e.lit) - len(lit) // correct position + } + if lit != elit { + t.Errorf("bad literal for %q: got %q, expected %q", lit, lit, elit) + } } if tokenclass(tok) != e.class { t.Errorf("bad class for %q: got %d, expected %d", lit, tokenclass(tok), e.class) diff --git a/libgo/go/hash/crc32/crc32_generic.go b/libgo/go/hash/crc32/crc32_generic.go index 27aabd903bb..c3fdcd685c5 100644 --- a/libgo/go/hash/crc32/crc32_generic.go +++ b/libgo/go/hash/crc32/crc32_generic.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build 386 arm + package crc32 // The file contains the generic version of updateCastagnoli which just calls diff --git a/libgo/go/html/const.go b/libgo/go/html/const.go index 9078d260115..d7cc8bb9a99 100644 --- a/libgo/go/html/const.go +++ b/libgo/go/html/const.go @@ -4,10 +4,10 @@ package html -// Section 11.2.3.2 of the HTML5 specification says "The following elements +// Section 12.2.3.2 of the HTML5 specification says "The following elements // have varying levels of special parsing rules". // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#the-stack-of-open-elements -var isSpecialElement = map[string]bool{ +var isSpecialElementMap = map[string]bool{ "address": true, "applet": true, "area": true, @@ -88,3 +88,13 @@ var isSpecialElement = map[string]bool{ "wbr": true, "xmp": true, } + +func isSpecialElement(element *Node) bool { + switch element.Namespace { + case "", "html": + return isSpecialElementMap[element.Data] + case "svg": + return element.Data == "foreignObject" + } + return false +} diff --git a/libgo/go/html/escape.go b/libgo/go/html/escape.go index ac9e100df0c..42be865ef08 100644 --- a/libgo/go/html/escape.go +++ b/libgo/go/html/escape.go @@ -78,7 +78,7 @@ func unescapeEntity(b []byte, dst, src int, attribute bool) (dst1, src1 int) { i++ } - x := rune(0) + x := '\x00' for i < len(s) { c = s[i] i++ diff --git a/libgo/go/html/foreign.go b/libgo/go/html/foreign.go new file mode 100644 index 00000000000..0f9b4ad560d --- /dev/null +++ b/libgo/go/html/foreign.go @@ -0,0 +1,56 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package html + +// Section 12.2.5.5. +var breakout = map[string]bool{ + "b": true, + "big": true, + "blockquote": true, + "body": true, + "br": true, + "center": true, + "code": true, + "dd": true, + "div": true, + "dl": true, + "dt": true, + "em": true, + "embed": true, + "font": true, + "h1": true, + "h2": true, + "h3": true, + "h4": true, + "h5": true, + "h6": true, + "head": true, + "hr": true, + "i": true, + "img": true, + "li": true, + "listing": true, + "menu": true, + "meta": true, + "nobr": true, + "ol": true, + "p": true, + "pre": true, + "ruby": true, + "s": true, + "small": true, + "span": true, + "strong": true, + "strike": true, + "sub": true, + "sup": true, + "table": true, + "tt": true, + "u": true, + "ul": true, + "var": true, +} + +// TODO: add look-up tables for MathML and SVG adjustments. diff --git a/libgo/go/html/node.go b/libgo/go/html/node.go index 5ca6035c118..4ba3f5fb627 100644 --- a/libgo/go/html/node.go +++ b/libgo/go/html/node.go @@ -17,21 +17,22 @@ const ( scopeMarkerNode ) -// Section 11.2.3.3 says "scope markers are inserted when entering applet +// Section 12.2.3.3 says "scope markers are inserted when entering applet // elements, buttons, object elements, marquees, table cells, and table // captions, and are used to prevent formatting from 'leaking'". var scopeMarker = Node{Type: scopeMarkerNode} // A Node consists of a NodeType and some Data (tag name for element nodes, // content for text) and are part of a tree of Nodes. Element nodes may also -// contain a slice of Attributes. Data is unescaped, so that it looks like -// "a<b" rather than "a<b". +// have a Namespace and contain a slice of Attributes. Data is unescaped, so +// that it looks like "a<b" rather than "a<b". type Node struct { - Parent *Node - Child []*Node - Type NodeType - Data string - Attr []Attribute + Parent *Node + Child []*Node + Type NodeType + Data string + Namespace string + Attr []Attribute } // Add adds a node as a child of n. diff --git a/libgo/go/html/parse.go b/libgo/go/html/parse.go index dd2d8165bdb..6962e643932 100644 --- a/libgo/go/html/parse.go +++ b/libgo/go/html/parse.go @@ -22,12 +22,12 @@ type parser struct { hasSelfClosingToken bool // doc is the document root element. doc *Node - // The stack of open elements (section 11.2.3.2) and active formatting - // elements (section 11.2.3.3). + // The stack of open elements (section 12.2.3.2) and active formatting + // elements (section 12.2.3.3). oe, afe nodeStack - // Element pointers (section 11.2.3.4). + // Element pointers (section 12.2.3.4). head, form *Node - // Other parsing state flags (section 11.2.3.5). + // Other parsing state flags (section 12.2.3.5). scripting, framesetOK bool // im is the current insertion mode. im insertionMode @@ -35,12 +35,12 @@ type parser struct { // or inTableText insertion mode. originalIM insertionMode // fosterParenting is whether new elements should be inserted according to - // the foster parenting rules (section 11.2.5.3). + // the foster parenting rules (section 12.2.5.3). fosterParenting bool // quirks is whether the parser is operating in "quirks mode." quirks bool // context is the context element when parsing an HTML fragment - // (section 11.4). + // (section 12.4). context *Node } @@ -51,7 +51,7 @@ func (p *parser) top() *Node { return p.doc } -// stopTags for use in popUntil. These come from section 11.2.3.2. +// stopTags for use in popUntil. These come from section 12.2.3.2. var ( defaultScopeStopTags = []string{"applet", "caption", "html", "table", "td", "th", "marquee", "object"} listItemScopeStopTags = []string{"applet", "caption", "html", "table", "td", "th", "marquee", "object", "ol", "ul"} @@ -130,7 +130,7 @@ func (p *parser) addChild(n *Node) { } // fosterParent adds a child node according to the foster parenting rules. -// Section 11.2.5.3, "foster parenting". +// Section 12.2.5.3, "foster parenting". func (p *parser) fosterParent(n *Node) { p.fosterParenting = false var table, parent *Node @@ -192,20 +192,21 @@ func (p *parser) addText(text string) { // addElement calls addChild with an element node. func (p *parser) addElement(tag string, attr []Attribute) { p.addChild(&Node{ - Type: ElementNode, - Data: tag, - Attr: attr, + Type: ElementNode, + Data: tag, + Namespace: p.top().Namespace, + Attr: attr, }) } -// Section 11.2.3.3. +// Section 12.2.3.3. func (p *parser) addFormattingElement(tag string, attr []Attribute) { p.addElement(tag, attr) p.afe = append(p.afe, p.top()) // TODO. } -// Section 11.2.3.3. +// Section 12.2.3.3. func (p *parser) clearActiveFormattingElements() { for { n := p.afe.pop() @@ -215,7 +216,7 @@ func (p *parser) clearActiveFormattingElements() { } } -// Section 11.2.3.3. +// Section 12.2.3.3. func (p *parser) reconstructActiveFormattingElements() { n := p.afe.top() if n == nil { @@ -265,12 +266,12 @@ func (p *parser) read() error { return nil } -// Section 11.2.4. +// Section 12.2.4. func (p *parser) acknowledgeSelfClosingTag() { p.hasSelfClosingToken = false } -// An insertion mode (section 11.2.3.1) is the state transition function from +// An insertion mode (section 12.2.3.1) is the state transition function from // a particular state in the HTML5 parser's state machine. It updates the // parser's fields depending on parser.tok (where ErrorToken means EOF). // It returns whether the token was consumed. @@ -278,7 +279,7 @@ type insertionMode func(*parser) bool // setOriginalIM sets the insertion mode to return to after completing a text or // inTableText insertion mode. -// Section 11.2.3.1, "using the rules for". +// Section 12.2.3.1, "using the rules for". func (p *parser) setOriginalIM() { if p.originalIM != nil { panic("html: bad parser state: originalIM was set twice") @@ -286,7 +287,7 @@ func (p *parser) setOriginalIM() { p.originalIM = p.im } -// Section 11.2.3.1, "reset the insertion mode". +// Section 12.2.3.1, "reset the insertion mode". func (p *parser) resetInsertionMode() { for i := len(p.oe) - 1; i >= 0; i-- { n := p.oe[i] @@ -327,7 +328,7 @@ func (p *parser) resetInsertionMode() { const whitespace = " \t\r\n\f" -// Section 11.2.5.4.1. +// Section 12.2.5.4.1. func initialIM(p *parser) bool { switch p.tok.Type { case TextToken: @@ -354,7 +355,7 @@ func initialIM(p *parser) bool { return false } -// Section 11.2.5.4.2. +// Section 12.2.5.4.2. func beforeHTMLIM(p *parser) bool { switch p.tok.Type { case TextToken: @@ -390,7 +391,7 @@ func beforeHTMLIM(p *parser) bool { return false } -// Section 11.2.5.4.3. +// Section 12.2.5.4.3. func beforeHeadIM(p *parser) bool { var ( add bool @@ -439,7 +440,7 @@ func beforeHeadIM(p *parser) bool { return !implied } -// Section 11.2.5.4.4. +// Section 12.2.5.4.4. func inHeadIM(p *parser) bool { var ( pop bool @@ -506,7 +507,7 @@ func inHeadIM(p *parser) bool { return true } -// Section 11.2.5.4.6. +// Section 12.2.5.4.6. func afterHeadIM(p *parser) bool { var ( add bool @@ -594,7 +595,7 @@ func copyAttributes(dst *Node, src Token) { } } -// Section 11.2.5.4.7. +// Section 12.2.5.4.7. func inBodyIM(p *parser) bool { switch p.tok.Type { case TextToken: @@ -701,7 +702,7 @@ func inBodyIM(p *parser) bool { case "address", "div", "p": continue default: - if !isSpecialElement[node.Data] { + if !isSpecialElement(node) { continue } } @@ -719,7 +720,7 @@ func inBodyIM(p *parser) bool { case "address", "div", "p": continue default: - if !isSpecialElement[node.Data] { + if !isSpecialElement(node) { continue } } @@ -730,6 +731,11 @@ func inBodyIM(p *parser) bool { case "plaintext": p.popUntil(buttonScopeStopTags, "p") p.addElement(p.tok.Data, p.tok.Attr) + case "button": + p.popUntil(defaultScopeStopTags, "button") + p.reconstructActiveFormattingElements() + p.addElement(p.tok.Data, p.tok.Attr) + p.framesetOK = false case "optgroup", "option": if p.top().Data == "option" { p.oe.pop() @@ -792,6 +798,20 @@ func inBodyIM(p *parser) bool { p.reconstructActiveFormattingElements() p.framesetOK = false p.addElement(p.tok.Data, p.tok.Attr) + case "math", "svg": + p.reconstructActiveFormattingElements() + namespace := "" + if p.tok.Data == "math" { + // TODO: adjust MathML attributes. + namespace = "mathml" + } else { + // TODO: adjust SVG attributes. + namespace = "svg" + } + // TODO: adjust foreign attributes. + p.addElement(p.tok.Data, p.tok.Attr) + p.top().Namespace = namespace + return true case "caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr": // Ignore the token. default: @@ -871,7 +891,7 @@ func (p *parser) inBodyEndTagFormatting(tag string) { // Steps 5-6. Find the furthest block. var furthestBlock *Node for _, e := range p.oe[feIndex:] { - if isSpecialElement[e.Data] { + if isSpecialElement(e) { furthestBlock = e break } @@ -964,13 +984,13 @@ func (p *parser) inBodyEndTagOther(tag string) { p.oe = p.oe[:i] break } - if isSpecialElement[p.oe[i].Data] { + if isSpecialElement(p.oe[i]) { break } } } -// Section 11.2.5.4.8. +// Section 12.2.5.4.8. func textIM(p *parser) bool { switch p.tok.Type { case ErrorToken: @@ -986,7 +1006,7 @@ func textIM(p *parser) bool { return p.tok.Type == EndTagToken } -// Section 11.2.5.4.9. +// Section 12.2.5.4.9. func inTableIM(p *parser) bool { switch p.tok.Type { case ErrorToken: @@ -1075,7 +1095,7 @@ func (p *parser) clearStackToContext(stopTags []string) { } } -// Section 11.2.5.4.11. +// Section 12.2.5.4.11. func inCaptionIM(p *parser) bool { switch p.tok.Type { case StartTagToken: @@ -1115,7 +1135,7 @@ func inCaptionIM(p *parser) bool { return inBodyIM(p) } -// Section 11.2.5.4.12. +// Section 12.2.5.4.12. func inColumnGroupIM(p *parser) bool { switch p.tok.Type { case CommentToken: @@ -1142,8 +1162,8 @@ func inColumnGroupIM(p *parser) bool { case "colgroup": if p.oe.top().Data != "html" { p.oe.pop() + p.im = inTableIM } - p.im = inTableIM return true case "col": // Ignore the token. @@ -1152,12 +1172,13 @@ func inColumnGroupIM(p *parser) bool { } if p.oe.top().Data != "html" { p.oe.pop() + p.im = inTableIM + return false } - p.im = inTableIM - return false + return true } -// Section 11.2.5.4.13. +// Section 12.2.5.4.13. func inTableBodyIM(p *parser) bool { var ( add bool @@ -1181,6 +1202,13 @@ func inTableBodyIM(p *parser) bool { add = true data = "tr" consumed = false + case "caption", "col", "colgroup", "tbody", "tfoot", "thead": + if !p.popUntil(tableScopeStopTags, "tbody", "thead", "tfoot") { + // Ignore the token. + return true + } + p.im = inTableIM + return false default: // TODO. } @@ -1213,7 +1241,7 @@ func inTableBodyIM(p *parser) bool { return inTableIM(p) } -// Section 11.2.5.4.14. +// Section 12.2.5.4.14. func inRowIM(p *parser) bool { switch p.tok.Type { case ErrorToken: @@ -1272,7 +1300,7 @@ func inRowIM(p *parser) bool { return inTableIM(p) } -// Section 11.2.5.4.15. +// Section 12.2.5.4.15. func inCellIM(p *parser) bool { var ( closeTheCellAndReprocess bool @@ -1317,7 +1345,7 @@ func inCellIM(p *parser) bool { return inBodyIM(p) } -// Section 11.2.5.4.16. +// Section 12.2.5.4.16. func inSelectIM(p *parser) bool { endSelect := false switch p.tok.Type { @@ -1394,7 +1422,7 @@ func inSelectIM(p *parser) bool { return true } -// Section 11.2.5.4.18. +// Section 12.2.5.4.18. func afterBodyIM(p *parser) bool { switch p.tok.Type { case ErrorToken: @@ -1424,7 +1452,7 @@ func afterBodyIM(p *parser) bool { return false } -// Section 11.2.5.4.19. +// Section 12.2.5.4.19. func inFramesetIM(p *parser) bool { switch p.tok.Type { case CommentToken: @@ -1432,6 +1460,18 @@ func inFramesetIM(p *parser) bool { Type: CommentNode, Data: p.tok.Data, }) + case TextToken: + // Ignore all text but whitespace. + s := strings.Map(func(c rune) rune { + switch c { + case ' ', '\t', '\n', '\f', '\r': + return c + } + return -1 + }, p.tok.Data) + if s != "" { + p.addText(s) + } case StartTagToken: switch p.tok.Data { case "html": @@ -1462,7 +1502,7 @@ func inFramesetIM(p *parser) bool { return true } -// Section 11.2.5.4.20. +// Section 12.2.5.4.20. func afterFramesetIM(p *parser) bool { switch p.tok.Type { case CommentToken: @@ -1470,6 +1510,18 @@ func afterFramesetIM(p *parser) bool { Type: CommentNode, Data: p.tok.Data, }) + case TextToken: + // Ignore all text but whitespace. + s := strings.Map(func(c rune) rune { + switch c { + case ' ', '\t', '\n', '\f', '\r': + return c + } + return -1 + }, p.tok.Data) + if s != "" { + p.addText(s) + } case StartTagToken: switch p.tok.Data { case "html": @@ -1489,7 +1541,7 @@ func afterFramesetIM(p *parser) bool { return true } -// Section 11.2.5.4.21. +// Section 12.2.5.4.21. func afterAfterBodyIM(p *parser) bool { switch p.tok.Type { case ErrorToken: @@ -1512,7 +1564,7 @@ func afterAfterBodyIM(p *parser) bool { return false } -// Section 11.2.5.4.22. +// Section 12.2.5.4.22. func afterAfterFramesetIM(p *parser) bool { switch p.tok.Type { case CommentToken: @@ -1520,6 +1572,19 @@ func afterAfterFramesetIM(p *parser) bool { Type: CommentNode, Data: p.tok.Data, }) + case TextToken: + // Ignore all text but whitespace. + s := strings.Map(func(c rune) rune { + switch c { + case ' ', '\t', '\n', '\f', '\r': + return c + } + return -1 + }, p.tok.Data) + if s != "" { + p.reconstructActiveFormattingElements() + p.addText(s) + } case StartTagToken: switch p.tok.Data { case "html": @@ -1533,6 +1598,78 @@ func afterAfterFramesetIM(p *parser) bool { return true } +// Section 12.2.5.5. +func parseForeignContent(p *parser) bool { + switch p.tok.Type { + case TextToken: + // TODO: HTML integration points. + if p.top().Namespace == "" { + inBodyIM(p) + p.resetInsertionMode() + return true + } + if p.framesetOK { + p.framesetOK = strings.TrimLeft(p.tok.Data, whitespace) == "" + } + p.addText(p.tok.Data) + case CommentToken: + p.addChild(&Node{ + Type: CommentNode, + Data: p.tok.Data, + }) + case StartTagToken: + if breakout[p.tok.Data] { + for i := len(p.oe) - 1; i >= 0; i-- { + // TODO: HTML, MathML integration points. + if p.oe[i].Namespace == "" { + p.oe = p.oe[:i+1] + break + } + } + return false + } + switch p.top().Namespace { + case "mathml": + // TODO: adjust MathML attributes. + case "svg": + // TODO: adjust SVG tag names. + // TODO: adjust SVG attributes. + default: + panic("html: bad parser state: unexpected namespace") + } + // TODO: adjust foreign attributes. + p.addElement(p.tok.Data, p.tok.Attr) + case EndTagToken: + for i := len(p.oe) - 1; i >= 0; i-- { + if p.oe[i].Namespace == "" { + return p.im(p) + } + if strings.EqualFold(p.oe[i].Data, p.tok.Data) { + p.oe = p.oe[:i] + break + } + } + return true + default: + // Ignore the token. + } + return true +} + +// Section 12.2.5. +func (p *parser) inForeignContent() bool { + if len(p.oe) == 0 { + return false + } + n := p.oe[len(p.oe)-1] + if n.Namespace == "" { + return false + } + // TODO: MathML, HTML integration points. + // TODO: MathML's annotation-xml combining with SVG's svg. + return true +} + func (p *parser) parse() error { // Iterate until EOF. Any other error will cause an early return. consumed := true @@ -1545,7 +1682,11 @@ func (p *parser) parse() error { return err } } - consumed = p.im(p) + if p.inForeignContent() { + consumed = parseForeignContent(p) + } else { + consumed = p.im(p) + } } // Loop until the final token (the ErrorToken signifying EOF) is consumed. for { diff --git a/libgo/go/html/parse_test.go b/libgo/go/html/parse_test.go index 5062a6edcb8..015b5838f0b 100644 --- a/libgo/go/html/parse_test.go +++ b/libgo/go/html/parse_test.go @@ -98,7 +98,11 @@ func dumpLevel(w io.Writer, n *Node, level int) error { case DocumentNode: return errors.New("unexpected DocumentNode") case ElementNode: - fmt.Fprintf(w, "<%s>", n.Data) + if n.Namespace != "" { + fmt.Fprintf(w, "<%s %s>", n.Namespace, n.Data) + } else { + fmt.Fprintf(w, "<%s>", n.Data) + } for _, a := range n.Attr { io.WriteString(w, "\n") dumpIndent(w, level+1) @@ -161,13 +165,15 @@ func TestParser(t *testing.T) { n int }{ // TODO(nigeltao): Process all the test cases from all the .dat files. + {"adoption01.dat", -1}, {"doctype01.dat", -1}, {"tests1.dat", -1}, {"tests2.dat", -1}, {"tests3.dat", -1}, {"tests4.dat", -1}, {"tests5.dat", -1}, - {"tests6.dat", 7}, + {"tests6.dat", 47}, + {"tests10.dat", 16}, } for _, tf := range testFiles { f, err := os.Open("testdata/webkit/" + tf.filename) diff --git a/libgo/go/html/render.go b/libgo/go/html/render.go index 7e1a4669657..20751938d9d 100644 --- a/libgo/go/html/render.go +++ b/libgo/go/html/render.go @@ -247,7 +247,7 @@ func writeQuoted(w writer, s string) error { return nil } -// Section 13.1.2, "Elements", gives this list of void elements. Void elements +// Section 12.1.2, "Elements", gives this list of void elements. Void elements // are those that can't have any contents. var voidElements = map[string]bool{ "area": true, diff --git a/libgo/go/html/template/css.go b/libgo/go/html/template/css.go index b0a2f013d29..3bcd9849831 100644 --- a/libgo/go/html/template/css.go +++ b/libgo/go/html/template/css.go @@ -106,7 +106,7 @@ func isHex(c byte) bool { // hexDecode decodes a short hex digit sequence: "10" -> 16. func hexDecode(s []byte) rune { - n := rune(0) + n := '\x00' for _, c := range s { n <<= 4 switch { diff --git a/libgo/go/html/template/error.go b/libgo/go/html/template/error.go index 9622d7e48ee..dcac7489676 100644 --- a/libgo/go/html/template/error.go +++ b/libgo/go/html/template/error.go @@ -183,11 +183,11 @@ const ( func (e *Error) Error() string { if e.Line != 0 { - return fmt.Sprintf("exp/template/html:%s:%d: %s", e.Name, e.Line, e.Description) + return fmt.Sprintf("html/template:%s:%d: %s", e.Name, e.Line, e.Description) } else if e.Name != "" { - return fmt.Sprintf("exp/template/html:%s: %s", e.Name, e.Description) + return fmt.Sprintf("html/template:%s: %s", e.Name, e.Description) } - return "exp/template/html: " + e.Description + return "html/template: " + e.Description } // errorf creates an error given a format string f and args. diff --git a/libgo/go/html/template/escape.go b/libgo/go/html/template/escape.go index 2f6be3b6c21..c6f723ae4a4 100644 --- a/libgo/go/html/template/escape.go +++ b/libgo/go/html/template/escape.go @@ -486,9 +486,17 @@ func (e *escaper) escapeTree(c context, name string, line int) (context, string) } t := e.template(name) if t == nil { + // Two cases: The template exists but is empty, or has never been mentioned at + // all. Distinguish the cases in the error messages. + if e.tmpl.set[name] != nil { + return context{ + state: stateError, + err: errorf(ErrNoSuchTemplate, line, "%q is an incomplete or empty template", name), + }, dname + } return context{ state: stateError, - err: errorf(ErrNoSuchTemplate, line, "no such template %s", name), + err: errorf(ErrNoSuchTemplate, line, "no such template %q", name), }, dname } if dname != name { diff --git a/libgo/go/html/template/escape_test.go b/libgo/go/html/template/escape_test.go index cdeed48b822..a57f9826b5b 100644 --- a/libgo/go/html/template/escape_test.go +++ b/libgo/go/html/template/escape_test.go @@ -654,7 +654,7 @@ func TestEscape(t *testing.T) { for _, test := range tests { tmpl := New(test.name) // TODO: Move noescape into template/func.go - tmpl.Funcs(template.FuncMap{ + tmpl.Funcs(FuncMap{ "noescape": func(a ...interface{}) string { return fmt.Sprint(a...) }, @@ -792,7 +792,7 @@ func TestEscapeSet(t *testing.T) { // pred is a template function that returns the predecessor of a // natural number for testing recursive templates. - fns := template.FuncMap{"pred": func(a ...interface{}) (interface{}, error) { + fns := FuncMap{"pred": func(a ...interface{}) (interface{}, error) { if len(a) == 1 { if i, _ := a[0].(int); i > 0 { return i - 1, nil @@ -928,7 +928,7 @@ func TestErrors(t *testing.T) { }, { `{{template "foo"}}`, - "z:1: no such template foo", + "z:1: no such template \"foo\"", }, { `<div{{template "y"}}>` + @@ -944,23 +944,23 @@ func TestErrors(t *testing.T) { }, { `<input type=button value=onclick=>`, - `exp/template/html:z: "=" in unquoted attr: "onclick="`, + `html/template:z: "=" in unquoted attr: "onclick="`, }, { `<input type=button value= onclick=>`, - `exp/template/html:z: "=" in unquoted attr: "onclick="`, + `html/template:z: "=" in unquoted attr: "onclick="`, }, { `<input type=button value= 1+1=2>`, - `exp/template/html:z: "=" in unquoted attr: "1+1=2"`, + `html/template:z: "=" in unquoted attr: "1+1=2"`, }, { "<a class=`foo>", - "exp/template/html:z: \"`\" in unquoted attr: \"`foo\"", + "html/template:z: \"`\" in unquoted attr: \"`foo\"", }, { `<a style=font:'Arial'>`, - `exp/template/html:z: "'" in unquoted attr: "font:'Arial'"`, + `html/template:z: "'" in unquoted attr: "font:'Arial'"`, }, { `<a=foo>`, diff --git a/libgo/go/html/template/template.go b/libgo/go/html/template/template.go index fa2ed18874c..9ffe41413a8 100644 --- a/libgo/go/html/template/template.go +++ b/libgo/go/html/template/template.go @@ -49,20 +49,28 @@ func (t *Template) Execute(wr io.Writer, data interface{}) (err error) { // ExecuteTemplate applies the template associated with t that has the given // name to the specified data object and writes the output to wr. -func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) (err error) { +func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error { + tmpl, err := t.lookupAndEscapeTemplate(wr, name) + if err != nil { + return err + } + return tmpl.text.Execute(wr, data) +} + +// lookupAndEscapeTemplate guarantees that the template with the given name +// is escaped, or returns an error if it cannot be. It returns the named +// template. +func (t *Template) lookupAndEscapeTemplate(wr io.Writer, name string) (tmpl *Template, err error) { t.nameSpace.mu.Lock() - tmpl := t.set[name] + defer t.nameSpace.mu.Unlock() + tmpl = t.set[name] if (tmpl == nil) != (t.text.Lookup(name) == nil) { panic("html/template internal error: template escaping out of sync") } if tmpl != nil && !tmpl.escaped { err = escapeTemplates(tmpl, name) } - t.nameSpace.mu.Unlock() - if err != nil { - return - } - return t.text.ExecuteTemplate(wr, name, data) + return tmpl, err } // Parse parses a string into a template. Nested template definitions @@ -146,12 +154,20 @@ func (t *Template) Name() string { return t.text.Name() } +// FuncMap is the type of the map defining the mapping from names to +// functions. Each function must have either a single return value, or two +// return values of which the second has type error. In that case, if the +// second (error) argument evaluates to non-nil during execution, execution +// terminates and Execute returns that error. FuncMap has the same base type +// as template.FuncMap, copied here so clients need not import "text/template". +type FuncMap map[string]interface{} + // Funcs adds the elements of the argument map to the template's function map. // It panics if a value in the map is not a function with appropriate return // type. However, it is legal to overwrite elements of the map. The return // value is the template, so calls can be chained. -func (t *Template) Funcs(funcMap template.FuncMap) *Template { - t.text.Funcs(funcMap) +func (t *Template) Funcs(funcMap FuncMap) *Template { + t.text.Funcs(template.FuncMap(funcMap)) return t } @@ -175,7 +191,9 @@ func (t *Template) Lookup(name string) *Template { // Must panics if err is non-nil in the same way as template.Must. func Must(t *Template, err error) *Template { - t.text = template.Must(t.text, err) + if err != nil { + panic(err) + } return t } diff --git a/libgo/go/image/color/ycbcr.go b/libgo/go/image/color/ycbcr.go new file mode 100644 index 00000000000..c79816dd45a --- /dev/null +++ b/libgo/go/image/color/ycbcr.go @@ -0,0 +1,99 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package color + +// RGBToYCbCr converts an RGB triple to a Y'CbCr triple. All components lie +// within the range [0, 255]. +func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) { + // The JFIF specification says: + // Y' = 0.2990*R + 0.5870*G + 0.1140*B + // Cb = -0.1687*R - 0.3313*G + 0.5000*B + 128 + // Cr = 0.5000*R - 0.4187*G - 0.0813*B + 128 + // http://www.w3.org/Graphics/JPEG/jfif3.pdf says Y but means Y'. + r1 := int(r) + g1 := int(g) + b1 := int(b) + yy := (19595*r1 + 38470*g1 + 7471*b1 + 1<<15) >> 16 + cb := (-11056*r1 - 21712*g1 + 32768*b1 + 257<<15) >> 16 + cr := (32768*r1 - 27440*g1 - 5328*b1 + 257<<15) >> 16 + if yy < 0 { + yy = 0 + } else if yy > 255 { + yy = 255 + } + if cb < 0 { + cb = 0 + } else if cb > 255 { + cb = 255 + } + if cr < 0 { + cr = 0 + } else if cr > 255 { + cr = 255 + } + return uint8(yy), uint8(cb), uint8(cr) +} + +// YCbCrToRGB converts a Y'CbCr triple to an RGB triple. All components lie +// within the range [0, 255]. +func YCbCrToRGB(y, cb, cr uint8) (uint8, uint8, uint8) { + // The JFIF specification says: + // R = Y' + 1.40200*(Cr-128) + // G = Y' - 0.34414*(Cb-128) - 0.71414*(Cr-128) + // B = Y' + 1.77200*(Cb-128) + // http://www.w3.org/Graphics/JPEG/jfif3.pdf says Y but means Y'. + yy1 := int(y)<<16 + 1<<15 + cb1 := int(cb) - 128 + cr1 := int(cr) - 128 + r := (yy1 + 91881*cr1) >> 16 + g := (yy1 - 22554*cb1 - 46802*cr1) >> 16 + b := (yy1 + 116130*cb1) >> 16 + if r < 0 { + r = 0 + } else if r > 255 { + r = 255 + } + if g < 0 { + g = 0 + } else if g > 255 { + g = 255 + } + if b < 0 { + b = 0 + } else if b > 255 { + b = 255 + } + return uint8(r), uint8(g), uint8(b) +} + +// YCbCr represents a fully opaque 24-bit Y'CbCr color, having 8 bits each for +// one luma and two chroma components. +// +// JPEG, VP8, the MPEG family and other codecs use this color model. Such +// codecs often use the terms YUV and Y'CbCr interchangeably, but strictly +// speaking, the term YUV applies only to analog video signals, and Y' (luma) +// is Y (luminance) after applying gamma correction. +// +// Conversion between RGB and Y'CbCr is lossy and there are multiple, slightly +// different formulae for converting between the two. This package follows +// the JFIF specification at http://www.w3.org/Graphics/JPEG/jfif3.pdf. +type YCbCr struct { + Y, Cb, Cr uint8 +} + +func (c YCbCr) RGBA() (uint32, uint32, uint32, uint32) { + r, g, b := YCbCrToRGB(c.Y, c.Cb, c.Cr) + return uint32(r) * 0x101, uint32(g) * 0x101, uint32(b) * 0x101, 0xffff +} + +// YCbCrModel is the Model for Y'CbCr colors. +var YCbCrModel Model = ModelFunc(func(c Color) Color { + if _, ok := c.(YCbCr); ok { + return c + } + r, g, b, _ := c.RGBA() + y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8)) + return YCbCr{y, u, v} +}) diff --git a/libgo/go/image/ycbcr/ycbcr_test.go b/libgo/go/image/color/ycbcr_test.go index 2e60a6f61f9..92a0e6ff1e7 100644 --- a/libgo/go/image/ycbcr/ycbcr_test.go +++ b/libgo/go/image/color/ycbcr_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package ycbcr +package color import ( "testing" diff --git a/libgo/go/image/draw/bench_test.go b/libgo/go/image/draw/bench_test.go index 2be91185af2..4cd2095c441 100644 --- a/libgo/go/image/draw/bench_test.go +++ b/libgo/go/image/draw/bench_test.go @@ -7,7 +7,6 @@ package draw import ( "image" "image/color" - "image/ycbcr" "testing" ) @@ -51,7 +50,7 @@ func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) { } dst = dst1 default: - panic("unreachable") + b.Fatal("unknown destination color model", dcm) } var src image.Image @@ -97,7 +96,7 @@ func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) { } } src = src1 - case ycbcr.YCbCrColorModel: + case color.YCbCrModel: yy := make([]uint8, srcw*srch) cb := make([]uint8, srcw*srch) cr := make([]uint8, srcw*srch) @@ -106,17 +105,17 @@ func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) { cb[i] = uint8(5 * i % 0x100) cr[i] = uint8(7 * i % 0x100) } - src = &ycbcr.YCbCr{ + src = &image.YCbCr{ Y: yy, Cb: cb, Cr: cr, YStride: srcw, CStride: srcw, - SubsampleRatio: ycbcr.SubsampleRatio444, + SubsampleRatio: image.YCbCrSubsampleRatio444, Rect: image.Rect(0, 0, srcw, srch), } default: - panic("unreachable") + b.Fatal("unknown source color model", scm) } var mask image.Image @@ -137,7 +136,7 @@ func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) { } mask = mask1 default: - panic("unreachable") + b.Fatal("unknown mask color model", mcm) } b.StartTimer() @@ -177,7 +176,7 @@ func BenchmarkNRGBASrc(b *testing.B) { } func BenchmarkYCbCr(b *testing.B) { - bench(b, color.RGBAModel, ycbcr.YCbCrColorModel, nil, Over) + bench(b, color.RGBAModel, color.YCbCrModel, nil, Over) } func BenchmarkGlyphOver(b *testing.B) { diff --git a/libgo/go/image/draw/draw.go b/libgo/go/image/draw/draw.go index af02639ccd5..228ed6e719c 100644 --- a/libgo/go/image/draw/draw.go +++ b/libgo/go/image/draw/draw.go @@ -11,7 +11,6 @@ package draw import ( "image" "image/color" - "image/ycbcr" ) // m is the maximum color value returned by image.Color.RGBA. @@ -81,7 +80,7 @@ func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mas case *image.NRGBA: drawNRGBAOver(dst0, r, src0, sp) return - case *ycbcr.YCbCr: + case *image.YCbCr: drawYCbCr(dst0, r, src0, sp) return } @@ -104,7 +103,7 @@ func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mas case *image.NRGBA: drawNRGBASrc(dst0, r, src0, sp) return - case *ycbcr.YCbCr: + case *image.YCbCr: drawYCbCr(dst0, r, src0, sp) return } @@ -346,8 +345,8 @@ func drawNRGBASrc(dst *image.RGBA, r image.Rectangle, src *image.NRGBA, sp image } } -func drawYCbCr(dst *image.RGBA, r image.Rectangle, src *ycbcr.YCbCr, sp image.Point) { - // A YCbCr image is always fully opaque, and so if the mask is implicitly nil +func drawYCbCr(dst *image.RGBA, r image.Rectangle, src *image.YCbCr, sp image.Point) { + // An image.YCbCr is always fully opaque, and so if the mask is implicitly nil // (i.e. fully opaque) then the op is effectively always Src. var ( yy, cb, cr uint8 @@ -357,7 +356,7 @@ func drawYCbCr(dst *image.RGBA, r image.Rectangle, src *ycbcr.YCbCr, sp image.Po y0 := r.Min.Y - dst.Rect.Min.Y y1 := r.Max.Y - dst.Rect.Min.Y switch src.SubsampleRatio { - case ycbcr.SubsampleRatio422: + case image.YCbCrSubsampleRatio422: for y, sy := y0, sp.Y; y != y1; y, sy = y+1, sy+1 { dpix := dst.Pix[y*dst.Stride:] for x, sx := x0, sp.X; x != x1; x, sx = x+4, sx+1 { @@ -365,14 +364,14 @@ func drawYCbCr(dst *image.RGBA, r image.Rectangle, src *ycbcr.YCbCr, sp image.Po yy = src.Y[sy*src.YStride+sx] cb = src.Cb[sy*src.CStride+i] cr = src.Cr[sy*src.CStride+i] - rr, gg, bb := ycbcr.YCbCrToRGB(yy, cb, cr) + rr, gg, bb := color.YCbCrToRGB(yy, cb, cr) dpix[x+0] = rr dpix[x+1] = gg dpix[x+2] = bb dpix[x+3] = 255 } } - case ycbcr.SubsampleRatio420: + case image.YCbCrSubsampleRatio420: for y, sy := y0, sp.Y; y != y1; y, sy = y+1, sy+1 { dpix := dst.Pix[y*dst.Stride:] for x, sx := x0, sp.X; x != x1; x, sx = x+4, sx+1 { @@ -380,7 +379,7 @@ func drawYCbCr(dst *image.RGBA, r image.Rectangle, src *ycbcr.YCbCr, sp image.Po yy = src.Y[sy*src.YStride+sx] cb = src.Cb[j*src.CStride+i] cr = src.Cr[j*src.CStride+i] - rr, gg, bb := ycbcr.YCbCrToRGB(yy, cb, cr) + rr, gg, bb := color.YCbCrToRGB(yy, cb, cr) dpix[x+0] = rr dpix[x+1] = gg dpix[x+2] = bb @@ -395,7 +394,7 @@ func drawYCbCr(dst *image.RGBA, r image.Rectangle, src *ycbcr.YCbCr, sp image.Po yy = src.Y[sy*src.YStride+sx] cb = src.Cb[sy*src.CStride+sx] cr = src.Cr[sy*src.CStride+sx] - rr, gg, bb := ycbcr.YCbCrToRGB(yy, cb, cr) + rr, gg, bb := color.YCbCrToRGB(yy, cb, cr) dpix[x+0] = rr dpix[x+1] = gg dpix[x+2] = bb diff --git a/libgo/go/image/draw/draw_test.go b/libgo/go/image/draw/draw_test.go index 663ab67a190..56a45026b57 100644 --- a/libgo/go/image/draw/draw_test.go +++ b/libgo/go/image/draw/draw_test.go @@ -7,7 +7,6 @@ package draw import ( "image" "image/color" - "image/ycbcr" "testing" ) @@ -56,13 +55,13 @@ func vgradGreenNRGBA(alpha int) image.Image { } func vgradCr() image.Image { - m := &ycbcr.YCbCr{ + m := &image.YCbCr{ Y: make([]byte, 16*16), Cb: make([]byte, 16*16), Cr: make([]byte, 16*16), YStride: 16, CStride: 16, - SubsampleRatio: ycbcr.SubsampleRatio444, + SubsampleRatio: image.YCbCrSubsampleRatio444, Rect: image.Rect(0, 0, 16, 16), } for y := 0; y < 16; y++ { diff --git a/libgo/go/image/jpeg/reader.go b/libgo/go/image/jpeg/reader.go index c1fc2d590f5..ed1a962586d 100644 --- a/libgo/go/image/jpeg/reader.go +++ b/libgo/go/image/jpeg/reader.go @@ -11,7 +11,6 @@ import ( "bufio" "image" "image/color" - "image/ycbcr" "io" ) @@ -97,7 +96,7 @@ type decoder struct { r Reader width, height int img1 *image.Gray - img3 *ycbcr.YCbCr + img3 *image.YCbCr ri int // Restart Interval. nComp int comp [nColorComponent]component @@ -203,20 +202,20 @@ func (d *decoder) makeImg(h0, v0, mxx, myy int) { d.img1 = m.SubImage(image.Rect(0, 0, d.width, d.height)).(*image.Gray) return } - var subsampleRatio ycbcr.SubsampleRatio + var subsampleRatio image.YCbCrSubsampleRatio n := h0 * v0 switch n { case 1: - subsampleRatio = ycbcr.SubsampleRatio444 + subsampleRatio = image.YCbCrSubsampleRatio444 case 2: - subsampleRatio = ycbcr.SubsampleRatio422 + subsampleRatio = image.YCbCrSubsampleRatio422 case 4: - subsampleRatio = ycbcr.SubsampleRatio420 + subsampleRatio = image.YCbCrSubsampleRatio420 default: panic("unreachable") } b := make([]byte, mxx*myy*(1*8*8*n+2*8*8)) - d.img3 = &ycbcr.YCbCr{ + d.img3 = &image.YCbCr{ Y: b[mxx*myy*(0*8*8*n+0*8*8) : mxx*myy*(1*8*8*n+0*8*8)], Cb: b[mxx*myy*(1*8*8*n+0*8*8) : mxx*myy*(1*8*8*n+1*8*8)], Cr: b[mxx*myy*(1*8*8*n+1*8*8) : mxx*myy*(1*8*8*n+2*8*8)], @@ -466,7 +465,7 @@ func DecodeConfig(r io.Reader) (image.Config, error) { case nGrayComponent: return image.Config{color.GrayModel, d.width, d.height}, nil case nColorComponent: - return image.Config{ycbcr.YCbCrColorModel, d.width, d.height}, nil + return image.Config{color.YCbCrModel, d.width, d.height}, nil } return image.Config{}, FormatError("missing SOF marker") } diff --git a/libgo/go/image/jpeg/writer.go b/libgo/go/image/jpeg/writer.go index fab0bd0bfc1..71fe37ce7ff 100644 --- a/libgo/go/image/jpeg/writer.go +++ b/libgo/go/image/jpeg/writer.go @@ -8,7 +8,7 @@ import ( "bufio" "errors" "image" - "image/ycbcr" + "image/color" "io" ) @@ -379,7 +379,7 @@ func toYCbCr(m image.Image, p image.Point, yBlock, cbBlock, crBlock *block) { for j := 0; j < 8; j++ { for i := 0; i < 8; i++ { r, g, b, _ := m.At(min(p.X+i, xmax), min(p.Y+j, ymax)).RGBA() - yy, cb, cr := ycbcr.RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8)) + yy, cb, cr := color.RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8)) yBlock[8*j+i] = int(yy) cbBlock[8*j+i] = int(cb) crBlock[8*j+i] = int(cr) @@ -404,7 +404,7 @@ func rgbaToYCbCr(m *image.RGBA, p image.Point, yBlock, cbBlock, crBlock *block) sx = xmax } pix := m.Pix[offset+sx*4:] - yy, cb, cr := ycbcr.RGBToYCbCr(pix[0], pix[1], pix[2]) + yy, cb, cr := color.RGBToYCbCr(pix[0], pix[1], pix[2]) yBlock[8*j+i] = int(yy) cbBlock[8*j+i] = int(cb) crBlock[8*j+i] = int(cr) diff --git a/libgo/go/image/jpeg/writer_test.go b/libgo/go/image/jpeg/writer_test.go index 28e87321361..e4b56d28847 100644 --- a/libgo/go/image/jpeg/writer_test.go +++ b/libgo/go/image/jpeg/writer_test.go @@ -105,7 +105,7 @@ func BenchmarkEncodeRGBOpaque(b *testing.B) { } } if !img.Opaque() { - panic("expected image to be opaque") + b.Fatal("expected image to be opaque") } b.SetBytes(640 * 480 * 4) b.StartTimer() diff --git a/libgo/go/image/png/writer_test.go b/libgo/go/image/png/writer_test.go index 1757e14cada..228ecccfb4f 100644 --- a/libgo/go/image/png/writer_test.go +++ b/libgo/go/image/png/writer_test.go @@ -125,7 +125,7 @@ func BenchmarkEncodeRGBOpaque(b *testing.B) { } } if !img.Opaque() { - panic("expected image to be opaque") + b.Fatal("expected image to be opaque") } b.SetBytes(640 * 480 * 4) b.StartTimer() @@ -138,7 +138,7 @@ func BenchmarkEncodeRGBA(b *testing.B) { b.StopTimer() img := image.NewRGBA(image.Rect(0, 0, 640, 480)) if img.Opaque() { - panic("expected image to not be opaque") + b.Fatal("expected image to not be opaque") } b.SetBytes(640 * 480 * 4) b.StartTimer() diff --git a/libgo/go/image/tiff/reader_test.go b/libgo/go/image/tiff/reader_test.go index 1a3d23bbd75..ee5dafd9962 100644 --- a/libgo/go/image/tiff/reader_test.go +++ b/libgo/go/image/tiff/reader_test.go @@ -113,7 +113,7 @@ func BenchmarkDecode(b *testing.B) { for i := 0; i < b.N; i++ { _, err := Decode(r) if err != nil { - panic(err) + b.Fatal("Decode:", err) } } } diff --git a/libgo/go/image/ycbcr.go b/libgo/go/image/ycbcr.go new file mode 100644 index 00000000000..81f3c9fe03d --- /dev/null +++ b/libgo/go/image/ycbcr.go @@ -0,0 +1,87 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package image + +import ( + "image/color" +) + +// YCbCrSubsampleRatio is the chroma subsample ratio used in a YCbCr image. +type YCbCrSubsampleRatio int + +const ( + YCbCrSubsampleRatio444 YCbCrSubsampleRatio = iota + YCbCrSubsampleRatio422 + YCbCrSubsampleRatio420 +) + +// YCbCr is an in-memory image of Y'CbCr colors. There is one Y sample per +// pixel, but each Cb and Cr sample can span one or more pixels. +// YStride is the Y slice index delta between vertically adjacent pixels. +// CStride is the Cb and Cr slice index delta between vertically adjacent pixels +// that map to separate chroma samples. +// It is not an absolute requirement, but YStride and len(Y) are typically +// multiples of 8, and: +// For 4:4:4, CStride == YStride/1 && len(Cb) == len(Cr) == len(Y)/1. +// For 4:2:2, CStride == YStride/2 && len(Cb) == len(Cr) == len(Y)/2. +// For 4:2:0, CStride == YStride/2 && len(Cb) == len(Cr) == len(Y)/4. +type YCbCr struct { + Y []uint8 + Cb []uint8 + Cr []uint8 + YStride int + CStride int + SubsampleRatio YCbCrSubsampleRatio + Rect Rectangle +} + +func (p *YCbCr) ColorModel() color.Model { + return color.YCbCrModel +} + +func (p *YCbCr) Bounds() Rectangle { + return p.Rect +} + +func (p *YCbCr) At(x, y int) color.Color { + if !(Point{x, y}.In(p.Rect)) { + return color.YCbCr{} + } + switch p.SubsampleRatio { + case YCbCrSubsampleRatio422: + i := x / 2 + return color.YCbCr{ + p.Y[y*p.YStride+x], + p.Cb[y*p.CStride+i], + p.Cr[y*p.CStride+i], + } + case YCbCrSubsampleRatio420: + i, j := x/2, y/2 + return color.YCbCr{ + p.Y[y*p.YStride+x], + p.Cb[j*p.CStride+i], + p.Cr[j*p.CStride+i], + } + } + // Default to 4:4:4 subsampling. + return color.YCbCr{ + p.Y[y*p.YStride+x], + p.Cb[y*p.CStride+x], + p.Cr[y*p.CStride+x], + } +} + +// SubImage returns an image representing the portion of the image p visible +// through r. The returned value shares pixels with the original image. +func (p *YCbCr) SubImage(r Rectangle) Image { + q := new(YCbCr) + *q = *p + q.Rect = q.Rect.Intersect(r) + return q +} + +func (p *YCbCr) Opaque() bool { + return true +} diff --git a/libgo/go/image/ycbcr/ycbcr.go b/libgo/go/image/ycbcr/ycbcr.go deleted file mode 100644 index 84a35a3fb5e..00000000000 --- a/libgo/go/image/ycbcr/ycbcr.go +++ /dev/null @@ -1,184 +0,0 @@ -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package ycbcr provides images from the Y'CbCr color model. -// -// JPEG, VP8, the MPEG family and other codecs use this color model. Such -// codecs often use the terms YUV and Y'CbCr interchangeably, but strictly -// speaking, the term YUV applies only to analog video signals. -// -// Conversion between RGB and Y'CbCr is lossy and there are multiple, slightly -// different formulae for converting between the two. This package follows -// the JFIF specification at http://www.w3.org/Graphics/JPEG/jfif3.pdf. -package ycbcr - -import ( - "image" - "image/color" -) - -// RGBToYCbCr converts an RGB triple to a YCbCr triple. All components lie -// within the range [0, 255]. -func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) { - // The JFIF specification says: - // Y' = 0.2990*R + 0.5870*G + 0.1140*B - // Cb = -0.1687*R - 0.3313*G + 0.5000*B + 128 - // Cr = 0.5000*R - 0.4187*G - 0.0813*B + 128 - // http://www.w3.org/Graphics/JPEG/jfif3.pdf says Y but means Y'. - r1 := int(r) - g1 := int(g) - b1 := int(b) - yy := (19595*r1 + 38470*g1 + 7471*b1 + 1<<15) >> 16 - cb := (-11056*r1 - 21712*g1 + 32768*b1 + 257<<15) >> 16 - cr := (32768*r1 - 27440*g1 - 5328*b1 + 257<<15) >> 16 - if yy < 0 { - yy = 0 - } else if yy > 255 { - yy = 255 - } - if cb < 0 { - cb = 0 - } else if cb > 255 { - cb = 255 - } - if cr < 0 { - cr = 0 - } else if cr > 255 { - cr = 255 - } - return uint8(yy), uint8(cb), uint8(cr) -} - -// YCbCrToRGB converts a YCbCr triple to an RGB triple. All components lie -// within the range [0, 255]. -func YCbCrToRGB(y, cb, cr uint8) (uint8, uint8, uint8) { - // The JFIF specification says: - // R = Y' + 1.40200*(Cr-128) - // G = Y' - 0.34414*(Cb-128) - 0.71414*(Cr-128) - // B = Y' + 1.77200*(Cb-128) - // http://www.w3.org/Graphics/JPEG/jfif3.pdf says Y but means Y'. - yy1 := int(y)<<16 + 1<<15 - cb1 := int(cb) - 128 - cr1 := int(cr) - 128 - r := (yy1 + 91881*cr1) >> 16 - g := (yy1 - 22554*cb1 - 46802*cr1) >> 16 - b := (yy1 + 116130*cb1) >> 16 - if r < 0 { - r = 0 - } else if r > 255 { - r = 255 - } - if g < 0 { - g = 0 - } else if g > 255 { - g = 255 - } - if b < 0 { - b = 0 - } else if b > 255 { - b = 255 - } - return uint8(r), uint8(g), uint8(b) -} - -// YCbCrColor represents a fully opaque 24-bit Y'CbCr color, having 8 bits for -// each of one luma and two chroma components. -type YCbCrColor struct { - Y, Cb, Cr uint8 -} - -func (c YCbCrColor) RGBA() (uint32, uint32, uint32, uint32) { - r, g, b := YCbCrToRGB(c.Y, c.Cb, c.Cr) - return uint32(r) * 0x101, uint32(g) * 0x101, uint32(b) * 0x101, 0xffff -} - -func toYCbCrColor(c color.Color) color.Color { - if _, ok := c.(YCbCrColor); ok { - return c - } - r, g, b, _ := c.RGBA() - y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8)) - return YCbCrColor{y, u, v} -} - -// YCbCrColorModel is the color model for YCbCrColor. -var YCbCrColorModel color.Model = color.ModelFunc(toYCbCrColor) - -// SubsampleRatio is the chroma subsample ratio used in a YCbCr image. -type SubsampleRatio int - -const ( - SubsampleRatio444 SubsampleRatio = iota - SubsampleRatio422 - SubsampleRatio420 -) - -// YCbCr is an in-memory image of YCbCr colors. There is one Y sample per pixel, -// but each Cb and Cr sample can span one or more pixels. -// YStride is the Y slice index delta between vertically adjacent pixels. -// CStride is the Cb and Cr slice index delta between vertically adjacent pixels -// that map to separate chroma samples. -// It is not an absolute requirement, but YStride and len(Y) are typically -// multiples of 8, and: -// For 4:4:4, CStride == YStride/1 && len(Cb) == len(Cr) == len(Y)/1. -// For 4:2:2, CStride == YStride/2 && len(Cb) == len(Cr) == len(Y)/2. -// For 4:2:0, CStride == YStride/2 && len(Cb) == len(Cr) == len(Y)/4. -type YCbCr struct { - Y []uint8 - Cb []uint8 - Cr []uint8 - YStride int - CStride int - SubsampleRatio SubsampleRatio - Rect image.Rectangle -} - -func (p *YCbCr) ColorModel() color.Model { - return YCbCrColorModel -} - -func (p *YCbCr) Bounds() image.Rectangle { - return p.Rect -} - -func (p *YCbCr) At(x, y int) color.Color { - if !(image.Point{x, y}.In(p.Rect)) { - return YCbCrColor{} - } - switch p.SubsampleRatio { - case SubsampleRatio422: - i := x / 2 - return YCbCrColor{ - p.Y[y*p.YStride+x], - p.Cb[y*p.CStride+i], - p.Cr[y*p.CStride+i], - } - case SubsampleRatio420: - i, j := x/2, y/2 - return YCbCrColor{ - p.Y[y*p.YStride+x], - p.Cb[j*p.CStride+i], - p.Cr[j*p.CStride+i], - } - } - // Default to 4:4:4 subsampling. - return YCbCrColor{ - p.Y[y*p.YStride+x], - p.Cb[y*p.CStride+x], - p.Cr[y*p.CStride+x], - } -} - -// SubImage returns an image representing the portion of the image p visible -// through r. The returned value shares pixels with the original image. -func (p *YCbCr) SubImage(r image.Rectangle) image.Image { - q := new(YCbCr) - *q = *p - q.Rect = q.Rect.Intersect(r) - return q -} - -func (p *YCbCr) Opaque() bool { - return true -} diff --git a/libgo/go/io/ioutil/ioutil_test.go b/libgo/go/io/ioutil/ioutil_test.go index 89d6815ad50..103066817cd 100644 --- a/libgo/go/io/ioutil/ioutil_test.go +++ b/libgo/go/io/ioutil/ioutil_test.go @@ -37,7 +37,11 @@ func TestReadFile(t *testing.T) { } func TestWriteFile(t *testing.T) { - filename := "_test/rumpelstilzchen" + f, err := TempFile("", "ioutil-test") + if err != nil { + t.Fatal(err) + } + filename := f.Name() data := "Programming today is a race between software engineers striving to " + "build bigger and better idiot-proof programs, and the Universe trying " + "to produce bigger and better idiots. So far, the Universe is winning." @@ -56,6 +60,7 @@ func TestWriteFile(t *testing.T) { } // cleanup + f.Close() os.Remove(filename) // ignore error } @@ -66,26 +71,28 @@ func TestReadDir(t *testing.T) { t.Fatalf("ReadDir %s: error expected, none found", dirname) } - dirname = "." + dirname = ".." list, err := ReadDir(dirname) if err != nil { t.Fatalf("ReadDir %s: %v", dirname, err) } - foundTest := false - foundTestDir := false +/* Does not work in gccgo testing environment. + foundFile := false + foundSubDir := false for _, dir := range list { switch { - case !dir.IsDir() && dir.Name() == "ioutil_test.go": - foundTest = true - case dir.IsDir() && dir.Name() == "_test": - foundTestDir = true + case !dir.IsDir() && dir.Name() == "io_test.go": + foundFile = true + case dir.IsDir() && dir.Name() == "ioutil": + foundSubDir = true } } - if !foundTest { - t.Fatalf("ReadDir %s: test file not found", dirname) + if !foundFile { + t.Fatalf("ReadDir %s: io_test.go file not found", dirname) } - if !foundTestDir { - t.Fatalf("ReadDir %s: _test directory not found", dirname) + if !foundSubDir { + t.Fatalf("ReadDir %s: ioutil directory not found", dirname) } +*/ } diff --git a/libgo/go/io/pipe_test.go b/libgo/go/io/pipe_test.go index fabf79bd61c..7718151b0e5 100644 --- a/libgo/go/io/pipe_test.go +++ b/libgo/go/io/pipe_test.go @@ -165,7 +165,7 @@ var pipeTests = []pipeTest{ } func delayClose(t *testing.T, cl closer, ch chan int, tt pipeTest) { - time.Sleep(1e6) // 1 ms + time.Sleep(1 * time.Millisecond) var err error if tt.closeWithError { err = cl.CloseWithError(tt.err) diff --git a/libgo/go/log/syslog/syslog.go b/libgo/go/log/syslog/syslog.go index 546bc296a5f..914391af80d 100644 --- a/libgo/go/log/syslog/syslog.go +++ b/libgo/go/log/syslog/syslog.go @@ -92,11 +92,13 @@ func (w *Writer) Emerg(m string) (err error) { _, err = w.writeString(LOG_EMERG, m) return err } + // Crit logs a message using the LOG_CRIT priority. func (w *Writer) Crit(m string) (err error) { _, err = w.writeString(LOG_CRIT, m) return err } + // ERR logs a message using the LOG_ERR priority. func (w *Writer) Err(m string) (err error) { _, err = w.writeString(LOG_ERR, m) @@ -114,11 +116,13 @@ func (w *Writer) Notice(m string) (err error) { _, err = w.writeString(LOG_NOTICE, m) return err } + // Info logs a message using the LOG_INFO priority. func (w *Writer) Info(m string) (err error) { _, err = w.writeString(LOG_INFO, m) return err } + // Debug logs a message using the LOG_DEBUG priority. func (w *Writer) Debug(m string) (err error) { _, err = w.writeString(LOG_DEBUG, m) diff --git a/libgo/go/math/abs.go b/libgo/go/math/abs.go index 4c6297c6f33..6818998ee91 100644 --- a/libgo/go/math/abs.go +++ b/libgo/go/math/abs.go @@ -9,7 +9,12 @@ package math // Special cases are: // Abs(±Inf) = +Inf // Abs(NaN) = NaN +func libc_fabs(float64) float64 __asm__("fabs") func Abs(x float64) float64 { + return libc_fabs(x) +} + +func abs(x float64) float64 { switch { case x < 0: return -x diff --git a/libgo/go/math/abs_decl.go b/libgo/go/math/abs_decl.go deleted file mode 100644 index 6be9305ac50..00000000000 --- a/libgo/go/math/abs_decl.go +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -func Abs(x float64) float64 diff --git a/libgo/go/math/acosh.go b/libgo/go/math/acosh.go index 7e8740b89bd..8d556377f59 100644 --- a/libgo/go/math/acosh.go +++ b/libgo/go/math/acosh.go @@ -36,6 +36,7 @@ package math // Acosh(x) calculates the inverse hyperbolic cosine of x. // // Special cases are: +// Acosh(+Inf) = +Inf // Acosh(x) = NaN if x < 1 // Acosh(NaN) = NaN func Acosh(x float64) float64 { diff --git a/libgo/go/math/all_test.go b/libgo/go/math/all_test.go index 7e63023a1c4..101c8dd85b4 100644 --- a/libgo/go/math/all_test.go +++ b/libgo/go/math/all_test.go @@ -22,6 +22,7 @@ var vf = []float64{ 1.8253080916808550e+00, -8.6859247685756013e+00, } + // The expected results below were computed by the high precision calculators // at http://keisan.casio.com/. More exact input values (array vf[], above) // were obtained by printing them with "%.26f". The answers were calculated @@ -159,6 +160,7 @@ var cos = []float64{ -2.517729313893103197176091e-01, -7.39241351595676573201918e-01, } + // Results for 100000 * Pi + vf[i] var cosLarge = []float64{ 2.634752141185559426744e-01, @@ -514,6 +516,7 @@ var sin = []float64{ 9.6778633541687993721617774e-01, -6.734405869050344734943028e-01, } + // Results for 100000 * Pi + vf[i] var sinLarge = []float64{ -9.646661658548936063912e-01, @@ -563,6 +566,7 @@ var tan = []float64{ -3.843885560201130679995041e+00, 9.10988793377685105753416e-01, } + // Results for 100000 * Pi + vf[i] var tanLarge = []float64{ -3.66131656475596512705e+00, @@ -958,6 +962,75 @@ var fabsSC = []float64{ NaN(), } +var vffdimSC = [][2]float64{ + {Inf(-1), Inf(-1)}, + {Inf(-1), Inf(1)}, + {Inf(-1), NaN()}, + {Copysign(0, -1), Copysign(0, -1)}, + {Copysign(0, -1), 0}, + {0, Copysign(0, -1)}, + {0, 0}, + {Inf(1), Inf(-1)}, + {Inf(1), Inf(1)}, + {Inf(1), NaN()}, + {NaN(), Inf(-1)}, + {NaN(), Copysign(0, -1)}, + {NaN(), 0}, + {NaN(), Inf(1)}, + {NaN(), NaN()}, +} +var fdimSC = []float64{ + NaN(), + 0, + NaN(), + 0, + 0, + 0, + 0, + Inf(1), + NaN(), + NaN(), + NaN(), + NaN(), + NaN(), + NaN(), + NaN(), +} +var fmaxSC = []float64{ + Inf(-1), + Inf(1), + NaN(), + Copysign(0, -1), + 0, + 0, + 0, + Inf(1), + Inf(1), + Inf(1), + NaN(), + NaN(), + NaN(), + Inf(1), + NaN(), +} +var fminSC = []float64{ + Inf(-1), + Inf(-1), + Inf(-1), + Copysign(0, -1), + Copysign(0, -1), + Copysign(0, -1), + 0, + Inf(-1), + Inf(1), + NaN(), + Inf(-1), + NaN(), + NaN(), + NaN(), + NaN(), +} + var vffmodSC = [][2]float64{ {Inf(-1), Inf(-1)}, {Inf(-1), -Pi}, @@ -1259,12 +1332,26 @@ var modfSC = [][2]float64{ } var vfnextafterSC = [][2]float64{ + {0, 0}, + {0, Copysign(0, -1)}, + {0, -1}, {0, NaN()}, + {Copysign(0, -1), 1}, + {Copysign(0, -1), 0}, + {Copysign(0, -1), Copysign(0, -1)}, + {Copysign(0, -1), -1}, {NaN(), 0}, {NaN(), NaN()}, } var nextafterSC = []float64{ + 0, + 0, + -4.9406564584124654418e-324, // Float64frombits(0x8000000000000001) NaN(), + 4.9406564584124654418e-324, // Float64frombits(0x0000000000000001) + Copysign(0, -1), + Copysign(0, -1), + -4.9406564584124654418e-324, // Float64frombits(0x8000000000000001) NaN(), NaN(), } @@ -1875,6 +1962,11 @@ func TestDim(t *testing.T) { t.Errorf("Dim(%g, %g) = %g, want %g", vf[i], 0.0, f, fdim[i]) } } + for i := 0; i < len(vffdimSC); i++ { + if f := Dim(vffdimSC[i][0], vffdimSC[i][1]); !alike(fdimSC[i], f) { + t.Errorf("Dim(%g, %g) = %g, want %g", vffdimSC[i][0], vffdimSC[i][1], f, fdimSC[i]) + } + } } func TestFloor(t *testing.T) { @@ -1896,6 +1988,11 @@ func TestMax(t *testing.T) { t.Errorf("Max(%g, %g) = %g, want %g", vf[i], ceil[i], f, ceil[i]) } } + for i := 0; i < len(vffdimSC); i++ { + if f := Max(vffdimSC[i][0], vffdimSC[i][1]); !alike(fmaxSC[i], f) { + t.Errorf("Max(%g, %g) = %g, want %g", vffdimSC[i][0], vffdimSC[i][1], f, fmaxSC[i]) + } + } } func TestMin(t *testing.T) { @@ -1904,6 +2001,11 @@ func TestMin(t *testing.T) { t.Errorf("Min(%g, %g) = %g, want %g", vf[i], floor[i], f, floor[i]) } } + for i := 0; i < len(vffdimSC); i++ { + if f := Min(vffdimSC[i][0], vffdimSC[i][1]); !alike(fminSC[i], f) { + t.Errorf("Min(%g, %g) = %g, want %g", vffdimSC[i][0], vffdimSC[i][1], f, fminSC[i]) + } + } } func TestMod(t *testing.T) { @@ -1964,6 +2066,20 @@ func TestHypot(t *testing.T) { } } +func TestHypotGo(t *testing.T) { + for i := 0; i < len(vf); i++ { + a := Abs(1e200 * tanh[i] * Sqrt(2)) + if f := HypotGo(1e200*tanh[i], 1e200*tanh[i]); !veryclose(a, f) { + t.Errorf("HypotGo(%g, %g) = %g, want %g", 1e200*tanh[i], 1e200*tanh[i], f, a) + } + } + for i := 0; i < len(vfhypotSC); i++ { + if f := HypotGo(vfhypotSC[i][0], vfhypotSC[i][1]); !alike(hypotSC[i], f) { + t.Errorf("HypotGo(%g, %g) = %g, want %g", vfhypotSC[i][0], vfhypotSC[i][1], f, hypotSC[i]) + } + } +} + func TestIlogb(t *testing.T) { for i := 0; i < len(vf); i++ { a := frexp[i].i - 1 // adjust because fr in the interval [½, 1) @@ -2175,7 +2291,7 @@ func TestNextafter(t *testing.T) { t.Errorf("Nextafter(%g, %g) = %g want %g", vf[i], 10.0, f, nextafter[i]) } } - for i := 0; i < len(vfmodfSC); i++ { + for i := 0; i < len(vfnextafterSC); i++ { if f := Nextafter(vfnextafterSC[i][0], vfnextafterSC[i][1]); !alike(nextafterSC[i], f) { t.Errorf("Nextafter(%g, %g) = %g want %g", vfnextafterSC[i][0], vfnextafterSC[i][1], f, nextafterSC[i]) } diff --git a/libgo/go/math/asin.go b/libgo/go/math/asin.go index 0a0b0a11cb4..d67f32a5097 100644 --- a/libgo/go/math/asin.go +++ b/libgo/go/math/asin.go @@ -16,7 +16,12 @@ package math // Special cases are: // Asin(±0) = ±0 // Asin(x) = NaN if x < -1 or x > 1 +func libc_asin(float64) float64 __asm__("asin") func Asin(x float64) float64 { + return libc_asin(x) +} + +func asin(x float64) float64 { if x == 0 { return x // special case } @@ -46,4 +51,11 @@ func Asin(x float64) float64 { // // Special case is: // Acos(x) = NaN if x < -1 or x > 1 -func Acos(x float64) float64 { return Pi/2 - Asin(x) } +func libc_acos(float64) float64 __asm__("acos") +func Acos(x float64) float64 { + return libc_acos(x) +} + +func acos(x float64) float64 { + return Pi/2 - Asin(x) +} diff --git a/libgo/go/math/asinh.go b/libgo/go/math/asinh.go index d6979463d65..f786dd9f8f0 100644 --- a/libgo/go/math/asinh.go +++ b/libgo/go/math/asinh.go @@ -33,6 +33,7 @@ package math // Asinh(x) calculates the inverse hyperbolic sine of x. // // Special cases are: +// Asinh(±0) = ±0 // Asinh(±Inf) = ±Inf // Asinh(NaN) = NaN func Asinh(x float64) float64 { diff --git a/libgo/go/math/atan.go b/libgo/go/math/atan.go index 9d4ec2f72db..ff52cf3d367 100644 --- a/libgo/go/math/atan.go +++ b/libgo/go/math/atan.go @@ -51,7 +51,12 @@ func satan(arg float64) float64 { // Special cases are: // Atan(±0) = ±0 // Atan(±Inf) = ±Pi/2 +func libc_atan(float64) float64 __asm__("atan") func Atan(x float64) float64 { + return libc_atan(x) +} + +func atan(x float64) float64 { if x == 0 { return x } diff --git a/libgo/go/math/atan2.go b/libgo/go/math/atan2.go index 49d4bdd7191..7260f986fad 100644 --- a/libgo/go/math/atan2.go +++ b/libgo/go/math/atan2.go @@ -26,7 +26,12 @@ package math // Atan2(y<0, -Inf) = -Pi // Atan2(+Inf, x) = +Pi/2 // Atan2(-Inf, x) = -Pi/2 +func libc_atan2(float64, float64) float64 __asm__("atan2") func Atan2(y, x float64) float64 { + return libc_atan2(y, x) +} + +func atan2(y, x float64) float64 { // TODO(rsc): Remove manual inlining of IsNaN, IsInf // when compiler does it for us // special cases diff --git a/libgo/go/math/atanh.go b/libgo/go/math/atanh.go index ed38fcac665..e150673c700 100644 --- a/libgo/go/math/atanh.go +++ b/libgo/go/math/atanh.go @@ -39,9 +39,10 @@ package math // Atanh(x) calculates the inverse hyperbolic tangent of x. // // Special cases are: -// Atanh(x) = NaN if x < -1 or x > 1 // Atanh(1) = +Inf +// Atanh(±0) = ±0 // Atanh(-1) = -Inf +// Atanh(x) = NaN if x < -1 or x > 1 // Atanh(NaN) = NaN func Atanh(x float64) float64 { const NearZero = 1.0 / (1 << 28) // 2**-28 diff --git a/libgo/go/math/big/int_test.go b/libgo/go/math/big/int_test.go index aa7c1949549..9c4b7301874 100644 --- a/libgo/go/math/big/int_test.go +++ b/libgo/go/math/big/int_test.go @@ -9,6 +9,7 @@ import ( "encoding/gob" "encoding/hex" "fmt" + "math/rand" "testing" "testing/quick" ) @@ -1405,3 +1406,9 @@ func TestIntGobEncoding(t *testing.T) { } } } + +func TestIssue2607(t *testing.T) { + // This code sequence used to hang. + n := NewInt(10) + n.Rand(rand.New(rand.NewSource(9)), n) +} diff --git a/libgo/go/math/big/nat.go b/libgo/go/math/big/nat.go index 680445dc9a7..69681ae2d64 100644 --- a/libgo/go/math/big/nat.go +++ b/libgo/go/math/big/nat.go @@ -592,7 +592,7 @@ func (x nat) bitLen() int { const MaxBase = 'z' - 'a' + 10 + 1 // = hexValue('z') + 1 func hexValue(ch rune) Word { - d := MaxBase + 1 // illegal base + d := int(MaxBase + 1) // illegal base switch { case '0' <= ch && ch <= '9': d = int(ch - '0') @@ -1196,12 +1196,16 @@ func (x nat) powersOfTwoDecompose() (q nat, k int) { // random creates a random integer in [0..limit), using the space in z if // possible. n is the bit length of limit. func (z nat) random(rand *rand.Rand, limit nat, n int) nat { + if alias(z, limit) { + z = nil // z is an alias for limit - cannot reuse + } + z = z.make(len(limit)) + bitLengthOfMSW := uint(n % _W) if bitLengthOfMSW == 0 { bitLengthOfMSW = _W } mask := Word((1 << bitLengthOfMSW) - 1) - z = z.make(len(limit)) for { for i := range z { diff --git a/libgo/go/math/big/nat_test.go b/libgo/go/math/big/nat_test.go index e3c6552d9fb..25e39273c0c 100644 --- a/libgo/go/math/big/nat_test.go +++ b/libgo/go/math/big/nat_test.go @@ -5,7 +5,6 @@ package big import ( - "fmt" "io" "strings" "testing" @@ -402,7 +401,7 @@ func ScanHelper(b *testing.B, base int, x, y Word) { var s string s = z.string(lowercaseDigits[0:base]) if t := toString(z, lowercaseDigits[0:base]); t != s { - panic(fmt.Sprintf("scanning: got %s; want %s", s, t)) + b.Fatalf("scanning: got %s; want %s", s, t) } b.StartTimer() diff --git a/libgo/go/math/const.go b/libgo/go/math/const.go index a108d3e294d..282561f98bb 100644 --- a/libgo/go/math/const.go +++ b/libgo/go/math/const.go @@ -49,5 +49,3 @@ const ( MaxUint32 = 1<<32 - 1 MaxUint64 = 1<<64 - 1 ) - -// BUG(rsc): The manual should define the special cases for all of these functions. diff --git a/libgo/go/math/dim.go b/libgo/go/math/dim.go index d2eb52f3bf1..24a84e6d03b 100644 --- a/libgo/go/math/dim.go +++ b/libgo/go/math/dim.go @@ -5,15 +5,45 @@ package math // Dim returns the maximum of x-y or 0. +// +// Special cases are: +// Dim(+Inf, +Inf) = NaN +// Dim(-Inf, -Inf) = NaN +// Dim(x, NaN) = Dim(NaN, x) = NaN func Dim(x, y float64) float64 { - if x > y { - return x - y - } - return 0 + return dim(x, y) +} + +func dim(x, y float64) float64 { + return max(x-y, 0) } // Max returns the larger of x or y. +// +// Special cases are: +// Max(x, +Inf) = Max(+Inf, x) = +Inf +// Max(x, NaN) = Max(NaN, x) = NaN +// Max(+0, ±0) = Max(±0, +0) = +0 +// Max(-0, -0) = -0 func Max(x, y float64) float64 { + return max(x, y) +} + +func max(x, y float64) float64 { + // TODO(rsc): Remove manual inlining of IsNaN, IsInf + // when compiler does it for us + // special cases + switch { + case x > MaxFloat64 || y > MaxFloat64: // IsInf(x, 1) || IsInf(y, 1): + return Inf(1) + case x != x || y != y: // IsNaN(x) || IsNaN(y): + return NaN() + case x == 0 && x == y: + if Signbit(x) { + return y + } + return x + } if x > y { return x } @@ -21,7 +51,30 @@ func Max(x, y float64) float64 { } // Min returns the smaller of x or y. +// +// Special cases are: +// Min(x, -Inf) = Min(-Inf, x) = -Inf +// Min(x, NaN) = Min(NaN, x) = NaN +// Min(-0, ±0) = Min(±0, -0) = -0 func Min(x, y float64) float64 { + return min(x, y) +} + +func min(x, y float64) float64 { + // TODO(rsc): Remove manual inlining of IsNaN, IsInf + // when compiler does it for us + // special cases + switch { + case x < -MaxFloat64 || y < -MaxFloat64: // IsInf(x, -1) || IsInf(y, -1): + return Inf(-1) + case x != x || y != y: // IsNaN(x) || IsNaN(y): + return NaN() + case x == 0 && x == y: + if Signbit(x) { + return x + } + return y + } if x < y { return x } diff --git a/libgo/go/math/dim_decl.go b/libgo/go/math/dim_decl.go deleted file mode 100644 index 196f84fd793..00000000000 --- a/libgo/go/math/dim_decl.go +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -func Dim(x, y float64) float64 -func Max(x, y float64) float64 -func Min(x, y float64) float64 diff --git a/libgo/go/math/exp.go b/libgo/go/math/exp.go index c519c2cb6b6..2db691f38f1 100644 --- a/libgo/go/math/exp.go +++ b/libgo/go/math/exp.go @@ -11,4 +11,190 @@ package math // Exp(NaN) = NaN // Very large values overflow to 0 or +Inf. // Very small values underflow to 1. -func Exp(x float64) float64 { return expGo(x) } +func libc_exp(float64) float64 __asm__("exp") +func Exp(x float64) float64 { + return libc_exp(x) +} + +// The original C code, the long comment, and the constants +// below are from FreeBSD's /usr/src/lib/msun/src/e_exp.c +// and came with this notice. The go code is a simplified +// version of the original C. +// +// ==================================================== +// Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. +// +// Permission to use, copy, modify, and distribute this +// software is freely granted, provided that this notice +// is preserved. +// ==================================================== +// +// +// exp(x) +// Returns the exponential of x. +// +// Method +// 1. Argument reduction: +// Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. +// Given x, find r and integer k such that +// +// x = k*ln2 + r, |r| <= 0.5*ln2. +// +// Here r will be represented as r = hi-lo for better +// accuracy. +// +// 2. Approximation of exp(r) by a special rational function on +// the interval [0,0.34658]: +// Write +// R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... +// We use a special Remes algorithm on [0,0.34658] to generate +// a polynomial of degree 5 to approximate R. The maximum error +// of this polynomial approximation is bounded by 2**-59. In +// other words, +// R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 +// (where z=r*r, and the values of P1 to P5 are listed below) +// and +// | 5 | -59 +// | 2.0+P1*z+...+P5*z - R(z) | <= 2 +// | | +// The computation of exp(r) thus becomes +// 2*r +// exp(r) = 1 + ------- +// R - r +// r*R1(r) +// = 1 + r + ----------- (for better accuracy) +// 2 - R1(r) +// where +// 2 4 10 +// R1(r) = r - (P1*r + P2*r + ... + P5*r ). +// +// 3. Scale back to obtain exp(x): +// From step 1, we have +// exp(x) = 2**k * exp(r) +// +// Special cases: +// exp(INF) is INF, exp(NaN) is NaN; +// exp(-INF) is 0, and +// for finite argument, only exp(0)=1 is exact. +// +// Accuracy: +// according to an error analysis, the error is always less than +// 1 ulp (unit in the last place). +// +// Misc. info. +// For IEEE double +// if x > 7.09782712893383973096e+02 then exp(x) overflow +// if x < -7.45133219101941108420e+02 then exp(x) underflow +// +// Constants: +// The hexadecimal values are the intended ones for the following +// constants. The decimal values may be used, provided that the +// compiler will convert from decimal to binary accurately enough +// to produce the hexadecimal values shown. + +func exp(x float64) float64 { + const ( + Ln2Hi = 6.93147180369123816490e-01 + Ln2Lo = 1.90821492927058770002e-10 + Log2e = 1.44269504088896338700e+00 + + Overflow = 7.09782712893383973096e+02 + Underflow = -7.45133219101941108420e+02 + NearZero = 1.0 / (1 << 28) // 2**-28 + ) + + // TODO(rsc): Remove manual inlining of IsNaN, IsInf + // when compiler does it for us + // special cases + switch { + case x != x || x > MaxFloat64: // IsNaN(x) || IsInf(x, 1): + return x + case x < -MaxFloat64: // IsInf(x, -1): + return 0 + case x > Overflow: + return Inf(1) + case x < Underflow: + return 0 + case -NearZero < x && x < NearZero: + return 1 + x + } + + // reduce; computed as r = hi - lo for extra precision. + var k int + switch { + case x < 0: + k = int(Log2e*x - 0.5) + case x > 0: + k = int(Log2e*x + 0.5) + } + hi := x - float64(k)*Ln2Hi + lo := float64(k) * Ln2Lo + + // compute + return expmulti(hi, lo, k) +} + +// Exp2 returns 2**x, the base-2 exponential of x. +// +// Special cases are the same as Exp. +func Exp2(x float64) float64 { + return exp2(x) +} + +func exp2(x float64) float64 { + const ( + Ln2Hi = 6.93147180369123816490e-01 + Ln2Lo = 1.90821492927058770002e-10 + + Overflow = 1.0239999999999999e+03 + Underflow = -1.0740e+03 + ) + + // TODO: remove manual inlining of IsNaN and IsInf + // when compiler does it for us + // special cases + switch { + case x != x || x > MaxFloat64: // IsNaN(x) || IsInf(x, 1): + return x + case x < -MaxFloat64: // IsInf(x, -1): + return 0 + case x > Overflow: + return Inf(1) + case x < Underflow: + return 0 + } + + // argument reduction; x = r×lg(e) + k with |r| ≤ ln(2)/2. + // computed as r = hi - lo for extra precision. + var k int + switch { + case x > 0: + k = int(x + 0.5) + case x < 0: + k = int(x - 0.5) + } + t := x - float64(k) + hi := t * Ln2Hi + lo := -t * Ln2Lo + + // compute + return expmulti(hi, lo, k) +} + +// exp1 returns e**r × 2**k where r = hi - lo and |r| ≤ ln(2)/2. +func expmulti(hi, lo float64, k int) float64 { + const ( + P1 = 1.66666666666666019037e-01 /* 0x3FC55555; 0x5555553E */ + P2 = -2.77777777770155933842e-03 /* 0xBF66C16C; 0x16BEBD93 */ + P3 = 6.61375632143793436117e-05 /* 0x3F11566A; 0xAF25DE2C */ + P4 = -1.65339022054652515390e-06 /* 0xBEBBBD41; 0xC5D26BF1 */ + P5 = 4.13813679705723846039e-08 /* 0x3E663769; 0x72BEA4D0 */ + ) + + r := hi - lo + t := r * r + c := r - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))) + y := 1 - ((lo - (r*c)/(2-c)) - hi) + // TODO(rsc): make sure Ldexp can handle boundary k + return Ldexp(y, k) +} diff --git a/libgo/go/math/exp2.go b/libgo/go/math/exp2.go deleted file mode 100644 index 1cface9d360..00000000000 --- a/libgo/go/math/exp2.go +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -// Exp2 returns 2**x, the base-2 exponential of x. -// -// Special cases are the same as Exp. -func Exp2(x float64) float64 { return exp2Go(x) } diff --git a/libgo/go/math/exp_port.go b/libgo/go/math/exp_port.go deleted file mode 100644 index 618c31a5d11..00000000000 --- a/libgo/go/math/exp_port.go +++ /dev/null @@ -1,191 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -// The original C code, the long comment, and the constants -// below are from FreeBSD's /usr/src/lib/msun/src/e_exp.c -// and came with this notice. The go code is a simplified -// version of the original C. -// -// ==================================================== -// Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. -// -// Permission to use, copy, modify, and distribute this -// software is freely granted, provided that this notice -// is preserved. -// ==================================================== -// -// -// exp(x) -// Returns the exponential of x. -// -// Method -// 1. Argument reduction: -// Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. -// Given x, find r and integer k such that -// -// x = k*ln2 + r, |r| <= 0.5*ln2. -// -// Here r will be represented as r = hi-lo for better -// accuracy. -// -// 2. Approximation of exp(r) by a special rational function on -// the interval [0,0.34658]: -// Write -// R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... -// We use a special Remes algorithm on [0,0.34658] to generate -// a polynomial of degree 5 to approximate R. The maximum error -// of this polynomial approximation is bounded by 2**-59. In -// other words, -// R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 -// (where z=r*r, and the values of P1 to P5 are listed below) -// and -// | 5 | -59 -// | 2.0+P1*z+...+P5*z - R(z) | <= 2 -// | | -// The computation of exp(r) thus becomes -// 2*r -// exp(r) = 1 + ------- -// R - r -// r*R1(r) -// = 1 + r + ----------- (for better accuracy) -// 2 - R1(r) -// where -// 2 4 10 -// R1(r) = r - (P1*r + P2*r + ... + P5*r ). -// -// 3. Scale back to obtain exp(x): -// From step 1, we have -// exp(x) = 2**k * exp(r) -// -// Special cases: -// exp(INF) is INF, exp(NaN) is NaN; -// exp(-INF) is 0, and -// for finite argument, only exp(0)=1 is exact. -// -// Accuracy: -// according to an error analysis, the error is always less than -// 1 ulp (unit in the last place). -// -// Misc. info. -// For IEEE double -// if x > 7.09782712893383973096e+02 then exp(x) overflow -// if x < -7.45133219101941108420e+02 then exp(x) underflow -// -// Constants: -// The hexadecimal values are the intended ones for the following -// constants. The decimal values may be used, provided that the -// compiler will convert from decimal to binary accurately enough -// to produce the hexadecimal values shown. - -// Exp returns e**x, the base-e exponential of x. -// -// Special cases are: -// Exp(+Inf) = +Inf -// Exp(NaN) = NaN -// Very large values overflow to 0 or +Inf. -// Very small values underflow to 1. -func expGo(x float64) float64 { - const ( - Ln2Hi = 6.93147180369123816490e-01 - Ln2Lo = 1.90821492927058770002e-10 - Log2e = 1.44269504088896338700e+00 - - Overflow = 7.09782712893383973096e+02 - Underflow = -7.45133219101941108420e+02 - NearZero = 1.0 / (1 << 28) // 2**-28 - ) - - // TODO(rsc): Remove manual inlining of IsNaN, IsInf - // when compiler does it for us - // special cases - switch { - case x != x || x > MaxFloat64: // IsNaN(x) || IsInf(x, 1): - return x - case x < -MaxFloat64: // IsInf(x, -1): - return 0 - case x > Overflow: - return Inf(1) - case x < Underflow: - return 0 - case -NearZero < x && x < NearZero: - return 1 + x - } - - // reduce; computed as r = hi - lo for extra precision. - var k int - switch { - case x < 0: - k = int(Log2e*x - 0.5) - case x > 0: - k = int(Log2e*x + 0.5) - } - hi := x - float64(k)*Ln2Hi - lo := float64(k) * Ln2Lo - - // compute - return exp(hi, lo, k) -} - -// Exp2 returns 2**x, the base-2 exponential of x. -// -// Special cases are the same as Exp. -func exp2Go(x float64) float64 { - const ( - Ln2Hi = 6.93147180369123816490e-01 - Ln2Lo = 1.90821492927058770002e-10 - - Overflow = 1.0239999999999999e+03 - Underflow = -1.0740e+03 - ) - - // TODO: remove manual inlining of IsNaN and IsInf - // when compiler does it for us - // special cases - switch { - case x != x || x > MaxFloat64: // IsNaN(x) || IsInf(x, 1): - return x - case x < -MaxFloat64: // IsInf(x, -1): - return 0 - case x > Overflow: - return Inf(1) - case x < Underflow: - return 0 - } - - // argument reduction; x = r×lg(e) + k with |r| ≤ ln(2)/2. - // computed as r = hi - lo for extra precision. - var k int - switch { - case x > 0: - k = int(x + 0.5) - case x < 0: - k = int(x - 0.5) - } - t := x - float64(k) - hi := t * Ln2Hi - lo := -t * Ln2Lo - - // compute - return exp(hi, lo, k) -} - -// exp returns e**r × 2**k where r = hi - lo and |r| ≤ ln(2)/2. -func exp(hi, lo float64, k int) float64 { - const ( - P1 = 1.66666666666666019037e-01 /* 0x3FC55555; 0x5555553E */ - P2 = -2.77777777770155933842e-03 /* 0xBF66C16C; 0x16BEBD93 */ - P3 = 6.61375632143793436117e-05 /* 0x3F11566A; 0xAF25DE2C */ - P4 = -1.65339022054652515390e-06 /* 0xBEBBBD41; 0xC5D26BF1 */ - P5 = 4.13813679705723846039e-08 /* 0x3E663769; 0x72BEA4D0 */ - ) - - r := hi - lo - t := r * r - c := r - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))) - y := 1 - ((lo - (r*c)/(2-c)) - hi) - // TODO(rsc): make sure Ldexp can handle boundary k - return Ldexp(y, k) -} diff --git a/libgo/go/math/exp_test.go b/libgo/go/math/exp_test.go deleted file mode 100644 index 7381fd5ad34..00000000000 --- a/libgo/go/math/exp_test.go +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -// Make expGo and exp2Go available for testing. - -func ExpGo(x float64) float64 { return expGo(x) } -func Exp2Go(x float64) float64 { return exp2Go(x) } diff --git a/libgo/go/math/expm1.go b/libgo/go/math/expm1.go index e9f833140b5..5bffdb323a4 100644 --- a/libgo/go/math/expm1.go +++ b/libgo/go/math/expm1.go @@ -121,7 +121,12 @@ package math // Expm1(-Inf) = -1 // Expm1(NaN) = NaN // Very large values overflow to -1 or +Inf. +func libc_expm1(float64) float64 __asm__("expm1") func Expm1(x float64) float64 { + return libc_expm1(x) +} + +func expm1(x float64) float64 { const ( Othreshold = 7.09782712893383973096e+02 // 0x40862E42FEFA39EF Ln2X56 = 3.88162421113569373274e+01 // 0x4043687a9f1af2b1 diff --git a/libgo/go/math/export_test.go b/libgo/go/math/export_test.go new file mode 100644 index 00000000000..02992d70e84 --- /dev/null +++ b/libgo/go/math/export_test.go @@ -0,0 +1,11 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package math + +// Export internal functions for testing. +var ExpGo = exp +var Exp2Go = exp2 +var HypotGo = hypot +var SqrtGo = sqrt diff --git a/libgo/go/math/floor.go b/libgo/go/math/floor.go index 8de4d7e2ce6..4d5f4a42707 100644 --- a/libgo/go/math/floor.go +++ b/libgo/go/math/floor.go @@ -7,9 +7,15 @@ package math // Floor returns the greatest integer value less than or equal to x. // // Special cases are: +// Floor(±0) = ±0 // Floor(±Inf) = ±Inf // Floor(NaN) = NaN +func libc_floor(float64) float64 __asm__("floor") func Floor(x float64) float64 { + return libc_floor(x) +} + +func floor(x float64) float64 { // TODO(rsc): Remove manual inlining of IsNaN, IsInf // when compiler does it for us if x == 0 || x != x || x > MaxFloat64 || x < -MaxFloat64 { // x == 0 || IsNaN(x) || IsInf(x, 0) @@ -29,16 +35,30 @@ func Floor(x float64) float64 { // Ceil returns the least integer value greater than or equal to x. // // Special cases are: +// Ceil(±0) = ±0 // Ceil(±Inf) = ±Inf // Ceil(NaN) = NaN -func Ceil(x float64) float64 { return -Floor(-x) } +func libc_ceil(float64) float64 __asm__("ceil") +func Ceil(x float64) float64 { + return libc_ceil(x) +} + +func ceil(x float64) float64 { + return -Floor(-x) +} // Trunc returns the integer value of x. // // Special cases are: +// Trunc(±0) = ±0 // Trunc(±Inf) = ±Inf // Trunc(NaN) = NaN +func libc_trunc(float64) float64 __asm__("trunc") func Trunc(x float64) float64 { + return libc_trunc(x) +} + +func trunc(x float64) float64 { // TODO(rsc): Remove manual inlining of IsNaN, IsInf // when compiler does it for us if x == 0 || x != x || x > MaxFloat64 || x < -MaxFloat64 { // x == 0 || IsNaN(x) || IsInf(x, 0) diff --git a/libgo/go/math/frexp.go b/libgo/go/math/frexp.go index 867b78f3648..6b928e714c5 100644 --- a/libgo/go/math/frexp.go +++ b/libgo/go/math/frexp.go @@ -14,6 +14,10 @@ package math // Frexp(±Inf) = ±Inf, 0 // Frexp(NaN) = NaN, 0 func Frexp(f float64) (frac float64, exp int) { + return frexp(f) +} + +func frexp(f float64) (frac float64, exp int) { // TODO(rsc): Remove manual inlining of IsNaN, IsInf // when compiler does it for us // special cases diff --git a/libgo/go/math/hypot.go b/libgo/go/math/hypot.go index ecd115d9ef8..5c7c3a832b3 100644 --- a/libgo/go/math/hypot.go +++ b/libgo/go/math/hypot.go @@ -15,6 +15,10 @@ package math // Hypot(p, q) = +Inf if p or q is infinite // Hypot(p, q) = NaN if p or q is NaN func Hypot(p, q float64) float64 { + return hypot(p, q) +} + +func hypot(p, q float64) float64 { // TODO(rsc): Remove manual inlining of IsNaN, IsInf // when compiler does it for us // special cases diff --git a/libgo/go/math/hypot_port.go b/libgo/go/math/hypot_port.go deleted file mode 100644 index 27f335ba2de..00000000000 --- a/libgo/go/math/hypot_port.go +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2009-2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -/* - Hypot -- sqrt(p*p + q*q), but overflows only if the result does. - See: - Cleve Moler and Donald Morrison, - Replacing Square Roots by Pythagorean Sums - IBM Journal of Research and Development, - Vol. 27, Number 6, pp. 577-581, Nov. 1983 -*/ - -// Hypot computes Sqrt(p*p + q*q), taking care to avoid -// unnecessary overflow and underflow. -// -// Special cases are: -// Hypot(p, q) = +Inf if p or q is infinite -// Hypot(p, q) = NaN if p or q is NaN -func hypotGo(p, q float64) float64 { - // TODO(rsc): Remove manual inlining of IsNaN, IsInf - // when compiler does it for us - // special cases - switch { - case p < -MaxFloat64 || p > MaxFloat64 || q < -MaxFloat64 || q > MaxFloat64: // IsInf(p, 0) || IsInf(q, 0): - return Inf(1) - case p != p || q != q: // IsNaN(p) || IsNaN(q): - return NaN() - } - if p < 0 { - p = -p - } - if q < 0 { - q = -q - } - - if p < q { - p, q = q, p - } - - if p == 0 { - return 0 - } - - pfac := p - q = q / p - r := q - p = 1 - for { - r = r * r - s := r + 4 - if s == 4 { - return p * pfac - } - r = r / s - p = p + 2*r*p - q = q * r - r = q / p - } - panic("unreachable") -} diff --git a/libgo/go/math/hypot_test.go b/libgo/go/math/hypot_test.go deleted file mode 100644 index 85ce1d404d6..00000000000 --- a/libgo/go/math/hypot_test.go +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -// Make hypotGo available for testing. - -func HypotGo(x, y float64) float64 { return hypotGo(x, y) } diff --git a/libgo/go/math/ldexp.go b/libgo/go/math/ldexp.go index 96c95cad4ae..4601cd58cbb 100644 --- a/libgo/go/math/ldexp.go +++ b/libgo/go/math/ldexp.go @@ -11,7 +11,12 @@ package math // Ldexp(±0, exp) = ±0 // Ldexp(±Inf, exp) = ±Inf // Ldexp(NaN, exp) = NaN +func libc_ldexp(float64, int) float64 __asm__("ldexp") func Ldexp(frac float64, exp int) float64 { + return libc_ldexp(frac, exp) +} + +func ldexp(frac float64, exp int) float64 { // TODO(rsc): Remove manual inlining of IsNaN, IsInf // when compiler does it for us // special cases diff --git a/libgo/go/math/log.go b/libgo/go/math/log.go index a786c8ce3ab..f06611dfdd2 100644 --- a/libgo/go/math/log.go +++ b/libgo/go/math/log.go @@ -77,7 +77,12 @@ package math // Log(0) = -Inf // Log(x < 0) = NaN // Log(NaN) = NaN +func libc_log(float64) float64 __asm__("log") func Log(x float64) float64 { + return libc_log(x) +} + +func log(x float64) float64 { const ( Ln2Hi = 6.93147180369123816490e-01 /* 3fe62e42 fee00000 */ Ln2Lo = 1.90821492927058770002e-10 /* 3dea39ef 35793c76 */ diff --git a/libgo/go/math/log10.go b/libgo/go/math/log10.go index 6d18baae2a9..5204492b3c4 100644 --- a/libgo/go/math/log10.go +++ b/libgo/go/math/log10.go @@ -6,8 +6,22 @@ package math // Log10 returns the decimal logarithm of x. // The special cases are the same as for Log. -func Log10(x float64) float64 { return Log(x) * (1 / Ln10) } +func libc_log10(float64) float64 __asm__("log10") +func Log10(x float64) float64 { + return libc_log10(x) +} + +func log10(x float64) float64 { + return Log(x) * (1 / Ln10) +} // Log2 returns the binary logarithm of x. // The special cases are the same as for Log. -func Log2(x float64) float64 { return Log(x) * (1 / Ln2) } +func libc_log2(float64) float64 __asm__("log2") +func Log2(x float64) float64 { + return libc_log2(x) +} + +func log2(x float64) float64 { + return Log(x) * (1 / Ln2) +} diff --git a/libgo/go/math/log10_decl.go b/libgo/go/math/log10_decl.go deleted file mode 100644 index 5aec94e1c4f..00000000000 --- a/libgo/go/math/log10_decl.go +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -func Log10(x float64) float64 -func Log2(x float64) float64 diff --git a/libgo/go/math/log1p.go b/libgo/go/math/log1p.go index e8914a1d053..39c1b40e802 100644 --- a/libgo/go/math/log1p.go +++ b/libgo/go/math/log1p.go @@ -92,7 +92,12 @@ package math // Log1p(-1) = -Inf // Log1p(x < -1) = NaN // Log1p(NaN) = NaN +func libc_log1p(float64) float64 __asm__("log1p") func Log1p(x float64) float64 { + return libc_log1p(x) +} + +func log1p(x float64) float64 { const ( Sqrt2M1 = 4.142135623730950488017e-01 // Sqrt(2)-1 = 0x3fda827999fcef34 Sqrt2HalfM1 = -2.928932188134524755992e-01 // Sqrt(2)/2-1 = 0xbfd2bec333018866 diff --git a/libgo/go/math/mod.go b/libgo/go/math/mod.go index 6b16abe5d13..347da70f832 100644 --- a/libgo/go/math/mod.go +++ b/libgo/go/math/mod.go @@ -13,9 +13,17 @@ package math // sign agrees with that of x. // // Special cases are: -// if x is not finite, Mod returns NaN -// if y is 0 or NaN, Mod returns NaN +// Mod(±Inf, y) = NaN +// Mod(NaN, y) = NaN +// Mod(x, 0) = NaN +// Mod(x, ±Inf) = x +// Mod(x, NaN) = NaN +func libc_fmod(float64, float64) float64 __asm__("fmod") func Mod(x, y float64) float64 { + return libc_fmod(x, y) +} + +func mod(x, y float64) float64 { // TODO(rsc): Remove manual inlining of IsNaN, IsInf // when compiler does it for us. if y == 0 || x > MaxFloat64 || x < -MaxFloat64 || x != x || y != y { // y == 0 || IsInf(x, 0) || IsNaN(x) || IsNan(y) diff --git a/libgo/go/math/mod_decl.go b/libgo/go/math/mod_decl.go deleted file mode 100644 index d5047a754a8..00000000000 --- a/libgo/go/math/mod_decl.go +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -func Mod(x, y float64) float64 diff --git a/libgo/go/math/modf.go b/libgo/go/math/modf.go index 34889e0c0af..ecec4b756c7 100644 --- a/libgo/go/math/modf.go +++ b/libgo/go/math/modf.go @@ -11,6 +11,10 @@ package math // Modf(±Inf) = ±Inf, NaN // Modf(NaN) = NaN, NaN func Modf(f float64) (int float64, frac float64) { + return modf(f) +} + +func modf(f float64) (int float64, frac float64) { if f < 1 { if f < 0 { int, frac = Modf(-f) diff --git a/libgo/go/math/nextafter.go b/libgo/go/math/nextafter.go index 86114340c1e..e7723baacfb 100644 --- a/libgo/go/math/nextafter.go +++ b/libgo/go/math/nextafter.go @@ -8,8 +8,8 @@ package math // If x == y, then x is returned. // // Special cases are: -// Nextafter(NaN, y) = NaN -// Nextafter(x, NaN) = NaN +// Nextafter(NaN, y) = NaN +// Nextafter(x, NaN) = NaN func Nextafter(x, y float64) (r float64) { // TODO(rsc): Remove manual inlining of IsNaN // when compiler does it for us @@ -25,5 +25,5 @@ func Nextafter(x, y float64) (r float64) { default: r = Float64frombits(Float64bits(x) - 1) } - return r + return } diff --git a/libgo/go/math/pow10.go b/libgo/go/math/pow10.go index 20f91bcb70c..f5ad28bb4b0 100644 --- a/libgo/go/math/pow10.go +++ b/libgo/go/math/pow10.go @@ -9,6 +9,10 @@ package math var pow10tab [70]float64 // Pow10 returns 10**e, the base-10 exponential of e. +// +// Special cases are: +// Pow10(e) = +Inf for e > 309 +// Pow10(e) = 0 for e < -324 func Pow10(e int) float64 { if e <= -325 { return 0 diff --git a/libgo/go/math/remainder.go b/libgo/go/math/remainder.go index 7fb8a12f9e6..77811b183f5 100644 --- a/libgo/go/math/remainder.go +++ b/libgo/go/math/remainder.go @@ -29,12 +29,16 @@ package math // Remainder returns the IEEE 754 floating-point remainder of x/y. // // Special cases are: -// Remainder(x, NaN) = NaN +// Remainder(±Inf, y) = NaN // Remainder(NaN, y) = NaN -// Remainder(Inf, y) = NaN // Remainder(x, 0) = NaN -// Remainder(x, Inf) = x +// Remainder(x, ±Inf) = x +// Remainder(x, NaN) = NaN func Remainder(x, y float64) float64 { + return remainder(x, y) +} + +func remainder(x, y float64) float64 { const ( Tiny = 4.45014771701440276618e-308 // 0x0020000000000000 HalfMax = MaxFloat64 / 2 diff --git a/libgo/go/math/sin.go b/libgo/go/math/sin.go index b2a3f8a4e0e..ebde7d43681 100644 --- a/libgo/go/math/sin.go +++ b/libgo/go/math/sin.go @@ -98,6 +98,7 @@ var _sin = [...]float64{ 8.33333333332211858878E-3, // 0x3f8111111110f7d0 -1.66666666666666307295E-1, // 0xbfc5555555555548 } + // cos coefficients var _cos = [...]float64{ -1.13585365213876817300E-11, // 0xbda8fa49a0861a9b @@ -113,7 +114,12 @@ var _cos = [...]float64{ // Special cases are: // Cos(±Inf) = NaN // Cos(NaN) = NaN +func libc_cos(float64) float64 __asm__("cos") func Cos(x float64) float64 { + return libc_cos(x) +} + +func cos(x float64) float64 { const ( PI4A = 7.85398125648498535156E-1 // 0x3fe921fb40000000, Pi/4 split into three parts PI4B = 3.77489470793079817668E-8 // 0x3e64442d00000000, @@ -166,11 +172,16 @@ func Cos(x float64) float64 { // Sin returns the sine of x. // -// Special conditions are: +// Special cases are: // Sin(±0) = ±0 // Sin(±Inf) = NaN // Sin(NaN) = NaN +func libc_sin(float64) float64 __asm__("sin") func Sin(x float64) float64 { + return libc_sin(x) +} + +func sin(x float64) float64 { const ( PI4A = 7.85398125648498535156E-1 // 0x3fe921fb40000000, Pi/4 split into three parts PI4B = 3.77489470793079817668E-8 // 0x3e64442d00000000, diff --git a/libgo/go/math/sincos.go b/libgo/go/math/sincos.go index 74294256beb..f0de0893e19 100644 --- a/libgo/go/math/sincos.go +++ b/libgo/go/math/sincos.go @@ -13,6 +13,10 @@ package math // Sincos(±Inf) = NaN, NaN // Sincos(NaN) = NaN, NaN func Sincos(x float64) (sin, cos float64) { + return sincos(x) +} + +func sincos(x float64) (sin, cos float64) { const ( PI4A = 7.85398125648498535156E-1 // 0x3fe921fb40000000, Pi/4 split into three parts PI4B = 3.77489470793079817668E-8 // 0x3e64442d00000000, diff --git a/libgo/go/math/sqrt.go b/libgo/go/math/sqrt.go index bf6ef64a0b4..fb3aff8e4fc 100644 --- a/libgo/go/math/sqrt.go +++ b/libgo/go/math/sqrt.go @@ -4,7 +4,95 @@ package math +// Sqrt returns the square root of x. +// +// Special cases are: +// Sqrt(+Inf) = +Inf +// Sqrt(±0) = ±0 +// Sqrt(x < 0) = NaN +// Sqrt(NaN) = NaN func libc_sqrt(float64) float64 __asm__("sqrt") +func Sqrt(x float64) float64 { + return libc_sqrt(x) +} + +// The original C code and the long comment below are +// from FreeBSD's /usr/src/lib/msun/src/e_sqrt.c and +// came with this notice. The go code is a simplified +// version of the original C. +// +// ==================================================== +// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. +// +// Developed at SunPro, a Sun Microsystems, Inc. business. +// Permission to use, copy, modify, and distribute this +// software is freely granted, provided that this notice +// is preserved. +// ==================================================== +// +// __ieee754_sqrt(x) +// Return correctly rounded sqrt. +// ----------------------------------------- +// | Use the hardware sqrt if you have one | +// ----------------------------------------- +// Method: +// Bit by bit method using integer arithmetic. (Slow, but portable) +// 1. Normalization +// Scale x to y in [1,4) with even powers of 2: +// find an integer k such that 1 <= (y=x*2**(2k)) < 4, then +// sqrt(x) = 2**k * sqrt(y) +// 2. Bit by bit computation +// Let q = sqrt(y) truncated to i bit after binary point (q = 1), +// i 0 +// i+1 2 +// s = 2*q , and y = 2 * ( y - q ). (1) +// i i i i +// +// To compute q from q , one checks whether +// i+1 i +// +// -(i+1) 2 +// (q + 2 ) <= y. (2) +// i +// -(i+1) +// If (2) is false, then q = q ; otherwise q = q + 2 . +// i+1 i i+1 i +// +// With some algebraic manipulation, it is not difficult to see +// that (2) is equivalent to +// -(i+1) +// s + 2 <= y (3) +// i i +// +// The advantage of (3) is that s and y can be computed by +// i i +// the following recurrence formula: +// if (3) is false +// +// s = s , y = y ; (4) +// i+1 i i+1 i +// +// otherwise, +// -i -(i+1) +// s = s + 2 , y = y - s - 2 (5) +// i+1 i i+1 i i +// +// One may easily use induction to prove (4) and (5). +// Note. Since the left hand side of (3) contain only i+2 bits, +// it does not necessary to do a full (53-bit) comparison +// in (3). +// 3. Final rounding +// After generating the 53 bits result, we compute one more bit. +// Together with the remainder, we can decide whether the +// result is exact, bigger than 1/2ulp, or less than 1/2ulp +// (it will never equal to 1/2ulp). +// The rounding mode can be detected by checking whether +// huge + tiny is equal to huge, and whether huge - tiny is +// equal to huge for some floating point number "huge" and "tiny". +// +// +// Notes: Rounding mode detection omitted. The constants "mask", "shift", +// and "bias" are found in src/pkg/math/bits.go // Sqrt returns the square root of x. // @@ -13,7 +101,7 @@ func libc_sqrt(float64) float64 __asm__("sqrt") // Sqrt(±0) = ±0 // Sqrt(x < 0) = NaN // Sqrt(NaN) = NaN -func Sqrt(x float64) float64 { +func sqrt(x float64) float64 { // special cases // TODO(rsc): Remove manual inlining of IsNaN, IsInf // when compiler does it for us @@ -23,6 +111,45 @@ func Sqrt(x float64) float64 { case x < 0: return NaN() } + ix := Float64bits(x) + // normalize x + exp := int((ix >> shift) & mask) + if exp == 0 { // subnormal x + for ix&1<<shift == 0 { + ix <<= 1 + exp-- + } + exp++ + } + exp -= bias // unbias exponent + ix &^= mask << shift + ix |= 1 << shift + if exp&1 == 1 { // odd exp, double x to make it even + ix <<= 1 + } + exp >>= 1 // exp = exp/2, exponent of square root + // generate sqrt(x) bit by bit + ix <<= 1 + var q, s uint64 // q = sqrt(x) + r := uint64(1 << (shift + 1)) // r = moving bit from MSB to LSB + for r != 0 { + t := s + r + if t <= ix { + s = t + r + ix -= t + q += r + } + ix <<= 1 + r >>= 1 + } + // final rounding + if ix != 0 { // remainder, result not exact + q += q & 1 // round according to extra bit + } + ix = q>>1 + uint64(exp-1+bias)<<shift // significand + biased exponent + return Float64frombits(ix) +} - return libc_sqrt(x) +func sqrtC(f float64, r *float64) { + *r = sqrt(f) } diff --git a/libgo/go/math/sqrt_decl.go b/libgo/go/math/sqrt_decl.go deleted file mode 100644 index e507746458b..00000000000 --- a/libgo/go/math/sqrt_decl.go +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -func Sqrt(x float64) float64 diff --git a/libgo/go/math/sqrt_port.go b/libgo/go/math/sqrt_port.go deleted file mode 100644 index 148239bcff6..00000000000 --- a/libgo/go/math/sqrt_port.go +++ /dev/null @@ -1,147 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -/* - Floating-point square root. -*/ - -// The original C code and the long comment below are -// from FreeBSD's /usr/src/lib/msun/src/e_sqrt.c and -// came with this notice. The go code is a simplified -// version of the original C. -// -// ==================================================== -// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. -// -// Developed at SunPro, a Sun Microsystems, Inc. business. -// Permission to use, copy, modify, and distribute this -// software is freely granted, provided that this notice -// is preserved. -// ==================================================== -// -// __ieee754_sqrt(x) -// Return correctly rounded sqrt. -// ----------------------------------------- -// | Use the hardware sqrt if you have one | -// ----------------------------------------- -// Method: -// Bit by bit method using integer arithmetic. (Slow, but portable) -// 1. Normalization -// Scale x to y in [1,4) with even powers of 2: -// find an integer k such that 1 <= (y=x*2**(2k)) < 4, then -// sqrt(x) = 2**k * sqrt(y) -// 2. Bit by bit computation -// Let q = sqrt(y) truncated to i bit after binary point (q = 1), -// i 0 -// i+1 2 -// s = 2*q , and y = 2 * ( y - q ). (1) -// i i i i -// -// To compute q from q , one checks whether -// i+1 i -// -// -(i+1) 2 -// (q + 2 ) <= y. (2) -// i -// -(i+1) -// If (2) is false, then q = q ; otherwise q = q + 2 . -// i+1 i i+1 i -// -// With some algebraic manipulation, it is not difficult to see -// that (2) is equivalent to -// -(i+1) -// s + 2 <= y (3) -// i i -// -// The advantage of (3) is that s and y can be computed by -// i i -// the following recurrence formula: -// if (3) is false -// -// s = s , y = y ; (4) -// i+1 i i+1 i -// -// otherwise, -// -i -(i+1) -// s = s + 2 , y = y - s - 2 (5) -// i+1 i i+1 i i -// -// One may easily use induction to prove (4) and (5). -// Note. Since the left hand side of (3) contain only i+2 bits, -// it does not necessary to do a full (53-bit) comparison -// in (3). -// 3. Final rounding -// After generating the 53 bits result, we compute one more bit. -// Together with the remainder, we can decide whether the -// result is exact, bigger than 1/2ulp, or less than 1/2ulp -// (it will never equal to 1/2ulp). -// The rounding mode can be detected by checking whether -// huge + tiny is equal to huge, and whether huge - tiny is -// equal to huge for some floating point number "huge" and "tiny". -// -// -// Notes: Rounding mode detection omitted. The constants "mask", "shift", -// and "bias" are found in src/pkg/math/bits.go - -// Sqrt returns the square root of x. -// -// Special cases are: -// Sqrt(+Inf) = +Inf -// Sqrt(±0) = ±0 -// Sqrt(x < 0) = NaN -// Sqrt(NaN) = NaN -func sqrtGo(x float64) float64 { - // special cases - // TODO(rsc): Remove manual inlining of IsNaN, IsInf - // when compiler does it for us - switch { - case x == 0 || x != x || x > MaxFloat64: // x == 0 || IsNaN(x) || IsInf(x, 1): - return x - case x < 0: - return NaN() - } - ix := Float64bits(x) - // normalize x - exp := int((ix >> shift) & mask) - if exp == 0 { // subnormal x - for ix&1<<shift == 0 { - ix <<= 1 - exp-- - } - exp++ - } - exp -= bias // unbias exponent - ix &^= mask << shift - ix |= 1 << shift - if exp&1 == 1 { // odd exp, double x to make it even - ix <<= 1 - } - exp >>= 1 // exp = exp/2, exponent of square root - // generate sqrt(x) bit by bit - ix <<= 1 - var q, s uint64 // q = sqrt(x) - r := uint64(1 << (shift + 1)) // r = moving bit from MSB to LSB - for r != 0 { - t := s + r - if t <= ix { - s = t + r - ix -= t - q += r - } - ix <<= 1 - r >>= 1 - } - // final rounding - if ix != 0 { // remainder, result not exact - q += q & 1 // round according to extra bit - } - ix = q>>1 + uint64(exp-1+bias)<<shift // significand + biased exponent - return Float64frombits(ix) -} - -func sqrtGoC(f float64, r *float64) { - *r = sqrtGo(f) -} diff --git a/libgo/go/math/sqrt_test.go b/libgo/go/math/sqrt_test.go deleted file mode 100644 index 84cbc169e81..00000000000 --- a/libgo/go/math/sqrt_test.go +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package math - -// Make sqrtGo available for testing. - -func SqrtGo(x float64) float64 { return sqrtGo(x) } diff --git a/libgo/go/math/tan.go b/libgo/go/math/tan.go index 76131fcd935..926bb4b227e 100644 --- a/libgo/go/math/tan.go +++ b/libgo/go/math/tan.go @@ -79,7 +79,12 @@ var _tanQ = [...]float64{ // Tan(±0) = ±0 // Tan(±Inf) = NaN // Tan(NaN) = NaN +func libc_tan(float64) float64 __asm__("tan") func Tan(x float64) float64 { + return libc_tan(x) +} + +func tan(x float64) float64 { const ( PI4A = 7.85398125648498535156E-1 // 0x3fe921fb40000000, Pi/4 split into three parts PI4B = 3.77489470793079817668E-8 // 0x3e64442d00000000, diff --git a/libgo/go/mime/type_unix.go b/libgo/go/mime/type_unix.go index 45127ba29df..2dab1eac78d 100644 --- a/libgo/go/mime/type_unix.go +++ b/libgo/go/mime/type_unix.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build darwin freebsd linux netbsd openbsd plan9 + package mime import ( diff --git a/libgo/go/mime/type_windows.go b/libgo/go/mime/type_windows.go index 7cf2d3984b1..bc388893b48 100644 --- a/libgo/go/mime/type_windows.go +++ b/libgo/go/mime/type_windows.go @@ -12,18 +12,18 @@ import ( func initMime() { var root syscall.Handle if syscall.RegOpenKeyEx(syscall.HKEY_CLASSES_ROOT, syscall.StringToUTF16Ptr(`\`), - 0, syscall.KEY_READ, &root) != 0 { + 0, syscall.KEY_READ, &root) != nil { return } defer syscall.RegCloseKey(root) var count uint32 - if syscall.RegQueryInfoKey(root, nil, nil, nil, &count, nil, nil, nil, nil, nil, nil, nil) != 0 { + if syscall.RegQueryInfoKey(root, nil, nil, nil, &count, nil, nil, nil, nil, nil, nil, nil) != nil { return } var buf [1 << 10]uint16 for i := uint32(0); i < count; i++ { n := uint32(len(buf)) - if syscall.RegEnumKeyEx(root, i, &buf[0], &n, nil, nil, nil, nil) != 0 { + if syscall.RegEnumKeyEx(root, i, &buf[0], &n, nil, nil, nil, nil) != nil { continue } ext := syscall.UTF16ToString(buf[:]) @@ -33,14 +33,14 @@ func initMime() { var h syscall.Handle if syscall.RegOpenKeyEx( syscall.HKEY_CLASSES_ROOT, syscall.StringToUTF16Ptr(`\`+ext), - 0, syscall.KEY_READ, &h) != 0 { + 0, syscall.KEY_READ, &h) != nil { continue } var typ uint32 n = uint32(len(buf) * 2) // api expects array of bytes, not uint16 if syscall.RegQueryValueEx( h, syscall.StringToUTF16Ptr("Content Type"), - nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != 0 { + nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != nil { syscall.RegCloseKey(h) continue } diff --git a/libgo/go/net/cgo_stub.go b/libgo/go/net/cgo_stub.go index fbe6150c26b..66aff837d0a 100644 --- a/libgo/go/net/cgo_stub.go +++ b/libgo/go/net/cgo_stub.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build openbsd +// +build nocgo // Stub cgo routines for systems that do not use cgo to do network lookups. diff --git a/libgo/go/net/dial.go b/libgo/go/net/dial.go index 43866dcb51d..00acb8477d2 100644 --- a/libgo/go/net/dial.go +++ b/libgo/go/net/dial.go @@ -4,6 +4,10 @@ package net +import ( + "time" +) + func resolveNetAddr(op, net, addr string) (a Addr, err error) { if addr == "" { return nil, &OpError{op, net, nil, errMissingAddress} @@ -42,11 +46,15 @@ func resolveNetAddr(op, net, addr string) (a Addr, err error) { // Dial("tcp", "google.com:80") // Dial("tcp", "[de:ad:be:ef::ca:fe]:80") // -func Dial(net, addr string) (c Conn, err error) { +func Dial(net, addr string) (Conn, error) { addri, err := resolveNetAddr("dial", net, addr) if err != nil { return nil, err } + return dialAddr(net, addr, addri) +} + +func dialAddr(net, addr string, addri Addr) (c Conn, err error) { switch ra := addri.(type) { case *TCPAddr: c, err = DialTCP(net, nil, ra) @@ -65,6 +73,62 @@ func Dial(net, addr string) (c Conn, err error) { return } +// DialTimeout acts like Dial but takes a timeout. +// The timeout includes name resolution, if required. +func DialTimeout(net, addr string, timeout time.Duration) (Conn, error) { + // TODO(bradfitz): the timeout should be pushed down into the + // net package's event loop, so on timeout to dead hosts we + // don't have a goroutine sticking around for the default of + // ~3 minutes. + t := time.NewTimer(timeout) + defer t.Stop() + type pair struct { + Conn + error + } + ch := make(chan pair, 1) + resolvedAddr := make(chan Addr, 1) + go func() { + addri, err := resolveNetAddr("dial", net, addr) + if err != nil { + ch <- pair{nil, err} + return + } + resolvedAddr <- addri // in case we need it for OpError + c, err := dialAddr(net, addr, addri) + ch <- pair{c, err} + }() + select { + case <-t.C: + // Try to use the real Addr in our OpError, if we resolved it + // before the timeout. Otherwise we just use stringAddr. + var addri Addr + select { + case a := <-resolvedAddr: + addri = a + default: + addri = &stringAddr{net, addr} + } + err := &OpError{ + Op: "dial", + Net: net, + Addr: addri, + Err: &timeoutError{}, + } + return nil, err + case p := <-ch: + return p.Conn, p.error + } + panic("unreachable") +} + +type stringAddr struct { + net, addr string +} + +func (a stringAddr) Network() string { return a.net } +func (a stringAddr) String() string { return a.addr } + // Listen announces on the local network address laddr. // The network string net must be a stream-oriented // network: "tcp", "tcp4", "tcp6", or "unix", or "unixpacket". diff --git a/libgo/go/net/dial_test.go b/libgo/go/net/dial_test.go new file mode 100644 index 00000000000..16b72631153 --- /dev/null +++ b/libgo/go/net/dial_test.go @@ -0,0 +1,88 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package net + +import ( + "runtime" + "testing" + "time" +) + +func newLocalListener(t *testing.T) Listener { + ln, err := Listen("tcp", "127.0.0.1:0") + if err != nil { + ln, err = Listen("tcp6", "[::1]:0") + } + if err != nil { + t.Fatal(err) + } + return ln +} + +func TestDialTimeout(t *testing.T) { + ln := newLocalListener(t) + defer ln.Close() + + errc := make(chan error) + + const SOMAXCONN = 0x80 // copied from syscall, but not always available + const numConns = SOMAXCONN + 10 + + // TODO(bradfitz): It's hard to test this in a portable + // way. This is unforunate, but works for now. + switch runtime.GOOS { + case "linux": + // The kernel will start accepting TCP connections before userspace + // gets a chance to not accept them, so fire off a bunch to fill up + // the kernel's backlog. Then we test we get a failure after that. + for i := 0; i < numConns; i++ { + go func() { + _, err := DialTimeout("tcp", ln.Addr().String(), 200*time.Millisecond) + errc <- err + }() + } + case "darwin": + // At least OS X 10.7 seems to accept any number of + // connections, ignoring listen's backlog, so resort + // to connecting to a hopefully-dead 127/8 address. + go func() { + _, err := DialTimeout("tcp", "127.0.71.111:80", 200*time.Millisecond) + errc <- err + }() + default: + // TODO(bradfitz): this probably doesn't work on + // Windows? SOMAXCONN is huge there. I'm not sure how + // listen works there. + // OpenBSD may have a reject route to 10/8. + // FreeBSD likely works, but is untested. + t.Logf("skipping test on %q; untested.", runtime.GOOS) + return + } + + connected := 0 + for { + select { + case <-time.After(15 * time.Second): + t.Fatal("too slow") + case err := <-errc: + if err == nil { + connected++ + if connected == numConns { + t.Fatal("all connections connected; expected some to time out") + } + } else { + terr, ok := err.(timeout) + if !ok { + t.Fatalf("got error %q; want error with timeout interface", err) + } + if !terr.Timeout() { + t.Fatalf("got error %q; not a timeout", err) + } + // Pass. We saw a timeout error. + return + } + } + } +} diff --git a/libgo/go/net/dnsclient_unix.go b/libgo/go/net/dnsclient_unix.go index 79a958e3cd0..07e72ccb862 100644 --- a/libgo/go/net/dnsclient_unix.go +++ b/libgo/go/net/dnsclient_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd // DNS client: see RFC 1035. // Has to be linked into package net for Dial. diff --git a/libgo/go/net/dnsconfig.go b/libgo/go/net/dnsconfig.go index 379fec95b86..c0ab80288da 100644 --- a/libgo/go/net/dnsconfig.go +++ b/libgo/go/net/dnsconfig.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd // Read system DNS config from /etc/resolv.conf diff --git a/libgo/go/net/fd.go b/libgo/go/net/fd.go index 5318c51c9a2..3dec9f4beb8 100644 --- a/libgo/go/net/fd.go +++ b/libgo/go/net/fd.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package net @@ -377,14 +377,6 @@ func (fd *netFD) CloseWrite() error { return fd.shutdown(syscall.SHUT_WR) } -type timeoutError struct{} - -func (e *timeoutError) Error() string { return "i/o timeout" } -func (e *timeoutError) Timeout() bool { return true } -func (e *timeoutError) Temporary() bool { return true } - -var errTimeout error = &timeoutError{} - func (fd *netFD) Read(p []byte) (n int, err error) { if fd == nil { return 0, os.EINVAL diff --git a/libgo/go/net/fd_linux.go b/libgo/go/net/fd_linux.go index 8e07833882e..c8df9c9326a 100644 --- a/libgo/go/net/fd_linux.go +++ b/libgo/go/net/fd_linux.go @@ -37,11 +37,17 @@ func newpollster() (p *pollster, err error) { p = new(pollster) var e error - // The arg to epoll_create is a hint to the kernel - // about the number of FDs we will care about. - // We don't know, and since 2.6.8 the kernel ignores it anyhow. - if p.epfd, e = syscall.EpollCreate(16); e != nil { - return nil, os.NewSyscallError("epoll_create", e) + if p.epfd, e = syscall.EpollCreate1(syscall.EPOLL_CLOEXEC); e != nil { + if e != syscall.ENOSYS { + return nil, os.NewSyscallError("epoll_create1", e) + } + // The arg to epoll_create is a hint to the kernel + // about the number of FDs we will care about. + // We don't know, and since 2.6.8 the kernel ignores it anyhow. + if p.epfd, e = syscall.EpollCreate(16); e != nil { + return nil, os.NewSyscallError("epoll_create", e) + } + syscall.CloseOnExec(p.epfd) } p.events = make(map[int]uint32) return p, nil diff --git a/libgo/go/net/fd_netbsd.go b/libgo/go/net/fd_netbsd.go new file mode 100644 index 00000000000..31d0744e2cb --- /dev/null +++ b/libgo/go/net/fd_netbsd.go @@ -0,0 +1,116 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Waiting for FDs via kqueue/kevent. + +package net + +import ( + "os" + "syscall" +) + +type pollster struct { + kq int + eventbuf [10]syscall.Kevent_t + events []syscall.Kevent_t + + // An event buffer for AddFD/DelFD. + // Must hold pollServer lock. + kbuf [1]syscall.Kevent_t +} + +func newpollster() (p *pollster, err error) { + p = new(pollster) + if p.kq, err = syscall.Kqueue(); err != nil { + return nil, os.NewSyscallError("kqueue", err) + } + syscall.CloseOnExec(p.kq) + p.events = p.eventbuf[0:0] + return p, nil +} + +func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) { + // pollServer is locked. + + var kmode int + if mode == 'r' { + kmode = syscall.EVFILT_READ + } else { + kmode = syscall.EVFILT_WRITE + } + ev := &p.kbuf[0] + // EV_ADD - add event to kqueue list + // EV_ONESHOT - delete the event the first time it triggers + flags := syscall.EV_ADD + if !repeat { + flags |= syscall.EV_ONESHOT + } + syscall.SetKevent(ev, fd, kmode, flags) + + n, e := syscall.Kevent(p.kq, p.kbuf[:], nil, nil) + if e != nil { + return false, os.NewSyscallError("kevent", e) + } + if n != 1 || (ev.Flags&syscall.EV_ERROR) == 0 || int(ev.Ident) != fd || int(ev.Filter) != kmode { + return false, os.NewSyscallError("kqueue phase error", e) + } + if ev.Data != 0 { + return false, syscall.Errno(int(ev.Data)) + } + return false, nil +} + +func (p *pollster) DelFD(fd int, mode int) { + // pollServer is locked. + + var kmode int + if mode == 'r' { + kmode = syscall.EVFILT_READ + } else { + kmode = syscall.EVFILT_WRITE + } + ev := &p.kbuf[0] + // EV_DELETE - delete event from kqueue list + syscall.SetKevent(ev, fd, kmode, syscall.EV_DELETE) + syscall.Kevent(p.kq, p.kbuf[:], nil, nil) +} + +func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err error) { + var t *syscall.Timespec + for len(p.events) == 0 { + if nsec > 0 { + if t == nil { + t = new(syscall.Timespec) + } + *t = syscall.NsecToTimespec(nsec) + } + + s.Unlock() + nn, e := syscall.Kevent(p.kq, nil, p.eventbuf[:], t) + s.Lock() + + if e != nil { + if e == syscall.EINTR { + continue + } + return -1, 0, os.NewSyscallError("kevent", e) + } + if nn == 0 { + return -1, 0, nil + } + p.events = p.eventbuf[0:nn] + } + ev := &p.events[0] + p.events = p.events[1:] + fd = int(ev.Ident) + if ev.Filter == syscall.EVFILT_READ { + mode = 'r' + } else { + mode = 'w' + } + return fd, mode, nil +} + +func (p *pollster) Close() error { return os.NewSyscallError("close", syscall.Close(p.kq)) } diff --git a/libgo/go/net/fd_openbsd.go b/libgo/go/net/fd_openbsd.go index e52ac356b9f..31d0744e2cb 100644 --- a/libgo/go/net/fd_openbsd.go +++ b/libgo/go/net/fd_openbsd.go @@ -26,6 +26,7 @@ func newpollster() (p *pollster, err error) { if p.kq, err = syscall.Kqueue(); err != nil { return nil, os.NewSyscallError("kqueue", err) } + syscall.CloseOnExec(p.kq) p.events = p.eventbuf[0:0] return p, nil } diff --git a/libgo/go/net/fd_windows.go b/libgo/go/net/fd_windows.go index 264b918c57d..7bffd1ca2fb 100644 --- a/libgo/go/net/fd_windows.go +++ b/libgo/go/net/fd_windows.go @@ -25,8 +25,8 @@ var initErr error func init() { var d syscall.WSAData e := syscall.WSAStartup(uint32(0x202), &d) - if e != 0 { - initErr = os.NewSyscallError("WSAStartup", syscall.Errno(e)) + if e != nil { + initErr = os.NewSyscallError("WSAStartup", e) } } diff --git a/libgo/go/net/file.go b/libgo/go/net/file.go index bf8cd9dae04..4ac280bd1a3 100644 --- a/libgo/go/net/file.go +++ b/libgo/go/net/file.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package net diff --git a/libgo/go/net/http/client.go b/libgo/go/net/http/client.go index 211ac44c58a..a4f8f19aac8 100644 --- a/libgo/go/net/http/client.go +++ b/libgo/go/net/http/client.go @@ -38,6 +38,11 @@ type Client struct { // If CheckRedirect is nil, the Client uses its default policy, // which is to stop after 10 consecutive requests. CheckRedirect func(req *Request, via []*Request) error + + // Jar specifies the cookie jar. + // If Jar is nil, cookies are not sent in requests and ignored + // in responses. + Jar CookieJar } // DefaultClient is the default Client and is used by Get, Head, and Post. @@ -180,6 +185,11 @@ func (c *Client) doFollowingRedirects(ireq *Request) (r *Response, err error) { return nil, errors.New("http: nil Request.URL") } + jar := c.Jar + if jar == nil { + jar = blackHoleJar{} + } + req := ireq urlStr := "" // next relative or absolute URL to fetch (after first request) for redirect := 0; ; redirect++ { @@ -203,12 +213,19 @@ func (c *Client) doFollowingRedirects(ireq *Request) (r *Response, err error) { break } } + for _, cookie := range jar.Cookies(req.URL) { + req.AddCookie(cookie) + } } urlStr = req.URL.String() if r, err = send(req, c.Transport); err != nil { break } + if c := r.Cookies(); len(c) > 0 { + jar.SetCookies(req.URL, c) + } + if shouldRedirect(r.StatusCode) { r.Body.Close() if urlStr = r.Header.Get("Location"); urlStr == "" { diff --git a/libgo/go/net/http/doc.go b/libgo/go/net/http/doc.go index 9c47ac7823f..2dbcf8dc97c 100644 --- a/libgo/go/net/http/doc.go +++ b/libgo/go/net/http/doc.go @@ -70,8 +70,8 @@ custom Server: s := &http.Server{ Addr: ":8080", Handler: myHandler, - ReadTimeout: 10e9, - WriteTimeout: 10e9, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } log.Fatal(s.ListenAndServe()) diff --git a/libgo/go/net/http/jar.go b/libgo/go/net/http/jar.go new file mode 100644 index 00000000000..2c2caa251fe --- /dev/null +++ b/libgo/go/net/http/jar.go @@ -0,0 +1,30 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package http + +import ( + "net/url" +) + +// A CookieJar manages storage and use of cookies in HTTP requests. +// +// Implementations of CookieJar must be safe for concurrent use by multiple +// goroutines. +type CookieJar interface { + // SetCookies handles the receipt of the cookies in a reply for the + // given URL. It may or may not choose to save the cookies, depending + // on the jar's policy and implementation. + SetCookies(u *url.URL, cookies []*Cookie) + + // Cookies returns the cookies to send in a request for the given URL. + // It is up to the implementation to honor the standard cookie use + // restrictions such as in RFC 6265. + Cookies(u *url.URL) []*Cookie +} + +type blackHoleJar struct{} + +func (blackHoleJar) SetCookies(u *url.URL, cookies []*Cookie) {} +func (blackHoleJar) Cookies(u *url.URL) []*Cookie { return nil } diff --git a/libgo/go/net/http/readrequest_test.go b/libgo/go/net/http/readrequest_test.go index c64fff6109f..ad7e3c02b0c 100644 --- a/libgo/go/net/http/readrequest_test.go +++ b/libgo/go/net/http/readrequest_test.go @@ -219,7 +219,7 @@ func TestReadRequest(t *testing.T) { t.Errorf("#%d: Body = %q want %q", i, body, tt.Body) } if !reflect.DeepEqual(tt.Trailer, req.Trailer) { - t.Errorf("%#d. Trailers differ.\n got: %v\nwant: %v", i, req.Trailer, tt.Trailer) + t.Errorf("#%d. Trailers differ.\n got: %v\nwant: %v", i, req.Trailer, tt.Trailer) } } } diff --git a/libgo/go/net/http/request_test.go b/libgo/go/net/http/request_test.go index 714cb64f47f..7b78645169e 100644 --- a/libgo/go/net/http/request_test.go +++ b/libgo/go/net/http/request_test.go @@ -202,8 +202,8 @@ func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) { if g, e := req.FormValue("texta"), textaValue; g != e { t.Errorf("texta value = %q, want %q", g, e) } - if g, e := req.FormValue("texta"), textaValue; g != e { - t.Errorf("texta value = %q, want %q", g, e) + if g, e := req.FormValue("textb"), textbValue; g != e { + t.Errorf("textb value = %q, want %q", g, e) } if g := req.FormValue("missing"); g != "" { t.Errorf("missing value = %q, want empty string", g) @@ -214,14 +214,16 @@ func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) { t.Error(n, " is *os.File, should not be") } } - fd := testMultipartFile(t, req, "filea", "filea.txt", fileaContents) - assertMem("filea", fd) - fd = testMultipartFile(t, req, "fileb", "fileb.txt", filebContents) + fda := testMultipartFile(t, req, "filea", "filea.txt", fileaContents) + defer fda.Close() + assertMem("filea", fda) + fdb := testMultipartFile(t, req, "fileb", "fileb.txt", filebContents) + defer fdb.Close() if allMem { - assertMem("fileb", fd) + assertMem("fileb", fdb) } else { - if _, ok := fd.(*os.File); !ok { - t.Errorf("fileb has unexpected underlying type %T", fd) + if _, ok := fdb.(*os.File); !ok { + t.Errorf("fileb has unexpected underlying type %T", fdb) } } diff --git a/libgo/go/net/http/serve_test.go b/libgo/go/net/http/serve_test.go index 670b5418fcd..24e6b50dab8 100644 --- a/libgo/go/net/http/serve_test.go +++ b/libgo/go/net/http/serve_test.go @@ -361,7 +361,7 @@ func TestIdentityResponse(t *testing.T) { // The ReadAll will hang for a failing test, so use a Timer to // fail explicitly. - goTimeout(t, 2e9, func() { + goTimeout(t, 2*time.Second, func() { got, _ := ioutil.ReadAll(conn) expectedSuffix := "\r\n\r\ntoo short" if !strings.HasSuffix(string(got), expectedSuffix) { @@ -395,7 +395,7 @@ func testTcpConnectionCloses(t *testing.T, req string, h Handler) { success := make(chan bool) go func() { select { - case <-time.After(5e9): + case <-time.After(5 * time.Second): t.Fatal("body not closed after 5s") case <-success: } @@ -538,7 +538,7 @@ func TestHeadResponses(t *testing.T) { func TestTLSHandshakeTimeout(t *testing.T) { ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {})) - ts.Config.ReadTimeout = 250e6 + ts.Config.ReadTimeout = 250 * time.Millisecond ts.StartTLS() defer ts.Close() conn, err := net.Dial("tcp", ts.Listener.Addr().String()) @@ -546,7 +546,7 @@ func TestTLSHandshakeTimeout(t *testing.T) { t.Fatalf("Dial: %v", err) } defer conn.Close() - goTimeout(t, 10e9, func() { + goTimeout(t, 10*time.Second, func() { var buf [1]byte n, err := conn.Read(buf[:]) if err == nil || n != 0 { @@ -576,7 +576,7 @@ func TestTLSServer(t *testing.T) { t.Fatalf("Dial: %v", err) } defer idleConn.Close() - goTimeout(t, 10e9, func() { + goTimeout(t, 10*time.Second, func() { if !strings.HasPrefix(ts.URL, "https://") { t.Errorf("expected test TLS server to start with https://, got %q", ts.URL) return @@ -925,7 +925,7 @@ func testHandlerPanic(t *testing.T, withHijack bool) { select { case <-done: return - case <-time.After(5e9): + case <-time.After(5 * time.Second): t.Fatal("expected server handler to log an error") } } @@ -1072,7 +1072,7 @@ func TestClientWriteShutdown(t *testing.T) { }() select { case <-donec: - case <-time.After(10e9): + case <-time.After(10 * time.Second): t.Fatalf("timeout") } } @@ -1103,10 +1103,10 @@ func TestServerBufferedChunking(t *testing.T) { } // goTimeout runs f, failing t if f takes more than ns to complete. -func goTimeout(t *testing.T, ns int64, f func()) { +func goTimeout(t *testing.T, d time.Duration, f func()) { ch := make(chan bool, 2) - timer := time.AfterFunc(ns, func() { - t.Errorf("Timeout expired after %d ns", ns) + timer := time.AfterFunc(d, func() { + t.Errorf("Timeout expired after %v", d) ch <- true }) defer timer.Stop() @@ -1164,15 +1164,15 @@ func BenchmarkClientServer(b *testing.B) { for i := 0; i < b.N; i++ { res, err := Get(ts.URL) if err != nil { - panic("Get: " + err.Error()) + b.Fatal("Get:", err) } all, err := ioutil.ReadAll(res.Body) if err != nil { - panic("ReadAll: " + err.Error()) + b.Fatal("ReadAll:", err) } body := string(all) if body != "Hello world.\n" { - panic("Got body: " + body) + b.Fatal("Got body:", body) } } diff --git a/libgo/go/net/http/server.go b/libgo/go/net/http/server.go index c100e4d5c6d..fa9009517db 100644 --- a/libgo/go/net/http/server.go +++ b/libgo/go/net/http/server.go @@ -261,7 +261,7 @@ func (w *response) Header() Header { } // maxPostHandlerReadBytes is the max number of Request.Body bytes not -// consumed by a handler that the server will read from the a client +// consumed by a handler that the server will read from the client // in order to keep a connection alive. If there are more bytes than // this then the server to be paranoid instead sends a "Connection: // close" response. @@ -952,11 +952,11 @@ func Serve(l net.Listener, handler Handler) error { // A Server defines parameters for running an HTTP server. type Server struct { - Addr string // TCP address to listen on, ":http" if empty - Handler Handler // handler to invoke, http.DefaultServeMux if nil - ReadTimeout int64 // the net.Conn.SetReadTimeout value for new connections - WriteTimeout int64 // the net.Conn.SetWriteTimeout value for new connections - MaxHeaderBytes int // maximum size of request headers, DefaultMaxHeaderBytes if 0 + Addr string // TCP address to listen on, ":http" if empty + Handler Handler // handler to invoke, http.DefaultServeMux if nil + ReadTimeout time.Duration // the net.Conn.SetReadTimeout value for new connections + WriteTimeout time.Duration // the net.Conn.SetWriteTimeout value for new connections + MaxHeaderBytes int // maximum size of request headers, DefaultMaxHeaderBytes if 0 } // ListenAndServe listens on the TCP network address srv.Addr and then @@ -989,10 +989,10 @@ func (srv *Server) Serve(l net.Listener) error { return e } if srv.ReadTimeout != 0 { - rw.SetReadTimeout(srv.ReadTimeout) + rw.SetReadTimeout(srv.ReadTimeout.Nanoseconds()) } if srv.WriteTimeout != 0 { - rw.SetWriteTimeout(srv.WriteTimeout) + rw.SetWriteTimeout(srv.WriteTimeout.Nanoseconds()) } c, err := srv.newConn(rw) if err != nil { @@ -1027,7 +1027,7 @@ func (srv *Server) Serve(l net.Listener) error { // http.HandleFunc("/hello", HelloServer) // err := http.ListenAndServe(":12345", nil) // if err != nil { -// log.Fatal("ListenAndServe: ", err.String()) +// log.Fatal("ListenAndServe: ", err) // } // } func ListenAndServe(addr string, handler Handler) error { diff --git a/libgo/go/net/http/transport.go b/libgo/go/net/http/transport.go index dc70be43f2d..33ad32861b5 100644 --- a/libgo/go/net/http/transport.go +++ b/libgo/go/net/http/transport.go @@ -539,12 +539,13 @@ func (pc *persistConn) readLoop() { resp, err := ReadResponse(pc.br, rc.req) if err == nil { - if rc.addedGzip && resp.Header.Get("Content-Encoding") == "gzip" { + hasBody := rc.req.Method != "HEAD" && resp.ContentLength != 0 + if rc.addedGzip && hasBody && resp.Header.Get("Content-Encoding") == "gzip" { resp.Header.Del("Content-Encoding") resp.Header.Del("Content-Length") resp.ContentLength = -1 gzReader, zerr := gzip.NewReader(resp.Body) - if err != nil { + if zerr != nil { pc.close() err = zerr } else { diff --git a/libgo/go/net/http/transport_test.go b/libgo/go/net/http/transport_test.go index 6f50f6f2767..ff12fa2d014 100644 --- a/libgo/go/net/http/transport_test.go +++ b/libgo/go/net/http/transport_test.go @@ -292,7 +292,7 @@ func TestTransportServerClosingUnexpectedly(t *testing.T) { // it on most fast machines, causing the next fetch() call to // succeed quickly. But if we do get errors, fetch() will retry 5 // times with some delays between. - time.Sleep(25e6) + time.Sleep(25 * time.Millisecond) body3 := fetch(3, 5) diff --git a/libgo/go/net/interface_bsd.go b/libgo/go/net/interface_bsd.go index e896d43c321..907f80a80f3 100644 --- a/libgo/go/net/interface_bsd.go +++ b/libgo/go/net/interface_bsd.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd openbsd +// +build darwin freebsd netbsd openbsd // Network interface identification for BSD variants @@ -18,21 +18,16 @@ import ( // network interfaces. Otheriwse it returns a mapping of a specific // interface. func interfaceTable(ifindex int) ([]Interface, error) { - var ( - tab []byte - e error - msgs []syscall.RoutingMessage - ift []Interface - ) - - tab, e = syscall.RouteRIB(syscall.NET_RT_IFLIST, ifindex) - if e != nil { - return nil, os.NewSyscallError("route rib", e) + var ift []Interface + + tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST, ifindex) + if err != nil { + return nil, os.NewSyscallError("route rib", err) } - msgs, e = syscall.ParseRoutingMessage(tab) - if e != nil { - return nil, os.NewSyscallError("route message", e) + msgs, err := syscall.ParseRoutingMessage(tab) + if err != nil { + return nil, os.NewSyscallError("route message", err) } for _, m := range msgs { @@ -54,9 +49,9 @@ func interfaceTable(ifindex int) ([]Interface, error) { func newLink(m *syscall.InterfaceMessage) ([]Interface, error) { var ift []Interface - sas, e := syscall.ParseRoutingSockaddr(m) - if e != nil { - return nil, os.NewSyscallError("route sockaddr", e) + sas, err := syscall.ParseRoutingSockaddr(m) + if err != nil { + return nil, os.NewSyscallError("route sockaddr", err) } for _, s := range sas { @@ -108,21 +103,16 @@ func linkFlags(rawFlags int32) Flags { // for all network interfaces. Otherwise it returns addresses // for a specific interface. func interfaceAddrTable(ifindex int) ([]Addr, error) { - var ( - tab []byte - e error - msgs []syscall.RoutingMessage - ifat []Addr - ) - - tab, e = syscall.RouteRIB(syscall.NET_RT_IFLIST, ifindex) - if e != nil { - return nil, os.NewSyscallError("route rib", e) + var ifat []Addr + + tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST, ifindex) + if err != nil { + return nil, os.NewSyscallError("route rib", err) } - msgs, e = syscall.ParseRoutingMessage(tab) - if e != nil { - return nil, os.NewSyscallError("route message", e) + msgs, err := syscall.ParseRoutingMessage(tab) + if err != nil { + return nil, os.NewSyscallError("route message", err) } for _, m := range msgs { @@ -133,7 +123,7 @@ func interfaceAddrTable(ifindex int) ([]Addr, error) { if err != nil { return nil, err } - ifat = append(ifat, ifa...) + ifat = append(ifat, ifa) } } } @@ -141,32 +131,41 @@ func interfaceAddrTable(ifindex int) ([]Addr, error) { return ifat, nil } -func newAddr(m *syscall.InterfaceAddrMessage) ([]Addr, error) { - var ifat []Addr +func newAddr(m *syscall.InterfaceAddrMessage) (Addr, error) { + ifa := &IPNet{} - sas, e := syscall.ParseRoutingSockaddr(m) - if e != nil { - return nil, os.NewSyscallError("route sockaddr", e) + sas, err := syscall.ParseRoutingSockaddr(m) + if err != nil { + return nil, os.NewSyscallError("route sockaddr", err) } - for _, s := range sas { + for i, s := range sas { switch v := s.(type) { case *syscall.SockaddrInet4: - ifa := &IPAddr{IP: IPv4(v.Addr[0], v.Addr[1], v.Addr[2], v.Addr[3])} - ifat = append(ifat, ifa.toAddr()) + switch i { + case 0: + ifa.Mask = IPv4Mask(v.Addr[0], v.Addr[1], v.Addr[2], v.Addr[3]) + case 1: + ifa.IP = IPv4(v.Addr[0], v.Addr[1], v.Addr[2], v.Addr[3]) + } case *syscall.SockaddrInet6: - ifa := &IPAddr{IP: make(IP, IPv6len)} - copy(ifa.IP, v.Addr[:]) - // NOTE: KAME based IPv6 protcol stack usually embeds - // the interface index in the interface-local or link- - // local address as the kernel-internal form. - if ifa.IP.IsLinkLocalUnicast() { - // remove embedded scope zone ID - ifa.IP[2], ifa.IP[3] = 0, 0 + switch i { + case 0: + ifa.Mask = make(IPMask, IPv6len) + copy(ifa.Mask, v.Addr[:]) + case 1: + ifa.IP = make(IP, IPv6len) + copy(ifa.IP, v.Addr[:]) + // NOTE: KAME based IPv6 protcol stack usually embeds + // the interface index in the interface-local or link- + // local address as the kernel-internal form. + if ifa.IP.IsLinkLocalUnicast() { + // remove embedded scope zone ID + ifa.IP[2], ifa.IP[3] = 0, 0 + } } - ifat = append(ifat, ifa.toAddr()) } } - return ifat, nil + return ifa, nil } diff --git a/libgo/go/net/interface_linux.go b/libgo/go/net/interface_linux.go index 96db7186af5..c0887c57efe 100644 --- a/libgo/go/net/interface_linux.go +++ b/libgo/go/net/interface_linux.go @@ -17,21 +17,16 @@ import ( // network interfaces. Otheriwse it returns a mapping of a specific // interface. func interfaceTable(ifindex int) ([]Interface, error) { - var ( - ift []Interface - tab []byte - msgs []syscall.NetlinkMessage - e error - ) + var ift []Interface - tab, e = syscall.NetlinkRIB(syscall.RTM_GETLINK, syscall.AF_UNSPEC) - if e != nil { - return nil, os.NewSyscallError("netlink rib", e) + tab, err := syscall.NetlinkRIB(syscall.RTM_GETLINK, syscall.AF_UNSPEC) + if err != nil { + return nil, os.NewSyscallError("netlink rib", err) } - msgs, e = syscall.ParseNetlinkMessage(tab) - if e != nil { - return nil, os.NewSyscallError("netlink message", e) + msgs, err := syscall.ParseNetlinkMessage(tab) + if err != nil { + return nil, os.NewSyscallError("netlink message", err) } for _, m := range msgs { @@ -41,11 +36,11 @@ func interfaceTable(ifindex int) ([]Interface, error) { case syscall.RTM_NEWLINK: ifim := (*syscall.IfInfomsg)(unsafe.Pointer(&m.Data[0])) if ifindex == 0 || ifindex == int(ifim.Index) { - attrs, e := syscall.ParseNetlinkRouteAttr(&m) - if e != nil { - return nil, os.NewSyscallError("netlink routeattr", e) + attrs, err := syscall.ParseNetlinkRouteAttr(&m) + if err != nil { + return nil, os.NewSyscallError("netlink routeattr", err) } - ifi := newLink(attrs, ifim) + ifi := newLink(ifim, attrs) ift = append(ift, ifi) } } @@ -55,7 +50,7 @@ done: return ift, nil } -func newLink(attrs []syscall.NetlinkRouteAttr, ifim *syscall.IfInfomsg) Interface { +func newLink(ifim *syscall.IfInfomsg, attrs []syscall.NetlinkRouteAttr) Interface { ifi := Interface{Index: int(ifim.Index), Flags: linkFlags(ifim.Flags)} for _, a := range attrs { switch a.Attr.Type { @@ -102,19 +97,19 @@ func linkFlags(rawFlags uint32) Flags { // for all network interfaces. Otherwise it returns addresses // for a specific interface. func interfaceAddrTable(ifindex int) ([]Addr, error) { - tab, e := syscall.NetlinkRIB(syscall.RTM_GETADDR, syscall.AF_UNSPEC) - if e != nil { - return nil, os.NewSyscallError("netlink rib", e) + tab, err := syscall.NetlinkRIB(syscall.RTM_GETADDR, syscall.AF_UNSPEC) + if err != nil { + return nil, os.NewSyscallError("netlink rib", err) } - msgs, e := syscall.ParseNetlinkMessage(tab) - if e != nil { - return nil, os.NewSyscallError("netlink message", e) + msgs, err := syscall.ParseNetlinkMessage(tab) + if err != nil { + return nil, os.NewSyscallError("netlink message", err) } - ifat, e := addrTable(msgs, ifindex) - if e != nil { - return nil, e + ifat, err := addrTable(msgs, ifindex) + if err != nil { + return nil, err } return ifat, nil @@ -130,11 +125,11 @@ func addrTable(msgs []syscall.NetlinkMessage, ifindex int) ([]Addr, error) { case syscall.RTM_NEWADDR: ifam := (*syscall.IfAddrmsg)(unsafe.Pointer(&m.Data[0])) if ifindex == 0 || ifindex == int(ifam.Index) { - attrs, e := syscall.ParseNetlinkRouteAttr(&m) - if e != nil { - return nil, os.NewSyscallError("netlink routeattr", e) + attrs, err := syscall.ParseNetlinkRouteAttr(&m) + if err != nil { + return nil, os.NewSyscallError("netlink routeattr", err) } - ifat = append(ifat, newAddr(attrs, int(ifam.Family))...) + ifat = append(ifat, newAddr(attrs, int(ifam.Family), int(ifam.Prefixlen))) } } } @@ -143,25 +138,23 @@ done: return ifat, nil } -func newAddr(attrs []syscall.NetlinkRouteAttr, family int) []Addr { - var ifat []Addr - +func newAddr(attrs []syscall.NetlinkRouteAttr, family, pfxlen int) Addr { + ifa := &IPNet{} for _, a := range attrs { switch a.Attr.Type { case syscall.IFA_ADDRESS: switch family { case syscall.AF_INET: - ifa := &IPAddr{IP: IPv4(a.Value[0], a.Value[1], a.Value[2], a.Value[3])} - ifat = append(ifat, ifa.toAddr()) + ifa.IP = IPv4(a.Value[0], a.Value[1], a.Value[2], a.Value[3]) + ifa.Mask = CIDRMask(pfxlen, 8*IPv4len) case syscall.AF_INET6: - ifa := &IPAddr{IP: make(IP, IPv6len)} + ifa.IP = make(IP, IPv6len) copy(ifa.IP, a.Value[:]) - ifat = append(ifat, ifa.toAddr()) + ifa.Mask = CIDRMask(pfxlen, 8*IPv6len) } } } - - return ifat + return ifa } // If the ifindex is zero, interfaceMulticastAddrTable returns @@ -169,8 +162,8 @@ func newAddr(attrs []syscall.NetlinkRouteAttr, family int) []Addr { // addresses for a specific interface. func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) { var ( - ifi *Interface err error + ifi *Interface ) if ifindex > 0 { diff --git a/libgo/go/net/interface_netbsd.go b/libgo/go/net/interface_netbsd.go new file mode 100644 index 00000000000..4150e9ad5d1 --- /dev/null +++ b/libgo/go/net/interface_netbsd.go @@ -0,0 +1,14 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Network interface identification for NetBSD + +package net + +// If the ifindex is zero, interfaceMulticastAddrTable returns +// addresses for all network interfaces. Otherwise it returns +// addresses for a specific interface. +func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) { + return nil, nil +} diff --git a/libgo/go/net/interface_test.go b/libgo/go/net/interface_test.go index cc614910fac..4ce01dc9061 100644 --- a/libgo/go/net/interface_test.go +++ b/libgo/go/net/interface_test.go @@ -24,7 +24,7 @@ func sameInterface(i, j *Interface) bool { func TestInterfaces(t *testing.T) { ift, err := Interfaces() if err != nil { - t.Fatalf("Interfaces() failed: %v", err) + t.Fatalf("Interfaces failed: %v", err) } t.Logf("table: len/cap = %v/%v\n", len(ift), cap(ift)) @@ -43,34 +43,57 @@ func TestInterfaces(t *testing.T) { if !sameInterface(ifxn, &ifi) { t.Fatalf("InterfaceByName(%#q) = %v, want %v", ifi.Name, *ifxn, ifi) } - ifat, err := ifi.Addrs() - if err != nil { - t.Fatalf("Interface.Addrs() failed: %v", err) - } - ifmat, err := ifi.MulticastAddrs() - if err != nil { - t.Fatalf("Interface.MulticastAddrs() failed: %v", err) - } t.Logf("%q: flags %q, ifindex %v, mtu %v\n", ifi.Name, ifi.Flags.String(), ifi.Index, ifi.MTU) - for _, ifa := range ifat { - t.Logf("\tinterface address %q\n", ifa.String()) - } - for _, ifma := range ifmat { - t.Logf("\tjoined group address %q\n", ifma.String()) - } t.Logf("\thardware address %q", ifi.HardwareAddr.String()) + testInterfaceAddrs(t, &ifi) + testInterfaceMulticastAddrs(t, &ifi) } } func TestInterfaceAddrs(t *testing.T) { ifat, err := InterfaceAddrs() if err != nil { - t.Fatalf("InterfaceAddrs() failed: %v", err) + t.Fatalf("InterfaceAddrs failed: %v", err) } t.Logf("table: len/cap = %v/%v\n", len(ifat), cap(ifat)) + testAddrs(t, ifat) +} + +func testInterfaceAddrs(t *testing.T, ifi *Interface) { + ifat, err := ifi.Addrs() + if err != nil { + t.Fatalf("Interface.Addrs failed: %v", err) + } + testAddrs(t, ifat) +} + +func testInterfaceMulticastAddrs(t *testing.T, ifi *Interface) { + ifmat, err := ifi.MulticastAddrs() + if err != nil { + t.Fatalf("Interface.MulticastAddrs failed: %v", err) + } + testMulticastAddrs(t, ifmat) +} +func testAddrs(t *testing.T, ifat []Addr) { for _, ifa := range ifat { - t.Logf("interface address %q\n", ifa.String()) + switch ifa.(type) { + case *IPAddr, *IPNet: + t.Logf("\tinterface address %q\n", ifa.String()) + default: + t.Errorf("\tunexpected type: %T", ifa) + } + } +} + +func testMulticastAddrs(t *testing.T, ifmat []Addr) { + for _, ifma := range ifmat { + switch ifma.(type) { + case *IPAddr: + t.Logf("\tjoined group address %q\n", ifma.String()) + default: + t.Errorf("\tunexpected type: %T", ifma) + } } } diff --git a/libgo/go/net/interface_windows.go b/libgo/go/net/interface_windows.go index 2ed66cdce37..add3dd3b9d9 100644 --- a/libgo/go/net/interface_windows.go +++ b/libgo/go/net/interface_windows.go @@ -31,7 +31,7 @@ func getAdapterList() (*syscall.IpAdapterInfo, error) { a = (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0])) e = syscall.GetAdaptersInfo(a, &l) } - if e != 0 { + if e != nil { return nil, os.NewSyscallError("GetAdaptersInfo", e) } return a, nil @@ -77,7 +77,7 @@ func interfaceTable(ifindex int) ([]Interface, error) { row := syscall.MibIfRow{Index: index} e := syscall.GetIfEntry(&row) - if e != 0 { + if e != nil { return nil, os.NewSyscallError("GetIfEntry", e) } diff --git a/libgo/go/net/ip.go b/libgo/go/net/ip.go index 4a388827875..979d7acd53d 100644 --- a/libgo/go/net/ip.go +++ b/libgo/go/net/ip.go @@ -450,6 +450,9 @@ func (n *IPNet) String() string { return nn.String() + "/" + itod(uint(l)) } +// Network returns the address's network name, "ip+net". +func (n *IPNet) Network() string { return "ip+net" } + // Parse IPv4 address (d.d.d.d). func parseIPv4(s string) IP { var p [IPv4len]byte diff --git a/libgo/go/net/ipraw_test.go b/libgo/go/net/ipraw_test.go index 60c405ab4ac..67a4049d5d3 100644 --- a/libgo/go/net/ipraw_test.go +++ b/libgo/go/net/ipraw_test.go @@ -59,6 +59,7 @@ func parsePingReply(p []byte) (id, seq int) { } var srchost = flag.String("srchost", "", "Source of the ICMP ECHO request") + // 127.0.0.1 because this is an IPv4-specific test. var dsthost = flag.String("dsthost", "127.0.0.1", "Destination for the ICMP ECHO request") diff --git a/libgo/go/net/iprawsock_posix.go b/libgo/go/net/iprawsock_posix.go index d3cb38a6517..7bb4c7dc0d3 100644 --- a/libgo/go/net/iprawsock_posix.go +++ b/libgo/go/net/iprawsock_posix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd windows +// +build darwin freebsd linux netbsd openbsd windows // (Raw) IP sockets diff --git a/libgo/go/net/ipsock_posix.go b/libgo/go/net/ipsock_posix.go index f0ca7dad345..d141c050b23 100644 --- a/libgo/go/net/ipsock_posix.go +++ b/libgo/go/net/ipsock_posix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd windows +// +build darwin freebsd linux netbsd openbsd windows package net diff --git a/libgo/go/net/lookup_unix.go b/libgo/go/net/lookup_unix.go index aae6d6ceb95..5c475477b83 100644 --- a/libgo/go/net/lookup_unix.go +++ b/libgo/go/net/lookup_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package net diff --git a/libgo/go/net/lookup_windows.go b/libgo/go/net/lookup_windows.go index 020871b46d6..51afbd4bb85 100644 --- a/libgo/go/net/lookup_windows.go +++ b/libgo/go/net/lookup_windows.go @@ -80,7 +80,7 @@ func LookupPort(network, service string) (port int, err error) { func LookupCNAME(name string) (cname string, err error) { var r *syscall.DNSRecord e := syscall.DnsQuery(name, syscall.DNS_TYPE_CNAME, 0, nil, &r, nil) - if e != 0 { + if e != nil { return "", os.NewSyscallError("LookupCNAME", e) } defer syscall.DnsRecordListFree(r, 1) @@ -109,7 +109,7 @@ func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err } var r *syscall.DNSRecord e := syscall.DnsQuery(target, syscall.DNS_TYPE_SRV, 0, nil, &r, nil) - if e != 0 { + if e != nil { return "", nil, os.NewSyscallError("LookupSRV", e) } defer syscall.DnsRecordListFree(r, 1) @@ -125,7 +125,7 @@ func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err func LookupMX(name string) (mx []*MX, err error) { var r *syscall.DNSRecord e := syscall.DnsQuery(name, syscall.DNS_TYPE_MX, 0, nil, &r, nil) - if e != 0 { + if e != nil { return nil, os.NewSyscallError("LookupMX", e) } defer syscall.DnsRecordListFree(r, 1) @@ -141,7 +141,7 @@ func LookupMX(name string) (mx []*MX, err error) { func LookupTXT(name string) (txt []string, err error) { var r *syscall.DNSRecord e := syscall.DnsQuery(name, syscall.DNS_TYPE_TEXT, 0, nil, &r, nil) - if e != 0 { + if e != nil { return nil, os.NewSyscallError("LookupTXT", e) } defer syscall.DnsRecordListFree(r, 1) @@ -163,7 +163,7 @@ func LookupAddr(addr string) (name []string, err error) { } var r *syscall.DNSRecord e := syscall.DnsQuery(arpa, syscall.DNS_TYPE_PTR, 0, nil, &r, nil) - if e != 0 { + if e != nil { return nil, os.NewSyscallError("LookupAddr", e) } defer syscall.DnsRecordListFree(r, 1) diff --git a/libgo/go/net/net.go b/libgo/go/net/net.go index 48f0ae791c8..b236dfdb1dd 100644 --- a/libgo/go/net/net.go +++ b/libgo/go/net/net.go @@ -157,6 +157,14 @@ func (e *OpError) Timeout() bool { return ok && t.Timeout() } +type timeoutError struct{} + +func (e *timeoutError) Error() string { return "i/o timeout" } +func (e *timeoutError) Timeout() bool { return true } +func (e *timeoutError) Temporary() bool { return true } + +var errTimeout error = &timeoutError{} + type AddrError struct { Err string Addr string diff --git a/libgo/go/net/newpollserver.go b/libgo/go/net/newpollserver.go index 035df4a6ff1..a410bb6ce1f 100644 --- a/libgo/go/net/newpollserver.go +++ b/libgo/go/net/newpollserver.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package net diff --git a/libgo/go/net/port.go b/libgo/go/net/port.go index 80597f7555d..16780da1160 100644 --- a/libgo/go/net/port.go +++ b/libgo/go/net/port.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd // Read system port mappings from /etc/services diff --git a/libgo/go/net/rpc/server_test.go b/libgo/go/net/rpc/server_test.go index f2895217aaf..c1845fa5073 100644 --- a/libgo/go/net/rpc/server_test.go +++ b/libgo/go/net/rpc/server_test.go @@ -27,7 +27,6 @@ var ( ) const ( - second = 1e9 newHttpPath = "/foo" ) @@ -388,12 +387,12 @@ func (WriteFailCodec) WriteRequest(*Request, interface{}) error { } func (WriteFailCodec) ReadResponseHeader(*Response) error { - time.Sleep(120e9) + time.Sleep(120 * time.Second) panic("unreachable") } func (WriteFailCodec) ReadResponseBody(interface{}) error { - time.Sleep(120e9) + time.Sleep(120 * time.Second) panic("unreachable") } @@ -413,7 +412,7 @@ func TestSendDeadlock(t *testing.T) { select { case <-done: return - case <-time.After(5e9): + case <-time.After(5 * time.Second): t.Fatal("deadlock") } } @@ -517,12 +516,10 @@ func benchmarkEndToEnd(dial func() (*Client, error), b *testing.B) { for atomic.AddInt32(&N, -1) >= 0 { err = client.Call("Arith.Add", args, reply) if err != nil { - fmt.Printf("Add: expected no error but got string %q", err.Error()) - panic("rpc error") + b.Fatalf("rpc error: Add: expected no error but got string %q", err.Error()) } if reply.C != args.A+args.B { - fmt.Printf("Add: expected %d got %d", reply.C, args.A+args.B) - panic("rpc error") + b.Fatalf("rpc error: Add: expected %d got %d", reply.C, args.A+args.B) } } wg.Done() @@ -537,8 +534,7 @@ func benchmarkEndToEndAsync(dial func() (*Client, error), b *testing.B) { once.Do(startServer) client, err := dial() if err != nil { - fmt.Println("error dialing", err) - return + b.Fatalf("error dialing:", err) } // Asynchronous calls @@ -562,12 +558,11 @@ func benchmarkEndToEndAsync(dial func() (*Client, error), b *testing.B) { }() go func() { for call := range res { - a := call.Args.(*Args).A - b := call.Args.(*Args).B - c := call.Reply.(*Reply).C - if a+b != c { - fmt.Printf("Add: expected %d got %d", a+b, c) - panic("incorrect reply") + A := call.Args.(*Args).A + B := call.Args.(*Args).B + C := call.Reply.(*Reply).C + if A+B != C { + b.Fatalf("incorrect reply: Add: expected %d got %d", A+B, C) } <-gate if atomic.AddInt32(&recv, -1) == 0 { diff --git a/libgo/go/net/sendfile_stub.go b/libgo/go/net/sendfile_stub.go index b0adea47873..ff76ab9cf0e 100644 --- a/libgo/go/net/sendfile_stub.go +++ b/libgo/go/net/sendfile_stub.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd openbsd +// +build darwin freebsd netbsd openbsd package net diff --git a/libgo/go/net/smtp/auth.go b/libgo/go/net/smtp/auth.go index 10a757fc643..6f0cde0d283 100644 --- a/libgo/go/net/smtp/auth.go +++ b/libgo/go/net/smtp/auth.go @@ -4,7 +4,11 @@ package smtp -import "errors" +import ( + "crypto/hmac" + "errors" + "fmt" +) // Auth is implemented by an SMTP authentication mechanism. type Auth interface { @@ -65,3 +69,29 @@ func (a *plainAuth) Next(fromServer []byte, more bool) ([]byte, error) { } return nil, nil } + +type cramMD5Auth struct { + username, secret string +} + +// CRAMMD5Auth returns an Auth that implements the CRAM-MD5 authentication +// mechanism as defined in RFC 2195. +// The returned Auth uses the given username and secret to authenticate +// to the server using the challenge-response mechanism. +func CRAMMD5Auth(username, secret string) Auth { + return &cramMD5Auth{username, secret} +} + +func (a *cramMD5Auth) Start(server *ServerInfo) (string, []byte, error) { + return "CRAM-MD5", nil, nil +} + +func (a *cramMD5Auth) Next(fromServer []byte, more bool) ([]byte, error) { + if more { + d := hmac.NewMD5([]byte(a.secret)) + d.Write(fromServer) + s := make([]byte, 0, d.Size()) + return []byte(fmt.Sprintf("%s %x", a.username, d.Sum(s))), nil + } + return nil, nil +} diff --git a/libgo/go/net/smtp/smtp_test.go b/libgo/go/net/smtp/smtp_test.go index d4e9c38bf46..ce887820531 100644 --- a/libgo/go/net/smtp/smtp_test.go +++ b/libgo/go/net/smtp/smtp_test.go @@ -23,6 +23,7 @@ type authTest struct { var authTests = []authTest{ {PlainAuth("", "user", "pass", "testserver"), []string{}, "PLAIN", []string{"\x00user\x00pass"}}, {PlainAuth("foo", "bar", "baz", "testserver"), []string{}, "PLAIN", []string{"foo\x00bar\x00baz"}}, + {CRAMMD5Auth("user", "pass"), []string{"<123456.1322876914@testserver>"}, "CRAM-MD5", []string{"", "user 287eb355114cf5c471c26a875f1ca4ae"}}, } func TestAuth(t *testing.T) { diff --git a/libgo/go/net/sock.go b/libgo/go/net/sock.go index 777f204b186..dc073927eb4 100644 --- a/libgo/go/net/sock.go +++ b/libgo/go/net/sock.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd windows +// +build darwin freebsd linux netbsd openbsd windows // Sockets diff --git a/libgo/go/net/sock_bsd.go b/libgo/go/net/sock_bsd.go index 7025edf7421..816e4fc3f74 100644 --- a/libgo/go/net/sock_bsd.go +++ b/libgo/go/net/sock_bsd.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd openbsd +// +build darwin freebsd netbsd openbsd // Sockets for BSD variants diff --git a/libgo/go/net/tcpsock_posix.go b/libgo/go/net/tcpsock_posix.go index 44890ba66bb..a7c09c73ed5 100644 --- a/libgo/go/net/tcpsock_posix.go +++ b/libgo/go/net/tcpsock_posix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd windows +// +build darwin freebsd linux netbsd openbsd windows // TCP sockets diff --git a/libgo/go/net/textproto/reader_test.go b/libgo/go/net/textproto/reader_test.go index 5aefe39867d..0460c1c8dee 100644 --- a/libgo/go/net/textproto/reader_test.go +++ b/libgo/go/net/textproto/reader_test.go @@ -203,7 +203,7 @@ func TestRFC959Lines(t *testing.T) { t.Errorf("#%d: code=%d, want %d", i, code, tt.wantCode) } if msg != tt.wantMsg { - t.Errorf("%#d: msg=%q, want %q", i, msg, tt.wantMsg) + t.Errorf("#%d: msg=%q, want %q", i, msg, tt.wantMsg) } } } diff --git a/libgo/go/net/udpsock_posix.go b/libgo/go/net/udpsock_posix.go index c25ec9c506a..6bb15714e2b 100644 --- a/libgo/go/net/udpsock_posix.go +++ b/libgo/go/net/udpsock_posix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd windows +// +build darwin freebsd linux netbsd openbsd windows // UDP sockets diff --git a/libgo/go/net/unixsock_posix.go b/libgo/go/net/unixsock_posix.go index 929f6409a4f..10632c1412e 100644 --- a/libgo/go/net/unixsock_posix.go +++ b/libgo/go/net/unixsock_posix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd windows +// +build darwin freebsd linux netbsd openbsd windows // Unix domain sockets diff --git a/libgo/go/old/netchan/common.go b/libgo/go/old/netchan/common.go index 03fa8ff6c41..710b63ac50d 100644 --- a/libgo/go/old/netchan/common.go +++ b/libgo/go/old/netchan/common.go @@ -155,7 +155,7 @@ func (cs *clientSet) drain(timeout time.Duration) error { if timeout > 0 && time.Now().After(deadline) { return errors.New("timeout") } - time.Sleep(100 * 1e6) // 100 milliseconds + time.Sleep(100 * time.Millisecond) } return nil } @@ -188,7 +188,7 @@ func (cs *clientSet) sync(timeout time.Duration) error { if timeout > 0 && time.Now().After(deadline) { return errors.New("timeout") } - time.Sleep(100 * 1e6) // 100 milliseconds + time.Sleep(100 * time.Millisecond) } return nil } diff --git a/libgo/go/old/netchan/import.go b/libgo/go/old/netchan/import.go index a6da8210b99..50abaa9fa5a 100644 --- a/libgo/go/old/netchan/import.go +++ b/libgo/go/old/netchan/import.go @@ -281,7 +281,7 @@ func (imp *Importer) Drain(timeout int64) error { if timeout > 0 && time.Now().After(deadline) { return errors.New("timeout") } - time.Sleep(100 * 1e6) + time.Sleep(100 * time.Millisecond) } return nil } diff --git a/libgo/go/old/netchan/netchan_test.go b/libgo/go/old/netchan/netchan_test.go index d11a6708661..53f0f787765 100644 --- a/libgo/go/old/netchan/netchan_test.go +++ b/libgo/go/old/netchan/netchan_test.go @@ -151,7 +151,7 @@ func TestErrorForIllegalChannel(t *testing.T) { // Expect an error now. Start a timeout. timeout := make(chan bool, 1) // buffered so closure will not hang around. go func() { - time.Sleep(10e9) // very long, to give even really slow machines a chance. + time.Sleep(10 * time.Second) // very long, to give even really slow machines a chance. timeout <- true }() select { @@ -300,7 +300,7 @@ func TestIndependentSends(t *testing.T) { go importReceive(imp, t, done) // wait for export side to try to deliver some values. - time.Sleep(0.25e9) + time.Sleep(250 * time.Millisecond) ctlch := make(chan int) if err := imp.ImportNValues("exportedCtl", ctlch, Send, 1, 1); err != nil { @@ -409,7 +409,7 @@ func TestImportFlowControl(t *testing.T) { func testFlow(sendDone chan bool, ch <-chan int, N int, t *testing.T) { go func() { - time.Sleep(0.5e9) + time.Sleep(500 * time.Millisecond) sendDone <- false }() diff --git a/libgo/go/old/regexp/all_test.go b/libgo/go/old/regexp/all_test.go index 9a04360dd11..180dac4d455 100644 --- a/libgo/go/old/regexp/all_test.go +++ b/libgo/go/old/regexp/all_test.go @@ -321,8 +321,7 @@ func BenchmarkLiteral(b *testing.B) { b.StartTimer() for i := 0; i < b.N; i++ { if !re.MatchString(x) { - println("no match!") - break + b.Fatal("no match!") } } } @@ -334,8 +333,7 @@ func BenchmarkNotLiteral(b *testing.B) { b.StartTimer() for i := 0; i < b.N; i++ { if !re.MatchString(x) { - println("no match!") - break + b.Fatal("no match!") } } } @@ -347,8 +345,7 @@ func BenchmarkMatchClass(b *testing.B) { b.StartTimer() for i := 0; i < b.N; i++ { if !re.MatchString(x) { - println("no match!") - break + b.Fatal("no match!") } } } @@ -362,8 +359,7 @@ func BenchmarkMatchClass_InRange(b *testing.B) { b.StartTimer() for i := 0; i < b.N; i++ { if !re.MatchString(x) { - println("no match!") - break + b.Fatal("no match!") } } } diff --git a/libgo/go/old/template/template_test.go b/libgo/go/old/template/template_test.go index a6e0c3e1b4e..f42a61a1b13 100644 --- a/libgo/go/old/template/template_test.go +++ b/libgo/go/old/template/template_test.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "io/ioutil" + "os" "strings" "testing" ) @@ -463,23 +464,32 @@ func TestAll(t *testing.T) { // Parse testAll(t, func(test *Test) (*Template, error) { return Parse(test.in, formatters) }) // ParseFile + f, err := ioutil.TempFile("", "template-test") + if err != nil { + t.Fatal(err) + } + defer func() { + name := f.Name() + f.Close() + os.Remove(name) + }() testAll(t, func(test *Test) (*Template, error) { - err := ioutil.WriteFile("_test/test.tmpl", []byte(test.in), 0600) + err := ioutil.WriteFile(f.Name(), []byte(test.in), 0600) if err != nil { t.Error("unexpected write error:", err) return nil, err } - return ParseFile("_test/test.tmpl", formatters) + return ParseFile(f.Name(), formatters) }) // tmpl.ParseFile testAll(t, func(test *Test) (*Template, error) { - err := ioutil.WriteFile("_test/test.tmpl", []byte(test.in), 0600) + err := ioutil.WriteFile(f.Name(), []byte(test.in), 0600) if err != nil { t.Error("unexpected write error:", err) return nil, err } tmpl := New(formatters) - return tmpl, tmpl.ParseFile("_test/test.tmpl") + return tmpl, tmpl.ParseFile(f.Name()) }) } diff --git a/libgo/go/os/dir_plan9.go b/libgo/go/os/dir_plan9.go index 263881e0c1c..78228205f47 100644 --- a/libgo/go/os/dir_plan9.go +++ b/libgo/go/os/dir_plan9.go @@ -64,9 +64,7 @@ func (file *File) Readdir(n int) (fi []FileInfo, err error) { if e != nil { return result, &PathError{"readdir", file.name, e} } - var f FileInfo - fileInfoFromStat(&f, dir) - result = append(result, f) + result = append(result, fileInfoFromStat(dir)) d.bufp += int(m) n-- @@ -94,7 +92,7 @@ func (file *File) Readdirnames(n int) (names []string, err error) { fi, err := file.Readdir(n) names = make([]string, len(fi)) for i := range fi { - names[i] = fi[i].Name + names[i] = fi[i].Name() } return } diff --git a/libgo/go/os/dir_unix.go b/libgo/go/os/dir_unix.go index e4dff835d89..e7a29554c15 100644 --- a/libgo/go/os/dir_unix.go +++ b/libgo/go/os/dir_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package os diff --git a/libgo/go/os/error_posix.go b/libgo/go/os/error_posix.go index dbe1b9a8d5e..ebbe4369703 100644 --- a/libgo/go/os/error_posix.go +++ b/libgo/go/os/error_posix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd windows +// +build darwin freebsd linux netbsd openbsd windows package os diff --git a/libgo/go/os/exec/exec.go b/libgo/go/os/exec/exec.go index 4c95c1b0dac..a00fdad497f 100644 --- a/libgo/go/os/exec/exec.go +++ b/libgo/go/os/exec/exec.go @@ -67,6 +67,9 @@ type Cmd struct { // ExtraFiles specifies additional open files to be inherited by the // new process. It does not include standard input, standard output, or // standard error. If non-nil, entry i becomes file descriptor 3+i. + // + // BUG: on OS X 10.6, child processes may sometimes inherit extra fds. + // http://golang.org/issue/2603 ExtraFiles []*os.File // SysProcAttr holds optional, operating system-specific attributes. diff --git a/libgo/go/os/exec/exec_test.go b/libgo/go/os/exec/exec_test.go index 8f63653c014..c68498047fa 100644 --- a/libgo/go/os/exec/exec_test.go +++ b/libgo/go/os/exec/exec_test.go @@ -10,6 +10,9 @@ import ( "fmt" "io" "io/ioutil" + "net" + "net/http" + "net/http/httptest" "os" "runtime" "strconv" @@ -18,7 +21,7 @@ import ( ) func helperCommand(s ...string) *Cmd { - cs := []string{"-test.run=exec.TestHelperProcess", "--"} + cs := []string{"-test.run=TestHelperProcess", "--"} cs = append(cs, s...) cmd := Command(os.Args[0], cs...) cmd.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...) @@ -146,6 +149,23 @@ func TestExtraFiles(t *testing.T) { t.Logf("no operating system support; skipping") return } + + // Force network usage, to verify the epoll (or whatever) fd + // doesn't leak to the child, + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer ln.Close() + + // Force TLS root certs to be loaded (which might involve + // cgo), to make sure none of that potential C code leaks fds. + ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Hello")) + })) + defer ts.Close() + http.Get(ts.URL) // ignore result; just calling to force root cert loading + tf, err := ioutil.TempFile("", "") if err != nil { t.Fatalf("TempFile: %v", err) @@ -167,7 +187,7 @@ func TestExtraFiles(t *testing.T) { c.ExtraFiles = []*os.File{tf} bs, err := c.CombinedOutput() if err != nil { - t.Fatalf("CombinedOutput: %v", err) + t.Fatalf("CombinedOutput: %v; output %q", err, bs) } if string(bs) != text { t.Errorf("got %q; want %q", string(bs), text) @@ -246,6 +266,32 @@ func TestHelperProcess(*testing.T) { fmt.Printf("ReadAll from fd 3: %v", err) os.Exit(1) } + switch runtime.GOOS { + case "darwin": + // TODO(bradfitz): broken? Sometimes. + // http://golang.org/issue/2603 + // Skip this additional part of the test for now. + default: + // Now verify that there are no other open fds. + var files []*os.File + for wantfd := os.Stderr.Fd() + 2; wantfd <= 100; wantfd++ { + f, err := os.Open(os.Args[0]) + if err != nil { + fmt.Printf("error opening file with expected fd %d: %v", wantfd, err) + os.Exit(1) + } + if got := f.Fd(); got != wantfd { + fmt.Printf("leaked parent file. fd = %d; want %d\n", got, wantfd) + out, _ := Command("lsof", "-p", fmt.Sprint(os.Getpid())).CombinedOutput() + fmt.Print(string(out)) + os.Exit(1) + } + files = append(files, f) + } + for _, f := range files { + f.Close() + } + } os.Stderr.Write(bs) case "exit": n, _ := strconv.Atoi(args[0]) diff --git a/libgo/go/os/exec/lp_plan9.go b/libgo/go/os/exec/lp_plan9.go index d4ffc17c006..d88cd0df959 100644 --- a/libgo/go/os/exec/lp_plan9.go +++ b/libgo/go/os/exec/lp_plan9.go @@ -18,7 +18,7 @@ func findExecutable(file string) error { if err != nil { return err } - if d.IsRegular() && d.Permission()&0111 != 0 { + if m := d.Mode(); !m.IsDir() && m&0111 != 0 { return nil } return os.EPERM diff --git a/libgo/go/os/exec/lp_unix.go b/libgo/go/os/exec/lp_unix.go index 9665ea8f413..a221137230d 100644 --- a/libgo/go/os/exec/lp_unix.go +++ b/libgo/go/os/exec/lp_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package exec diff --git a/libgo/go/os/exec_posix.go b/libgo/go/os/exec_posix.go index 8b08eebd0da..218b8cdc1d6 100644 --- a/libgo/go/os/exec_posix.go +++ b/libgo/go/os/exec_posix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd windows +// +build darwin freebsd linux netbsd openbsd windows package os @@ -24,7 +24,7 @@ func (sig UnixSignal) String() string { // StartProcess starts a new process with the program, arguments and attributes // specified by name, argv and attr. // -// StartProcess is a low-level interface. The exec package provides +// StartProcess is a low-level interface. The os/exec package provides // higher-level interfaces. func StartProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) { sysattr := &syscall.ProcAttr{ @@ -56,7 +56,7 @@ func (p *Process) Kill() error { // If successful, Exec never returns. If it fails, it returns an error. // // To run a child process, see StartProcess (for a low-level interface) -// or the exec package (for higher-level interfaces). +// or the os/exec package (for higher-level interfaces). func Exec(name string, argv []string, envv []string) error { if envv == nil { envv = Environ() diff --git a/libgo/go/os/exec_unix.go b/libgo/go/os/exec_unix.go index 3dcac414c5a..375813895d9 100644 --- a/libgo/go/os/exec_unix.go +++ b/libgo/go/os/exec_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package os diff --git a/libgo/go/os/file_plan9.go b/libgo/go/os/file_plan9.go index 42fefa96fe8..aa8f3934577 100644 --- a/libgo/go/os/file_plan9.go +++ b/libgo/go/os/file_plan9.go @@ -140,12 +140,12 @@ func (file *file) close() error { // Stat returns the FileInfo structure describing file. // It returns the FileInfo and an error, if any. -func (f *File) Stat() (fi *FileInfo, err error) { +func (f *File) Stat() (FileInfo, error) { d, err := dirstat(f) if err != nil { return nil, err } - return fileInfoFromStat(new(FileInfo), d), err + return fileInfoFromStat(d), nil } // Truncate changes the size of the file. diff --git a/libgo/go/os/file_posix.go b/libgo/go/os/file_posix.go index a4ab5d6ae25..8231ef4817c 100644 --- a/libgo/go/os/file_posix.go +++ b/libgo/go/os/file_posix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd windows +// +build darwin freebsd linux netbsd openbsd windows package os @@ -24,39 +24,6 @@ func epipecheck(file *File, e error) { } } -// Remove removes the named file or directory. -func Remove(name string) error { - // System call interface forces us to know - // whether name is a file or directory. - // Try both: it is cheaper on average than - // doing a Stat plus the right one. - e := syscall.Unlink(name) - if e == nil { - return nil - } - e1 := syscall.Rmdir(name) - if e1 == nil { - return nil - } - - // Both failed: figure out which error to return. - // OS X and Linux differ on whether unlink(dir) - // returns EISDIR, so can't use that. However, - // both agree that rmdir(file) returns ENOTDIR, - // so we can use that to decide which error is real. - // Rmdir might also return ENOTDIR if given a bad - // file path, like /etc/passwd/foo, but in that case, - // both errors will be ENOTDIR, so it's okay to - // use the error from unlink. - // For windows syscall.ENOTDIR is set - // to syscall.ERROR_PATH_NOT_FOUND, hopefully it should - // do the trick. - if e1 != syscall.ENOTDIR { - e = e1 - } - return &PathError{"remove", name, e} -} - // LinkError records an error during a link or symlink or rename // system call and the paths that caused it. type LinkError struct { diff --git a/libgo/go/os/file_unix.go b/libgo/go/os/file_unix.go index 671d1a4cb62..069baa14d4e 100644 --- a/libgo/go/os/file_unix.go +++ b/libgo/go/os/file_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package os @@ -67,8 +67,13 @@ func OpenFile(name string, flag int, perm uint32) (file *File, err error) { } // There's a race here with fork/exec, which we are - // content to live with. See ../syscall/exec.go - if syscall.O_CLOEXEC == 0 { // O_CLOEXEC not supported + // content to live with. See ../syscall/exec_unix.go. + // On OS X 10.6, the O_CLOEXEC flag is not respected. + // On OS X 10.7, the O_CLOEXEC flag works. + // Without a cheap & reliable way to detect 10.6 vs 10.7 at + // runtime, we just always call syscall.CloseOnExec on Darwin. + // Once >=10.7 is prevalent, this extra call can removed. + if syscall.O_CLOEXEC == 0 || runtime.GOOS == "darwin" { // O_CLOEXEC not supported syscall.CloseOnExec(r) } @@ -216,6 +221,36 @@ func Truncate(name string, size int64) error { return nil } +// Remove removes the named file or directory. +func Remove(name string) error { + // System call interface forces us to know + // whether name is a file or directory. + // Try both: it is cheaper on average than + // doing a Stat plus the right one. + e := syscall.Unlink(name) + if e == nil { + return nil + } + e1 := syscall.Rmdir(name) + if e1 == nil { + return nil + } + + // Both failed: figure out which error to return. + // OS X and Linux differ on whether unlink(dir) + // returns EISDIR, so can't use that. However, + // both agree that rmdir(file) returns ENOTDIR, + // so we can use that to decide which error is real. + // Rmdir might also return ENOTDIR if given a bad + // file path, like /etc/passwd/foo, but in that case, + // both errors will be ENOTDIR, so it's okay to + // use the error from unlink. + if e1 != syscall.ENOTDIR { + e = e1 + } + return &PathError{"remove", name, e} +} + // basename removes trailing slashes and the leading directory name from path name func basename(name string) string { i := len(name) - 1 diff --git a/libgo/go/os/os_test.go b/libgo/go/os/os_test.go index 299d2e86155..9a609908034 100644 --- a/libgo/go/os/os_test.go +++ b/libgo/go/os/os_test.go @@ -917,7 +917,7 @@ func TestReadAt(t *testing.T) { b := make([]byte, 5) n, err := f.ReadAt(b, 7) if err != nil || n != len(b) { - t.Fatalf("ReadAt 7: %d, %r", n, err) + t.Fatalf("ReadAt 7: %d, %v", n, err) } if string(b) != "world" { t.Fatalf("ReadAt 7: have %q want %q", string(b), "world") diff --git a/libgo/go/os/os_unix_test.go b/libgo/go/os/os_unix_test.go index 3109a8171a5..1f800d78cca 100644 --- a/libgo/go/os/os_unix_test.go +++ b/libgo/go/os/os_unix_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package os_test diff --git a/libgo/go/os/path_unix.go b/libgo/go/os/path_unix.go index 33045b60c45..30a167b1adc 100644 --- a/libgo/go/os/path_unix.go +++ b/libgo/go/os/path_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package os diff --git a/libgo/go/os/signal/signal.go b/libgo/go/os/signal/signal.go index 520f3f8a9ea..bce4530e7bc 100644 --- a/libgo/go/os/signal/signal.go +++ b/libgo/go/os/signal/signal.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build darwin freebsd linux netbsd openbsd + // Package signal implements operating system-independent signal handling. package signal @@ -31,3 +33,5 @@ func init() { Incoming = ch go process(ch) } + +// BUG(rsc): This package is unavailable on Plan 9 and Windows. diff --git a/libgo/go/os/signal/signal_test.go b/libgo/go/os/signal/signal_test.go index 00eb29578f9..4568aa9518e 100644 --- a/libgo/go/os/signal/signal_test.go +++ b/libgo/go/os/signal/signal_test.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build darwin freebsd linux netbsd openbsd + package signal import ( diff --git a/libgo/go/os/stat_plan9.go b/libgo/go/os/stat_plan9.go index e4a1dbbaea3..8d3b8a84d5c 100644 --- a/libgo/go/os/stat_plan9.go +++ b/libgo/go/os/stat_plan9.go @@ -4,87 +4,96 @@ package os -import "syscall" - -func fileInfoFromStat(fi *FileInfo, d *Dir) *FileInfo { - fi.Dev = uint64(d.Qid.Vers) | uint64(d.Qid.Type<<32) - fi.Ino = d.Qid.Path +import ( + "syscall" + "time" +) + +func sameFile(fs1, fs2 *FileStat) bool { + a := fs1.Sys.(*Dir) + b := fs2.Sys.(*Dir) + return a.Qid.Path == b.Qid.Path && a.Type == b.Type && a.Dev == b.Dev +} - fi.Mode = uint32(d.Mode) & 0777 - if (d.Mode & syscall.DMDIR) == syscall.DMDIR { - fi.Mode |= syscall.S_IFDIR - } else { - fi.Mode |= syscall.S_IFREG +func fileInfoFromStat(d *Dir) FileInfo { + fs := &FileStat{ + name: d.Name, + size: int64(d.Length), + modTime: time.Unix(int64(d.Mtime), 0), + Sys: d, } - - fi.Size = int64(d.Length) - fi.Atime_ns = 1e9 * int64(d.Atime) - fi.Mtime_ns = 1e9 * int64(d.Mtime) - fi.Name = d.Name - fi.FollowedSymlink = false - return fi + fs.mode = FileMode(d.Mode & 0777) + if d.Mode&syscall.DMDIR != 0 { + fs.mode |= ModeDir + } + if d.Mode&syscall.DMAPPEND != 0 { + fs.mode |= ModeAppend + } + if d.Mode&syscall.DMEXCL != 0 { + fs.mode |= ModeExclusive + } + if d.Mode&syscall.DMTMP != 0 { + fs.mode |= ModeTemporary + } + return fs } // arg is an open *File or a path string. func dirstat(arg interface{}) (d *Dir, err error) { var name string - nd := syscall.STATFIXLEN + 16*4 - for i := 0; i < 2; i++ { /* should work by the second try */ - buf := make([]byte, nd) + // This is big enough for most stat messages + // and rounded to a multiple of 128 bytes. + size := (syscall.STATFIXLEN + 16*4 + 128) &^ 128 - var n int - var e error + for i := 0; i < 2; i++ { + buf := make([]byte, size) - switch syscallArg := arg.(type) { + var n int + switch a := arg.(type) { case *File: - name = syscallArg.name - n, e = syscall.Fstat(syscallArg.fd, buf) + name = a.name + n, err = syscall.Fstat(a.fd, buf) case string: - name = syscallArg - n, e = syscall.Stat(name, buf) + name = a + n, err = syscall.Stat(name, buf) } - - if e != nil { - return nil, &PathError{"stat", name, e} + if err != nil { + return nil, &PathError{"stat", name, err} } - if n < syscall.STATFIXLEN { return nil, &PathError{"stat", name, Eshortstat} } - ntmp, _ := gbit16(buf) - nd = int(ntmp) + // Pull the real size out of the stat message. + s, _ := gbit16(buf) + size = int(s) - if nd <= n { - d, e := UnmarshalDir(buf[:n]) - - if e != nil { - return nil, &PathError{"stat", name, e} + // If the stat message is larger than our buffer we will + // go around the loop and allocate one that is big enough. + if size <= n { + d, err = UnmarshalDir(buf[:n]) + if err != nil { + return nil, &PathError{"stat", name, err} } - return d, e + return } } - return nil, &PathError{"stat", name, Ebadstat} } // Stat returns a FileInfo structure describing the named file and an error, if any. -func Stat(name string) (fi *FileInfo, err error) { +func Stat(name string) (FileInfo, error) { d, err := dirstat(name) if err != nil { return nil, err } - return fileInfoFromStat(new(FileInfo), d), err + return fileInfoFromStat(d), nil } // Lstat returns the FileInfo structure describing the named file and an // error, if any. If the file is a symbolic link (though Plan 9 does not have symbolic links), // the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link. -func Lstat(name string) (fi *FileInfo, err error) { - d, err := dirstat(name) - if err != nil { - return nil, err - } - return fileInfoFromStat(new(FileInfo), d), err +func Lstat(name string) (FileInfo, error) { + return Stat(name) } diff --git a/libgo/go/os/sys_bsd.go b/libgo/go/os/sys_bsd.go index c6a6de5c816..d196469e7d4 100644 --- a/libgo/go/os/sys_bsd.go +++ b/libgo/go/os/sys_bsd.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd openbsd +// +build darwin freebsd netbsd openbsd // os code shared between *BSD systems including OS X (Darwin) // and FreeBSD. diff --git a/libgo/go/os/user/lookup_stubs.go b/libgo/go/os/user/lookup_stubs.go index 0999dedbb95..42fa557445f 100644 --- a/libgo/go/os/user/lookup_stubs.go +++ b/libgo/go/os/user/lookup_stubs.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build openbsd plan9 windows +// +build nocgo windows package user diff --git a/libgo/go/os/user/lookup_unix.go b/libgo/go/os/user/lookup_unix.go index 89886cb03cf..602a3da2cd4 100644 --- a/libgo/go/os/user/lookup_unix.go +++ b/libgo/go/os/user/lookup_unix.go @@ -3,6 +3,7 @@ // license that can be found in the LICENSE file. // +build darwin freebsd linux +// +build cgo package user diff --git a/libgo/go/path/filepath/path.go b/libgo/go/path/filepath/path.go index e3d6c342ca6..3dc52aab467 100644 --- a/libgo/go/path/filepath/path.go +++ b/libgo/go/path/filepath/path.go @@ -147,6 +147,7 @@ func SplitList(path string) []string { // separating it into a directory and file name component. // If there is no Separator in path, Split returns an empty dir // and file set to path. +// The returned values have the property that path = dir+file. func Split(path string) (dir, file string) { vol := VolumeName(path) i := len(path) - 1 @@ -262,6 +263,8 @@ func Abs(path string) (string, error) { // Rel returns a relative path that is lexically equivalent to targpath when // joined to basepath with an intervening separator. That is, // Join(basepath, Rel(basepath, targpath)) is equivalent to targpath itself. +// On success, the returned path will always be relative to basepath, +// even if basepath and targpath share no elements. // An error is returned if targpath can't be made relative to basepath or if // knowing the current working directory would be necessary to compute it. func Rel(basepath, targpath string) (string, error) { @@ -423,6 +426,8 @@ func Base(path string) string { for len(path) > 0 && os.IsPathSeparator(path[len(path)-1]) { path = path[0 : len(path)-1] } + // Throw away volume name + path = path[len(VolumeName(path)):] // Find the last element i := len(path) - 1 for i >= 0 && !os.IsPathSeparator(path[i]) { @@ -437,3 +442,25 @@ func Base(path string) string { } return path } + +// Dir returns the all but the last element of path, typically the path's directory. +// Trailing path separators are removed before processing. +// If the path is empty, Dir returns ".". +// If the path consists entirely of separators, Dir returns a single separator. +// The returned path does not end in a separator unless it is the root directory. +func Dir(path string) string { + vol := VolumeName(path) + i := len(path) - 1 + for i >= len(vol) && !os.IsPathSeparator(path[i]) { + i-- + } + dir := Clean(path[len(vol) : i+1]) + last := len(dir) - 1 + if last > 0 && os.IsPathSeparator(dir[last]) { + dir = dir[:last] + } + if dir == "" { + dir = "." + } + return vol + dir +} diff --git a/libgo/go/path/filepath/path_test.go b/libgo/go/path/filepath/path_test.go index b5b0dedcd40..63adcb88c46 100644 --- a/libgo/go/path/filepath/path_test.go +++ b/libgo/go/path/filepath/path_test.go @@ -422,14 +422,77 @@ var basetests = []PathTest{ {"a/b/c.x", "c.x"}, } +var winbasetests = []PathTest{ + {`c:\`, `\`}, + {`c:.`, `.`}, + {`c:\a\b`, `b`}, + {`c:a\b`, `b`}, + {`c:a\b\c`, `c`}, + {`\\host\share\`, `\`}, + {`\\host\share\a`, `a`}, + {`\\host\share\a\b`, `b`}, +} + func TestBase(t *testing.T) { - for _, test := range basetests { - if s := filepath.ToSlash(filepath.Base(test.path)); s != test.result { + tests := basetests + if runtime.GOOS == "windows" { + // make unix tests work on windows + for i, _ := range tests { + tests[i].result = filepath.Clean(tests[i].result) + } + // add windows specific tests + tests = append(tests, winbasetests...) + } + for _, test := range tests { + if s := filepath.Base(test.path); s != test.result { t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result) } } } +var dirtests = []PathTest{ + {"", "."}, + {".", "."}, + {"/.", "/"}, + {"/", "/"}, + {"////", "/"}, + {"/foo", "/"}, + {"x/", "x"}, + {"abc", "."}, + {"abc/def", "abc"}, + {"a/b/.x", "a/b"}, + {"a/b/c.", "a/b"}, + {"a/b/c.x", "a/b"}, +} + +var windirtests = []PathTest{ + {`c:\`, `c:\`}, + {`c:.`, `c:.`}, + {`c:\a\b`, `c:\a`}, + {`c:a\b`, `c:a`}, + {`c:a\b\c`, `c:a\b`}, + {`\\host\share\`, `\\host\share\`}, + {`\\host\share\a`, `\\host\share\`}, + {`\\host\share\a\b`, `\\host\share\a`}, +} + +func TestDir(t *testing.T) { + tests := dirtests + if runtime.GOOS == "windows" { + // make unix tests work on windows + for i, _ := range tests { + tests[i].result = filepath.Clean(tests[i].result) + } + // add windows specific tests + tests = append(tests, windirtests...) + } + for _, test := range tests { + if s := filepath.Dir(test.path); s != test.result { + t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result) + } + } +} + type IsAbsTest struct { path string isAbs bool diff --git a/libgo/go/path/filepath/path_unix.go b/libgo/go/path/filepath/path_unix.go index daf0eb2af7c..c5ac71efe21 100644 --- a/libgo/go/path/filepath/path_unix.go +++ b/libgo/go/path/filepath/path_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package filepath diff --git a/libgo/go/path/path.go b/libgo/go/path/path.go index 235384667c6..20d89c9ff0c 100644 --- a/libgo/go/path/path.go +++ b/libgo/go/path/path.go @@ -160,3 +160,21 @@ func Base(path string) string { func IsAbs(path string) bool { return len(path) > 0 && path[0] == '/' } + +// Dir returns the all but the last element of path, typically the path's directory. +// Trailing path separators are removed before processing. +// If the path is empty, Dir returns ".". +// If the path consists entirely of separators, Dir returns a single separator. +// The returned path does not end in a separator unless it is the root directory. +func Dir(path string) string { + dir, _ := Split(path) + dir = Clean(dir) + last := len(dir) - 1 + if last > 0 && dir[last] == '/' { + dir = dir[:last] + } + if dir == "" { + dir = "." + } + return dir +} diff --git a/libgo/go/path/path_test.go b/libgo/go/path/path_test.go index 1fd57cc800e..77f080433b6 100644 --- a/libgo/go/path/path_test.go +++ b/libgo/go/path/path_test.go @@ -8,11 +8,11 @@ import ( "testing" ) -type CleanTest struct { - path, clean string +type PathTest struct { + path, result string } -var cleantests = []CleanTest{ +var cleantests = []PathTest{ // Already clean {"", "."}, {"abc", "abc"}, @@ -64,8 +64,8 @@ var cleantests = []CleanTest{ func TestClean(t *testing.T) { for _, test := range cleantests { - if s := Clean(test.path); s != test.clean { - t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.clean) + if s := Clean(test.path); s != test.result { + t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result) } } } @@ -148,7 +148,7 @@ func TestExt(t *testing.T) { } } -var basetests = []CleanTest{ +var basetests = []PathTest{ // Already clean {"", "."}, {".", "."}, @@ -165,8 +165,31 @@ var basetests = []CleanTest{ func TestBase(t *testing.T) { for _, test := range basetests { - if s := Base(test.path); s != test.clean { - t.Errorf("Base(%q) = %q, want %q", test.path, s, test.clean) + if s := Base(test.path); s != test.result { + t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result) + } + } +} + +var dirtests = []PathTest{ + {"", "."}, + {".", "."}, + {"/.", "/"}, + {"/", "/"}, + {"////", "/"}, + {"/foo", "/"}, + {"x/", "x"}, + {"abc", "."}, + {"abc/def", "abc"}, + {"a/b/.x", "a/b"}, + {"a/b/c.", "a/b"}, + {"a/b/c.x", "a/b"}, +} + +func TestDir(t *testing.T) { + for _, test := range dirtests { + if s := Dir(test.path); s != test.result { + t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result) } } } diff --git a/libgo/go/reflect/all_test.go b/libgo/go/reflect/all_test.go index 7d1cb094e5c..8e28a53f269 100644 --- a/libgo/go/reflect/all_test.go +++ b/libgo/go/reflect/all_test.go @@ -1561,14 +1561,26 @@ func TestSmallNegativeInt(t *testing.T) { func TestSlice(t *testing.T) { xs := []int{1, 2, 3, 4, 5, 6, 7, 8} v := ValueOf(xs).Slice(3, 5).Interface().([]int) - if len(v) != 2 || v[0] != 4 || v[1] != 5 { - t.Errorf("xs.Slice(3, 5) = %v", v) + if len(v) != 2 { + t.Errorf("len(xs.Slice(3, 5)) = %d", len(v)) + } + if cap(v) != 5 { + t.Errorf("cap(xs.Slice(3, 5)) = %d", cap(v)) + } + if !DeepEqual(v[0:5], xs[3:]) { + t.Errorf("xs.Slice(3, 5)[0:5] = %v", v[0:5]) } - xa := [7]int{10, 20, 30, 40, 50, 60, 70} + xa := [8]int{10, 20, 30, 40, 50, 60, 70, 80} v = ValueOf(&xa).Elem().Slice(2, 5).Interface().([]int) - if len(v) != 3 || v[0] != 30 || v[1] != 40 || v[2] != 50 { - t.Errorf("xa.Slice(2, 5) = %v", v) + if len(v) != 3 { + t.Errorf("len(xa.Slice(2, 5)) = %d", len(v)) + } + if cap(v) != 6 { + t.Errorf("cap(xa.Slice(2, 5)) = %d", cap(v)) + } + if !DeepEqual(v[0:6], xa[2:]) { + t.Errorf("xs.Slice(2, 5)[0:6] = %v", v[0:6]) } } diff --git a/libgo/go/reflect/value.go b/libgo/go/reflect/value.go index b57ed462c84..f9a3c8a23c5 100644 --- a/libgo/go/reflect/value.go +++ b/libgo/go/reflect/value.go @@ -1349,7 +1349,7 @@ func (v Value) Slice(beg, end int) Value { s := (*SliceHeader)(unsafe.Pointer(&x)) s.Data = uintptr(base) + uintptr(beg)*toCommonType(typ.elem).Size() s.Len = end - beg - s.Cap = end - beg + s.Cap = cap - beg fl := v.flag&flagRO | flagIndir | flag(Slice)<<flagKindShift return Value{typ.common(), unsafe.Pointer(&x), fl} diff --git a/libgo/go/regexp/all_test.go b/libgo/go/regexp/all_test.go index 8810796daf2..e729510b513 100644 --- a/libgo/go/regexp/all_test.go +++ b/libgo/go/regexp/all_test.go @@ -324,8 +324,7 @@ func BenchmarkLiteral(b *testing.B) { b.StartTimer() for i := 0; i < b.N; i++ { if !re.MatchString(x) { - println("no match!") - break + b.Fatalf("no match!") } } } @@ -337,8 +336,7 @@ func BenchmarkNotLiteral(b *testing.B) { b.StartTimer() for i := 0; i < b.N; i++ { if !re.MatchString(x) { - println("no match!") - break + b.Fatalf("no match!") } } } @@ -350,8 +348,7 @@ func BenchmarkMatchClass(b *testing.B) { b.StartTimer() for i := 0; i < b.N; i++ { if !re.MatchString(x) { - println("no match!") - break + b.Fatalf("no match!") } } } @@ -365,8 +362,7 @@ func BenchmarkMatchClass_InRange(b *testing.B) { b.StartTimer() for i := 0; i < b.N; i++ { if !re.MatchString(x) { - println("no match!") - break + b.Fatalf("no match!") } } } diff --git a/libgo/go/regexp/exec.go b/libgo/go/regexp/exec.go index d7057a191b7..e16a1b5b9e2 100644 --- a/libgo/go/regexp/exec.go +++ b/libgo/go/regexp/exec.go @@ -1,6 +1,9 @@ package regexp -import "regexp/syntax" +import ( + "io" + "regexp/syntax" +) // A queue is a 'sparse array' holding pending threads of execution. // See http://research.swtch.com/2008/03/using-uninitialized-memory-for-fun-and.html @@ -34,6 +37,28 @@ type machine struct { pool []*thread // pool of available threads matched bool // whether a match was found matchcap []int // capture information for the match + + // cached inputs, to avoid allocation + inputBytes inputBytes + inputString inputString + inputReader inputReader +} + +func (m *machine) newInputBytes(b []byte) input { + m.inputBytes.str = b + return &m.inputBytes +} + +func (m *machine) newInputString(s string) input { + m.inputString.str = s + return &m.inputString +} + +func (m *machine) newInputReader(r io.RuneReader) input { + m.inputReader.r = r + m.inputReader.atEOT = false + m.inputReader.pos = 0 + return &m.inputReader } // progMachine returns a new machine running the prog p. @@ -74,6 +99,9 @@ func (m *machine) alloc(i *syntax.Inst) *thread { // free returns t to the free pool. func (m *machine) free(t *thread) { + m.inputBytes.str = nil + m.inputString.str = "" + m.inputReader.r = nil m.pool = append(m.pool, t) } @@ -287,8 +315,16 @@ var empty = make([]int, 0) // doExecute finds the leftmost match in the input and returns // the position of its subexpressions. -func (re *Regexp) doExecute(i input, pos int, ncap int) []int { +func (re *Regexp) doExecute(r io.RuneReader, b []byte, s string, pos int, ncap int) []int { m := re.get() + var i input + if r != nil { + i = m.newInputReader(r) + } else if b != nil { + i = m.newInputBytes(b) + } else { + i = m.newInputString(s) + } m.init(ncap) if !m.match(i, pos) { re.put(m) diff --git a/libgo/go/regexp/exec_test.go b/libgo/go/regexp/exec_test.go index d981f5495e7..e668574a514 100644 --- a/libgo/go/regexp/exec_test.go +++ b/libgo/go/regexp/exec_test.go @@ -10,7 +10,6 @@ import ( "fmt" "io" "math/rand" - old "old/regexp" "os" "path/filepath" "regexp/syntax" @@ -674,19 +673,7 @@ func benchmark(b *testing.B, re string, n int) { b.SetBytes(int64(n)) for i := 0; i < b.N; i++ { if r.Match(t) { - panic("match!") - } - } -} - -func benchold(b *testing.B, re string, n int) { - r := old.MustCompile(re) - t := makeText(n) - b.ResetTimer() - b.SetBytes(int64(n)) - for i := 0; i < b.N; i++ { - if r.Match(t) { - panic("match!") + b.Fatal("match!") } } } @@ -700,35 +687,23 @@ const ( "(N)(O)(P)(Q)(R)(S)(T)(U)(V)(W)(X)(Y)(Z)$" ) -func BenchmarkMatchEasy0_1K(b *testing.B) { benchmark(b, easy0, 1<<10) } -func BenchmarkMatchEasy0_1K_Old(b *testing.B) { benchold(b, easy0, 1<<10) } -func BenchmarkMatchEasy0_1M(b *testing.B) { benchmark(b, easy0, 1<<20) } -func BenchmarkMatchEasy0_1M_Old(b *testing.B) { benchold(b, easy0, 1<<20) } -func BenchmarkMatchEasy0_32K(b *testing.B) { benchmark(b, easy0, 32<<10) } -func BenchmarkMatchEasy0_32K_Old(b *testing.B) { benchold(b, easy0, 32<<10) } -func BenchmarkMatchEasy0_32M(b *testing.B) { benchmark(b, easy0, 32<<20) } -func BenchmarkMatchEasy0_32M_Old(b *testing.B) { benchold(b, easy0, 32<<20) } -func BenchmarkMatchEasy1_1K(b *testing.B) { benchmark(b, easy1, 1<<10) } -func BenchmarkMatchEasy1_1K_Old(b *testing.B) { benchold(b, easy1, 1<<10) } -func BenchmarkMatchEasy1_1M(b *testing.B) { benchmark(b, easy1, 1<<20) } -func BenchmarkMatchEasy1_1M_Old(b *testing.B) { benchold(b, easy1, 1<<20) } -func BenchmarkMatchEasy1_32K(b *testing.B) { benchmark(b, easy1, 32<<10) } -func BenchmarkMatchEasy1_32K_Old(b *testing.B) { benchold(b, easy1, 32<<10) } -func BenchmarkMatchEasy1_32M(b *testing.B) { benchmark(b, easy1, 32<<20) } -func BenchmarkMatchEasy1_32M_Old(b *testing.B) { benchold(b, easy1, 32<<20) } -func BenchmarkMatchMedium_1K(b *testing.B) { benchmark(b, medium, 1<<10) } -func BenchmarkMatchMedium_1K_Old(b *testing.B) { benchold(b, medium, 1<<10) } -func BenchmarkMatchMedium_1M(b *testing.B) { benchmark(b, medium, 1<<20) } -func BenchmarkMatchMedium_1M_Old(b *testing.B) { benchold(b, medium, 1<<20) } -func BenchmarkMatchMedium_32K(b *testing.B) { benchmark(b, medium, 32<<10) } -func BenchmarkMatchMedium_32K_Old(b *testing.B) { benchold(b, medium, 32<<10) } -func BenchmarkMatchMedium_32M(b *testing.B) { benchmark(b, medium, 32<<20) } -func BenchmarkMatchMedium_32M_Old(b *testing.B) { benchold(b, medium, 32<<20) } -func BenchmarkMatchHard_1K(b *testing.B) { benchmark(b, hard, 1<<10) } -func BenchmarkMatchHard_1K_Old(b *testing.B) { benchold(b, hard, 1<<10) } -func BenchmarkMatchHard_1M(b *testing.B) { benchmark(b, hard, 1<<20) } -func BenchmarkMatchHard_1M_Old(b *testing.B) { benchold(b, hard, 1<<20) } -func BenchmarkMatchHard_32K(b *testing.B) { benchmark(b, hard, 32<<10) } -func BenchmarkMatchHard_32K_Old(b *testing.B) { benchold(b, hard, 32<<10) } -func BenchmarkMatchHard_32M(b *testing.B) { benchmark(b, hard, 32<<20) } -func BenchmarkMatchHard_32M_Old(b *testing.B) { benchold(b, hard, 32<<20) } +func BenchmarkMatchEasy0_32(b *testing.B) { benchmark(b, easy0, 32<<0) } +func BenchmarkMatchEasy0_1K(b *testing.B) { benchmark(b, easy0, 1<<10) } +func BenchmarkMatchEasy0_32K(b *testing.B) { benchmark(b, easy0, 32<<10) } +func BenchmarkMatchEasy0_1M(b *testing.B) { benchmark(b, easy0, 1<<20) } +func BenchmarkMatchEasy0_32M(b *testing.B) { benchmark(b, easy0, 32<<20) } +func BenchmarkMatchEasy1_32(b *testing.B) { benchmark(b, easy1, 32<<0) } +func BenchmarkMatchEasy1_1K(b *testing.B) { benchmark(b, easy1, 1<<10) } +func BenchmarkMatchEasy1_32K(b *testing.B) { benchmark(b, easy1, 32<<10) } +func BenchmarkMatchEasy1_1M(b *testing.B) { benchmark(b, easy1, 1<<20) } +func BenchmarkMatchEasy1_32M(b *testing.B) { benchmark(b, easy1, 32<<20) } +func BenchmarkMatchMedium_32(b *testing.B) { benchmark(b, medium, 1<<0) } +func BenchmarkMatchMedium_1K(b *testing.B) { benchmark(b, medium, 1<<10) } +func BenchmarkMatchMedium_32K(b *testing.B) { benchmark(b, medium, 32<<10) } +func BenchmarkMatchMedium_1M(b *testing.B) { benchmark(b, medium, 1<<20) } +func BenchmarkMatchMedium_32M(b *testing.B) { benchmark(b, medium, 32<<20) } +func BenchmarkMatchHard_32(b *testing.B) { benchmark(b, hard, 32<<0) } +func BenchmarkMatchHard_1K(b *testing.B) { benchmark(b, hard, 1<<10) } +func BenchmarkMatchHard_32K(b *testing.B) { benchmark(b, hard, 32<<10) } +func BenchmarkMatchHard_1M(b *testing.B) { benchmark(b, hard, 1<<20) } +func BenchmarkMatchHard_32M(b *testing.B) { benchmark(b, hard, 32<<20) } diff --git a/libgo/go/regexp/regexp.go b/libgo/go/regexp/regexp.go index 59f3be39d29..b0c6a0b1a14 100644 --- a/libgo/go/regexp/regexp.go +++ b/libgo/go/regexp/regexp.go @@ -240,10 +240,6 @@ type inputString struct { str string } -func newInputString(str string) *inputString { - return &inputString{str: str} -} - func (i *inputString) step(pos int) (rune, int) { if pos < len(i.str) { c := i.str[pos] @@ -283,10 +279,6 @@ type inputBytes struct { str []byte } -func newInputBytes(str []byte) *inputBytes { - return &inputBytes{str: str} -} - func (i *inputBytes) step(pos int) (rune, int) { if pos < len(i.str) { c := i.str[pos] @@ -328,10 +320,6 @@ type inputReader struct { pos int } -func newInputReader(r io.RuneReader) *inputReader { - return &inputReader{r: r} -} - func (i *inputReader) step(pos int) (rune, int) { if !i.atEOT && pos != i.pos { return endOfText, 0 @@ -373,19 +361,19 @@ func (re *Regexp) LiteralPrefix() (prefix string, complete bool) { // RuneReader. The return value is a boolean: true for match, false for no // match. func (re *Regexp) MatchReader(r io.RuneReader) bool { - return re.doExecute(newInputReader(r), 0, 0) != nil + return re.doExecute(r, nil, "", 0, 0) != nil } // MatchString returns whether the Regexp matches the string s. // The return value is a boolean: true for match, false for no match. func (re *Regexp) MatchString(s string) bool { - return re.doExecute(newInputString(s), 0, 0) != nil + return re.doExecute(nil, nil, s, 0, 0) != nil } // Match returns whether the Regexp matches the byte slice b. // The return value is a boolean: true for match, false for no match. func (re *Regexp) Match(b []byte) bool { - return re.doExecute(newInputBytes(b), 0, 0) != nil + return re.doExecute(nil, b, "", 0, 0) != nil } // MatchReader checks whether a textual regular expression matches the text @@ -437,7 +425,7 @@ func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) str searchPos := 0 // position where we next look for a match buf := new(bytes.Buffer) for searchPos <= len(src) { - a := re.doExecute(newInputString(src), searchPos, 2) + a := re.doExecute(nil, nil, src, searchPos, 2) if len(a) == 0 { break // no more matches } @@ -489,7 +477,7 @@ func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte { searchPos := 0 // position where we next look for a match buf := new(bytes.Buffer) for searchPos <= len(src) { - a := re.doExecute(newInputBytes(src), searchPos, 2) + a := re.doExecute(nil, src, "", searchPos, 2) if len(a) == 0 { break // no more matches } @@ -577,13 +565,7 @@ func (re *Regexp) allMatches(s string, b []byte, n int, deliver func([]int)) { } for pos, i, prevMatchEnd := 0, 0, -1; i < n && pos <= end; { - var in input - if b == nil { - in = newInputString(s) - } else { - in = newInputBytes(b) - } - matches := re.doExecute(in, pos, re.prog.NumCap) + matches := re.doExecute(nil, b, s, pos, re.prog.NumCap) if len(matches) == 0 { break } @@ -623,7 +605,7 @@ func (re *Regexp) allMatches(s string, b []byte, n int, deliver func([]int)) { // Find returns a slice holding the text of the leftmost match in b of the regular expression. // A return value of nil indicates no match. func (re *Regexp) Find(b []byte) []byte { - a := re.doExecute(newInputBytes(b), 0, 2) + a := re.doExecute(nil, b, "", 0, 2) if a == nil { return nil } @@ -635,7 +617,7 @@ func (re *Regexp) Find(b []byte) []byte { // b[loc[0]:loc[1]]. // A return value of nil indicates no match. func (re *Regexp) FindIndex(b []byte) (loc []int) { - a := re.doExecute(newInputBytes(b), 0, 2) + a := re.doExecute(nil, b, "", 0, 2) if a == nil { return nil } @@ -648,7 +630,7 @@ func (re *Regexp) FindIndex(b []byte) (loc []int) { // an empty string. Use FindStringIndex or FindStringSubmatch if it is // necessary to distinguish these cases. func (re *Regexp) FindString(s string) string { - a := re.doExecute(newInputString(s), 0, 2) + a := re.doExecute(nil, nil, s, 0, 2) if a == nil { return "" } @@ -660,7 +642,7 @@ func (re *Regexp) FindString(s string) string { // itself is at s[loc[0]:loc[1]]. // A return value of nil indicates no match. func (re *Regexp) FindStringIndex(s string) []int { - a := re.doExecute(newInputString(s), 0, 2) + a := re.doExecute(nil, nil, s, 0, 2) if a == nil { return nil } @@ -672,7 +654,7 @@ func (re *Regexp) FindStringIndex(s string) []int { // the RuneReader. The match itself is at s[loc[0]:loc[1]]. A return // value of nil indicates no match. func (re *Regexp) FindReaderIndex(r io.RuneReader) []int { - a := re.doExecute(newInputReader(r), 0, 2) + a := re.doExecute(r, nil, "", 0, 2) if a == nil { return nil } @@ -685,7 +667,7 @@ func (re *Regexp) FindReaderIndex(r io.RuneReader) []int { // comment. // A return value of nil indicates no match. func (re *Regexp) FindSubmatch(b []byte) [][]byte { - a := re.doExecute(newInputBytes(b), 0, re.prog.NumCap) + a := re.doExecute(nil, b, "", 0, re.prog.NumCap) if a == nil { return nil } @@ -704,7 +686,7 @@ func (re *Regexp) FindSubmatch(b []byte) [][]byte { // in the package comment. // A return value of nil indicates no match. func (re *Regexp) FindSubmatchIndex(b []byte) []int { - return re.pad(re.doExecute(newInputBytes(b), 0, re.prog.NumCap)) + return re.pad(re.doExecute(nil, b, "", 0, re.prog.NumCap)) } // FindStringSubmatch returns a slice of strings holding the text of the @@ -713,7 +695,7 @@ func (re *Regexp) FindSubmatchIndex(b []byte) []int { // package comment. // A return value of nil indicates no match. func (re *Regexp) FindStringSubmatch(s string) []string { - a := re.doExecute(newInputString(s), 0, re.prog.NumCap) + a := re.doExecute(nil, nil, s, 0, re.prog.NumCap) if a == nil { return nil } @@ -732,7 +714,7 @@ func (re *Regexp) FindStringSubmatch(s string) []string { // 'Index' descriptions in the package comment. // A return value of nil indicates no match. func (re *Regexp) FindStringSubmatchIndex(s string) []int { - return re.pad(re.doExecute(newInputString(s), 0, re.prog.NumCap)) + return re.pad(re.doExecute(nil, nil, s, 0, re.prog.NumCap)) } // FindReaderSubmatchIndex returns a slice holding the index pairs @@ -741,7 +723,7 @@ func (re *Regexp) FindStringSubmatchIndex(s string) []int { // by the 'Submatch' and 'Index' descriptions in the package comment. A // return value of nil indicates no match. func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int { - return re.pad(re.doExecute(newInputReader(r), 0, re.prog.NumCap)) + return re.pad(re.doExecute(r, nil, "", 0, re.prog.NumCap)) } const startSize = 10 // The size at which to start a slice in the 'All' routines. diff --git a/libgo/go/regexp/syntax/parse.go b/libgo/go/regexp/syntax/parse.go index 2ad682f0047..ba641c1e6fd 100644 --- a/libgo/go/regexp/syntax/parse.go +++ b/libgo/go/regexp/syntax/parse.go @@ -1694,7 +1694,7 @@ func appendFoldedClass(r []rune, x []rune) []rune { // appendNegatedClass returns the result of appending the negation of the class x to the class r. // It assumes x is clean. func appendNegatedClass(r []rune, x []rune) []rune { - nextLo := rune('\u0000') + nextLo := '\u0000' for i := 0; i < len(x); i += 2 { lo, hi := x[i], x[i+1] if nextLo <= lo-1 { @@ -1735,7 +1735,7 @@ func appendTable(r []rune, x *unicode.RangeTable) []rune { // appendNegatedTable returns the result of appending the negation of x to the class r. func appendNegatedTable(r []rune, x *unicode.RangeTable) []rune { - nextLo := rune('\u0000') // lo end of next class to add + nextLo := '\u0000' // lo end of next class to add for _, xr := range x.R16 { lo, hi, stride := rune(xr.Lo), rune(xr.Hi), rune(xr.Stride) if stride == 1 { @@ -1777,8 +1777,8 @@ func appendNegatedTable(r []rune, x *unicode.RangeTable) []rune { // negateClass overwrites r and returns r's negation. // It assumes the class r is already clean. func negateClass(r []rune) []rune { - nextLo := rune('\u0000') // lo end of next class to add - w := 0 // write index + nextLo := '\u0000' // lo end of next class to add + w := 0 // write index for i := 0; i < len(r); i += 2 { lo, hi := r[i], r[i+1] if nextLo <= lo-1 { diff --git a/libgo/go/sort/example_test.go b/libgo/go/sort/example_test.go index 2f5ee90818c..2224db7e13c 100644 --- a/libgo/go/sort/example_test.go +++ b/libgo/go/sort/example_test.go @@ -11,7 +11,7 @@ import ( // [1 2 3 4 5 6] func ExampleInts() { - s := []int{5, 2, 6, 3, 1, 4} + s := []int{5, 2, 6, 3, 1, 4} // unsorted sort.Ints(s) fmt.Println(s) } diff --git a/libgo/go/sort/sort.go b/libgo/go/sort/sort.go index 83ee170cbab..4aa4ca6d7da 100644 --- a/libgo/go/sort/sort.go +++ b/libgo/go/sort/sort.go @@ -240,14 +240,18 @@ func (p StringSlice) Sort() { Sort(p) } // Ints sorts a slice of ints in increasing order. func Ints(a []int) { Sort(IntSlice(a)) } + // Float64s sorts a slice of float64s in increasing order. func Float64s(a []float64) { Sort(Float64Slice(a)) } + // Strings sorts a slice of strings in increasing order. func Strings(a []string) { Sort(StringSlice(a)) } // IntsAreSorted tests whether a slice of ints is sorted in increasing order. func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) } + // Float64sAreSorted tests whether a slice of float64s is sorted in increasing order. func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) } + // StringsAreSorted tests whether a slice of strings is sorted in increasing order. func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) } diff --git a/libgo/go/strconv/atob.go b/libgo/go/strconv/atob.go index 1508118d2ac..d0cb097213f 100644 --- a/libgo/go/strconv/atob.go +++ b/libgo/go/strconv/atob.go @@ -14,7 +14,7 @@ func ParseBool(str string) (value bool, err error) { case "0", "f", "F", "false", "FALSE", "False": return false, nil } - return false, &NumError{str, ErrSyntax} + return false, syntaxError("ParseBool", str) } // FormatBool returns "true" or "false" according to the value of b diff --git a/libgo/go/strconv/atof.go b/libgo/go/strconv/atof.go index 1642c18d748..42fc431db8a 100644 --- a/libgo/go/strconv/atof.go +++ b/libgo/go/strconv/atof.go @@ -263,6 +263,18 @@ func (d *decimal) atof32int() float32 { return f } +// Reads a uint64 decimal mantissa, which might be truncated. +func (d *decimal) atou64() (mant uint64, digits int) { + const uint64digits = 19 + for i, c := range d.d[:d.nd] { + if i == uint64digits { + return mant, i + } + mant = 10*mant + uint64(c-'0') + } + return mant, d.nd +} + // Exact powers of 10. var float64pow10 = []float64{ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, @@ -338,6 +350,8 @@ func (d *decimal) atof32() (f float32, ok bool) { return } +const fnParseFloat = "ParseFloat" + func atof32(s string) (f float32, err error) { if val, ok := special(s); ok { return float32(val), nil @@ -345,7 +359,7 @@ func atof32(s string) (f float32, err error) { var d decimal if !d.set(s) { - return 0, &NumError{s, ErrSyntax} + return 0, syntaxError(fnParseFloat, s) } if optimize { if f, ok := d.atof32(); ok { @@ -355,7 +369,7 @@ func atof32(s string) (f float32, err error) { b, ovf := d.floatBits(&float32info) f = math.Float32frombits(uint32(b)) if ovf { - err = &NumError{s, ErrRange} + err = rangeError(fnParseFloat, s) } return f, err } @@ -367,17 +381,28 @@ func atof64(s string) (f float64, err error) { var d decimal if !d.set(s) { - return 0, &NumError{s, ErrSyntax} + return 0, syntaxError(fnParseFloat, s) } if optimize { if f, ok := d.atof64(); ok { return f, nil } + + // Try another fast path. + ext := new(extFloat) + if ok := ext.AssignDecimal(&d); ok { + b, ovf := ext.floatBits() + f = math.Float64frombits(b) + if ovf { + err = rangeError(fnParseFloat, s) + } + return f, err + } } b, ovf := d.floatBits(&float64info) f = math.Float64frombits(b) if ovf { - err = &NumError{s, ErrRange} + err = rangeError(fnParseFloat, s) } return f, err } diff --git a/libgo/go/strconv/atof_test.go b/libgo/go/strconv/atof_test.go index a9820d1bbad..3fa637d2bc6 100644 --- a/libgo/go/strconv/atof_test.go +++ b/libgo/go/strconv/atof_test.go @@ -5,9 +5,12 @@ package strconv_test import ( + "math" + "math/rand" "reflect" . "strconv" "testing" + "time" ) type atofTest struct { @@ -111,17 +114,56 @@ var atoftests = []atofTest{ {"2.2250738585072012e-308", "2.2250738585072014e-308", nil}, // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/ {"2.2250738585072011e-308", "2.225073858507201e-308", nil}, + + // A very large number (initially wrongly parsed by the fast algorithm). + {"4.630813248087435e+307", "4.630813248087435e+307", nil}, +} + +type atofSimpleTest struct { + x float64 + s string } +var ( + atofRandomTests []atofSimpleTest + benchmarksRandomBits [1024]string + benchmarksRandomNormal [1024]string +) + func init() { // The atof routines return NumErrors wrapping // the error and the string. Convert the table above. for i := range atoftests { test := &atoftests[i] if test.err != nil { - test.err = &NumError{test.in, test.err} + test.err = &NumError{"ParseFloat", test.in, test.err} } } + + // Generate random inputs for tests and benchmarks + rand.Seed(time.Now().UnixNano()) + if testing.Short() { + atofRandomTests = make([]atofSimpleTest, 100) + } else { + atofRandomTests = make([]atofSimpleTest, 10000) + } + for i := range atofRandomTests { + n := uint64(rand.Uint32())<<32 | uint64(rand.Uint32()) + x := math.Float64frombits(n) + s := FormatFloat(x, 'g', -1, 64) + atofRandomTests[i] = atofSimpleTest{x, s} + } + + for i := range benchmarksRandomBits { + bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32()) + x := math.Float64frombits(bits) + benchmarksRandomBits[i] = FormatFloat(x, 'g', -1, 64) + } + + for i := range benchmarksRandomNormal { + x := rand.NormFloat64() + benchmarksRandomNormal[i] = FormatFloat(x, 'g', -1, 64) + } } func testAtof(t *testing.T, opt bool) { @@ -156,6 +198,19 @@ func TestAtof(t *testing.T) { testAtof(t, true) } func TestAtofSlow(t *testing.T) { testAtof(t, false) } +func TestAtofRandom(t *testing.T) { + for _, test := range atofRandomTests { + x, _ := ParseFloat(test.s, 64) + switch { + default: + t.Errorf("number %s badly parsed as %b (expected %b)", test.s, x, test.x) + case x == test.x: + case math.IsNaN(test.x) && math.IsNaN(x): + } + } + t.Logf("tested %d random numbers", len(atofRandomTests)) +} + func BenchmarkAtof64Decimal(b *testing.B) { for i := 0; i < b.N; i++ { ParseFloat("33909", 64) @@ -179,3 +234,15 @@ func BenchmarkAtof64Big(b *testing.B) { ParseFloat("123456789123456789123456789", 64) } } + +func BenchmarkAtof64RandomBits(b *testing.B) { + for i := 0; i < b.N; i++ { + ParseFloat(benchmarksRandomBits[i%1024], 64) + } +} + +func BenchmarkAtof64RandomFloats(b *testing.B) { + for i := 0; i < b.N; i++ { + ParseFloat(benchmarksRandomNormal[i%1024], 64) + } +} diff --git a/libgo/go/strconv/atoi.go b/libgo/go/strconv/atoi.go index 438d496948d..59ef264d17c 100644 --- a/libgo/go/strconv/atoi.go +++ b/libgo/go/strconv/atoi.go @@ -14,11 +14,22 @@ var ErrSyntax = errors.New("invalid syntax") // A NumError records a failed conversion. type NumError struct { - Num string // the input - Err error // the reason the conversion failed (ErrRange, ErrSyntax) + Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat) + Num string // the input + Err error // the reason the conversion failed (ErrRange, ErrSyntax) } -func (e *NumError) Error() string { return `parsing "` + e.Num + `": ` + e.Err.Error() } +func (e *NumError) Error() string { + return "strconv." + e.Func + ": " + `parsing "` + e.Num + `": ` + e.Err.Error() +} + +func syntaxError(fn, str string) *NumError { + return &NumError{fn, str, ErrSyntax} +} + +func rangeError(fn, str string) *NumError { + return &NumError{fn, str, ErrRange} +} const intSize = 32 << uint(^uint(0)>>63) @@ -116,13 +127,13 @@ func ParseUint(s string, b int, bitSize int) (n uint64, err error) { return n, nil Error: - return n, &NumError{s0, err} + return n, &NumError{"ParseUint", s0, err} } -// ParseInt interprets a string s in an arbitrary base b (2 to 36) -// and returns the corresponding value n. If b == 0, the base -// is taken from the string prefix: base 16 for "0x", base 8 for "0", -// and base 10 otherwise. +// ParseInt interprets a string s in the given base (2 to 36) and +// returns the corresponding value i. If base == 0, the base is +// implied by the string's prefix: base 16 for "0x", base 8 for +// "0", and base 10 otherwise. // // The bitSize argument specifies the integer type // that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 @@ -134,13 +145,15 @@ Error: // to s cannot be represented by a signed integer of the // given size, err.Error = ErrRange. func ParseInt(s string, base int, bitSize int) (i int64, err error) { + const fnParseInt = "ParseInt" + if bitSize == 0 { bitSize = int(IntSize) } // Empty string bad. if len(s) == 0 { - return 0, &NumError{s, ErrSyntax} + return 0, syntaxError(fnParseInt, s) } // Pick off leading sign. @@ -157,15 +170,16 @@ func ParseInt(s string, base int, bitSize int) (i int64, err error) { var un uint64 un, err = ParseUint(s, base, bitSize) if err != nil && err.(*NumError).Err != ErrRange { + err.(*NumError).Func = fnParseInt err.(*NumError).Num = s0 return 0, err } cutoff := uint64(1 << uint(bitSize-1)) if !neg && un >= cutoff { - return int64(cutoff - 1), &NumError{s0, ErrRange} + return int64(cutoff - 1), rangeError(fnParseInt, s0) } if neg && un > cutoff { - return -int64(cutoff), &NumError{s0, ErrRange} + return -int64(cutoff), rangeError(fnParseInt, s0) } n := int64(un) if neg { diff --git a/libgo/go/strconv/atoi_test.go b/libgo/go/strconv/atoi_test.go index 2d06efed0de..d0e7b61dba8 100644 --- a/libgo/go/strconv/atoi_test.go +++ b/libgo/go/strconv/atoi_test.go @@ -152,37 +152,37 @@ func init() { for i := range atoui64tests { test := &atoui64tests[i] if test.err != nil { - test.err = &NumError{test.in, test.err} + test.err = &NumError{"ParseUint", test.in, test.err} } } for i := range btoui64tests { test := &btoui64tests[i] if test.err != nil { - test.err = &NumError{test.in, test.err} + test.err = &NumError{"ParseUint", test.in, test.err} } } for i := range atoi64tests { test := &atoi64tests[i] if test.err != nil { - test.err = &NumError{test.in, test.err} + test.err = &NumError{"ParseInt", test.in, test.err} } } for i := range btoi64tests { test := &btoi64tests[i] if test.err != nil { - test.err = &NumError{test.in, test.err} + test.err = &NumError{"ParseInt", test.in, test.err} } } for i := range atoui32tests { test := &atoui32tests[i] if test.err != nil { - test.err = &NumError{test.in, test.err} + test.err = &NumError{"ParseUint", test.in, test.err} } } for i := range atoi32tests { test := &atoi32tests[i] if test.err != nil { - test.err = &NumError{test.in, test.err} + test.err = &NumError{"ParseInt", test.in, test.err} } } } diff --git a/libgo/go/strconv/decimal.go b/libgo/go/strconv/decimal.go index 541553097bb..cc5591a8d8f 100644 --- a/libgo/go/strconv/decimal.go +++ b/libgo/go/strconv/decimal.go @@ -14,9 +14,9 @@ package strconv type decimal struct { // TODO(rsc): Can make d[] a bit smaller and add // truncated bool; - d [2000]byte // digits - nd int // number of digits used - dp int // decimal point + d [800]byte // digits + nd int // number of digits used + dp int // decimal point neg bool } diff --git a/libgo/go/strconv/extfloat.go b/libgo/go/strconv/extfloat.go new file mode 100644 index 00000000000..980052a778b --- /dev/null +++ b/libgo/go/strconv/extfloat.go @@ -0,0 +1,311 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package strconv + +import "math" + +// An extFloat represents an extended floating-point number, with more +// precision than a float64. It does not try to save bits: the +// number represented by the structure is mant*(2^exp), with a negative +// sign if neg is true. +type extFloat struct { + mant uint64 + exp int + neg bool +} + +// Powers of ten taken from double-conversion library. +// http://code.google.com/p/double-conversion/ +const ( + firstPowerOfTen = -348 + stepPowerOfTen = 8 +) + +var smallPowersOfTen = [...]extFloat{ + {1 << 63, -63, false}, // 1 + {0xa << 60, -60, false}, // 1e1 + {0x64 << 57, -57, false}, // 1e2 + {0x3e8 << 54, -54, false}, // 1e3 + {0x2710 << 50, -50, false}, // 1e4 + {0x186a0 << 47, -47, false}, // 1e5 + {0xf4240 << 44, -44, false}, // 1e6 + {0x989680 << 40, -40, false}, // 1e7 +} + +var powersOfTen = [...]extFloat{ + {0xfa8fd5a0081c0288, -1220, false}, // 10^-348 + {0xbaaee17fa23ebf76, -1193, false}, // 10^-340 + {0x8b16fb203055ac76, -1166, false}, // 10^-332 + {0xcf42894a5dce35ea, -1140, false}, // 10^-324 + {0x9a6bb0aa55653b2d, -1113, false}, // 10^-316 + {0xe61acf033d1a45df, -1087, false}, // 10^-308 + {0xab70fe17c79ac6ca, -1060, false}, // 10^-300 + {0xff77b1fcbebcdc4f, -1034, false}, // 10^-292 + {0xbe5691ef416bd60c, -1007, false}, // 10^-284 + {0x8dd01fad907ffc3c, -980, false}, // 10^-276 + {0xd3515c2831559a83, -954, false}, // 10^-268 + {0x9d71ac8fada6c9b5, -927, false}, // 10^-260 + {0xea9c227723ee8bcb, -901, false}, // 10^-252 + {0xaecc49914078536d, -874, false}, // 10^-244 + {0x823c12795db6ce57, -847, false}, // 10^-236 + {0xc21094364dfb5637, -821, false}, // 10^-228 + {0x9096ea6f3848984f, -794, false}, // 10^-220 + {0xd77485cb25823ac7, -768, false}, // 10^-212 + {0xa086cfcd97bf97f4, -741, false}, // 10^-204 + {0xef340a98172aace5, -715, false}, // 10^-196 + {0xb23867fb2a35b28e, -688, false}, // 10^-188 + {0x84c8d4dfd2c63f3b, -661, false}, // 10^-180 + {0xc5dd44271ad3cdba, -635, false}, // 10^-172 + {0x936b9fcebb25c996, -608, false}, // 10^-164 + {0xdbac6c247d62a584, -582, false}, // 10^-156 + {0xa3ab66580d5fdaf6, -555, false}, // 10^-148 + {0xf3e2f893dec3f126, -529, false}, // 10^-140 + {0xb5b5ada8aaff80b8, -502, false}, // 10^-132 + {0x87625f056c7c4a8b, -475, false}, // 10^-124 + {0xc9bcff6034c13053, -449, false}, // 10^-116 + {0x964e858c91ba2655, -422, false}, // 10^-108 + {0xdff9772470297ebd, -396, false}, // 10^-100 + {0xa6dfbd9fb8e5b88f, -369, false}, // 10^-92 + {0xf8a95fcf88747d94, -343, false}, // 10^-84 + {0xb94470938fa89bcf, -316, false}, // 10^-76 + {0x8a08f0f8bf0f156b, -289, false}, // 10^-68 + {0xcdb02555653131b6, -263, false}, // 10^-60 + {0x993fe2c6d07b7fac, -236, false}, // 10^-52 + {0xe45c10c42a2b3b06, -210, false}, // 10^-44 + {0xaa242499697392d3, -183, false}, // 10^-36 + {0xfd87b5f28300ca0e, -157, false}, // 10^-28 + {0xbce5086492111aeb, -130, false}, // 10^-20 + {0x8cbccc096f5088cc, -103, false}, // 10^-12 + {0xd1b71758e219652c, -77, false}, // 10^-4 + {0x9c40000000000000, -50, false}, // 10^4 + {0xe8d4a51000000000, -24, false}, // 10^12 + {0xad78ebc5ac620000, 3, false}, // 10^20 + {0x813f3978f8940984, 30, false}, // 10^28 + {0xc097ce7bc90715b3, 56, false}, // 10^36 + {0x8f7e32ce7bea5c70, 83, false}, // 10^44 + {0xd5d238a4abe98068, 109, false}, // 10^52 + {0x9f4f2726179a2245, 136, false}, // 10^60 + {0xed63a231d4c4fb27, 162, false}, // 10^68 + {0xb0de65388cc8ada8, 189, false}, // 10^76 + {0x83c7088e1aab65db, 216, false}, // 10^84 + {0xc45d1df942711d9a, 242, false}, // 10^92 + {0x924d692ca61be758, 269, false}, // 10^100 + {0xda01ee641a708dea, 295, false}, // 10^108 + {0xa26da3999aef774a, 322, false}, // 10^116 + {0xf209787bb47d6b85, 348, false}, // 10^124 + {0xb454e4a179dd1877, 375, false}, // 10^132 + {0x865b86925b9bc5c2, 402, false}, // 10^140 + {0xc83553c5c8965d3d, 428, false}, // 10^148 + {0x952ab45cfa97a0b3, 455, false}, // 10^156 + {0xde469fbd99a05fe3, 481, false}, // 10^164 + {0xa59bc234db398c25, 508, false}, // 10^172 + {0xf6c69a72a3989f5c, 534, false}, // 10^180 + {0xb7dcbf5354e9bece, 561, false}, // 10^188 + {0x88fcf317f22241e2, 588, false}, // 10^196 + {0xcc20ce9bd35c78a5, 614, false}, // 10^204 + {0x98165af37b2153df, 641, false}, // 10^212 + {0xe2a0b5dc971f303a, 667, false}, // 10^220 + {0xa8d9d1535ce3b396, 694, false}, // 10^228 + {0xfb9b7cd9a4a7443c, 720, false}, // 10^236 + {0xbb764c4ca7a44410, 747, false}, // 10^244 + {0x8bab8eefb6409c1a, 774, false}, // 10^252 + {0xd01fef10a657842c, 800, false}, // 10^260 + {0x9b10a4e5e9913129, 827, false}, // 10^268 + {0xe7109bfba19c0c9d, 853, false}, // 10^276 + {0xac2820d9623bf429, 880, false}, // 10^284 + {0x80444b5e7aa7cf85, 907, false}, // 10^292 + {0xbf21e44003acdd2d, 933, false}, // 10^300 + {0x8e679c2f5e44ff8f, 960, false}, // 10^308 + {0xd433179d9c8cb841, 986, false}, // 10^316 + {0x9e19db92b4e31ba9, 1013, false}, // 10^324 + {0xeb96bf6ebadf77d9, 1039, false}, // 10^332 + {0xaf87023b9bf0ee6b, 1066, false}, // 10^340 +} + +// floatBits returns the bits of the float64 that best approximates +// the extFloat passed as receiver. Overflow is set to true if +// the resulting float64 is ±Inf. +func (f *extFloat) floatBits() (bits uint64, overflow bool) { + flt := &float64info + f.Normalize() + + exp := f.exp + 63 + + // Exponent too small. + if exp < flt.bias+1 { + n := flt.bias + 1 - exp + f.mant >>= uint(n) + exp += n + } + + // Extract 1+flt.mantbits bits. + mant := f.mant >> (63 - flt.mantbits) + if f.mant&(1<<(62-flt.mantbits)) != 0 { + // Round up. + mant += 1 + } + + // Rounding might have added a bit; shift down. + if mant == 2<<flt.mantbits { + mant >>= 1 + exp++ + } + + // Infinities. + if exp-flt.bias >= 1<<flt.expbits-1 { + goto overflow + } + + // Denormalized? + if mant&(1<<flt.mantbits) == 0 { + exp = flt.bias + } + goto out + +overflow: + // ±Inf + mant = 0 + exp = 1<<flt.expbits - 1 + flt.bias + overflow = true + +out: + // Assemble bits. + bits = mant & (uint64(1)<<flt.mantbits - 1) + bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits + if f.neg { + bits |= 1 << (flt.mantbits + flt.expbits) + } + return +} + +// Assign sets f to the value of x. +func (f *extFloat) Assign(x float64) { + if x < 0 { + x = -x + f.neg = true + } + x, f.exp = math.Frexp(x) + f.mant = uint64(x * float64(1<<64)) + f.exp -= 64 +} + +// Normalize normalizes f so that the highest bit of the mantissa is +// set, and returns the number by which the mantissa was left-shifted. +func (f *extFloat) Normalize() uint { + if f.mant == 0 { + return 0 + } + exp_before := f.exp + for f.mant < (1 << 55) { + f.mant <<= 8 + f.exp -= 8 + } + for f.mant < (1 << 63) { + f.mant <<= 1 + f.exp -= 1 + } + return uint(exp_before - f.exp) +} + +// Multiply sets f to the product f*g: the result is correctly rounded, +// but not normalized. +func (f *extFloat) Multiply(g extFloat) { + fhi, flo := f.mant>>32, uint64(uint32(f.mant)) + ghi, glo := g.mant>>32, uint64(uint32(g.mant)) + + // Cross products. + cross1 := fhi * glo + cross2 := flo * ghi + + // f.mant*g.mant is fhi*ghi << 64 + (cross1+cross2) << 32 + flo*glo + f.mant = fhi*ghi + (cross1 >> 32) + (cross2 >> 32) + rem := uint64(uint32(cross1)) + uint64(uint32(cross2)) + ((flo * glo) >> 32) + // Round up. + rem += (1 << 31) + + f.mant += (rem >> 32) + f.exp = f.exp + g.exp + 64 +} + +var uint64pow10 = [...]uint64{ + 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, + 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, +} + +// AssignDecimal sets f to an approximate value of the decimal d. It +// returns true if the value represented by f is guaranteed to be the +// best approximation of d after being rounded to a float64. +func (f *extFloat) AssignDecimal(d *decimal) (ok bool) { + const uint64digits = 19 + const errorscale = 8 + mant10, digits := d.atou64() + exp10 := d.dp - digits + errors := 0 // An upper bound for error, computed in errorscale*ulp. + + if digits < d.nd { + // the decimal number was truncated. + errors += errorscale / 2 + } + + f.mant = mant10 + f.exp = 0 + f.neg = d.neg + + // Multiply by powers of ten. + i := (exp10 - firstPowerOfTen) / stepPowerOfTen + if exp10 < firstPowerOfTen || i >= len(powersOfTen) { + return false + } + adjExp := (exp10 - firstPowerOfTen) % stepPowerOfTen + + // We multiply by exp%step + if digits+adjExp <= uint64digits { + // We can multiply the mantissa + f.mant *= uint64(float64pow10[adjExp]) + f.Normalize() + } else { + f.Normalize() + f.Multiply(smallPowersOfTen[adjExp]) + errors += errorscale / 2 + } + + // We multiply by 10 to the exp - exp%step. + f.Multiply(powersOfTen[i]) + if errors > 0 { + errors += 1 + } + errors += errorscale / 2 + + // Normalize + shift := f.Normalize() + errors <<= shift + + // Now f is a good approximation of the decimal. + // Check whether the error is too large: that is, if the mantissa + // is perturbated by the error, the resulting float64 will change. + // The 64 bits mantissa is 1 + 52 bits for float64 + 11 extra bits. + // + // In many cases the approximation will be good enough. + const denormalExp = -1023 - 63 + flt := &float64info + var extrabits uint + if f.exp <= denormalExp { + extrabits = uint(63 - flt.mantbits + 1 + uint(denormalExp-f.exp)) + } else { + extrabits = uint(63 - flt.mantbits) + } + + halfway := uint64(1) << (extrabits - 1) + mant_extra := f.mant & (1<<extrabits - 1) + + // Do a signed comparison here! If the error estimate could make + // the mantissa round differently for the conversion to double, + // then we can't give a definite answer. + if int64(halfway)-int64(errors) < int64(mant_extra) && + int64(mant_extra) < int64(halfway)+int64(errors) { + return false + } + return true +} diff --git a/libgo/go/strconv/ftoa.go b/libgo/go/strconv/ftoa.go index e1ea0a35038..f4434fd5175 100644 --- a/libgo/go/strconv/ftoa.go +++ b/libgo/go/strconv/ftoa.go @@ -40,25 +40,31 @@ var float64info = floatInfo{52, 11, -1023} // For 'e', 'E', and 'f' it is the number of digits after the decimal point. // For 'g' and 'G' it is the total number of digits. // The special precision -1 uses the smallest number of digits -// necessary such that Atof32 will return f exactly. -// -// Ftoa32(f) is not the same as Ftoa64(float32(f)), -// because correct rounding and the number of digits -// needed to identify f depend on the precision of the representation. -func FormatFloat(f float64, fmt byte, prec int, n int) string { - if n == 32 { - return genericFtoa(uint64(math.Float32bits(float32(f))), fmt, prec, &float32info) - } - return genericFtoa(math.Float64bits(f), fmt, prec, &float64info) +// necessary such that ParseFloat will return f exactly. +func FormatFloat(f float64, fmt byte, prec, bitSize int) string { + return string(genericFtoa(make([]byte, 0, max(prec+4, 24)), f, fmt, prec, bitSize)) } // AppendFloat appends the string form of the floating-point number f, // as generated by FormatFloat, to dst and returns the extended buffer. -func AppendFloat(dst []byte, f float64, fmt byte, prec int, n int) []byte { - return append(dst, FormatFloat(f, fmt, prec, n)...) +func AppendFloat(dst []byte, f float64, fmt byte, prec int, bitSize int) []byte { + return genericFtoa(dst, f, fmt, prec, bitSize) } -func genericFtoa(bits uint64, fmt byte, prec int, flt *floatInfo) string { +func genericFtoa(dst []byte, val float64, fmt byte, prec, bitSize int) []byte { + var bits uint64 + var flt *floatInfo + switch bitSize { + case 32: + bits = uint64(math.Float32bits(float32(val))) + flt = &float32info + case 64: + bits = math.Float64bits(val) + flt = &float64info + default: + panic("strconv: illegal AppendFloat/FormatFloat bitSize") + } + neg := bits>>(flt.expbits+flt.mantbits) != 0 exp := int(bits>>flt.mantbits) & (1<<flt.expbits - 1) mant := bits & (uint64(1)<<flt.mantbits - 1) @@ -66,13 +72,16 @@ func genericFtoa(bits uint64, fmt byte, prec int, flt *floatInfo) string { switch exp { case 1<<flt.expbits - 1: // Inf, NaN - if mant != 0 { - return "NaN" - } - if neg { - return "-Inf" + var s string + switch { + case mant != 0: + s = "NaN" + case neg: + s = "-Inf" + default: + s = "+Inf" } - return "+Inf" + return append(dst, s...) case 0: // denormalized @@ -86,7 +95,7 @@ func genericFtoa(bits uint64, fmt byte, prec int, flt *floatInfo) string { // Pick off easy binary format. if fmt == 'b' { - return fmtB(neg, mant, exp, flt) + return fmtB(dst, neg, mant, exp, flt) } // Create exact decimal representation. @@ -127,9 +136,9 @@ func genericFtoa(bits uint64, fmt byte, prec int, flt *floatInfo) string { switch fmt { case 'e', 'E': - return fmtE(neg, d, prec, fmt) + return fmtE(dst, neg, d, prec, fmt) case 'f': - return fmtF(neg, d, prec) + return fmtF(dst, neg, d, prec) case 'g', 'G': // trailing fractional zeros in 'e' form will be trimmed. eprec := prec @@ -147,15 +156,16 @@ func genericFtoa(bits uint64, fmt byte, prec int, flt *floatInfo) string { if prec > d.nd { prec = d.nd } - return fmtE(neg, d, prec-1, fmt+'e'-'g') + return fmtE(dst, neg, d, prec-1, fmt+'e'-'g') } if prec > d.dp { prec = d.nd } - return fmtF(neg, d, max(prec-d.dp, 0)) + return fmtF(dst, neg, d, max(prec-d.dp, 0)) } - return "%" + string(fmt) + // unknown format + return append(dst, '%', fmt) } // Round d (= mant * 2^exp) to the shortest number of digits @@ -250,121 +260,103 @@ func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) { } // %e: -d.ddddde±dd -func fmtE(neg bool, d *decimal, prec int, fmt byte) string { - buf := make([]byte, 3+max(prec, 0)+30) // "-0." + prec digits + exp - w := 0 // write index - +func fmtE(dst []byte, neg bool, d *decimal, prec int, fmt byte) []byte { // sign if neg { - buf[w] = '-' - w++ + dst = append(dst, '-') } // first digit - if d.nd == 0 { - buf[w] = '0' - } else { - buf[w] = d.d[0] + ch := byte('0') + if d.nd != 0 { + ch = d.d[0] } - w++ + dst = append(dst, ch) // .moredigits if prec > 0 { - buf[w] = '.' - w++ - for i := 0; i < prec; i++ { - if 1+i < d.nd { - buf[w] = d.d[1+i] - } else { - buf[w] = '0' + dst = append(dst, '.') + for i := 1; i <= prec; i++ { + ch = '0' + if i < d.nd { + ch = d.d[i] } - w++ + dst = append(dst, ch) } } // e± - buf[w] = fmt - w++ + dst = append(dst, fmt) exp := d.dp - 1 if d.nd == 0 { // special case: 0 has exponent 0 exp = 0 } if exp < 0 { - buf[w] = '-' + ch = '-' exp = -exp } else { - buf[w] = '+' + ch = '+' } - w++ + dst = append(dst, ch) // dddd - // count digits - n := 0 - for e := exp; e > 0; e /= 10 { - n++ - } - // leading zeros - for i := n; i < 2; i++ { - buf[w] = '0' - w++ + var buf [3]byte + i := len(buf) + for exp >= 10 { + i-- + buf[i] = byte(exp%10 + '0') + exp /= 10 } - // digits - w += n - n = 0 - for e := exp; e > 0; e /= 10 { - n++ - buf[w-n] = byte(e%10 + '0') + // exp < 10 + i-- + buf[i] = byte(exp + '0') + + // leading zeroes + if i > len(buf)-2 { + i-- + buf[i] = '0' } - return string(buf[0:w]) + return append(dst, buf[i:]...) } // %f: -ddddddd.ddddd -func fmtF(neg bool, d *decimal, prec int) string { - buf := make([]byte, 1+max(d.dp, 1)+1+max(prec, 0)) - w := 0 - +func fmtF(dst []byte, neg bool, d *decimal, prec int) []byte { // sign if neg { - buf[w] = '-' - w++ + dst = append(dst, '-') } // integer, padded with zeros as needed. if d.dp > 0 { var i int for i = 0; i < d.dp && i < d.nd; i++ { - buf[w] = d.d[i] - w++ + dst = append(dst, d.d[i]) } for ; i < d.dp; i++ { - buf[w] = '0' - w++ + dst = append(dst, '0') } } else { - buf[w] = '0' - w++ + dst = append(dst, '0') } // fraction if prec > 0 { - buf[w] = '.' - w++ + dst = append(dst, '.') for i := 0; i < prec; i++ { - if d.dp+i < 0 || d.dp+i >= d.nd { - buf[w] = '0' - } else { - buf[w] = d.d[d.dp+i] + ch := byte('0') + if j := d.dp + i; 0 <= j && j < d.nd { + ch = d.d[j] } - w++ + dst = append(dst, ch) } } - return string(buf[0:w]) + return dst } // %b: -ddddddddp+ddd -func fmtB(neg bool, mant uint64, exp int, flt *floatInfo) string { +func fmtB(dst []byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte { var buf [50]byte w := len(buf) exp -= int(flt.mantbits) @@ -395,7 +387,7 @@ func fmtB(neg bool, mant uint64, exp int, flt *floatInfo) string { w-- buf[w] = '-' } - return string(buf[w:]) + return append(dst, buf[w:]...) } func max(a, b int) int { diff --git a/libgo/go/strconv/ftoa_test.go b/libgo/go/strconv/ftoa_test.go index 02206d5ad25..c69f8c2466d 100644 --- a/libgo/go/strconv/ftoa_test.go +++ b/libgo/go/strconv/ftoa_test.go @@ -149,26 +149,75 @@ func TestFtoa(t *testing.T) { } } -func BenchmarkFtoa64Decimal(b *testing.B) { +/* This test relies on escape analysis which gccgo does not yet do. + +func TestAppendFloatDoesntAllocate(t *testing.T) { + n := numAllocations(func() { + var buf [64]byte + AppendFloat(buf[:0], 1.23, 'g', 5, 64) + }) + want := 1 // TODO(bradfitz): this might be 0, once escape analysis is better + if n != want { + t.Errorf("with local buffer, did %d allocations, want %d", n, want) + } + n = numAllocations(func() { + AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64) + }) + if n != 0 { + t.Errorf("with reused buffer, did %d allocations, want 0", n) + } +} + +*/ + +func BenchmarkFormatFloatDecimal(b *testing.B) { for i := 0; i < b.N; i++ { FormatFloat(33909, 'g', -1, 64) } } -func BenchmarkFtoa64Float(b *testing.B) { +func BenchmarkFormatFloat(b *testing.B) { for i := 0; i < b.N; i++ { FormatFloat(339.7784, 'g', -1, 64) } } -func BenchmarkFtoa64FloatExp(b *testing.B) { +func BenchmarkFormatFloatExp(b *testing.B) { for i := 0; i < b.N; i++ { FormatFloat(-5.09e75, 'g', -1, 64) } } -func BenchmarkFtoa64Big(b *testing.B) { +func BenchmarkFormatFloatBig(b *testing.B) { for i := 0; i < b.N; i++ { FormatFloat(123456789123456789123456789, 'g', -1, 64) } } + +func BenchmarkAppendFloatDecimal(b *testing.B) { + dst := make([]byte, 0, 30) + for i := 0; i < b.N; i++ { + AppendFloat(dst, 33909, 'g', -1, 64) + } +} + +func BenchmarkAppendFloat(b *testing.B) { + dst := make([]byte, 0, 30) + for i := 0; i < b.N; i++ { + AppendFloat(dst, 339.7784, 'g', -1, 64) + } +} + +func BenchmarkAppendFloatExp(b *testing.B) { + dst := make([]byte, 0, 30) + for i := 0; i < b.N; i++ { + AppendFloat(dst, -5.09e75, 'g', -1, 64) + } +} + +func BenchmarkAppendFloatBig(b *testing.B) { + dst := make([]byte, 0, 30) + for i := 0; i < b.N; i++ { + AppendFloat(dst, 123456789123456789123456789, 'g', -1, 64) + } +} diff --git a/libgo/go/strconv/itoa.go b/libgo/go/strconv/itoa.go index 65229f704b8..ca40dd7ef62 100644 --- a/libgo/go/strconv/itoa.go +++ b/libgo/go/strconv/itoa.go @@ -35,7 +35,11 @@ func AppendUint(dst []byte, i uint64, base int) []byte { return dst } -const digits = "0123456789abcdefghijklmnopqrstuvwxyz" +const ( + digits = "0123456789abcdefghijklmnopqrstuvwxyz" + digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" + digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999" +) var shifts = [len(digits) + 1]uint{ 1 << 1: 1, @@ -46,32 +50,42 @@ var shifts = [len(digits) + 1]uint{ } // formatBits computes the string representation of u in the given base. -// If negative is set, u is treated as negative int64 value. If append_ -// is set, the string is appended to dst and the resulting byte slice is +// If neg is set, u is treated as negative int64 value. If append_ is +// set, the string is appended to dst and the resulting byte slice is // returned as the first result value; otherwise the string is returned // as the second result value. // -func formatBits(dst []byte, u uint64, base int, negative, append_ bool) (d []byte, s string) { +func formatBits(dst []byte, u uint64, base int, neg, append_ bool) (d []byte, s string) { if base < 2 || base > len(digits) { - panic("invalid base") + panic("strconv: illegal AppendInt/FormatInt base") } // 2 <= base && base <= len(digits) var a [64 + 1]byte // +1 for sign of 64bit value in base 2 i := len(a) - if negative { + if neg { u = -u } // convert bits if base == 10 { - // common case: use constant 10 for / and % because - // the compiler can optimize it into a multiply+shift - for u >= 10 { + // common case: use constants for / and % because + // the compiler can optimize it into a multiply+shift, + // and unroll loop + for u >= 100 { + i -= 2 + q := u / 100 + j := uintptr(u - q*100) + a[i+1] = digits01[j] + a[i+0] = digits10[j] + u = q + } + if u >= 10 { i-- - a[i] = digits[u%10] - u /= 10 + q := u / 10 + a[i] = digits[uintptr(u-q*10)] + u = q } } else if s := shifts[base]; s > 0 { @@ -89,7 +103,7 @@ func formatBits(dst []byte, u uint64, base int, negative, append_ bool) (d []byt b := uint64(base) for u >= b { i-- - a[i] = digits[u%b] + a[i] = digits[uintptr(u%b)] u /= b } } @@ -99,7 +113,7 @@ func formatBits(dst []byte, u uint64, base int, negative, append_ bool) (d []byt a[i] = digits[uintptr(u)] // add sign, if any - if negative { + if neg { i-- a[i] = '-' } diff --git a/libgo/go/strconv/itoa_test.go b/libgo/go/strconv/itoa_test.go index e0213ae9afe..186f16c27db 100644 --- a/libgo/go/strconv/itoa_test.go +++ b/libgo/go/strconv/itoa_test.go @@ -5,6 +5,7 @@ package strconv_test import ( + "runtime" . "strconv" "testing" ) @@ -125,6 +126,37 @@ func TestUitoa(t *testing.T) { } } +func numAllocations(f func()) int { + runtime.UpdateMemStats() + n0 := runtime.MemStats.Mallocs + f() + runtime.UpdateMemStats() + return int(runtime.MemStats.Mallocs - n0) +} + +/* This test relies on escape analysis which gccgo does not yet do. + +var globalBuf [64]byte + +func TestAppendUintDoesntAllocate(t *testing.T) { + n := numAllocations(func() { + var buf [64]byte + AppendInt(buf[:0], 123, 10) + }) + want := 1 // TODO(bradfitz): this might be 0, once escape analysis is better + if n != want { + t.Errorf("with local buffer, did %d allocations, want %d", n, want) + } + n = numAllocations(func() { + AppendInt(globalBuf[:0], 123, 10) + }) + if n != 0 { + t.Errorf("with reused buffer, did %d allocations, want 0", n) + } +} + +*/ + func BenchmarkFormatInt(b *testing.B) { for i := 0; i < b.N; i++ { for _, test := range itob64tests { diff --git a/libgo/go/strconv/quote.go b/libgo/go/strconv/quote.go index 30b384df8e3..edba62954be 100644 --- a/libgo/go/strconv/quote.go +++ b/libgo/go/strconv/quote.go @@ -116,30 +116,30 @@ func AppendQuoteToASCII(dst []byte, s string) []byte { // rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) // for control characters and non-printable characters as defined by // unicode.IsPrint. -func QuoteRune(rune int) string { +func QuoteRune(r rune) string { // TODO: avoid the allocation here. - return quoteWith(string(rune), '\'', false) + return quoteWith(string(r), '\'', false) } // AppendQuoteRune appends a single-quoted Go character literal representing the rune, // as generated by QuoteRune, to dst and returns the extended buffer. -func AppendQuoteRune(dst []byte, rune int) []byte { - return append(dst, QuoteRune(rune)...) +func AppendQuoteRune(dst []byte, r rune) []byte { + return append(dst, QuoteRune(r)...) } // QuoteRuneToASCII returns a single-quoted Go character literal representing // the rune. The returned string uses Go escape sequences (\t, \n, \xFF, // \u0100) for non-ASCII characters and non-printable characters as defined // by unicode.IsPrint. -func QuoteRuneToASCII(rune int) string { +func QuoteRuneToASCII(r rune) string { // TODO: avoid the allocation here. - return quoteWith(string(rune), '\'', true) + return quoteWith(string(r), '\'', true) } // AppendQuoteRune appends a single-quoted Go character literal representing the rune, // as generated by QuoteRuneToASCII, to dst and returns the extended buffer. -func AppendQuoteRuneToASCII(dst []byte, rune int) []byte { - return append(dst, QuoteRuneToASCII(rune)...) +func AppendQuoteRuneToASCII(dst []byte, r rune) []byte { + return append(dst, QuoteRuneToASCII(r)...) } // CanBackquote returns whether the string s would be diff --git a/libgo/go/strconv/quote_test.go b/libgo/go/strconv/quote_test.go index e440797162b..419943d83c7 100644 --- a/libgo/go/strconv/quote_test.go +++ b/libgo/go/strconv/quote_test.go @@ -47,7 +47,7 @@ func TestQuoteToASCII(t *testing.T) { } type quoteRuneTest struct { - in int + in rune out string ascii string } diff --git a/libgo/go/strings/strings.go b/libgo/go/strings/strings.go index 53fdeadf97b..b411ba5d8b3 100644 --- a/libgo/go/strings/strings.go +++ b/libgo/go/strings/strings.go @@ -434,7 +434,7 @@ func Title(s string) string { // Use a closure here to remember state. // Hackish but effective. Depends on Map scanning in order and calling // the closure once per rune. - prev := rune(' ') + prev := ' ' return Map( func(r rune) rune { if isSeparator(prev) { diff --git a/libgo/go/strings/strings_test.go b/libgo/go/strings/strings_test.go index 957af67b2ba..54046d68aa0 100644 --- a/libgo/go/strings/strings_test.go +++ b/libgo/go/strings/strings_test.go @@ -8,7 +8,6 @@ import ( "bytes" "io" "reflect" - "strconv" . "strings" "testing" "unicode" @@ -143,7 +142,7 @@ const benchmarkString = "some_text=some☺value" func BenchmarkIndexRune(b *testing.B) { if got := IndexRune(benchmarkString, '☺'); got != 14 { - panic("wrong index: got=" + strconv.Itoa(got)) + b.Fatalf("wrong index: expected 14, got=%d", got) } for i := 0; i < b.N; i++ { IndexRune(benchmarkString, '☺') @@ -152,7 +151,7 @@ func BenchmarkIndexRune(b *testing.B) { func BenchmarkIndexRuneFastPath(b *testing.B) { if got := IndexRune(benchmarkString, 'v'); got != 17 { - panic("wrong index: got=" + strconv.Itoa(got)) + b.Fatalf("wrong index: expected 17, got=%d", got) } for i := 0; i < b.N; i++ { IndexRune(benchmarkString, 'v') @@ -161,7 +160,7 @@ func BenchmarkIndexRuneFastPath(b *testing.B) { func BenchmarkIndex(b *testing.B) { if got := Index(benchmarkString, "v"); got != 17 { - panic("wrong index: got=" + strconv.Itoa(got)) + b.Fatalf("wrong index: expected 17, got=%d", got) } for i := 0; i < b.N; i++ { Index(benchmarkString, "v") @@ -642,7 +641,7 @@ func equal(m string, s1, s2 string, t *testing.T) bool { func TestCaseConsistency(t *testing.T) { // Make a string of all the runes. - numRunes := unicode.MaxRune + 1 + numRunes := int(unicode.MaxRune + 1) if testing.Short() { numRunes = 1000 } diff --git a/libgo/go/syscall/bpf_bsd.go b/libgo/go/syscall/bpf_bsd.go index f94b7233b67..f98036c42cd 100644 --- a/libgo/go/syscall/bpf_bsd.go +++ b/libgo/go/syscall/bpf_bsd.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd openbsd +// +build darwin freebsd netbsd openbsd // Berkeley packet filter for BSD variants diff --git a/libgo/go/syscall/env_unix.go b/libgo/go/syscall/env_unix.go index df259097c6a..3ba0fb1b098 100644 --- a/libgo/go/syscall/env_unix.go +++ b/libgo/go/syscall/env_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd // Unix environment variables. diff --git a/libgo/go/syscall/exec_unix.go b/libgo/go/syscall/exec_unix.go index c9814b7050e..0cd37c4a0b5 100644 --- a/libgo/go/syscall/exec_unix.go +++ b/libgo/go/syscall/exec_unix.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build darwin freebsd linux netbsd openbsd + // Fork, exec, wait, etc. package syscall diff --git a/libgo/go/syscall/libcall_linux.go b/libgo/go/syscall/libcall_linux.go index 79f5d48ae8b..3ff5d5f913f 100644 --- a/libgo/go/syscall/libcall_linux.go +++ b/libgo/go/syscall/libcall_linux.go @@ -207,12 +207,6 @@ func PtraceDetach(pid int) (err error) { return ptrace(PTRACE_DETACH, pid, 0, 0) // //sysnb Gettid() (tid int) // //gettid() Pid_t -//sys Ioperm(from int, num int, on int) (err error) -//ioperm(from _C_long, num _C_long, on int) int - -//sys Iopl(level int) (err error) -//iopl(level int) int - // FIXME: mksysinfo linux_dirent // Or just abandon this function. // //sys Getdents(fd int, buf []byte) (n int, err error) @@ -278,19 +272,19 @@ func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err e func Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error) { var lroff _loff_t var plroff *_loff_t - if (roff != nil) { + if roff != nil { plroff = &lroff } var lwoff _loff_t var plwoff *_loff_t - if (woff != nil) { + if woff != nil { plwoff = &lwoff } n, err = splice(rfd, plroff, wfd, plwoff, len, flags) - if (roff != nil) { + if roff != nil { *roff = int64(lroff) } - if (woff != nil) { + if woff != nil { *woff = int64(lwoff) } return diff --git a/libgo/go/syscall/libcall_linux_386.go b/libgo/go/syscall/libcall_linux_386.go new file mode 100644 index 00000000000..ae7fcd9fbac --- /dev/null +++ b/libgo/go/syscall/libcall_linux_386.go @@ -0,0 +1,13 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// GNU/Linux library calls 386 specific. + +package syscall + +//sys Ioperm(from int, num int, on int) (err error) +//ioperm(from _C_long, num _C_long, on int) int + +//sys Iopl(level int) (err error) +//iopl(level int) int diff --git a/libgo/go/syscall/libcall_linux_alpha.go b/libgo/go/syscall/libcall_linux_alpha.go new file mode 100644 index 00000000000..85e1c594c1f --- /dev/null +++ b/libgo/go/syscall/libcall_linux_alpha.go @@ -0,0 +1,13 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// GNU/Linux library calls Alpha specific. + +package syscall + +//sys Ioperm(from int, num int, on int) (err error) +//ioperm(from _C_long, num _C_long, on int) int + +//sys Iopl(level int) (err error) +//iopl(level int) int diff --git a/libgo/go/syscall/libcall_linux_amd64.go b/libgo/go/syscall/libcall_linux_amd64.go new file mode 100644 index 00000000000..9cab9ba40d5 --- /dev/null +++ b/libgo/go/syscall/libcall_linux_amd64.go @@ -0,0 +1,13 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// GNU/Linux library calls amd64 specific. + +package syscall + +//sys Ioperm(from int, num int, on int) (err error) +//ioperm(from _C_long, num _C_long, on int) int + +//sys Iopl(level int) (err error) +//iopl(level int) int diff --git a/libgo/go/syscall/route_bsd.go b/libgo/go/syscall/route_bsd.go index bc4c15e950b..e17d976b154 100644 --- a/libgo/go/syscall/route_bsd.go +++ b/libgo/go/syscall/route_bsd.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd openbsd +// +build darwin freebsd netbsd openbsd // Routing sockets and messages @@ -85,8 +85,8 @@ func (m *RouteMessage) sockaddr() []Sockaddr { rsa := (*RawSockaddr)(unsafe.Pointer(&buf[0])) switch i { case RTAX_DST, RTAX_GATEWAY: - sa, e := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa))) - if e != nil { + sa, err := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa))) + if err != nil { return nil } if i == RTAX_DST { @@ -128,8 +128,8 @@ func (m *InterfaceMessage) sockaddr() (sas []Sockaddr) { if m.Header.Addrs&RTA_IFP == 0 { return nil } - sa, e := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(&m.Data[0]))) - if e != nil { + sa, err := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(&m.Data[0]))) + if err != nil { return nil } return append(sas, sa) @@ -157,12 +157,21 @@ func (m *InterfaceAddrMessage) sockaddr() (sas []Sockaddr) { rsa := (*RawSockaddr)(unsafe.Pointer(&buf[0])) switch i { case RTAX_IFA: - sa, e := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa))) - if e != nil { + sa, err := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa))) + if err != nil { return nil } sas = append(sas, sa) - case RTAX_NETMASK, RTAX_BRD: + case RTAX_NETMASK: + if rsa.Family == AF_UNSPEC { + rsa.Family = AF_INET // an old fasion, AF_UNSPEC means AF_INET + } + sa, err := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa))) + if err != nil { + return nil + } + sas = append(sas, sa) + case RTAX_BRD: // nothing to do } buf = buf[rsaAlignOf(int(rsa.Len)):] diff --git a/libgo/go/syscall/route_netbsd.go b/libgo/go/syscall/route_netbsd.go new file mode 100644 index 00000000000..d6d9031bcbf --- /dev/null +++ b/libgo/go/syscall/route_netbsd.go @@ -0,0 +1,35 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Routing sockets and messages for NetBSD + +package syscall + +import ( + "unsafe" +) + +func (any *anyMessage) toRoutingMessage(buf []byte) RoutingMessage { + switch any.Type { + case RTM_ADD, RTM_DELETE, RTM_CHANGE, RTM_GET, RTM_LOSING, RTM_REDIRECT, RTM_MISS, RTM_LOCK, RTM_RESOLVE: + p := (*RouteMessage)(unsafe.Pointer(any)) + rtm := &RouteMessage{} + rtm.Header = p.Header + rtm.Data = buf[SizeofRtMsghdr:any.Msglen] + return rtm + case RTM_IFINFO: + p := (*InterfaceMessage)(unsafe.Pointer(any)) + ifm := &InterfaceMessage{} + ifm.Header = p.Header + ifm.Data = buf[SizeofIfMsghdr:any.Msglen] + return ifm + case RTM_NEWADDR, RTM_DELADDR: + p := (*InterfaceAddrMessage)(unsafe.Pointer(any)) + ifam := &InterfaceAddrMessage{} + ifam.Header = p.Header + ifam.Data = buf[SizeofIfaMsghdr:any.Msglen] + return ifam + } + return nil +} diff --git a/libgo/go/syscall/sockcmsg_unix.go b/libgo/go/syscall/sockcmsg_unix.go index 84c1383d7e2..d279decb65a 100644 --- a/libgo/go/syscall/sockcmsg_unix.go +++ b/libgo/go/syscall/sockcmsg_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd // Socket control messages diff --git a/libgo/go/syscall/socket_linux.go b/libgo/go/syscall/socket_linux.go index 212e0b2d418..42ab2185084 100644 --- a/libgo/go/syscall/socket_linux.go +++ b/libgo/go/syscall/socket_linux.go @@ -167,6 +167,9 @@ func anyToSockaddrOS(rsa *RawSockaddrAny) (Sockaddr, error) { //sysnb EpollCreate(size int) (fd int, err error) //epoll_create(size int) int +//sysnb EpollCreate1(flags int) (fd int, err error) +//epoll_create1(flags int) int + //sysnb EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error) //epoll_ctl(epfd int, op int, fd int, event *EpollEvent) int diff --git a/libgo/go/syscall/syscall_unix.go b/libgo/go/syscall/syscall_unix.go index 07d3af3a622..ba109f63ac1 100644 --- a/libgo/go/syscall/syscall_unix.go +++ b/libgo/go/syscall/syscall_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package syscall diff --git a/libgo/go/testing/benchmark.go b/libgo/go/testing/benchmark.go index e81e5c5845c..4ce637082ca 100644 --- a/libgo/go/testing/benchmark.go +++ b/libgo/go/testing/benchmark.go @@ -25,12 +25,12 @@ type InternalBenchmark struct { // B is a type passed to Benchmark functions to manage benchmark // timing and to specify the number of iterations to run. type B struct { + common N int benchmark InternalBenchmark - ns time.Duration bytes int64 - start time.Time timerOn bool + result BenchmarkResult } // StartTimer starts timing a test. This function is called automatically @@ -48,7 +48,7 @@ func (b *B) StartTimer() { // want to measure. func (b *B) StopTimer() { if b.timerOn { - b.ns += time.Now().Sub(b.start) + b.duration += time.Now().Sub(b.start) b.timerOn = false } } @@ -59,7 +59,7 @@ func (b *B) ResetTimer() { if b.timerOn { b.start = time.Now() } - b.ns = 0 + b.duration = 0 } // SetBytes records the number of bytes processed in a single operation. @@ -70,7 +70,7 @@ func (b *B) nsPerOp() int64 { if b.N <= 0 { return 0 } - return b.ns.Nanoseconds() / int64(b.N) + return b.duration.Nanoseconds() / int64(b.N) } // runN runs a single benchmark for the specified number of iterations. @@ -127,17 +127,25 @@ func roundUp(n int) int { return 10 * base } -// run times the benchmark function. It gradually increases the number +// run times the benchmark function in a separate goroutine. +func (b *B) run() BenchmarkResult { + go b.launch() + <-b.signal + return b.result +} + +// launch launches the benchmark function. It gradually increases the number // of benchmark iterations until the benchmark runs for a second in order // to get a reasonable measurement. It prints timing information in this form // testing.BenchmarkHello 100000 19 ns/op -func (b *B) run() BenchmarkResult { +// launch is run by the fun function as a separate goroutine. +func (b *B) launch() { // Run the benchmark for a single iteration in case it's expensive. n := 1 b.runN(n) // Run the benchmark for at least the specified amount of time. d := time.Duration(*benchTime * float64(time.Second)) - for b.ns < d && n < 1e9 { + for !b.failed && b.duration < d && n < 1e9 { last := n // Predict iterations/sec. if b.nsPerOp() == 0 { @@ -153,7 +161,8 @@ func (b *B) run() BenchmarkResult { n = roundUp(n) b.runN(n) } - return BenchmarkResult{b.N, b.ns, b.bytes} + b.result = BenchmarkResult{b.N, b.duration, b.bytes} + b.signal <- b } // The results of a benchmark run. @@ -215,14 +224,32 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ } for _, procs := range cpuList { runtime.GOMAXPROCS(procs) - b := &B{benchmark: Benchmark} + b := &B{ + common: common{ + signal: make(chan interface{}), + }, + benchmark: Benchmark, + } benchName := Benchmark.Name if procs != 1 { benchName = fmt.Sprintf("%s-%d", Benchmark.Name, procs) } fmt.Printf("%s\t", benchName) r := b.run() + if b.failed { + // The output could be very long here, but probably isn't. + // We print it all, regardless, because we don't want to trim the reason + // the benchmark failed. + fmt.Printf("--- FAIL: %s\n%s", benchName, b.output) + continue + } fmt.Printf("%v\n", r) + // Unlike with tests, we ignore the -chatty flag and always print output for + // benchmarks since the output generation time will skew the results. + if len(b.output) > 0 { + b.trimOutput() + fmt.Printf("--- BENCH: %s\n%s", benchName, b.output) + } if p := runtime.GOMAXPROCS(-1); p != procs { fmt.Fprintf(os.Stderr, "testing: %s left GOMAXPROCS set to %d\n", benchName, p) } @@ -230,9 +257,31 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ } } +// trimOutput shortens the output from a benchmark, which can be very long. +func (b *B) trimOutput() { + // The output is likely to appear multiple times because the benchmark + // is run multiple times, but at least it will be seen. This is not a big deal + // because benchmarks rarely print, but just in case, we trim it if it's too long. + const maxNewlines = 10 + for nlCount, j := 0, 0; j < len(b.output); j++ { + if b.output[j] == '\n' { + nlCount++ + if nlCount >= maxNewlines { + b.output = append(b.output[:j], "\n\t... [output truncated]\n"...) + break + } + } + } +} + // Benchmark benchmarks a single function. Useful for creating // custom benchmarks that do not use gotest. func Benchmark(f func(b *B)) BenchmarkResult { - b := &B{benchmark: InternalBenchmark{"", f}} + b := &B{ + common: common{ + signal: make(chan interface{}), + }, + benchmark: InternalBenchmark{"", f}, + } return b.run() } diff --git a/libgo/go/testing/example.go b/libgo/go/testing/example.go index e23f13b6f16..fdeda137e76 100644 --- a/libgo/go/testing/example.go +++ b/libgo/go/testing/example.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "os" + "strings" "time" ) @@ -67,11 +68,9 @@ func RunExamples(examples []InternalExample) (ok bool) { // report any errors tstr := fmt.Sprintf("(%.2f seconds)", dt.Seconds()) - if out != eg.Output { - fmt.Printf( - "--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n", - eg.Name, tstr, out, eg.Output, - ) + if g, e := strings.TrimSpace(out), strings.TrimSpace(eg.Output); g != e { + fmt.Printf("--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n", + eg.Name, tstr, g, e) ok = false } else if *chatty { fmt.Printf("--- PASS: %s %s\n", eg.Name, tstr) diff --git a/libgo/go/testing/testing.go b/libgo/go/testing/testing.go index 0b3a07108cc..16890e0b3fa 100644 --- a/libgo/go/testing/testing.go +++ b/libgo/go/testing/testing.go @@ -23,8 +23,8 @@ // } // The benchmark package will vary b.N until the benchmark function lasts // long enough to be timed reliably. The output -// testing.BenchmarkHello 500000 4076 ns/op -// means that the loop ran 500000 times at a speed of 4076 ns per loop. +// testing.BenchmarkHello 10000000 282 ns/op +// means that the loop ran 10000000 times at a speed of 282 ns per loop. // // If a benchmark needs some expensive setup before running, the timer // may be stopped: @@ -70,16 +70,27 @@ var ( cpuList []int ) +// common holds the elements common between T and B and +// captures common methods such as Errorf. +type common struct { + output []byte // Output generated by test or benchmark. + failed bool // Test or benchmark has failed. + start time.Time // Time test or benchmark started + duration time.Duration + self interface{} // To be sent on signal channel when done. + signal chan interface{} // Output for serial tests. +} + // Short reports whether the -test.short flag is set. func Short() bool { return *short } -// decorate inserts the a final newline if needed and indentation tabs for formatting. +// decorate inserts the final newline if needed and indentation tabs for formatting. // If addFileLine is true, it also prefixes the string with the file and line of the call site. func decorate(s string, addFileLine bool) string { if addFileLine { - _, file, line, ok := runtime.Caller(3) // decorate + log + public function. + _, file, line, ok := runtime.Caller(4) // decorate + log + public function. if ok { // Truncate file name at last file name separator. if index := strings.LastIndex(file, "/"); index >= 0 { @@ -111,70 +122,68 @@ func decorate(s string, addFileLine bool) string { // T is a type passed to Test functions to manage test state and support formatted test logs. // Logs are accumulated during execution and dumped to standard error when done. type T struct { - name string // Name of test. - errors string // Error string from test. - failed bool // Test has failed. - ch chan *T // Output for serial tests. - startParallel chan bool // Parallel tests will wait on this. - start time.Time // Time test started - dt time.Duration // Length of test + common + name string // Name of test. + startParallel chan bool // Parallel tests will wait on this. } -// Fail marks the Test function as having failed but continues execution. -func (t *T) Fail() { t.failed = true } +// Fail marks the function as having failed but continues execution. +func (c *common) Fail() { c.failed = true } -// Failed returns whether the Test function has failed. -func (t *T) Failed() bool { return t.failed } +// Failed returns whether the function has failed. +func (c *common) Failed() bool { return c.failed } -// FailNow marks the Test function as having failed and stops its execution. +// FailNow marks the function as having failed and stops its execution. // Execution will continue at the next Test. -func (t *T) FailNow() { - t.dt = time.Now().Sub(t.start) - t.Fail() - t.ch <- t +func (c *common) FailNow() { + c.duration = time.Now().Sub(c.start) + c.Fail() + c.signal <- c.self runtime.Goexit() } // log generates the output. It's always at the same stack depth. -func (t *T) log(s string) { t.errors += decorate(s, true) } +func (c *common) log(s string) { + c.output = append(c.output, decorate(s, true)...) +} -// Log formats its arguments using default formatting, analogous to Print(), +// Log formats its arguments using default formatting, analogous to Println(), // and records the text in the error log. -func (t *T) Log(args ...interface{}) { t.log(fmt.Sprintln(args...)) } +func (c *common) Log(args ...interface{}) { c.log(fmt.Sprintln(args...)) } // Logf formats its arguments according to the format, analogous to Printf(), // and records the text in the error log. -func (t *T) Logf(format string, args ...interface{}) { t.log(fmt.Sprintf(format, args...)) } +func (c *common) Logf(format string, args ...interface{}) { c.log(fmt.Sprintf(format, args...)) } // Error is equivalent to Log() followed by Fail(). -func (t *T) Error(args ...interface{}) { - t.log(fmt.Sprintln(args...)) - t.Fail() +func (c *common) Error(args ...interface{}) { + c.log(fmt.Sprintln(args...)) + c.Fail() } // Errorf is equivalent to Logf() followed by Fail(). -func (t *T) Errorf(format string, args ...interface{}) { - t.log(fmt.Sprintf(format, args...)) - t.Fail() +func (c *common) Errorf(format string, args ...interface{}) { + c.log(fmt.Sprintf(format, args...)) + c.Fail() } // Fatal is equivalent to Log() followed by FailNow(). -func (t *T) Fatal(args ...interface{}) { - t.log(fmt.Sprintln(args...)) - t.FailNow() +func (c *common) Fatal(args ...interface{}) { + c.log(fmt.Sprintln(args...)) + c.FailNow() } // Fatalf is equivalent to Logf() followed by FailNow(). -func (t *T) Fatalf(format string, args ...interface{}) { - t.log(fmt.Sprintf(format, args...)) - t.FailNow() +func (c *common) Fatalf(format string, args ...interface{}) { + c.log(fmt.Sprintf(format, args...)) + c.FailNow() } // Parallel signals that this test is to be run in parallel with (and only with) // other parallel tests in this CPU group. func (t *T) Parallel() { - t.ch <- nil // Release main testing loop - <-t.startParallel // Wait for serial tests to finish + t.signal <- (*T)(nil) // Release main testing loop + <-t.startParallel // Wait for serial tests to finish } // An internal type but exported because it is cross-package; part of the implementation @@ -187,8 +196,8 @@ type InternalTest struct { func tRunner(t *T, test *InternalTest) { t.start = time.Now() test.F(t) - t.dt = time.Now().Sub(t.start) - t.ch <- t + t.duration = time.Now().Sub(t.start) + t.signal <- t } // An internal function but exported because it is cross-package; part of the implementation @@ -211,13 +220,13 @@ func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, after() } -func report(t *T) { - tstr := fmt.Sprintf("(%.2f seconds)", t.dt.Seconds()) +func (t *T) report() { + tstr := fmt.Sprintf("(%.2f seconds)", t.duration.Seconds()) format := "--- %s: %s %s\n%s" if t.failed { - fmt.Printf(format, "FAIL", t.name, tstr, t.errors) + fmt.Printf(format, "FAIL", t.name, tstr, t.output) } else if *chatty { - fmt.Printf(format, "PASS", t.name, tstr, t.errors) + fmt.Printf(format, "PASS", t.name, tstr, t.output) } } @@ -227,9 +236,14 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT fmt.Fprintln(os.Stderr, "testing: warning: no tests to run") return } - ch := make(chan *T) for _, procs := range cpuList { runtime.GOMAXPROCS(procs) + // We build a new channel tree for each run of the loop. + // collector merges in one channel all the upstream signals from parallel tests. + // If all tests pump to the same channel, a bug can occur where a test + // kicks off a goroutine that Fails, yet the test still delivers a completion signal, + // which skews the counting. + var collector = make(chan interface{}) numParallel := 0 startParallel := make(chan bool) @@ -247,17 +261,27 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT if procs != 1 { testName = fmt.Sprintf("%s-%d", tests[i].Name, procs) } - t := &T{ch: ch, name: testName, startParallel: startParallel} + t := &T{ + common: common{ + signal: make(chan interface{}), + }, + name: testName, + startParallel: startParallel, + } + t.self = t if *chatty { fmt.Printf("=== RUN %s\n", t.name) } go tRunner(t, &tests[i]) - out := <-t.ch + out := (<-t.signal).(*T) if out == nil { // Parallel run. + go func() { + collector <- <-t.signal + }() numParallel++ continue } - report(t) + t.report() ok = ok && !out.failed } @@ -269,8 +293,8 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT numParallel-- continue } - t := <-ch - report(t) + t := (<-collector).(*T) + t.report() ok = ok && !t.failed running-- } @@ -322,7 +346,7 @@ var timer *time.Timer // startAlarm starts an alarm if requested. func startAlarm() { if *timeout > 0 { - timer = time.AfterFunc(*timeout*1e9, alarm) + timer = time.AfterFunc(time.Duration(*timeout)*time.Second, alarm) } } diff --git a/libgo/go/testing/wrapper.go b/libgo/go/testing/wrapper.go new file mode 100644 index 00000000000..2bef9df9c6f --- /dev/null +++ b/libgo/go/testing/wrapper.go @@ -0,0 +1,105 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file contains wrappers so t.Errorf etc. have documentation. +// TODO: delete when godoc shows exported methods for unexported embedded fields. +// TODO: need to change the argument to runtime.Caller in testing.go from 4 to 3 at that point. + +package testing + +// Fail marks the function as having failed but continues execution. +func (b *B) Fail() { + b.common.Fail() +} + +// Failed returns whether the function has failed. +func (b *B) Failed() bool { + return b.common.Failed() +} + +// FailNow marks the function as having failed and stops its execution. +// Execution will continue at the next Test. +func (b *B) FailNow() { + b.common.FailNow() +} + +// Log formats its arguments using default formatting, analogous to Println(), +// and records the text in the error log. +func (b *B) Log(args ...interface{}) { + b.common.Log(args...) +} + +// Logf formats its arguments according to the format, analogous to Printf(), +// and records the text in the error log. +func (b *B) Logf(format string, args ...interface{}) { + b.common.Logf(format, args...) +} + +// Error is equivalent to Log() followed by Fail(). +func (b *B) Error(args ...interface{}) { + b.common.Error(args...) +} + +// Errorf is equivalent to Logf() followed by Fail(). +func (b *B) Errorf(format string, args ...interface{}) { + b.common.Errorf(format, args...) +} + +// Fatal is equivalent to Log() followed by FailNow(). +func (b *B) Fatal(args ...interface{}) { + b.common.Fatal(args...) +} + +// Fatalf is equivalent to Logf() followed by FailNow(). +func (b *B) Fatalf(format string, args ...interface{}) { + b.common.Fatalf(format, args...) +} + +// Fail marks the function as having failed but continues execution. +func (t *T) Fail() { + t.common.Fail() +} + +// Failed returns whether the function has failed. +func (t *T) Failed() bool { + return t.common.Failed() +} + +// FailNow marks the function as having failed and stops its execution. +// Execution will continue at the next Test. +func (t *T) FailNow() { + t.common.FailNow() +} + +// Log formats its arguments using default formatting, analogous to Println(), +// and records the text in the error log. +func (t *T) Log(args ...interface{}) { + t.common.Log(args...) +} + +// Logf formats its arguments according to the format, analogous to Printf(), +// and records the text in the error log. +func (t *T) Logf(format string, args ...interface{}) { + t.common.Logf(format, args...) +} + +// Error is equivalent to Log() followed by Fail(). +func (t *T) Error(args ...interface{}) { + t.common.Error(args...) +} + +// Errorf is equivalent to Logf() followed by Fail(). +func (t *T) Errorf(format string, args ...interface{}) { + t.common.Errorf(format, args...) +} + +// Fatal is equivalent to Log() followed by FailNow(). +func (t *T) Fatal(args ...interface{}) { + t.common.Fatal(args...) +} + +// Fatalf is equivalent to Logf() followed by FailNow(). +func (t *T) Fatalf(format string, args ...interface{}) { + t.common.Fatalf(format, args...) +} diff --git a/libgo/go/text/template/exec.go b/libgo/go/text/template/exec.go index b74bc3b01c9..acb88afee36 100644 --- a/libgo/go/text/template/exec.go +++ b/libgo/go/text/template/exec.go @@ -107,7 +107,7 @@ func (t *Template) Execute(wr io.Writer, data interface{}) (err error) { vars: []variable{{"$", value}}, } if t.Tree == nil || t.Root == nil { - state.errorf("must be parsed before execution") + state.errorf("%q is an incomplete or empty template", t.name) } state.walk(value, t.Root) return @@ -497,7 +497,13 @@ func (s *state) evalCall(dot, fun reflect.Value, name string, args []parse.Node, // validateType guarantees that the value is valid and assignable to the type. func (s *state) validateType(value reflect.Value, typ reflect.Type) reflect.Value { if !value.IsValid() { - s.errorf("invalid value; expected %s", typ) + switch typ.Kind() { + case reflect.Interface, reflect.Ptr, reflect.Chan, reflect.Map, reflect.Slice, reflect.Func: + // An untyped nil interface{}. Accept as a proper nil value. + value = reflect.Zero(typ) + default: + s.errorf("invalid value; expected %s", typ) + } } if !value.Type().AssignableTo(typ) { // Does one dereference or indirection work? We could do more, as we diff --git a/libgo/go/text/template/exec_test.go b/libgo/go/text/template/exec_test.go index cf3c4157281..e33988b86c0 100644 --- a/libgo/go/text/template/exec_test.go +++ b/libgo/go/text/template/exec_test.go @@ -157,6 +157,10 @@ func (t *T) Method2(a uint16, b string) string { return fmt.Sprintf("Method2: %d %s", a, b) } +func (t *T) Method3(v interface{}) string { + return fmt.Sprintf("Method3: %v", v) +} + func (t *T) MAdd(a int, b []int) []int { v := make([]int, len(b)) for i, x := range b { @@ -293,6 +297,7 @@ var execTests = []execTest{ {".Method2(3, .X)", "-{{.Method2 3 .X}}-", "-Method2: 3 x-", tVal, true}, {".Method2(.U16, `str`)", "-{{.Method2 .U16 `str`}}-", "-Method2: 16 str-", tVal, true}, {".Method2(.U16, $x)", "{{if $x := .X}}-{{.Method2 .U16 $x}}{{end}}-", "-Method2: 16 x-", tVal, true}, + {".Method3(nil)", "-{{.Method3 .MXI.unset}}-", "-Method3: <nil>-", tVal, true}, {"method on var", "{{if $x := .}}-{{$x.Method2 .U16 $x.X}}{{end}}-", "-Method2: 16 x-", tVal, true}, {"method on chained var", "{{range .MSIone}}{{if $.U.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}", @@ -322,6 +327,8 @@ var execTests = []execTest{ {"if slice", "{{if .SI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true}, {"if emptymap", "{{if .MSIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, {"if map", "{{if .MSI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true}, + {"if map unset", "{{if .MXI.none}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true}, + {"if map not unset", "{{if not .MXI.none}}ZERO{{else}}NON-ZERO{{end}}", "ZERO", tVal, true}, {"if $x with $y int", "{{if $x := true}}{{with $y := .I}}{{$x}},{{$y}}{{end}}{{end}}", "true,17", tVal, true}, {"if $x with $x int", "{{if $x := true}}{{with $x := .I}}{{$x}},{{end}}{{$x}}{{end}}", "17,true", tVal, true}, diff --git a/libgo/go/time/internal_test.go b/libgo/go/time/internal_test.go index 2c4df335f9b..b753896d775 100644 --- a/libgo/go/time/internal_test.go +++ b/libgo/go/time/internal_test.go @@ -10,3 +10,4 @@ func init() { } var Interrupt = interrupt +var DaysIn = daysIn diff --git a/libgo/go/time/sleep.go b/libgo/go/time/sleep.go index 1e23118f378..b4680db2387 100644 --- a/libgo/go/time/sleep.go +++ b/libgo/go/time/sleep.go @@ -4,6 +4,9 @@ package time +// Sleep pauses the current goroutine for the duration d. +func Sleep(d Duration) + func nano() int64 { sec, nsec := now() return sec*1e9 + int64(nsec) @@ -72,13 +75,13 @@ func After(d Duration) <-chan Time { return NewTimer(d).C } -// AfterFunc waits at least ns nanoseconds before calling f +// AfterFunc waits for the duration to elapse and then calls f // in its own goroutine. It returns a Timer that can // be used to cancel the call using its Stop method. -func AfterFunc(ns int64, f func()) *Timer { +func AfterFunc(d Duration, f func()) *Timer { t := &Timer{ r: runtimeTimer{ - when: nano() + ns, + when: nano() + int64(d), f: goFunc, arg: f, }, diff --git a/libgo/go/time/sleep_test.go b/libgo/go/time/sleep_test.go index cbcc897fd40..3cb088c319f 100644 --- a/libgo/go/time/sleep_test.go +++ b/libgo/go/time/sleep_test.go @@ -39,7 +39,7 @@ func TestAfterFunc(t *testing.T) { i-- if i >= 0 { AfterFunc(0, f) - Sleep(1e9) + Sleep(1 * Second) } else { c <- true } @@ -91,7 +91,7 @@ func BenchmarkAfter(b *testing.B) { func BenchmarkStop(b *testing.B) { for i := 0; i < b.N; i++ { - NewTimer(1e9).Stop() + NewTimer(1 * Second).Stop() } } @@ -119,19 +119,17 @@ func TestAfterTick(t *testing.T) { t1 := Now() d := t1.Sub(t0) target := Delta * Count - slop := target * 2 / 10 - if d < target-slop || d > target+slop { + if d < target*9/10 || d > target*30/10 { t.Fatalf("%d ticks of %s took %s, expected %s", Count, Delta, d, target) } } func TestAfterStop(t *testing.T) { - const msec = 1e6 - AfterFunc(100*msec, func() {}) - t0 := NewTimer(50 * msec) + AfterFunc(100*Millisecond, func() {}) + t0 := NewTimer(50 * Millisecond) c1 := make(chan bool, 1) - t1 := AfterFunc(150*msec, func() { c1 <- true }) - c2 := After(200 * msec) + t1 := AfterFunc(150*Millisecond, func() { c1 <- true }) + c2 := After(200 * Millisecond) if !t0.Stop() { t.Fatalf("failed to stop event 0") } @@ -199,9 +197,8 @@ func testAfterQueuing(t *testing.T) error { } dt := r.t.Sub(t0) target := Duration(slot) * Delta - slop := Delta / 4 - if dt < target-slop || dt > target+slop { - return fmt.Errorf("After(%s) arrived at %s, expected [%s,%s]", target, dt, target-slop, target+slop) + if dt < target-Delta/2 || dt > target+Delta*10 { + return fmt.Errorf("After(%s) arrived at %s, expected [%s,%s]", target, dt, target-Delta/2, target+Delta*10) } } return nil @@ -213,12 +210,12 @@ func TestTimerStopStress(t *testing.T) { } for i := 0; i < 100; i++ { go func(i int) { - timer := AfterFunc(2e9, func() { + timer := AfterFunc(2*Second, func() { t.Fatalf("timer %d was not stopped", i) }) - Sleep(1e9) + Sleep(1 * Second) timer.Stop() }(i) } - Sleep(3e9) + Sleep(3 * Second) } diff --git a/libgo/go/time/sys.go b/libgo/go/time/sys.go deleted file mode 100644 index fe6bc27d301..00000000000 --- a/libgo/go/time/sys.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package time - -import "syscall" - -// Sleep pauses the current goroutine for the duration d. -func Sleep(d Duration) - -// readFile reads and returns the content of the named file. -// It is a trivial implementation of ioutil.ReadFile, reimplemented -// here to avoid depending on io/ioutil or os. -func readFile(name string) ([]byte, error) { - f, err := syscall.Open(name, syscall.O_RDONLY, 0) - if err != nil { - return nil, err - } - defer syscall.Close(f) - var ( - buf [4096]byte - ret []byte - n int - ) - for { - n, err = syscall.Read(f, buf[:]) - if n > 0 { - ret = append(ret, buf[:n]...) - } - if n == 0 || err != nil { - break - } - } - return ret, err -} diff --git a/libgo/go/time/sys_plan9.go b/libgo/go/time/sys_plan9.go index e58fb519ea3..c7cfa792a29 100644 --- a/libgo/go/time/sys_plan9.go +++ b/libgo/go/time/sys_plan9.go @@ -2,9 +2,39 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build plan9 + package time +import "syscall" + // for testing: whatever interrupts a sleep func interrupt() { // cannot predict pid, don't want to kill group } + +// readFile reads and returns the content of the named file. +// It is a trivial implementation of ioutil.ReadFile, reimplemented +// here to avoid depending on io/ioutil or os. +func readFile(name string) ([]byte, error) { + f, err := syscall.Open(name, syscall.O_RDONLY) + if err != nil { + return nil, err + } + defer syscall.Close(f) + var ( + buf [4096]byte + ret []byte + n int + ) + for { + n, err = syscall.Read(f, buf[:]) + if n > 0 { + ret = append(ret, buf[:n]...) + } + if n == 0 || err != nil { + break + } + } + return ret, err +} diff --git a/libgo/go/time/sys_unix.go b/libgo/go/time/sys_unix.go index 715d186be17..56a7414e0ce 100644 --- a/libgo/go/time/sys_unix.go +++ b/libgo/go/time/sys_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd package time @@ -12,3 +12,29 @@ import "syscall" func interrupt() { syscall.Kill(syscall.Getpid(), syscall.SIGCHLD) } + +// readFile reads and returns the content of the named file. +// It is a trivial implementation of ioutil.ReadFile, reimplemented +// here to avoid depending on io/ioutil or os. +func readFile(name string) ([]byte, error) { + f, err := syscall.Open(name, syscall.O_RDONLY, 0) + if err != nil { + return nil, err + } + defer syscall.Close(f) + var ( + buf [4096]byte + ret []byte + n int + ) + for { + n, err = syscall.Read(f, buf[:]) + if n > 0 { + ret = append(ret, buf[:n]...) + } + if n == 0 || err != nil { + break + } + } + return ret, err +} diff --git a/libgo/go/time/time.go b/libgo/go/time/time.go index 04ed86cf25f..33d557f7369 100644 --- a/libgo/go/time/time.go +++ b/libgo/go/time/time.go @@ -7,6 +7,8 @@ // The calendrical calculations always assume a Gregorian calendar. package time +import "errors" + // A Time represents an instant in time with nanosecond precision. // // Programs using times should typically store and pass them as values, @@ -548,7 +550,7 @@ func (d Duration) Hours() float64 { func (t Time) Add(d Duration) Time { t.sec += int64(d / 1e9) t.nsec += int32(d % 1e9) - if t.nsec > 1e9 { + if t.nsec >= 1e9 { t.sec++ t.nsec -= 1e9 } else if t.nsec < 0 { @@ -564,6 +566,20 @@ func (t Time) Sub(u Time) Duration { return Duration(t.sec-u.sec)*Second + Duration(t.nsec-u.nsec) } +// AddDate returns the time corresponding to adding the +// given number of years, months, and days to t. +// For example, AddDate(-1, 2, 3) applied to January 1, 2011 +// returns March 4, 2010. +// +// AddDate normalizes its result in the same way that Date does, +// so, for example, adding one month to October 31 yields +// December 1, the normalized form for November 31. +func (t Time) AddDate(years int, months int, days int) Time { + year, month, day := t.Date() + hour, min, sec := t.Clock() + return Date(year+years, month+Month(months), day+days, hour, min, sec, int(t.nsec), t.loc) +} + const ( secondsPerMinute = 60 secondsPerHour = 60 * 60 @@ -673,7 +689,7 @@ func daysIn(m Month, year int) int { if m == February && isLeap(year) { return 29 } - return int(daysBefore[m+1] - daysBefore[m]) + return int(daysBefore[m] - daysBefore[m-1]) } // Provided by package runtime. @@ -736,6 +752,132 @@ func (t Time) UnixNano() int64 { return (t.sec+internalToUnix)*1e9 + int64(t.nsec) } +type gobError string + +func (g gobError) Error() string { return string(g) } + +const timeGobVersion byte = 1 + +// GobEncode implements the gob.GobEncoder interface. +func (t Time) GobEncode() ([]byte, error) { + var offsetMin int16 // minutes east of UTC. -1 is UTC. + + if t.Location() == &utcLoc { + offsetMin = -1 + } else { + _, offset := t.Zone() + if offset%60 != 0 { + return nil, errors.New("Time.GobEncode: zone offset has fractional minute") + } + offset /= 60 + if offset < -32768 || offset == -1 || offset > 32767 { + return nil, errors.New("Time.GobEncode: unexpected zone offset") + } + offsetMin = int16(offset) + } + + enc := []byte{ + timeGobVersion, // byte 0 : version + byte(t.sec >> 56), // bytes 1-8: seconds + byte(t.sec >> 48), + byte(t.sec >> 40), + byte(t.sec >> 32), + byte(t.sec >> 24), + byte(t.sec >> 16), + byte(t.sec >> 8), + byte(t.sec), + byte(t.nsec >> 24), // bytes 9-12: nanoseconds + byte(t.nsec >> 16), + byte(t.nsec >> 8), + byte(t.nsec), + byte(offsetMin >> 8), // bytes 13-14: zone offset in minutes + byte(offsetMin), + } + + return enc, nil +} + +// GobDecode implements the gob.GobDecoder interface. +func (t *Time) GobDecode(buf []byte) error { + if len(buf) == 0 { + return errors.New("Time.GobDecode: no data") + } + + if buf[0] != timeGobVersion { + return errors.New("Time.GobDecode: unsupported version") + } + + if len(buf) != /*version*/ 1+ /*sec*/ 8+ /*nsec*/ 4+ /*zone offset*/ 2 { + return errors.New("Time.GobDecode: invalid length") + } + + buf = buf[1:] + t.sec = int64(buf[7]) | int64(buf[6])<<8 | int64(buf[5])<<16 | int64(buf[4])<<24 | + int64(buf[3])<<32 | int64(buf[2])<<40 | int64(buf[1])<<48 | int64(buf[0])<<56 + + buf = buf[8:] + t.nsec = int32(buf[3]) | int32(buf[2])<<8 | int32(buf[1])<<16 | int32(buf[0])<<24 + + buf = buf[4:] + offset := int(int16(buf[1])|int16(buf[0])<<8) * 60 + + if offset == -1*60 { + t.loc = &utcLoc + } else if _, localoff, _, _, _ := Local.lookup(t.sec + internalToUnix); offset == localoff { + t.loc = Local + } else { + t.loc = FixedZone("", offset) + } + + return nil +} + +// MarshalJSON implements the json.Marshaler interface. +// Time is formatted as RFC3339. +func (t Time) MarshalJSON() ([]byte, error) { + yearInt := t.Year() + if yearInt < 0 || yearInt > 9999 { + return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]") + } + + // We need a four-digit year, but Format produces variable-width years. + year := itoa(yearInt) + year = "0000"[:4-len(year)] + year + + var formattedTime string + if t.nsec == 0 { + // RFC3339, no fractional second + formattedTime = t.Format("-01-02T15:04:05Z07:00") + } else { + // RFC3339 with fractional second + formattedTime = t.Format("-01-02T15:04:05.000000000Z07:00") + + // Trim trailing zeroes from fractional second. + const nanoEnd = 24 // Index of last digit of fractional second + var i int + for i = nanoEnd; formattedTime[i] == '0'; i-- { + // Seek backwards until first significant digit is found. + } + + formattedTime = formattedTime[:i+1] + formattedTime[nanoEnd+1:] + } + + buf := make([]byte, 0, 1+len(year)+len(formattedTime)+1) + buf = append(buf, '"') + buf = append(buf, year...) + buf = append(buf, formattedTime...) + buf = append(buf, '"') + return buf, nil +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +// Time is expected in RFC3339 format. +func (t *Time) UnmarshalJSON(data []byte) (err error) { + *t, err = Parse("\""+RFC3339+"\"", string(data)) + // Fractional seconds are handled implicitly by Parse. + return +} + // Unix returns the local Time corresponding to the given Unix time, // sec seconds and nsec nanoseconds since January 1, 1970 UTC. // It is valid to pass nsec outside the range [0, 999999999]. diff --git a/libgo/go/time/time_test.go b/libgo/go/time/time_test.go index 6d1e79b542b..484ae4266a3 100644 --- a/libgo/go/time/time_test.go +++ b/libgo/go/time/time_test.go @@ -5,6 +5,9 @@ package time_test import ( + "bytes" + "encoding/gob" + "encoding/json" "strconv" "strings" "testing" @@ -632,6 +635,187 @@ func TestDate(t *testing.T) { } } +// Several ways of getting from +// Fri Nov 18 7:56:35 PST 2011 +// to +// Thu Mar 19 7:56:35 PST 2016 +var addDateTests = []struct { + years, months, days int +}{ + {4, 4, 1}, + {3, 16, 1}, + {3, 15, 30}, + {5, -6, -18 - 30 - 12}, +} + +func TestAddDate(t *testing.T) { + t0 := Date(2011, 11, 18, 7, 56, 35, 0, UTC) + t1 := Date(2016, 3, 19, 7, 56, 35, 0, UTC) + for _, at := range addDateTests { + time := t0.AddDate(at.years, at.months, at.days) + if !time.Equal(t1) { + t.Errorf("AddDate(%d, %d, %d) = %v, want %v", + at.years, at.months, at.days, + time, t1) + } + } +} + +var daysInTests = []struct { + year, month, di int +}{ + {2011, 1, 31}, // January, first month, 31 days + {2011, 2, 28}, // February, non-leap year, 28 days + {2012, 2, 29}, // February, leap year, 29 days + {2011, 6, 30}, // June, 30 days + {2011, 12, 31}, // December, last month, 31 days +} + +func TestDaysIn(t *testing.T) { + // The daysIn function is not exported. + // Test the daysIn function via the `var DaysIn = daysIn` + // statement in the internal_test.go file. + for _, tt := range daysInTests { + di := DaysIn(Month(tt.month), tt.year) + if di != tt.di { + t.Errorf("got %d; expected %d for %d-%02d", + di, tt.di, tt.year, tt.month) + } + } +} + +func TestAddToExactSecond(t *testing.T) { + // Add an amount to the current time to round it up to the next exact second. + // This test checks that the nsec field still lies within the range [0, 999999999]. + t1 := Now() + t2 := t1.Add(Second - Duration(t1.Nanosecond())) + sec := (t1.Second() + 1) % 60 + if t2.Second() != sec || t2.Nanosecond() != 0 { + t.Errorf("sec = %d, nsec = %d, want sec = %d, nsec = 0", t2.Second(), t2.Nanosecond(), sec) + } +} + +func equalTimeAndZone(a, b Time) bool { + aname, aoffset := a.Zone() + bname, boffset := b.Zone() + return a.Equal(b) && aoffset == boffset && aname == bname +} + +var gobTests = []Time{ + Date(0, 1, 2, 3, 4, 5, 6, UTC), + Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)), + Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF + Time{}, // nil location + Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)), + Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)), +} + +func TestTimeGob(t *testing.T) { + var b bytes.Buffer + enc := gob.NewEncoder(&b) + dec := gob.NewDecoder(&b) + for _, tt := range gobTests { + var gobtt Time + if err := enc.Encode(&tt); err != nil { + t.Errorf("%v gob Encode error = %q, want nil", tt, err) + } else if err := dec.Decode(&gobtt); err != nil { + t.Errorf("%v gob Decode error = %q, want nil", tt, err) + } else if !equalTimeAndZone(gobtt, tt) { + t.Errorf("Decoded time = %v, want %v", gobtt, tt) + } + b.Reset() + } +} + +var invalidEncodingTests = []struct { + bytes []byte + want string +}{ + {[]byte{}, "Time.GobDecode: no data"}, + {[]byte{0, 2, 3}, "Time.GobDecode: unsupported version"}, + {[]byte{1, 2, 3}, "Time.GobDecode: invalid length"}, +} + +func TestInvalidTimeGob(t *testing.T) { + for _, tt := range invalidEncodingTests { + var ignored Time + err := ignored.GobDecode(tt.bytes) + if err == nil || err.Error() != tt.want { + t.Errorf("time.GobDecode(%#v) error = %v, want %v", tt.bytes, err, tt.want) + } + } +} + +var notEncodableTimes = []struct { + time Time + want string +}{ + {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 1)), "Time.GobEncode: zone offset has fractional minute"}, + {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.GobEncode: unexpected zone offset"}, + {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.GobEncode: unexpected zone offset"}, + {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.GobEncode: unexpected zone offset"}, +} + +func TestNotGobEncodableTime(t *testing.T) { + for _, tt := range notEncodableTimes { + _, err := tt.time.GobEncode() + if err == nil || err.Error() != tt.want { + t.Errorf("%v GobEncode error = %v, want %v", tt.time, err, tt.want) + } + } +} + +var jsonTests = []struct { + time Time + json string +}{ + {Date(9999, 4, 12, 23, 20, 50, .52*1e9, UTC), `"9999-04-12T23:20:50.52Z"`}, + {Date(1996, 12, 19, 16, 39, 57, 0, Local), `"1996-12-19T16:39:57-08:00"`}, + {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.000000001+00:01"`}, +} + +func TestTimeJSON(t *testing.T) { + for _, tt := range jsonTests { + var jsonTime Time + + if jsonBytes, err := json.Marshal(tt.time); err != nil { + t.Errorf("%v json.Marshal error = %v, want nil", tt.time, err) + } else if string(jsonBytes) != tt.json { + t.Errorf("%v JSON = %q, want %q", tt.time, string(jsonBytes), tt.json) + } else if err = json.Unmarshal(jsonBytes, &jsonTime); err != nil { + t.Errorf("%v json.Unmarshal error = %v, want nil", tt.time, err) + } else if !equalTimeAndZone(jsonTime, tt.time) { + t.Errorf("Unmarshaled time = %v, want %v", jsonTime, tt.time) + } + } +} + +func TestInvalidTimeJSON(t *testing.T) { + var tt Time + err := json.Unmarshal([]byte(`{"now is the time":"buddy"}`), &tt) + _, isParseErr := err.(*ParseError) + if !isParseErr { + t.Errorf("expected *time.ParseError unmarshaling JSON, got %v", err) + } +} + +var notJSONEncodableTimes = []struct { + time Time + want string +}{ + {Date(10000, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"}, + {Date(-1, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"}, +} + +func TestNotJSONEncodableTime(t *testing.T) { + for _, tt := range notJSONEncodableTimes { + _, err := tt.time.MarshalJSON() + if err == nil || err.Error() != tt.want { + t.Errorf("%v MarshalJSON error = %v, want %v", tt.time, err, tt.want) + } + } +} + func BenchmarkNow(b *testing.B) { for i := 0; i < b.N; i++ { Now() diff --git a/libgo/go/time/zoneinfo_plan9.go b/libgo/go/time/zoneinfo_plan9.go index 38aefc7a977..9c052d42cd3 100644 --- a/libgo/go/time/zoneinfo_plan9.go +++ b/libgo/go/time/zoneinfo_plan9.go @@ -6,61 +6,152 @@ package time -//import ( -// "strconv" -// "strings" -//) +import ( + "errors" + "syscall" +) -func parseZones(s string) (zt []zonetime) { - f := strings.Fields(s) +var badData = errors.New("malformed time zone information") + +func isSpace(r rune) bool { + return r == ' ' || r == '\t' || r == '\n' +} + +// Copied from strings to avoid a dependency. +func fields(s string) []string { + // First count the fields. + n := 0 + inField := false + for _, rune := range s { + wasInField := inField + inField = !isSpace(rune) + if inField && !wasInField { + n++ + } + } + + // Now create them. + a := make([]string, n) + na := 0 + fieldStart := -1 // Set to -1 when looking for start of field. + for i, rune := range s { + if isSpace(rune) { + if fieldStart >= 0 { + a[na] = s[fieldStart:i] + na++ + fieldStart = -1 + } + } else if fieldStart == -1 { + fieldStart = i + } + } + if fieldStart >= 0 { // Last field might end at EOF. + a[na] = s[fieldStart:] + } + return a +} + +func loadZoneData(s string) (l *Location, err error) { + f := fields(s) if len(f) < 4 { - return + if len(f) == 2 && f[0] == "GMT" { + return UTC, nil + } + return nil, badData } + var zones [2]zone + // standard timezone offset - o, err := strconv.Atoi(f[1]) + o, err := atoi(f[1]) if err != nil { - return + return nil, badData } - std := &zone{name: f[0], utcoff: o, isdst: false} + zones[0] = zone{name: f[0], offset: o, isDST: false} // alternate timezone offset - o, err = strconv.Atoi(f[3]) + o, err = atoi(f[3]) if err != nil { - return + return nil, badData } - dst := &zone{name: f[2], utcoff: o, isdst: true} + zones[1] = zone{name: f[2], offset: o, isDST: true} // transition time pairs + var tx []zoneTrans f = f[4:] for i := 0; i < len(f); i++ { - z := std + zi := 0 if i%2 == 0 { - z = dst + zi = 1 } - t, err := strconv.Atoi(f[i]) + t, err := atoi(f[i]) if err != nil { - return nil + return nil, badData } - t -= std.utcoff - zt = append(zt, zonetime{time: int32(t), zone: z}) + t -= zones[0].offset + tx = append(tx, zoneTrans{when: int64(t), index: uint8(zi)}) } - return + + // Committed to succeed. + l = &Location{zone: zones[:], tx: tx} + + // Fill in the cache with information about right now, + // since that will be the most common lookup. + sec, _ := now() + for i := range tx { + if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) { + l.cacheStart = tx[i].when + l.cacheEnd = 1<<63 - 1 + if i+1 < len(tx) { + l.cacheEnd = tx[i+1].when + } + l.cacheZone = &l.zone[tx[i].index] + } + } + + return l, nil } -func initLocal() { - t, err := os.Getenverror("timezone") +func loadZoneFile(name string) (*Location, error) { + b, err := readFile(name) if err != nil { - // do nothing: use UTC - return + return nil, err } - zones = parseZones(t) + return loadZoneData(string(b)) } func initTestingZone() { - buf, err := readFile("/adm/timezone/US_Pacific") - if err != nil { + if z, err := loadZoneFile("/adm/timezone/US_Pacific"); err == nil { + localLoc = *z return } - zones = parseZones(string(buf)) + + // Fall back to UTC. + localLoc.name = "UTC" +} + +func initLocal() { + t, ok := syscall.Getenv("timezone") + if ok { + if z, err := loadZoneData(t); err == nil { + localLoc = *z + return + } + } else { + if z, err := loadZoneFile("/adm/timezone/local"); err == nil { + localLoc = *z + localLoc.name = "Local" + return + } + } + + // Fall back to UTC. + localLoc.name = "UTC" +} + +func loadLocation(name string) (*Location, error) { + if z, err := loadZoneFile("/adm/timezone/" + name); err == nil { + return z, nil + } + return nil, errors.New("unknown time zone " + name) } diff --git a/libgo/go/time/zoneinfo_unix.go b/libgo/go/time/zoneinfo_unix.go index 83d5b983c6e..540b653c57d 100644 --- a/libgo/go/time/zoneinfo_unix.go +++ b/libgo/go/time/zoneinfo_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd +// +build darwin freebsd linux netbsd openbsd // Parse "zoneinfo" time zone file. // This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others. diff --git a/libgo/go/unicode/letter.go b/libgo/go/unicode/letter.go index 01c485b6931..dcc160a5b7d 100644 --- a/libgo/go/unicode/letter.go +++ b/libgo/go/unicode/letter.go @@ -7,10 +7,10 @@ package unicode const ( - MaxRune = 0x10FFFF // Maximum valid Unicode code point. - ReplacementChar = 0xFFFD // Represents invalid code points. - MaxASCII = 0x7F // maximum ASCII value. - MaxLatin1 = 0xFF // maximum Latin-1 value. + MaxRune = '\U0010FFFF' // Maximum valid Unicode code point. + ReplacementChar = '\uFFFD' // Represents invalid code points. + MaxASCII = '\u007F' // maximum ASCII value. + MaxLatin1 = '\u00FF' // maximum Latin-1 value. ) // RangeTable defines a set of Unicode code points by listing the ranges of diff --git a/libgo/go/websocket/server.go b/libgo/go/websocket/server.go index 8320b032ead..63f48e25693 100644 --- a/libgo/go/websocket/server.go +++ b/libgo/go/websocket/server.go @@ -74,7 +74,7 @@ A trivial example server: http.Handle("/echo", websocket.Handler(EchoServer)); err := http.ListenAndServe(":12345", nil); if err != nil { - panic("ListenAndServe: " + err.String()) + panic("ListenAndServe: " + err.Error()) } } */ diff --git a/libgo/merge.sh b/libgo/merge.sh index 496abe53ad5..c8a57ca2816 100755 --- a/libgo/merge.sh +++ b/libgo/merge.sh @@ -168,8 +168,8 @@ for f in $runtime; do merge_c $f $f done -merge_c linux/thread.c thread-linux.c -merge_c linux/mem.c mem.c +merge_c thread_linux.c thread-linux.c +merge_c mem_linux.c mem.c (cd ${OLDDIR}/src/pkg && find . -name '*.go' -print) | while read f; do oldfile=${OLDDIR}/src/pkg/$f diff --git a/libgo/mksysinfo.sh b/libgo/mksysinfo.sh index ca9e5f686b1..2d3aa8f6205 100755 --- a/libgo/mksysinfo.sh +++ b/libgo/mksysinfo.sh @@ -89,6 +89,15 @@ cat > sysinfo.c <<EOF #if defined(HAVE_NET_IF_H) #include <net/if.h> #endif + +/* Constants that may only be defined as expressions on some systems, + expressions too complex for -fdump-go-spec to handle. These are + handled specially below. */ +enum { +#ifdef TIOCGWINSZ + TIOCGWINSZ_val = TIOCGWINSZ, +#endif +}; EOF ${CC} -fdump-go-spec=gen-sysinfo.go -std=gnu99 -S -o sysinfo.s sysinfo.c @@ -193,10 +202,13 @@ grep '^const __PC' gen-sysinfo.go | # epoll constants. grep '^const _EPOLL' gen-sysinfo.go | sed -e 's/^\(const \)_\(EPOLL[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT} -# Make sure EPOLLRDHUP is defined. +# Make sure EPOLLRDHUP and EPOLL_CLOEXEC are defined. if ! grep '^const EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then echo "const EPOLLRDHUP = 0x2000" >> ${OUT} fi +if ! grep '^const EPOLL_CLOEXEC' ${OUT} >/dev/null 2>&1; then + echo "const EPOLL_CLOEXEC = 02000000" >> ${OUT} +fi # Ptrace constants. grep '^const _PTRACE' gen-sysinfo.go | @@ -521,7 +533,14 @@ grep '^type _passwd ' gen-sysinfo.go | \ # The ioctl flags for the controlling TTY. grep '^const _TIOC' gen-sysinfo.go | \ + grep -v '_val =' | \ sed -e 's/^\(const \)_\(TIOC[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT} +# We need TIOCGWINSZ. +if ! grep '^const TIOCGWINSZ' ${OUT} >/dev/null 2>&1; then + if grep '^const _TIOCGWINSZ_val' ${OUT} >/dev/null 2>&1; then + echo 'const TIOCGWINSZ = TIOCGWINSZ_val' >> ${OUT} + fi +fi # The ioctl flags for terminal control grep '^const _TC[GS]ET' gen-sysinfo.go | \ diff --git a/libgo/runtime/goc2c.c b/libgo/runtime/goc2c.c index fe413fe42af..93031ffe253 100644 --- a/libgo/runtime/goc2c.c +++ b/libgo/runtime/goc2c.c @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build ignore + /* * Translate a .goc file into a .c file. A .goc file is a combination * of a limited form of Go with C. @@ -774,6 +776,7 @@ main(int argc, char **argv) } } + printf("// AUTO-GENERATED by autogen.sh; DO NOT EDIT\n\n"); process_file(); exit(0); } diff --git a/libgo/runtime/lock_futex.c b/libgo/runtime/lock_futex.c index 4f3d507726d..cdc12d7c75c 100644 --- a/libgo/runtime/lock_futex.c +++ b/libgo/runtime/lock_futex.c @@ -2,17 +2,19 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build freebsd linux + #include "runtime.h" // This implementation depends on OS-specific implementations of // -// runtime.futexsleep(uint32 *addr, uint32 val, int64 ns) +// runtime_futexsleep(uint32 *addr, uint32 val, int64 ns) // Atomically, // if(*addr == val) sleep // Might be woken up spuriously; that's allowed. // Don't sleep longer than ns; ns < 0 means forever. // -// runtime.futexwakeup(uint32 *addr, uint32 cnt) +// runtime_futexwakeup(uint32 *addr, uint32 cnt) // If any procs are sleeping on addr, wake up at most cnt. enum diff --git a/libgo/runtime/lock_sema.c b/libgo/runtime/lock_sema.c index 6b4fffbdcf8..b2a8f53be41 100644 --- a/libgo/runtime/lock_sema.c +++ b/libgo/runtime/lock_sema.c @@ -2,21 +2,23 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build darwin netbsd openbsd plan9 windows + #include "runtime.h" // This implementation depends on OS-specific implementations of // -// uintptr runtime.semacreate(void) +// uintptr runtime_semacreate(void) // Create a semaphore, which will be assigned to m->waitsema. // The zero value is treated as absence of any semaphore, // so be sure to return a non-zero value. // -// int32 runtime.semasleep(int64 ns) +// int32 runtime_semasleep(int64 ns) // If ns < 0, acquire m->waitsema and return 0. // If ns >= 0, try to acquire m->waitsema for at most ns nanoseconds. // Return 0 if the semaphore was acquired, -1 if interrupted or timed out. // -// int32 runtime.semawakeup(M *mp) +// int32 runtime_semawakeup(M *mp) // Wake up mp, which is or will soon be sleeping on mp->waitsema. // diff --git a/libgo/runtime/mem.c b/libgo/runtime/mem.c index 72ab4d695fa..04fca2c65b1 100644 --- a/libgo/runtime/mem.c +++ b/libgo/runtime/mem.c @@ -69,9 +69,9 @@ runtime_SysAlloc(uintptr n) void runtime_SysUnused(void *v, uintptr n) { - USED(v); - USED(n); - // TODO(rsc): call madvise MADV_DONTNEED +#ifdef MADV_DONTNEED + runtime_madvise(v, n, MADV_DONTNEED); +#endif } void diff --git a/libgo/runtime/mprof.goc b/libgo/runtime/mprof.goc index f44f45083f7..d143d19e5ba 100644 --- a/libgo/runtime/mprof.goc +++ b/libgo/runtime/mprof.goc @@ -12,8 +12,6 @@ package runtime #include "defs.h" #include "go-type.h" -typedef struct __go_open_array Slice; - // NOTE(rsc): Everything here could use cas if contention became an issue. static Lock proflock; diff --git a/libgo/runtime/runtime.h b/libgo/runtime/runtime.h index ed626efe8a2..94113b8db83 100644 --- a/libgo/runtime/runtime.h +++ b/libgo/runtime/runtime.h @@ -196,7 +196,7 @@ enum /* Macros. */ -#ifdef __WINDOWS__ +#ifdef GOOS_windows enum { Windows = 1 }; @@ -343,7 +343,6 @@ void runtime_panic(Eface); #define runtime_printf printf #define runtime_malloc(s) __go_alloc(s) #define runtime_free(p) __go_free(p) -#define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size)) #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2)) #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s)) #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s)) @@ -352,8 +351,6 @@ MCache* runtime_allocmcache(void); void free(void *v); struct __go_func_type; bool runtime_addfinalizer(void*, void(*fn)(void*), const struct __go_func_type *); -#define runtime_mmap mmap -#define runtime_munmap(p, s) munmap((p), (s)) #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new) #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new) #define runtime_xadd(p, v) __sync_add_and_fetch (p, v) @@ -383,6 +380,14 @@ void runtime_osyield(void); void runtime_LockOSThread(void) __asm__("libgo_runtime.runtime.LockOSThread"); void runtime_UnlockOSThread(void) __asm__("libgo_runtime.runtime.UnlockOSThread"); +/* + * low level C-called + */ +#define runtime_mmap mmap +#define runtime_munmap munmap +#define runtime_madvise madvise +#define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size)) + struct __go_func_type; void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool, void **, void **) diff --git a/libgo/runtime/string.goc b/libgo/runtime/string.goc index fbbe393bbef..486caf09a42 100644 --- a/libgo/runtime/string.goc +++ b/libgo/runtime/string.goc @@ -4,6 +4,9 @@ package runtime #include "runtime.h" +#include "arch.h" +#include "malloc.h" + #define charntorune(pv, str, len) __go_get_rune(str, len, pv) int32 |