This can be very helpful if you lay out each value's string
representation like this:
and // &
andAnd // &&
or // |
orOr // ||
Without the use of comments, it's impossible to use stringer with these
names as the characters & and | cannot form valid identifiers in a Go
program.
Fixes#20483.
Change-Id: I4d36c74059dd48ae3a5e09b70a429a75853ef179
Reviewed-on: https://go-review.googlesource.com/44076
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Rob Pike <r@golang.org>
In the generated code, we want to pull in as few dependencies as
possible. fmt is heavier than strconv, and the latter can be used with a
bit of extra code for the same results.
More importantly, this will allow stringer to be used in some std
packages that cannot import fmt, such as regexp/syntax. While strconv
lies in L2 in deps_test.go, fmt lies in L4.
This means that many other packages will also be able to use stringer
where it could be useful, such as path/filepath, os/exec, or io/ioutil.
Since some of these types may be 64-bit integers, use FormatInt instead
of Itoa to avoid overflows with int on 32-bit.
Also double-checked that the generated code is still formatted properly.
Change-Id: Iffb3bd2df5c94407705689719240aca0c7474a89
Reviewed-on: https://go-review.googlesource.com/77473
Reviewed-by: Ian Lance Taylor <iant@golang.org>
To trim a string prefix from the names when generating their final
strings. Add a simple test too.
There is no automatic detection of prefixes for now. That can be added
later, building on top of this first simple implementation.
Fixes#16539.
Change-Id: Ica37273ac74bb0a6cbd43e61823786963d86a492
Reviewed-on: https://go-review.googlesource.com/76650
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This means that running stringer should always
have the intended effect, without having to
go install the package first, which was a common
source of confusion.
The source importer is marginally slower,
but stringer is run infrequently,
and we're only typechecking one package (and fmt),
not an entire tree, as vet does.
Fixesgolang/go#10249
Change-Id: Ib8cde29bd6cc596964dbe7348065932dd59075fc
Reviewed-on: https://go-review.googlesource.com/40403
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Robert Griesemer <gri@golang.org>
This reverts commit 9a286cdc33.
Reason for revert: go1.8 code in a go1.7 file.
Change-Id: I5465820c60197f4288243d1b15b1be20531b923d
Reviewed-on: https://go-review.googlesource.com/32733
Reviewed-by: Alan Donovan <adonovan@google.com>
This CL only copies files and updates build tags.
Substantive changes will come in follow-ups.
This is a workaround for git's lack of rename/copy tracking.
Tested with go1.6, go1.7, and tip (go1.8).
Change-Id: Id88a05273fb963586b228d5e5dfacab32133a960
Reviewed-on: https://go-review.googlesource.com/32630
Reviewed-by: Robert Griesemer <gri@golang.org>
Per https://groups.google.com/forum/#!topic/golang-announce/qu_rAphYdxY
this change deletes the packages
go/exact
go/gccgoimporter
go/gcimporter
go/importer
go/types
cmd/vet
from the x/tools repo and any files depending on those packages
building against Go 1.4.
x/tools packages depending on any of these libraries must use the
respective versions from the std lib or use vendored versions if
building against 1.4.
Remaining packages may or may not build against Go 1.4 anymore
and will not be supported against 1.4.
Change-Id: I1c655fc30aee49b6c7326ebd4eb1bb0836ac97e0
Reviewed-on: https://go-review.googlesource.com/20810
Reviewed-by: Alan Donovan <adonovan@google.com>
The previous change updated the import of golang.org/x/tools/go/types
to the standard go/types package, causing 1.4 builds to fail.
Fixes#13874.
Change-Id: Idf0b4ac5e4e01dd5cd7cc9b89d017fbffb7a29df
Reviewed-on: https://go-review.googlesource.com/18430
Reviewed-by: Robert Griesemer <gri@golang.org>
A few files have been forked and tagged "go1.5,!go1.6" to work around
minor API changes between the two types packages:
- constant.Value.String() in oracle/describe.go and its tests;
- constant.ToInt must now be called before constant.Int64Val.
- types.Config{Importer: importer.Default()} in a number of places
- go/types/typeutil/import_test.go uses lowercase names to avoid 'import "C"'.
Files in go/types/typesutil, missing from my previous CL, have been
tagged !go1.5; these files will be deleted in February.
All affected packages were tested using 1.4.1, 1.5, and ~1.6 (tip).
Change-Id: Iec7fd370e1434508149b378438fb37f65b8d2ba8
Reviewed-on: https://go-review.googlesource.com/18207
Reviewed-by: Robert Griesemer <gri@golang.org>
When String() was called on the maximum value of an integer type (eg
255 for uint8) this would cause an integer overflow, which would cause
an index error later in the code.
Fixed by re-arranging the code slightly.
Fixesgolang/go#10563
Change-Id: I9fd016afc5eea22adbc3843f6081091fd50deccf
Reviewed-on: https://go-review.googlesource.com/9255
Reviewed-by: Rob Pike <r@golang.org>
The caller of Usage should call os.Exit -- Usage shouldn't call it.
Change-Id: I3decf662883fb2a6b19b7035138ee8a06a02de08
Reviewed-on: https://go-review.googlesource.com/7110
Reviewed-by: Andrew Gerrand <adg@golang.org>
Suggestion by dsymonds: Save code not data.
Add an extra element to the index array and an if can be eliminated.
Old generated code:
const _Day_name = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"
var _Day_index = [...]uint8{6, 13, 22, 30, 36, 44, 50}
func (i Day) String() string {
if i < 0 || i >= Day(len(_Day_index)) {
return fmt.Sprintf("Day(%d)", i)
}
hi := _Day_index[i]
lo := uint8(0)
if i > 0 {
lo = _Day_index[i-1]
}
return _Day_name[lo:hi]
}
New generated code:
const _Day_name = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"
var _Day_index = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50}
func (i Day) String() string {
if i < 0 || i+1 >= Day(len(_Day_index)) {
return fmt.Sprintf("Day(%d)", i)
}
return _Day_name[_Day_index[i]:_Day_index[i+1]]
}
Change-Id: I6f46a4892d5813a12ec1ad01738c6a21c7e45172
Reviewed-on: https://go-review.googlesource.com/1990
Reviewed-by: David Symonds <dsymonds@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Avoid error "could not import C (can't find import: C)"
Fixesgolang/go#9169.
LGTM=adonovan, r
R=golang-codereviews, adonovan, r
CC=golang-codereviews
https://golang.org/cl/184730043
Rewrite performed with this command:
sed -i '' 's_code.google.com/p/go\._golang.org/x/_g' \
$(grep -lr 'code.google.com/p/go.' *)
LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/170920043
Documentation change only. The binary will not be installed
using the "go tool" mechanism.
LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/133710046
Missed comment from previous code review.
Next up: execution tests so this won't happen again
LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/134480043
Improve the generated code by using a const instead of a var for the names string.
This requires some refactoring to get neat const() and var() blocks.
Also change the generate map code go use a single sliced string, to reduce the
size of the compiled representation (only one string value).
LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/135450044
Refactor a little to make testing easier.
Add golden tests and a check fo splitIntoRuns, which is the subtlest piece.
Still to come: execution tests.
Also fix a few issues in the generated code.
LGTM=gri
R=gri
CC=golang-codereviews, josharian
https://golang.org/cl/134450044
This tool creates String methods from constant definitions.
It's a time-saver designed to be used from go generate.
The methods generated are efficient, more so than one
is likely to create by hand.
Given
package date
type Day int
const (
Monday Day = iota
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
)
the command
stringer -type Day
will create the file day_string.go containing
package date
import "fmt"
var (
_Day_indexes = []uint8{6, 13, 22, 30, 36, 44, 50}
_Day_names = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"
)
func (i Day) String() string {
if i < 0 || i >= Day(len(_Day_indexes)) {
return fmt.Sprintf("Day(%d)", i)
}
hi := _Day_indexes[i]
lo := uint8(0)
if i > 0 {
lo = _Day_indexes[i-1]
}
return _Day_names[lo:hi]
}
There are several strategies for the created method chosen according to
the structure of the sequence of constants.
Handles integer types only, both signed and unsigned. That's probably
all that is needed.
Tests to follow, but the test structure will be large so sending this out
separately. The code has been heavily hand-tested but there are
some bugs. Don't depend on this until the tests are installed.
LGTM=gri
R=gri, josharian
CC=golang-codereviews
https://golang.org/cl/136180043