aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/fmt
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2018-01-09 01:23:08 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2018-01-09 01:23:08 +0000
commit1a2f01efa63036a5104f203a4789e682c0e0915d (patch)
tree373e15778dc8295354584e1f86915ae493b604ff /libgo/go/fmt
parent8799df67f2dab88f9fda11739c501780a85575e2 (diff)
downloadgcc-1a2f01efa63036a5104f203a4789e682c0e0915d.zip
gcc-1a2f01efa63036a5104f203a4789e682c0e0915d.tar.gz
gcc-1a2f01efa63036a5104f203a4789e682c0e0915d.tar.bz2
libgo: update to Go1.10beta1
Update the Go library to the 1.10beta1 release. Requires a few changes to the compiler for modifications to the map runtime code, and to handle some nowritebarrier cases in the runtime. Reviewed-on: https://go-review.googlesource.com/86455 gotools/: * Makefile.am (go_cmd_vet_files): New variable. (go_cmd_buildid_files, go_cmd_test2json_files): New variables. (s-zdefaultcc): Change from constants to functions. (noinst_PROGRAMS): Add vet, buildid, and test2json. (cgo$(EXEEXT)): Link against $(LIBGOTOOL). (vet$(EXEEXT)): New target. (buildid$(EXEEXT)): New target. (test2json$(EXEEXT)): New target. (install-exec-local): Install all $(noinst_PROGRAMS). (uninstall-local): Uninstasll all $(noinst_PROGRAMS). (check-go-tool): Depend on $(noinst_PROGRAMS). Copy down objabi.go. (check-runtime): Depend on $(noinst_PROGRAMS). (check-cgo-test, check-carchive-test): Likewise. (check-vet): New target. (check): Depend on check-vet. Look at cmd_vet-testlog. (.PHONY): Add check-vet. * Makefile.in: Rebuild. From-SVN: r256365
Diffstat (limited to 'libgo/go/fmt')
-rw-r--r--libgo/go/fmt/doc.go5
-rw-r--r--libgo/go/fmt/example_test.go31
-rw-r--r--libgo/go/fmt/fmt_test.go30
-rw-r--r--libgo/go/fmt/print.go16
-rw-r--r--libgo/go/fmt/scan.go34
5 files changed, 90 insertions, 26 deletions
diff --git a/libgo/go/fmt/doc.go b/libgo/go/fmt/doc.go
index 014ba06..375cdb4 100644
--- a/libgo/go/fmt/doc.go
+++ b/libgo/go/fmt/doc.go
@@ -47,6 +47,8 @@
%X base 16, upper-case, two characters per byte
Pointer:
%p base 16 notation, with leading 0x
+ The %b, %d, %o, %x and %X verbs also work with pointers,
+ formatting the value exactly as if it were an integer.
The default format for %v is:
bool: %t
@@ -79,7 +81,8 @@
that is, runes. (This differs from C's printf where the
units are always measured in bytes.) Either or both of the flags
may be replaced with the character '*', causing their values to be
- obtained from the next operand, which must be of type int.
+ obtained from the next operand (preceding the one to format),
+ which must be of type int.
For most values, width is the minimum number of runes to output,
padding the formatted form with spaces if necessary.
diff --git a/libgo/go/fmt/example_test.go b/libgo/go/fmt/example_test.go
new file mode 100644
index 0000000..aa3cd05
--- /dev/null
+++ b/libgo/go/fmt/example_test.go
@@ -0,0 +1,31 @@
+// Copyright 2017 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.
+
+// +build ignore
+
+package fmt_test
+
+import (
+ "fmt"
+)
+
+// Animal has a Name and an Age to represent an animal.
+type Animal struct {
+ Name string
+ Age uint
+}
+
+// String makes Animal satisfy the Stringer interface.
+func (a Animal) String() string {
+ return fmt.Sprintf("%v (%d)", a.Name, a.Age)
+}
+
+func ExampleStringer() {
+ a := Animal{
+ Name: "Gopher",
+ Age: 2,
+ }
+ fmt.Println(a)
+ // Output: Gopher (2)
+}
diff --git a/libgo/go/fmt/fmt_test.go b/libgo/go/fmt/fmt_test.go
index 9e6fcfa..b56b84f 100644
--- a/libgo/go/fmt/fmt_test.go
+++ b/libgo/go/fmt/fmt_test.go
@@ -131,6 +131,19 @@ func (byteFormatter) Format(f State, _ rune) {
var byteFormatterSlice = []byteFormatter{'h', 'e', 'l', 'l', 'o'}
+// Copy of io.stringWriter interface used by writeStringFormatter for type assertion.
+type stringWriter interface {
+ WriteString(s string) (n int, err error)
+}
+
+type writeStringFormatter string
+
+func (sf writeStringFormatter) Format(f State, c rune) {
+ if sw, ok := f.(stringWriter); ok {
+ sw.WriteString("***" + string(sf) + "***")
+ }
+}
+
var fmtTests = []struct {
fmt string
val interface{}
@@ -977,6 +990,11 @@ var fmtTests = []struct {
// This next case seems wrong, but the docs say the Formatter wins here.
{"%#v", byteFormatterSlice, "[]fmt_test.byteFormatter{X, X, X, X, X}"},
+ // pp.WriteString
+ {"%s", writeStringFormatter(""), "******"},
+ {"%s", writeStringFormatter("xyz"), "***xyz***"},
+ {"%s", writeStringFormatter("⌘/⌘"), "***⌘/⌘***"},
+
// reflect.Value handled specially in Go 1.5, making it possible to
// see inside non-exported fields (which cannot be accessed with Interface()).
// Issue 8965.
@@ -1201,6 +1219,14 @@ func BenchmarkSprintfTruncateString(b *testing.B) {
})
}
+func BenchmarkSprintfSlowParsingPath(b *testing.B) {
+ b.RunParallel(func(pb *testing.PB) {
+ for pb.Next() {
+ Sprintf("%.v", nil)
+ }
+ })
+}
+
func BenchmarkSprintfQuoteString(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
@@ -1709,12 +1735,14 @@ func TestIsSpace(t *testing.T) {
}
}
+func hideFromVet(s string) string { return s }
+
func TestNilDoesNotBecomeTyped(t *testing.T) {
type A struct{}
type B struct{}
var a *A = nil
var b B = B{}
- got := Sprintf("%s %s %s %s %s", nil, a, nil, b, nil) // go vet should complain about this line.
+ got := Sprintf(hideFromVet("%s %s %s %s %s"), nil, a, nil, b, nil)
const expect = "%!s(<nil>) %!s(*fmt_test.A=<nil>) %!s(<nil>) {} %!s(<nil>)"
if got != expect {
t.Errorf("expected:\n\t%q\ngot:\n\t%q", expect, got)
diff --git a/libgo/go/fmt/print.go b/libgo/go/fmt/print.go
index 2bd88f9..98c156a 100644
--- a/libgo/go/fmt/print.go
+++ b/libgo/go/fmt/print.go
@@ -172,6 +172,13 @@ func (p *pp) Write(b []byte) (ret int, err error) {
return len(b), nil
}
+// Implement WriteString so that we can call io.WriteString
+// on a pp (through state), for efficiency.
+func (p *pp) WriteString(s string) (ret int, err error) {
+ p.buf.WriteString(s)
+ return len(s), nil
+}
+
// These routines end in 'f' and take a format string.
// Fprintf formats according to a format specifier and writes to w.
@@ -837,7 +844,7 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
p.buf.WriteByte(']')
}
case reflect.Ptr:
- // pointer to array or slice or struct? ok at top level
+ // pointer to array or slice or struct? ok at top level
// but not embedded (avoid loops)
if depth == 0 && f.Pointer() != 0 {
switch a := f.Elem(); a.Kind() {
@@ -1067,8 +1074,11 @@ formatLoop:
break
}
- verb, w := utf8.DecodeRuneInString(format[i:])
- i += w
+ verb, size := rune(format[i]), 1
+ if verb >= utf8.RuneSelf {
+ verb, size = utf8.DecodeRuneInString(format[i:])
+ }
+ i += size
switch {
case verb == '%': // Percent does not absorb operands and ignores f.wid and f.prec.
diff --git a/libgo/go/fmt/scan.go b/libgo/go/fmt/scan.go
index cd7232c..ae79e39 100644
--- a/libgo/go/fmt/scan.go
+++ b/libgo/go/fmt/scan.go
@@ -298,13 +298,6 @@ func notSpace(r rune) bool {
return !isSpace(r)
}
-// SkipSpace provides Scan methods the ability to skip space and newline
-// characters in keeping with the current scanning mode set by format strings
-// and Scan/Scanln.
-func (s *ss) SkipSpace() {
- s.skipSpace(false)
-}
-
// readRune is a structure to enable reading UTF-8 encoded code points
// from an io.Reader. It is used if the Reader given to the scanner does
// not already implement io.RuneScanner.
@@ -421,8 +414,10 @@ func (s *ss) free(old ssave) {
ssFree.Put(s)
}
-// skipSpace skips spaces and maybe newlines.
-func (s *ss) skipSpace(stopAtNewline bool) {
+// SkipSpace provides Scan methods the ability to skip space and newline
+// characters in keeping with the current scanning mode set by format strings
+// and Scan/Scanln.
+func (s *ss) SkipSpace() {
for {
r := s.getRune()
if r == eof {
@@ -432,9 +427,6 @@ func (s *ss) skipSpace(stopAtNewline bool) {
continue
}
if r == '\n' {
- if stopAtNewline {
- break
- }
if s.nlIsSpace {
continue
}
@@ -453,7 +445,7 @@ func (s *ss) skipSpace(stopAtNewline bool) {
// newlines are treated as spaces.
func (s *ss) token(skipSpace bool, f func(rune) bool) []byte {
if skipSpace {
- s.skipSpace(false)
+ s.SkipSpace()
}
// read until white space or newline
for {
@@ -537,7 +529,7 @@ func (s *ss) okVerb(verb rune, okVerbs, typ string) bool {
// scanBool returns the value of the boolean represented by the next token.
func (s *ss) scanBool(verb rune) bool {
- s.skipSpace(false)
+ s.SkipSpace()
s.notEOF()
if !s.okVerb(verb, "tv", "boolean") {
return false
@@ -641,7 +633,7 @@ func (s *ss) scanInt(verb rune, bitSize int) int64 {
if verb == 'c' {
return s.scanRune(bitSize)
}
- s.skipSpace(false)
+ s.SkipSpace()
s.notEOF()
base, digits := s.getBase(verb)
haveDigits := false
@@ -674,7 +666,7 @@ func (s *ss) scanUint(verb rune, bitSize int) uint64 {
if verb == 'c' {
return uint64(s.scanRune(bitSize))
}
- s.skipSpace(false)
+ s.SkipSpace()
s.notEOF()
base, digits := s.getBase(verb)
haveDigits := false
@@ -795,7 +787,7 @@ func (s *ss) scanComplex(verb rune, n int) complex128 {
if !s.okVerb(verb, floatVerbs, "complex") {
return 0
}
- s.skipSpace(false)
+ s.SkipSpace()
s.notEOF()
sreal, simag := s.complexTokens()
real := s.convertFloat(sreal, n/2)
@@ -809,7 +801,7 @@ func (s *ss) convertString(verb rune) (str string) {
if !s.okVerb(verb, "svqxX", "string") {
return ""
}
- s.skipSpace(false)
+ s.SkipSpace()
s.notEOF()
switch verb {
case 'q':
@@ -973,13 +965,13 @@ func (s *ss) scanOne(verb rune, arg interface{}) {
// scan in high precision and convert, in order to preserve the correct error condition.
case *float32:
if s.okVerb(verb, floatVerbs, "float32") {
- s.skipSpace(false)
+ s.SkipSpace()
s.notEOF()
*v = float32(s.convertFloat(s.floatToken(), 32))
}
case *float64:
if s.okVerb(verb, floatVerbs, "float64") {
- s.skipSpace(false)
+ s.SkipSpace()
s.notEOF()
*v = s.convertFloat(s.floatToken(), 64)
}
@@ -1017,7 +1009,7 @@ func (s *ss) scanOne(verb rune, arg interface{}) {
v.Index(i).SetUint(uint64(str[i]))
}
case reflect.Float32, reflect.Float64:
- s.skipSpace(false)
+ s.SkipSpace()
s.notEOF()
v.SetFloat(s.convertFloat(s.floatToken(), v.Type().Bits()))
case reflect.Complex64, reflect.Complex128: