Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(424)

Issue 9680043: code review 9680043: fmt.Printf: introduce notation for random access to arg... (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
11 years, 10 months ago by r
Modified:
11 years, 10 months ago
Reviewers:
rsc
CC:
rsc, bradfitz, rog, minux1, pja, golang-dev
Visibility:
Public.

Description

fmt.Printf: introduce notation for random access to arguments. This text is added to doc.go: Explicit argument indexes: In Printf, Sprintf, and Fprintf, the default behavior is for each formatting verb to format successive arguments passed in the call. However, the notation [n] immediately before the verb indicates that the nth one-indexed argument is to be formatted instead. The same notation before a '*' for a width or precision selects the argument index holding the value. After processing a bracketed expression [n], arguments n+1, n+2, etc. will be processed unless otherwise directed. For example, fmt.Sprintf("%[2]d %[1]d\n", 11, 22) will yield "22, 11", while fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6), equivalent to fmt.Sprintf("%6.2f", 12.0), will yield " 12.00". Because an explicit index affects subsequent verbs, this notation can be used to print the same values multiple times by resetting the index for the first argument to be repeated: fmt.Sprintf("%d %d %#[1]x %#x", 16, 17) will yield "16 17 0x10 0x11". The notation chosen differs from that in C, but I believe it's easier to read and to remember (we're indexing the arguments), and compatibility with C's printf was never a strong goal anyway. While we're here, change the word "field" to "arg" or "argument" in the code; it was being misused and was confusing.

Patch Set 1 #

Patch Set 2 : diff -r 8d5f80a6d6db https://code.google.com/p/go/ #

Patch Set 3 : diff -r 656591eaf189 https://code.google.com/p/go/ #

Total comments: 1

Patch Set 4 : diff -r 9b11496e569e https://code.google.com/p/go/ #

Unified diffs Side-by-side diffs Delta from patch set Stats (+218 lines, -98 lines) Patch
M doc/go1.2.txt View 1 2 3 1 chunk +1 line, -0 lines 0 comments Download
M src/pkg/fmt/doc.go View 1 2 chunks +24 lines, -0 lines 0 comments Download
M src/pkg/fmt/fmt_test.go View 1 3 chunks +38 lines, -2 lines 0 comments Download
M src/pkg/fmt/print.go View 1 2 3 25 chunks +133 lines, -74 lines 0 comments Download
M src/pkg/fmt/scan.go View 10 chunks +22 lines, -22 lines 0 comments Download

Messages

Total messages: 21
r
Hello rsc (cc: golang-dev@googlegroups.com), I'd like you to review this change to https://code.google.com/p/go/
11 years, 10 months ago (2013-05-22 22:45:39 UTC) #1
bradfitz
What's the motivation here? (there's no bug for background) Is this for gettext-like i18n stuff? ...
11 years, 10 months ago (2013-05-22 22:59:58 UTC) #2
r
Yes, it's for internationalization and translation databases and the like. It's the one piece we ...
11 years, 10 months ago (2013-05-22 23:50:54 UTC) #3
rog
why one-indexed? On 22 May 2013 23:45, <r@golang.org> wrote: > Reviewers: rsc, > > Message: ...
11 years, 10 months ago (2013-05-23 13:27:32 UTC) #4
minux1
On Thu, May 23, 2013 at 9:27 PM, roger peppe <rogpeppe@gmail.com> wrote: > why one-indexed? ...
11 years, 10 months ago (2013-05-23 14:00:19 UTC) #5
r
Lucio suggested that too. Since we're breaking C compatibility, if we ever had it, it ...
11 years, 10 months ago (2013-05-23 14:18:30 UTC) #6
rog
On 23 May 2013 15:18, Rob Pike <r@golang.org> wrote: > Lucio suggested that too. Since ...
11 years, 10 months ago (2013-05-23 15:07:27 UTC) #7
r
I'm unsure what's right here; flags complicate things. The current design has the index modify ...
11 years, 10 months ago (2013-05-23 16:03:36 UTC) #8
r
Let me retract that inclination, because I really am unsure. I'm still thinking about it. ...
11 years, 10 months ago (2013-05-23 16:15:54 UTC) #9
r
Here's the argument that convinced me the new proposal is better: With it, in the ...
11 years, 10 months ago (2013-05-23 16:29:14 UTC) #10
r
Hello rsc@golang.org, bradfitz@golang.org, rogpeppe@gmail.com, minux.ma@gmail.com (cc: golang-dev@googlegroups.com), Please take another look.
11 years, 10 months ago (2013-05-23 16:51:06 UTC) #11
minux1
could you please re-upload? I got "error: old chunk mismatch" for patch set 2. also, ...
11 years, 10 months ago (2013-05-23 20:21:44 UTC) #12
r
done.
11 years, 10 months ago (2013-05-23 20:23:45 UTC) #13
pja
I have a rather philosophical question: What determines if changes such as this violate the ...
11 years, 10 months ago (2013-05-24 11:28:03 UTC) #14
rog
On 23 May 2013 17:28, Rob Pike <r@golang.org> wrote: > Here's the argument that convinced ...
11 years, 10 months ago (2013-05-24 14:30:57 UTC) #15
bradfitz
On Wed, May 22, 2013 at 4:50 PM, Rob Pike <r@golang.org> wrote: > Yes, it's ...
11 years, 10 months ago (2013-05-24 14:35:26 UTC) #16
r
By "the format various translation databases use", do you mean the $ notation? The thing ...
11 years, 10 months ago (2013-05-24 15:11:20 UTC) #17
minux1
On Fri, May 24, 2013 at 7:28 PM, Peter Armitage <peter.armitage@gmail.com>wrote: > I have a ...
11 years, 10 months ago (2013-05-24 15:30:01 UTC) #18
rsc
LGTM I like the new syntax and semantics.
11 years, 10 months ago (2013-05-24 21:49:10 UTC) #19
minux1
changes look good. https://codereview.appspot.com/9680043/diff/20001/src/pkg/fmt/print.go File src/pkg/fmt/print.go (right): https://codereview.appspot.com/9680043/diff/20001/src/pkg/fmt/print.go#newcode220 src/pkg/fmt/print.go:220: n64, err := w.Write(p.buf) it could ...
11 years, 10 months ago (2013-05-24 21:52:08 UTC) #20
r
11 years, 10 months ago (2013-05-24 22:49:30 UTC) #21
*** Submitted as https://code.google.com/p/go/source/detail?r=e9e3359d9c84 ***

fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:

        Explicit argument indexes:

        In Printf, Sprintf, and Fprintf, the default behavior is for each
        formatting verb to format successive arguments passed in the call.
        However, the notation [n] immediately before the verb indicates that the
        nth one-indexed argument is to be formatted instead. The same notation
        before a '*' for a width or precision selects the argument index holding
        the value. After processing a bracketed expression [n], arguments n+1,
        n+2, etc. will be processed unless otherwise directed.

        For example,
                fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
        will yield "22, 11", while
                fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
        equivalent to
                fmt.Sprintf("%6.2f", 12.0),
        will yield " 12.00". Because an explicit index affects subsequent verbs,
        this notation can be used to print the same values multiple times
        by resetting the index for the first argument to be repeated:
                fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
        will yield "16 17 0x10 0x11".

The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.

While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.

R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://codereview.appspot.com/9680043
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b