summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/text/message/message.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/text/message/message.go')
-rw-r--r--vendor/golang.org/x/text/message/message.go103
1 files changed, 60 insertions, 43 deletions
diff --git a/vendor/golang.org/x/text/message/message.go b/vendor/golang.org/x/text/message/message.go
index ba4f95ab0..a3473bffd 100644
--- a/vendor/golang.org/x/text/message/message.go
+++ b/vendor/golang.org/x/text/message/message.go
@@ -8,26 +8,28 @@ import (
"io"
"os"
+ // Include features to facilitate generated catalogs.
+ _ "golang.org/x/text/feature/plural"
+
+ "golang.org/x/text/internal/number"
"golang.org/x/text/language"
"golang.org/x/text/message/catalog"
)
-// TODO: allow more than one goroutine per printer. This will allow porting from
-// fmt much less error prone.
-
// A Printer implements language-specific formatted I/O analogous to the fmt
-// package. Only one goroutine may use a Printer at the same time.
+// package.
type Printer struct {
- // Wrap the fields in a hidden type to hide some of the implemented methods.
- printer printer
+ // the language
+ tag language.Tag
+
+ toDecimal number.Formatter
+ toScientific number.Formatter
- // NOTE: limiting one goroutine per Printer allows for many optimizations
- // and simplifications. We can consider removing this restriction down the
- // road if it the benefits do not seem to outweigh the disadvantages.
+ cat catalog.Catalog
}
type options struct {
- cat *catalog.Catalog
+ cat catalog.Catalog
// TODO:
// - allow %s to print integers in written form (tables are likely too large
// to enable this by default).
@@ -39,39 +41,42 @@ type options struct {
type Option func(o *options)
// Catalog defines the catalog to be used.
-func Catalog(c *catalog.Catalog) Option {
+func Catalog(c catalog.Catalog) Option {
return func(o *options) { o.cat = c }
}
// NewPrinter returns a Printer that formats messages tailored to language t.
func NewPrinter(t language.Tag, opts ...Option) *Printer {
options := &options{
- cat: defaultCatalog,
+ cat: DefaultCatalog,
}
for _, o := range opts {
o(options)
}
- p := &Printer{printer{
+ p := &Printer{
tag: t,
- }}
- p.printer.toDecimal.InitDecimal(t)
- p.printer.toScientific.InitScientific(t)
- p.printer.catContext = options.cat.Context(t, &p.printer)
+ cat: options.cat,
+ }
+ p.toDecimal.InitDecimal(t)
+ p.toScientific.InitScientific(t)
return p
}
// Sprint is like fmt.Sprint, but using language-specific formatting.
func (p *Printer) Sprint(a ...interface{}) string {
- p.printer.reset()
- p.printer.doPrint(a)
- return p.printer.String()
+ pp := newPrinter(p)
+ pp.doPrint(a)
+ s := pp.String()
+ pp.free()
+ return s
}
// Fprint is like fmt.Fprint, but using language-specific formatting.
func (p *Printer) Fprint(w io.Writer, a ...interface{}) (n int, err error) {
- p.printer.reset()
- p.printer.doPrint(a)
- n64, err := io.Copy(w, &p.printer.Buffer)
+ pp := newPrinter(p)
+ pp.doPrint(a)
+ n64, err := io.Copy(w, &pp.Buffer)
+ pp.free()
return int(n64), err
}
@@ -82,16 +87,19 @@ func (p *Printer) Print(a ...interface{}) (n int, err error) {
// Sprintln is like fmt.Sprintln, but using language-specific formatting.
func (p *Printer) Sprintln(a ...interface{}) string {
- p.printer.reset()
- p.printer.doPrintln(a)
- return p.printer.String()
+ pp := newPrinter(p)
+ pp.doPrintln(a)
+ s := pp.String()
+ pp.free()
+ return s
}
// Fprintln is like fmt.Fprintln, but using language-specific formatting.
func (p *Printer) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
- p.printer.reset()
- p.printer.doPrintln(a)
- n64, err := io.Copy(w, &p.printer.Buffer)
+ pp := newPrinter(p)
+ pp.doPrintln(a)
+ n64, err := io.Copy(w, &pp.Buffer)
+ pp.free()
return int(n64), err
}
@@ -102,25 +110,34 @@ func (p *Printer) Println(a ...interface{}) (n int, err error) {
// Sprintf is like fmt.Sprintf, but using language-specific formatting.
func (p *Printer) Sprintf(key Reference, a ...interface{}) string {
- lookupAndFormat(p, key, a)
- return p.printer.String()
+ pp := newPrinter(p)
+ lookupAndFormat(pp, key, a)
+ s := pp.String()
+ pp.free()
+ return s
}
// Fprintf is like fmt.Fprintf, but using language-specific formatting.
func (p *Printer) Fprintf(w io.Writer, key Reference, a ...interface{}) (n int, err error) {
- lookupAndFormat(p, key, a)
- return w.Write(p.printer.Bytes())
+ pp := newPrinter(p)
+ lookupAndFormat(pp, key, a)
+ n, err = w.Write(pp.Bytes())
+ pp.free()
+ return n, err
+
}
// Printf is like fmt.Printf, but using language-specific formatting.
func (p *Printer) Printf(key Reference, a ...interface{}) (n int, err error) {
- lookupAndFormat(p, key, a)
- return os.Stdout.Write(p.printer.Bytes())
+ pp := newPrinter(p)
+ lookupAndFormat(pp, key, a)
+ n, err = os.Stdout.Write(pp.Bytes())
+ pp.free()
+ return n, err
}
-func lookupAndFormat(p *Printer, r Reference, a []interface{}) {
- p.printer.reset()
- p.printer.args = a
+func lookupAndFormat(p *printer, r Reference, a []interface{}) {
+ p.fmt.Reset(a)
var id, msg string
switch v := r.(type) {
case string:
@@ -131,9 +148,9 @@ func lookupAndFormat(p *Printer, r Reference, a []interface{}) {
panic("key argument is not a Reference")
}
- if p.printer.catContext.Execute(id) == catalog.ErrNotFound {
- if p.printer.catContext.Execute(msg) == catalog.ErrNotFound {
- p.printer.Render(msg)
+ if p.catContext.Execute(id) == catalog.ErrNotFound {
+ if p.catContext.Execute(msg) == catalog.ErrNotFound {
+ p.Render(msg)
return
}
}
@@ -142,8 +159,8 @@ func lookupAndFormat(p *Printer, r Reference, a []interface{}) {
// Arg implements catmsg.Renderer.
func (p *printer) Arg(i int) interface{} { // TODO, also return "ok" bool
i--
- if uint(i) < uint(len(p.args)) {
- return p.args[i]
+ if uint(i) < uint(len(p.fmt.Args)) {
+ return p.fmt.Args[i]
}
return nil
}