aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/fmt
diff options
context:
space:
mode:
authorIan Lance Taylor <ian@gcc.gnu.org>2011-04-07 17:09:10 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2011-04-07 17:09:10 +0000
commit405ca10418216fc078ecb29ff13a39c911e8d806 (patch)
tree39c2c0da7b6c220dd344f134fae07fba0d0266b2 /libgo/go/fmt
parenta751005d50fa7347131660e562c5fd9ce3dff75d (diff)
downloadgcc-405ca10418216fc078ecb29ff13a39c911e8d806.zip
gcc-405ca10418216fc078ecb29ff13a39c911e8d806.tar.gz
gcc-405ca10418216fc078ecb29ff13a39c911e8d806.tar.bz2
libgo: Update to current Go library.
From-SVN: r172106
Diffstat (limited to 'libgo/go/fmt')
-rw-r--r--libgo/go/fmt/fmt_test.go10
-rw-r--r--libgo/go/fmt/format.go22
-rw-r--r--libgo/go/fmt/print.go12
3 files changed, 34 insertions, 10 deletions
diff --git a/libgo/go/fmt/fmt_test.go b/libgo/go/fmt/fmt_test.go
index 4d308ac..3766c838 100644
--- a/libgo/go/fmt/fmt_test.go
+++ b/libgo/go/fmt/fmt_test.go
@@ -139,7 +139,17 @@ var fmttests = []struct {
{"%5s", "abc", " abc"},
{"%2s", "\u263a", " \u263a"},
{"%-5s", "abc", "abc "},
+ {"%-8q", "abc", `"abc" `},
{"%05s", "abc", "00abc"},
+ {"%08q", "abc", `000"abc"`},
+ {"%5s", "abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"},
+ {"%.5s", "abcdefghijklmnopqrstuvwxyz", "abcde"},
+ {"%.5s", "日本語日本語", "日本語日本"},
+ {"%.5s", []byte("日本語日本語"), "日本語日本"},
+ {"%.5q", "abcdefghijklmnopqrstuvwxyz", `"abcde"`},
+ {"%.3q", "日本語日本語", `"\u65e5\u672c\u8a9e"`},
+ {"%.3q", []byte("日本語日本語"), `"\u65e5\u672c\u8a9e"`},
+ {"%10.1q", "日本語日本語", ` "\u65e5"`},
// integers
{"%d", 12345, "12345"},
diff --git a/libgo/go/fmt/format.go b/libgo/go/fmt/format.go
index caaa7ac..f9d2b4f 100644
--- a/libgo/go/fmt/format.go
+++ b/libgo/go/fmt/format.go
@@ -235,13 +235,24 @@ func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
f.pad(buf[i:])
}
-// fmt_s formats a string.
-func (f *fmt) fmt_s(s string) {
- if f.precPresent {
- if f.prec < len(s) {
- s = s[0:f.prec]
+// truncate truncates the string to the specified precision, if present.
+func (f *fmt) truncate(s string) string {
+ if f.precPresent && f.prec < utf8.RuneCountInString(s) {
+ n := f.prec
+ for i := range s {
+ if n == 0 {
+ s = s[:i]
+ break
+ }
+ n--
}
}
+ return s
+}
+
+// fmt_s formats a string.
+func (f *fmt) fmt_s(s string) {
+ s = f.truncate(s)
f.padString(s)
}
@@ -275,6 +286,7 @@ func (f *fmt) fmt_sX(s string) {
// fmt_q formats a string as a double-quoted, escaped Go string constant.
func (f *fmt) fmt_q(s string) {
+ s = f.truncate(s)
var quoted string
if f.sharp && strconv.CanBackquote(s) {
quoted = "`" + s + "`"
diff --git a/libgo/go/fmt/print.go b/libgo/go/fmt/print.go
index 4e14fda..4b68051 100644
--- a/libgo/go/fmt/print.go
+++ b/libgo/go/fmt/print.go
@@ -520,12 +520,14 @@ func (p *pp) fmtBytes(v []byte, verb int, goSyntax bool, depth int, value interf
}
func (p *pp) fmtPointer(field interface{}, value reflect.Value, verb int, goSyntax bool) {
- v, ok := value.(uintptrGetter)
- if !ok { // reflect.PtrValue is a uintptrGetter, so failure means it's not a pointer at all.
+ var u uintptr
+ switch value.(type) {
+ case *reflect.ChanValue, *reflect.FuncValue, *reflect.MapValue, *reflect.PtrValue, *reflect.SliceValue, *reflect.UnsafePointerValue:
+ u = value.(uintptrGetter).Get()
+ default:
p.badVerb(verb, field)
return
}
- u := v.Get()
if goSyntax {
p.add('(')
p.buf.WriteString(reflect.Typeof(field).String())
@@ -534,7 +536,7 @@ func (p *pp) fmtPointer(field interface{}, value reflect.Value, verb int, goSynt
if u == 0 {
p.buf.Write(nilBytes)
} else {
- p.fmt0x64(uint64(v.Get()), true)
+ p.fmt0x64(uint64(u), true)
}
p.add(')')
} else {
@@ -811,7 +813,7 @@ BigSwitch:
break
}
p.fmt0x64(uint64(v), true)
- case uintptrGetter:
+ case *reflect.ChanValue, *reflect.FuncValue, *reflect.UnsafePointerValue:
p.fmtPointer(field, value, verb, goSyntax)
default:
p.unknownType(f)