1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

First commit

This commit is contained in:
Frédéric Guillot 2017-11-19 21:10:04 -08:00
commit 8ffb773f43
2121 changed files with 1118910 additions and 0 deletions

28
vendor/golang.org/x/text/message/catalog.go generated vendored Normal file
View file

@ -0,0 +1,28 @@
// Copyright 2015 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.
package message
// TODO: some types in this file will need to be made public at some time.
// Documentation and method names will reflect this by using the exported name.
import (
"golang.org/x/text/language"
"golang.org/x/text/message/catalog"
)
// DefaultCatalog is used by SetString.
var DefaultCatalog *catalog.Catalog = defaultCatalog
var defaultCatalog = catalog.New()
// SetString calls SetString on the initial default Catalog.
func SetString(tag language.Tag, key string, msg string) error {
return defaultCatalog.SetString(tag, key, msg)
}
// Set calls Set on the initial default Catalog.
func Set(tag language.Tag, key string, msg ...catalog.Message) error {
return defaultCatalog.Set(tag, key, msg...)
}

292
vendor/golang.org/x/text/message/catalog/catalog.go generated vendored Normal file
View file

@ -0,0 +1,292 @@
// 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.
// Package catalog defines collections of translated format strings.
//
// This package mostly defines types for populating catalogs with messages. The
// catmsg package contains further definitions for creating custom message and
// dictionary types as well as packages that use Catalogs.
//
// Package catalog defines various interfaces: Dictionary, Loader, and Message.
// A Dictionary maintains a set of translations of format strings for a single
// language. The Loader interface defines a source of dictionaries. A
// translation of a format string is represented by a Message.
//
//
// Catalogs
//
// A Catalog defines a programmatic interface for setting message translations.
// It maintains a set of per-language dictionaries with translations for a set
// of keys. For message translation to function properly, a translation should
// be defined for each key for each supported language. A dictionary may be
// underspecified, though, if there is a parent language that already defines
// the key. For example, a Dictionary for "en-GB" could leave out entries that
// are identical to those in a dictionary for "en".
//
//
// Messages
//
// A Message is a format string which varies on the value of substitution
// variables. For instance, to indicate the number of results one could want "no
// results" if there are none, "1 result" if there is 1, and "%d results" for
// any other number. Catalog is agnostic to the kind of format strings that are
// used: for instance, messages can follow either the printf-style substitution
// from package fmt or use templates.
//
// A Message does not substitute arguments in the format string. This job is
// reserved for packages that render strings, such as message, that use Catalogs
// to selected string. This separation of concerns allows Catalog to be used to
// store any kind of formatting strings.
//
//
// Selecting messages based on linguistic features of substitution arguments
//
// Messages may vary based on any linguistic features of the argument values.
// The most common one is plural form, but others exist.
//
// Selection messages are provided in packages that provide support for a
// specific linguistic feature. The following snippet uses plural.Select:
//
// catalog.Set(language.English, "You are %d minute(s) late.",
// plural.Select(1,
// "one", "You are 1 minute late.",
// "other", "You are %d minutes late."))
//
// In this example, a message is stored in the Catalog where one of two messages
// is selected based on the first argument, a number. The first message is
// selected if the argument is singular (identified by the selector "one") and
// the second message is selected in all other cases. The selectors are defined
// by the plural rules defined in CLDR. The selector "other" is special and will
// always match. Each language always defines one of the linguistic categories
// to be "other." For English, singular is "one" and plural is "other".
//
// Selects can be nested. This allows selecting sentences based on features of
// multiple arguments or multiple linguistic properties of a single argument.
//
//
// String interpolation
//
// There is often a lot of commonality between the possible variants of a
// message. For instance, in the example above the word "minute" varies based on
// the plural catogory of the argument, but the rest of the sentence is
// identical. Using interpolation the above message can be rewritten as:
//
// catalog.Set(language.English, "You are %d minute(s) late.",
// catalog.Var("minutes",
// plural.Select(1, "one", "minute", "other", "minutes")),
// catalog.String("You are %[1]d ${minutes} late."))
//
// Var is defined to return the variable name if the message does not yield a
// match. This allows us to further simplify this snippet to
//
// catalog.Set(language.English, "You are %d minute(s) late.",
// catalog.Var("minutes", plural.Select(1, "one", "minute")),
// catalog.String("You are %d ${minutes} late."))
//
// Overall this is still only a minor improvement, but things can get a lot more
// unwieldy if more than one linguistic feature is used to determine a message
// variant. Consider the following example:
//
// // argument 1: list of hosts, argument 2: list of guests
// catalog.Set(language.English, "%[1]v invite(s) %[2]v to their party.",
// catalog.Var("their",
// plural.Select(1,
// "one", gender.Select(1, "female", "her", "other", "his"))),
// catalog.Var("invites", plural.Select(1, "one", "invite"))
// catalog.String("%[1]v ${invites} %[2]v to ${their} party.")),
//
// Without variable substitution, this would have to be written as
//
// // argument 1: list of hosts, argument 2: list of guests
// catalog.Set(language.English, "%[1]v invite(s) %[2]v to their party.",
// plural.Select(1,
// "one", gender.Select(1,
// "female", "%[1]v invites %[2]v to her party."
// "other", "%[1]v invites %[2]v to his party."),
// "other", "%[1]v invites %[2]v to their party.")
//
// Not necessarily shorter, but using variables there is less duplication and
// the messages are more maintenance friendly. Moreover, languages may have up
// to six plural forms. This makes the use of variables more welcome.
//
// Different messages using the same inflections can reuse variables by moving
// them to macros. Using macros we can rewrite the message as:
//
// // argument 1: list of hosts, argument 2: list of guests
// catalog.SetString(language.English, "%[1]v invite(s) %[2]v to their party.",
// "%[1]v ${invites(1)} %[2]v to ${their(1)} party.")
//
// Where the following macros were defined separately.
//
// catalog.SetMacro(language.English, "invites", plural.Select(1, "one", "invite"))
// catalog.SetMacro(language.English, "their", plural.Select(1,
// "one", gender.Select(1, "female", "her", "other", "his"))),
//
// Placeholders use parentheses and the arguments to invoke a macro.
//
//
// Looking up messages
//
// Message lookup using Catalogs is typically only done by specialized packages
// and is not something the user should be concerned with. For instance, to
// express the tardiness of a user using the related message we defined earlier,
// the user may use the package message like so:
//
// p := message.NewPrinter(language.English)
// p.Printf("You are %d minute(s) late.", 5)
//
// Which would print:
// You are 5 minutes late.
//
//
// This package is UNDER CONSTRUCTION and its API may change.
package catalog // import "golang.org/x/text/message/catalog"
// TODO:
// Some way to freeze a catalog.
// - Locking on each lockup turns out to be about 50% of the total running time
// for some of the benchmarks in the message package.
// Consider these:
// - Sequence type to support sequences in user-defined messages.
// - Garbage collection: Remove dictionaries that can no longer be reached
// as other dictionaries have been added that cover all possible keys.
import (
"errors"
"fmt"
"golang.org/x/text/internal/catmsg"
"golang.org/x/text/language"
)
// A Catalog holds translations for messages for supported languages.
type Catalog struct {
options
index store
macros store
}
type options struct{}
// An Option configures Catalog behavior.
type Option func(*options)
// TODO:
// // Catalogs specifies one or more sources for a Catalog.
// // Lookups are in order.
// // This can be changed inserting a Catalog used for setting, which implements
// // Loader, used for setting in the chain.
// func Catalogs(d ...Loader) Option {
// return nil
// }
//
// func Delims(start, end string) Option {}
//
// func Dict(tag language.Tag, d ...Dictionary) Option
// New returns a new Catalog.
func New(opts ...Option) *Catalog {
c := &Catalog{}
for _, o := range opts {
o(&c.options)
}
return c
}
// Languages returns all languages for which the Catalog contains variants.
func (c *Catalog) Languages() []language.Tag {
return c.index.languages()
}
// SetString is shorthand for Set(tag, key, String(msg)).
func (c *Catalog) SetString(tag language.Tag, key string, msg string) error {
return c.set(tag, key, &c.index, String(msg))
}
// Set sets the translation for the given language and key.
//
// When evaluation this message, the first Message in the sequence to msgs to
// evaluate to a string will be the message returned.
func (c *Catalog) Set(tag language.Tag, key string, msg ...Message) error {
return c.set(tag, key, &c.index, msg...)
}
// SetMacro defines a Message that may be substituted in another message.
// The arguments to a macro Message are passed as arguments in the
// placeholder the form "${foo(arg1, arg2)}".
func (c *Catalog) SetMacro(tag language.Tag, name string, msg ...Message) error {
return c.set(tag, name, &c.macros, msg...)
}
// ErrNotFound indicates there was no message for the given key.
var ErrNotFound = errors.New("catalog: message not found")
// A Message holds a collection of translations for the same phrase that may
// vary based on the values of substitution arguments.
type Message interface {
catmsg.Message
}
// String specifies a plain message string. It can be used as fallback if no
// other strings match or as a simple standalone message.
//
// It is an error to pass more than one String in a message sequence.
func String(name string) Message {
return catmsg.String(name)
}
// Var sets a variable that may be substituted in formatting patterns using
// named substitution of the form "${name}". The name argument is used as a
// fallback if the statements do not produce a match. The statement sequence may
// not contain any Var calls.
//
// The name passed to a Var must be unique within message sequence.
func Var(name string, msg ...Message) Message {
return &catmsg.Var{Name: name, Message: firstInSequence(msg)}
}
// firstInSequence is a message type that prints the first message in the
// sequence that resolves to a match for the given substitution arguments.
type firstInSequence []Message
func (s firstInSequence) Compile(e *catmsg.Encoder) error {
e.EncodeMessageType(catmsg.First)
err := catmsg.ErrIncomplete
for i, m := range s {
if err == nil {
return fmt.Errorf("catalog: message argument %d is complete and blocks subsequent messages", i-1)
}
err = e.EncodeMessage(m)
}
return err
}
// Context returns a Context for formatting messages.
// Only one Message may be formatted per context at any given time.
func (c *Catalog) Context(tag language.Tag, r catmsg.Renderer) *Context {
return &Context{
cat: c,
tag: tag,
dec: catmsg.NewDecoder(tag, r, &dict{&c.macros, tag}),
}
}
// A Context is used for evaluating Messages.
// Only one Message may be formatted per context at any given time.
type Context struct {
cat *Catalog
tag language.Tag
dec *catmsg.Decoder
}
// Execute looks up and executes the message with the given key.
// It returns ErrNotFound if no message could be found in the index.
func (c *Context) Execute(key string) error {
data, ok := c.cat.index.lookup(c.tag, key)
if !ok {
return ErrNotFound
}
return c.dec.Execute(data)
}

View file

@ -0,0 +1,194 @@
// 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.
package catalog
import (
"bytes"
"fmt"
"reflect"
"testing"
"golang.org/x/text/internal"
"golang.org/x/text/internal/catmsg"
"golang.org/x/text/language"
)
type entry struct {
tag, key string
msg interface{}
}
var testCases = []struct {
desc string
cat []entry
lookup []entry
}{{
desc: "empty catalog",
lookup: []entry{
{"en", "key", ""},
{"en", "", ""},
{"nl", "", ""},
},
}, {
desc: "one entry",
cat: []entry{
{"en", "hello", "Hello!"},
},
lookup: []entry{
{"und", "hello", ""},
{"nl", "hello", ""},
{"en", "hello", "Hello!"},
{"en-US", "hello", "Hello!"},
{"en-GB", "hello", "Hello!"},
{"en-oxendict", "hello", "Hello!"},
{"en-oxendict-u-ms-metric", "hello", "Hello!"},
},
}, {
desc: "hierarchical languages",
cat: []entry{
{"en", "hello", "Hello!"},
{"en-GB", "hello", "Hellø!"},
{"en-US", "hello", "Howdy!"},
{"en", "greetings", "Greetings!"},
},
lookup: []entry{
{"und", "hello", ""},
{"nl", "hello", ""},
{"en", "hello", "Hello!"},
{"en-US", "hello", "Howdy!"},
{"en-GB", "hello", "Hellø!"},
{"en-oxendict", "hello", "Hello!"},
{"en-US-oxendict-u-ms-metric", "hello", "Howdy!"},
{"und", "greetings", ""},
{"nl", "greetings", ""},
{"en", "greetings", "Greetings!"},
{"en-US", "greetings", "Greetings!"},
{"en-GB", "greetings", "Greetings!"},
{"en-oxendict", "greetings", "Greetings!"},
{"en-US-oxendict-u-ms-metric", "greetings", "Greetings!"},
},
}, {
desc: "variables",
cat: []entry{
{"en", "hello %s", []Message{
Var("person", String("Jane")),
String("Hello ${person}!"),
}},
{"en", "hello error", []Message{
Var("person", String("Jane")),
noMatchMessage{}, // trigger sequence path.
String("Hello ${person."),
}},
{"en", "fallback to var value", []Message{
Var("you", noMatchMessage{}, noMatchMessage{}),
String("Hello ${you}."),
}},
{"en", "scopes", []Message{
Var("person1", String("Mark")),
Var("person2", String("Jane")),
Var("couple",
Var("person1", String("Joe")),
String("${person1} and ${person2}")),
String("Hello ${couple}."),
}},
{"en", "missing var", String("Hello ${missing}.")},
},
lookup: []entry{
{"en", "hello %s", "Hello Jane!"},
{"en", "hello error", "Hello $!(MISSINGBRACE)"},
{"en", "fallback to var value", "Hello you."},
{"en", "scopes", "Hello Joe and Jane."},
{"en", "missing var", "Hello missing."},
},
}, {
desc: "macros",
cat: []entry{
{"en", "macro1", String("Hello ${macro1(1)}.")},
{"en", "macro2", String("Hello ${ macro1(2) }!")},
{"en", "macroWS", String("Hello ${ macro1( 2 ) }!")},
{"en", "missing", String("Hello ${ missing(1 }.")},
{"en", "badnum", String("Hello ${ badnum(1b) }.")},
{"en", "undefined", String("Hello ${ undefined(1) }.")},
{"en", "macroU", String("Hello ${ macroU(2) }!")},
},
lookup: []entry{
{"en", "macro1", "Hello Joe."},
{"en", "macro2", "Hello Joe!"},
{"en-US", "macroWS", "Hello Joe!"},
{"en-NL", "missing", "Hello $!(MISSINGPAREN)."},
{"en", "badnum", "Hello $!(BADNUM)."},
{"en", "undefined", "Hello undefined."},
{"en", "macroU", "Hello macroU!"},
}}}
func initCat(entries []entry) (*Catalog, []language.Tag) {
tags := []language.Tag{}
cat := New()
for _, e := range entries {
tag := language.MustParse(e.tag)
tags = append(tags, tag)
switch msg := e.msg.(type) {
case string:
cat.SetString(tag, e.key, msg)
case Message:
cat.Set(tag, e.key, msg)
case []Message:
cat.Set(tag, e.key, msg...)
}
}
return cat, internal.UniqueTags(tags)
}
func TestCatalog(t *testing.T) {
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s", tc.desc), func(t *testing.T) {
cat, wantTags := initCat(tc.cat)
cat.SetMacro(language.English, "macro1", String("Joe"))
cat.SetMacro(language.Und, "macro2", String("${macro1(1)}"))
cat.SetMacro(language.English, "macroU", noMatchMessage{})
if got := cat.Languages(); !reflect.DeepEqual(got, wantTags) {
t.Errorf("%s:Languages: got %v; want %v", tc.desc, got, wantTags)
}
for _, e := range tc.lookup {
t.Run(fmt.Sprintf("%s/%s", e.tag, e.key), func(t *testing.T) {
tag := language.MustParse(e.tag)
buf := testRenderer{}
ctx := cat.Context(tag, &buf)
want := e.msg.(string)
err := ctx.Execute(e.key)
gotFound := err != ErrNotFound
wantFound := want != ""
if gotFound != wantFound {
t.Fatalf("err: got %v (%v); want %v", gotFound, err, wantFound)
}
if got := buf.buf.String(); got != want {
t.Errorf("Lookup:\ngot %q\nwant %q", got, want)
}
})
}
})
}
}
type testRenderer struct {
buf bytes.Buffer
}
func (f *testRenderer) Arg(i int) interface{} { return nil }
func (f *testRenderer) Render(s string) { f.buf.WriteString(s) }
var msgNoMatch = catmsg.Register("no match", func(d *catmsg.Decoder) bool {
return false // no match
})
type noMatchMessage struct{}
func (noMatchMessage) Compile(e *catmsg.Encoder) error {
e.EncodeMessageType(msgNoMatch)
return catmsg.ErrIncomplete
}

90
vendor/golang.org/x/text/message/catalog/dict.go generated vendored Normal file
View file

@ -0,0 +1,90 @@
// 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.
package catalog
import (
"sync"
"golang.org/x/text/internal"
"golang.org/x/text/internal/catmsg"
"golang.org/x/text/language"
)
// TODO:
// Dictionary returns a Dictionary that returns the first Message, using the
// given language tag, that matches:
// 1. the last one registered by one of the Set methods
// 2. returned by one of the Loaders
// 3. repeat from 1. using the parent language
// This approach allows messages to be underspecified.
// func (c *Catalog) Dictionary(tag language.Tag) (Dictionary, error) {
// // TODO: verify dictionary exists.
// return &dict{&c.index, tag}, nil
// }
type dict struct {
s *store
tag language.Tag // TODO: make compact tag.
}
func (d *dict) Lookup(key string) (data string, ok bool) {
return d.s.lookup(d.tag, key)
}
func (c *Catalog) set(tag language.Tag, key string, s *store, msg ...Message) error {
data, err := catmsg.Compile(tag, &dict{&c.macros, tag}, firstInSequence(msg))
s.mutex.Lock()
defer s.mutex.Unlock()
m := s.index[tag]
if m == nil {
m = msgMap{}
if s.index == nil {
s.index = map[language.Tag]msgMap{}
}
s.index[tag] = m
}
m[key] = data
return err
}
type store struct {
mutex sync.RWMutex
index map[language.Tag]msgMap
}
type msgMap map[string]string
func (s *store) lookup(tag language.Tag, key string) (data string, ok bool) {
s.mutex.RLock()
defer s.mutex.RUnlock()
for ; ; tag = tag.Parent() {
if msgs, ok := s.index[tag]; ok {
if msg, ok := msgs[key]; ok {
return msg, true
}
}
if tag == language.Und {
break
}
}
return "", false
}
// Languages returns all languages for which the store contains variants.
func (s *store) languages() []language.Tag {
s.mutex.RLock()
defer s.mutex.RUnlock()
tags := make([]language.Tag, 0, len(s.index))
for t := range s.index {
tags = append(tags, t)
}
internal.SortTags(tags)
return tags
}

100
vendor/golang.org/x/text/message/doc.go generated vendored Normal file
View file

@ -0,0 +1,100 @@
// 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.
// Package message implements formatted I/O for localized strings with functions
// analogous to the fmt's print functions. It is a drop-in replacement for fmt.
//
//
// Localized Formatting
//
// A format string can be localized by replacing any of the print functions of
// fmt with an equivalent call to a Printer.
//
// p := message.NewPrinter(language.English)
// p.Println(123456.78) // Prints 123,456.78
//
// p.Printf("%d ducks in a row", 4331) // Prints 4,331 ducks in a row
//
// p := message.NewPrinter(language.Dutch)
// p.Println("Hoogte: %f meter", 1244.9) // Prints Hoogte: 1.244,9 meter
//
// p := message.NewPrinter(language.Bengali)
// p.Println(123456.78) // Prints ১,২৩,৪৫৬.৭৮
//
// Printer currently supports numbers and specialized types for which packages
// exist in x/text. Other builtin types such as time.Time and slices are
// planned.
//
// Format strings largely have the same meaning as with fmt with the following
// notable exceptions:
// - flag # always resorts to fmt for printing
// - verb 'f', 'e', 'g', 'd' use localized formatting unless the '#' flag is
// specified.
//
// See package fmt for more options.
//
//
// Translation
//
// The format strings that are passed to Printf, Sprintf, Fprintf, or Errorf
// are used as keys to look up translations for the specified languages.
// More on how these need to be specified below.
//
// One can use arbitrary keys to distinguish between otherwise ambiguous
// strings:
// p := message.NewPrinter(language.English)
// p.Printf("archive(noun)") // Prints "archive"
// p.Printf("archive(verb)") // Prints "archive"
//
// p := message.NewPrinter(language.German)
// p.Printf("archive(noun)") // Prints "Archiv"
// p.Printf("archive(verb)") // Prints "archivieren"
//
// To retain the fallback functionality, use Key:
// p.Printf(message.Key("archive(noun)", "archive"))
// p.Printf(message.Key("archive(verb)", "archive"))
//
//
// Translation Pipeline
//
// Format strings that contain text need to be translated to support different
// locales. The first step is to extract strings that need to be translated.
//
// 1. Install gotext
// go get -u golang.org/x/text/cmd/gotext
// gotext -help
//
// 2. Mark strings in your source to be translated by using message.Printer,
// instead of the functions of the fmt package.
//
// 3. Extract the strings from your source
//
// gotext extract
//
// The output will be written to the textdata directory.
//
// 4. Send the files for translation
//
// It is planned to support multiple formats, but for now one will have to
// rewrite the JSON output to the desired format.
//
// 5. Inject translations into program
//
// 6. Repeat from 2
//
// Right now this has to be done programmatically with calls to Set or
// SetString. These functions as well as the methods defined in
// see also package golang.org/x/text/message/catalog can be used to implement
// either dynamic or static loading of messages.
//
//
// Plural and Gender Forms
//
// Translated messages can vary based on the plural and gender forms of
// substitution values. In general, it is up to the translators to provide
// alternative translations for such forms. See the packages in
// golang.org/x/text/feature and golang.org/x/text/message/catalog for more
// information.
//
package message

42
vendor/golang.org/x/text/message/examples_test.go generated vendored Normal file
View file

@ -0,0 +1,42 @@
// 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.
package message_test
import (
"net/http"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
func Example_http() {
// languages supported by this service:
matcher := language.NewMatcher(message.DefaultCatalog.Languages())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
lang, _ := r.Cookie("lang")
accept := r.Header.Get("Accept-Language")
fallback := "en"
tag, _ := language.MatchStrings(matcher, lang.String(), accept, fallback)
p := message.NewPrinter(tag)
p.Fprintln(w, "User language is", tag)
})
}
func ExamplePrinter_numbers() {
for _, lang := range []string{"en", "de", "de-CH", "fr", "bn"} {
p := message.NewPrinter(language.Make(lang))
p.Printf("%-6s %g\n", lang, 123456.78)
}
// Output:
// en 123,456.78
// de 123.456,78
// de-CH 123456.78
// fr 123 456,78
// bn ১,২৩,৪৫৬.৭৮
}

1889
vendor/golang.org/x/text/message/fmt_test.go generated vendored Executable file

File diff suppressed because it is too large Load diff

532
vendor/golang.org/x/text/message/format.go generated vendored Normal file
View file

@ -0,0 +1,532 @@
// 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.
package message
import (
"bytes"
"strconv"
"unicode/utf8"
)
const (
ldigits = "0123456789abcdefx"
udigits = "0123456789ABCDEFX"
)
const (
signed = true
unsigned = false
)
// flags placed in a separate struct for easy clearing.
type fmtFlags struct {
widPresent bool
precPresent bool
minus bool
plus bool
sharp bool
space bool
zero bool
// For the formats %+v %#v, we set the plusV/sharpV flags
// and clear the plus/sharp flags since %+v and %#v are in effect
// different, flagless formats set at the top level.
plusV bool
sharpV bool
}
// A formatInfo is the raw formatter used by Printf etc.
// It prints into a buffer that must be set up separately.
type formatInfo struct {
buf *bytes.Buffer
fmtFlags
wid int // width
prec int // precision
// intbuf is large enough to store %b of an int64 with a sign and
// avoids padding at the end of the struct on 32 bit architectures.
intbuf [68]byte
}
func (f *formatInfo) clearflags() {
f.fmtFlags = fmtFlags{}
}
func (f *formatInfo) init(buf *bytes.Buffer) {
f.buf = buf
f.clearflags()
}
// writePadding generates n bytes of padding.
func (f *formatInfo) writePadding(n int) {
if n <= 0 { // No padding bytes needed.
return
}
f.buf.Grow(n)
// Decide which byte the padding should be filled with.
padByte := byte(' ')
if f.zero {
padByte = byte('0')
}
// Fill padding with padByte.
for i := 0; i < n; i++ {
f.buf.WriteByte(padByte) // TODO: make more efficient.
}
}
// pad appends b to f.buf, padded on left (!f.minus) or right (f.minus).
func (f *formatInfo) pad(b []byte) {
if !f.widPresent || f.wid == 0 {
f.buf.Write(b)
return
}
width := f.wid - utf8.RuneCount(b)
if !f.minus {
// left padding
f.writePadding(width)
f.buf.Write(b)
} else {
// right padding
f.buf.Write(b)
f.writePadding(width)
}
}
// padString appends s to f.buf, padded on left (!f.minus) or right (f.minus).
func (f *formatInfo) padString(s string) {
if !f.widPresent || f.wid == 0 {
f.buf.WriteString(s)
return
}
width := f.wid - utf8.RuneCountInString(s)
if !f.minus {
// left padding
f.writePadding(width)
f.buf.WriteString(s)
} else {
// right padding
f.buf.WriteString(s)
f.writePadding(width)
}
}
// fmt_boolean formats a boolean.
func (f *formatInfo) fmt_boolean(v bool) {
if v {
f.padString("true")
} else {
f.padString("false")
}
}
// fmt_unicode formats a uint64 as "U+0078" or with f.sharp set as "U+0078 'x'".
func (f *formatInfo) fmt_unicode(u uint64) {
buf := f.intbuf[0:]
// With default precision set the maximum needed buf length is 18
// for formatting -1 with %#U ("U+FFFFFFFFFFFFFFFF") which fits
// into the already allocated intbuf with a capacity of 68 bytes.
prec := 4
if f.precPresent && f.prec > 4 {
prec = f.prec
// Compute space needed for "U+" , number, " '", character, "'".
width := 2 + prec + 2 + utf8.UTFMax + 1
if width > len(buf) {
buf = make([]byte, width)
}
}
// Format into buf, ending at buf[i]. Formatting numbers is easier right-to-left.
i := len(buf)
// For %#U we want to add a space and a quoted character at the end of the buffer.
if f.sharp && u <= utf8.MaxRune && strconv.IsPrint(rune(u)) {
i--
buf[i] = '\''
i -= utf8.RuneLen(rune(u))
utf8.EncodeRune(buf[i:], rune(u))
i--
buf[i] = '\''
i--
buf[i] = ' '
}
// Format the Unicode code point u as a hexadecimal number.
for u >= 16 {
i--
buf[i] = udigits[u&0xF]
prec--
u >>= 4
}
i--
buf[i] = udigits[u]
prec--
// Add zeros in front of the number until requested precision is reached.
for prec > 0 {
i--
buf[i] = '0'
prec--
}
// Add a leading "U+".
i--
buf[i] = '+'
i--
buf[i] = 'U'
oldZero := f.zero
f.zero = false
f.pad(buf[i:])
f.zero = oldZero
}
// fmt_integer formats signed and unsigned integers.
func (f *formatInfo) fmt_integer(u uint64, base int, isSigned bool, digits string) {
negative := isSigned && int64(u) < 0
if negative {
u = -u
}
buf := f.intbuf[0:]
// The already allocated f.intbuf with a capacity of 68 bytes
// is large enough for integer formatting when no precision or width is set.
if f.widPresent || f.precPresent {
// Account 3 extra bytes for possible addition of a sign and "0x".
width := 3 + f.wid + f.prec // wid and prec are always positive.
if width > len(buf) {
// We're going to need a bigger boat.
buf = make([]byte, width)
}
}
// Two ways to ask for extra leading zero digits: %.3d or %03d.
// If both are specified the f.zero flag is ignored and
// padding with spaces is used instead.
prec := 0
if f.precPresent {
prec = f.prec
// Precision of 0 and value of 0 means "print nothing" but padding.
if prec == 0 && u == 0 {
oldZero := f.zero
f.zero = false
f.writePadding(f.wid)
f.zero = oldZero
return
}
} else if f.zero && f.widPresent {
prec = f.wid
if negative || f.plus || f.space {
prec-- // leave room for sign
}
}
// Because printing is easier right-to-left: format u into buf, ending at buf[i].
// We could make things marginally faster by splitting the 32-bit case out
// into a separate block but it's not worth the duplication, so u has 64 bits.
i := len(buf)
// Use constants for the division and modulo for more efficient code.
// Switch cases ordered by popularity.
switch base {
case 10:
for u >= 10 {
i--
next := u / 10
buf[i] = byte('0' + u - next*10)
u = next
}
case 16:
for u >= 16 {
i--
buf[i] = digits[u&0xF]
u >>= 4
}
case 8:
for u >= 8 {
i--
buf[i] = byte('0' + u&7)
u >>= 3
}
case 2:
for u >= 2 {
i--
buf[i] = byte('0' + u&1)
u >>= 1
}
default:
panic("fmt: unknown base; can't happen")
}
i--
buf[i] = digits[u]
for i > 0 && prec > len(buf)-i {
i--
buf[i] = '0'
}
// Various prefixes: 0x, -, etc.
if f.sharp {
switch base {
case 8:
if buf[i] != '0' {
i--
buf[i] = '0'
}
case 16:
// Add a leading 0x or 0X.
i--
buf[i] = digits[16]
i--
buf[i] = '0'
}
}
if negative {
i--
buf[i] = '-'
} else if f.plus {
i--
buf[i] = '+'
} else if f.space {
i--
buf[i] = ' '
}
// Left padding with zeros has already been handled like precision earlier
// or the f.zero flag is ignored due to an explicitly set precision.
oldZero := f.zero
f.zero = false
f.pad(buf[i:])
f.zero = oldZero
}
// truncate truncates the string to the specified precision, if present.
func (f *formatInfo) truncate(s string) string {
if f.precPresent {
n := f.prec
for i := range s {
n--
if n < 0 {
return s[:i]
}
}
}
return s
}
// fmt_s formats a string.
func (f *formatInfo) fmt_s(s string) {
s = f.truncate(s)
f.padString(s)
}
// fmt_sbx formats a string or byte slice as a hexadecimal encoding of its bytes.
func (f *formatInfo) fmt_sbx(s string, b []byte, digits string) {
length := len(b)
if b == nil {
// No byte slice present. Assume string s should be encoded.
length = len(s)
}
// Set length to not process more bytes than the precision demands.
if f.precPresent && f.prec < length {
length = f.prec
}
// Compute width of the encoding taking into account the f.sharp and f.space flag.
width := 2 * length
if width > 0 {
if f.space {
// Each element encoded by two hexadecimals will get a leading 0x or 0X.
if f.sharp {
width *= 2
}
// Elements will be separated by a space.
width += length - 1
} else if f.sharp {
// Only a leading 0x or 0X will be added for the whole string.
width += 2
}
} else { // The byte slice or string that should be encoded is empty.
if f.widPresent {
f.writePadding(f.wid)
}
return
}
// Handle padding to the left.
if f.widPresent && f.wid > width && !f.minus {
f.writePadding(f.wid - width)
}
// Write the encoding directly into the output buffer.
buf := f.buf
if f.sharp {
// Add leading 0x or 0X.
buf.WriteByte('0')
buf.WriteByte(digits[16])
}
var c byte
for i := 0; i < length; i++ {
if f.space && i > 0 {
// Separate elements with a space.
buf.WriteByte(' ')
if f.sharp {
// Add leading 0x or 0X for each element.
buf.WriteByte('0')
buf.WriteByte(digits[16])
}
}
if b != nil {
c = b[i] // Take a byte from the input byte slice.
} else {
c = s[i] // Take a byte from the input string.
}
// Encode each byte as two hexadecimal digits.
buf.WriteByte(digits[c>>4])
buf.WriteByte(digits[c&0xF])
}
// Handle padding to the right.
if f.widPresent && f.wid > width && f.minus {
f.writePadding(f.wid - width)
}
}
// fmt_sx formats a string as a hexadecimal encoding of its bytes.
func (f *formatInfo) fmt_sx(s, digits string) {
f.fmt_sbx(s, nil, digits)
}
// fmt_bx formats a byte slice as a hexadecimal encoding of its bytes.
func (f *formatInfo) fmt_bx(b []byte, digits string) {
f.fmt_sbx("", b, digits)
}
// fmt_q formats a string as a double-quoted, escaped Go string constant.
// If f.sharp is set a raw (backquoted) string may be returned instead
// if the string does not contain any control characters other than tab.
func (f *formatInfo) fmt_q(s string) {
s = f.truncate(s)
if f.sharp && strconv.CanBackquote(s) {
f.padString("`" + s + "`")
return
}
buf := f.intbuf[:0]
if f.plus {
f.pad(strconv.AppendQuoteToASCII(buf, s))
} else {
f.pad(strconv.AppendQuote(buf, s))
}
}
// fmt_c formats an integer as a Unicode character.
// If the character is not valid Unicode, it will print '\ufffd'.
func (f *formatInfo) fmt_c(c uint64) {
r := rune(c)
if c > utf8.MaxRune {
r = utf8.RuneError
}
buf := f.intbuf[:0]
w := utf8.EncodeRune(buf[:utf8.UTFMax], r)
f.pad(buf[:w])
}
// fmt_qc formats an integer as a single-quoted, escaped Go character constant.
// If the character is not valid Unicode, it will print '\ufffd'.
func (f *formatInfo) fmt_qc(c uint64) {
r := rune(c)
if c > utf8.MaxRune {
r = utf8.RuneError
}
buf := f.intbuf[:0]
if f.plus {
f.pad(strconv.AppendQuoteRuneToASCII(buf, r))
} else {
f.pad(strconv.AppendQuoteRune(buf, r))
}
}
// fmt_float formats a float64. It assumes that verb is a valid format specifier
// for strconv.AppendFloat and therefore fits into a byte.
func (f *formatInfo) fmt_float(v float64, size int, verb rune, prec int) {
// Explicit precision in format specifier overrules default precision.
if f.precPresent {
prec = f.prec
}
// Format number, reserving space for leading + sign if needed.
num := strconv.AppendFloat(f.intbuf[:1], v, byte(verb), prec, size)
if num[1] == '-' || num[1] == '+' {
num = num[1:]
} else {
num[0] = '+'
}
// f.space means to add a leading space instead of a "+" sign unless
// the sign is explicitly asked for by f.plus.
if f.space && num[0] == '+' && !f.plus {
num[0] = ' '
}
// Special handling for infinities and NaN,
// which don't look like a number so shouldn't be padded with zeros.
if num[1] == 'I' || num[1] == 'N' {
oldZero := f.zero
f.zero = false
// Remove sign before NaN if not asked for.
if num[1] == 'N' && !f.space && !f.plus {
num = num[1:]
}
f.pad(num)
f.zero = oldZero
return
}
// The sharp flag forces printing a decimal point for non-binary formats
// and retains trailing zeros, which we may need to restore.
if f.sharp && verb != 'b' {
digits := 0
switch verb {
case 'v', 'g', 'G':
digits = prec
// If no precision is set explicitly use a precision of 6.
if digits == -1 {
digits = 6
}
}
// Buffer pre-allocated with enough room for
// exponent notations of the form "e+123".
var tailBuf [5]byte
tail := tailBuf[:0]
hasDecimalPoint := false
// Starting from i = 1 to skip sign at num[0].
for i := 1; i < len(num); i++ {
switch num[i] {
case '.':
hasDecimalPoint = true
case 'e', 'E':
tail = append(tail, num[i:]...)
num = num[:i]
default:
digits--
}
}
if !hasDecimalPoint {
num = append(num, '.')
}
for digits > 0 {
num = append(num, '0')
digits--
}
num = append(num, tail...)
}
// We want a sign if asked for and if the sign is not positive.
if f.plus || num[0] != '+' {
// If we're zero padding to the left we want the sign before the leading zeros.
// Achieve this by writing the sign out and then padding the unsigned number.
if f.zero && f.widPresent && f.wid > len(num) {
f.buf.WriteByte(num[0])
f.writePadding(f.wid - len(num))
f.buf.Write(num[1:])
return
}
f.pad(num)
return
}
// No sign to show and the number is positive; just print the unsigned number.
f.pad(num[1:])
}

169
vendor/golang.org/x/text/message/message.go generated vendored Normal file
View file

@ -0,0 +1,169 @@
// Copyright 2015 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.
package message // import "golang.org/x/text/message"
import (
"io"
"os"
"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.
type Printer struct {
// Wrap the fields in a hidden type to hide some of the implemented methods.
printer printer
// 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.
}
type options struct {
cat *catalog.Catalog
// TODO:
// - allow %s to print integers in written form (tables are likely too large
// to enable this by default).
// - list behavior
//
}
// An Option defines an option of a Printer.
type Option func(o *options)
// Catalog defines the catalog to be used.
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,
}
for _, o := range opts {
o(options)
}
p := &Printer{printer{
tag: t,
}}
p.printer.toDecimal.InitDecimal(t)
p.printer.toScientific.InitScientific(t)
p.printer.catContext = options.cat.Context(t, &p.printer)
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()
}
// 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)
return int(n64), err
}
// Print is like fmt.Print, but using language-specific formatting.
func (p *Printer) Print(a ...interface{}) (n int, err error) {
return p.Fprint(os.Stdout, a...)
}
// 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()
}
// 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)
return int(n64), err
}
// Println is like fmt.Println, but using language-specific formatting.
func (p *Printer) Println(a ...interface{}) (n int, err error) {
return p.Fprintln(os.Stdout, a...)
}
// 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()
}
// 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())
}
// 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())
}
func lookupAndFormat(p *Printer, r Reference, a []interface{}) {
p.printer.reset()
p.printer.args = a
var id, msg string
switch v := r.(type) {
case string:
id, msg = v, v
case key:
id, msg = v.id, v.fallback
default:
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)
return
}
}
}
// 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]
}
return nil
}
// Render implements catmsg.Renderer.
func (p *printer) Render(msg string) {
p.doPrintf(msg)
}
// A Reference is a string or a message reference.
type Reference interface {
// TODO: also allow []string
}
// Key creates a message Reference for a message where the given id is used for
// message lookup and the fallback is returned when no matches are found.
func Key(id string, fallback string) Reference {
return key{id, fallback}
}
type key struct {
id, fallback string
}

181
vendor/golang.org/x/text/message/message_test.go generated vendored Normal file
View file

@ -0,0 +1,181 @@
// Copyright 2015 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.
package message
import (
"bytes"
"fmt"
"io"
"testing"
"golang.org/x/text/internal"
"golang.org/x/text/internal/format"
"golang.org/x/text/language"
"golang.org/x/text/message/catalog"
)
type formatFunc func(s fmt.State, v rune)
func (f formatFunc) Format(s fmt.State, v rune) { f(s, v) }
func TestBinding(t *testing.T) {
testCases := []struct {
tag string
value interface{}
want string
}{
{"en", 1, "1"},
{"en", "2", "2"},
{ // Language is passed.
"en",
formatFunc(func(fs fmt.State, v rune) {
s := fs.(format.State)
io.WriteString(s, s.Language().String())
}),
"en",
},
}
for i, tc := range testCases {
p := NewPrinter(language.MustParse(tc.tag))
if got := p.Sprint(tc.value); got != tc.want {
t.Errorf("%d:%s:Sprint(%v) = %q; want %q", i, tc.tag, tc.value, got, tc.want)
}
var buf bytes.Buffer
p.Fprint(&buf, tc.value)
if got := buf.String(); got != tc.want {
t.Errorf("%d:%s:Fprint(%v) = %q; want %q", i, tc.tag, tc.value, got, tc.want)
}
}
}
func TestLocalization(t *testing.T) {
type test struct {
tag string
key Reference
args []interface{}
want string
}
args := func(x ...interface{}) []interface{} { return x }
empty := []interface{}{}
joe := []interface{}{"Joe"}
joeAndMary := []interface{}{"Joe", "Mary"}
testCases := []struct {
desc string
cat []entry
test []test
}{{
desc: "empty",
test: []test{
{"en", "key", empty, "key"},
{"en", "", empty, ""},
{"nl", "", empty, ""},
},
}, {
desc: "hierarchical languages",
cat: []entry{
{"en", "hello %s", "Hello %s!"},
{"en-GB", "hello %s", "Hellø %s!"},
{"en-US", "hello %s", "Howdy %s!"},
{"en", "greetings %s and %s", "Greetings %s and %s!"},
},
test: []test{
{"und", "hello %s", joe, "hello Joe"},
{"nl", "hello %s", joe, "hello Joe"},
{"en", "hello %s", joe, "Hello Joe!"},
{"en-US", "hello %s", joe, "Howdy Joe!"},
{"en-GB", "hello %s", joe, "Hellø Joe!"},
{"en-oxendict", "hello %s", joe, "Hello Joe!"},
{"en-US-oxendict-u-ms-metric", "hello %s", joe, "Howdy Joe!"},
{"und", "greetings %s and %s", joeAndMary, "greetings Joe and Mary"},
{"nl", "greetings %s and %s", joeAndMary, "greetings Joe and Mary"},
{"en", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
{"en-US", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
{"en-GB", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
{"en-oxendict", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
{"en-US-oxendict-u-ms-metric", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
},
}, {
desc: "references",
cat: []entry{
{"en", "hello", "Hello!"},
},
test: []test{
{"en", "hello", empty, "Hello!"},
{"en", Key("hello", "fallback"), empty, "Hello!"},
{"en", Key("xxx", "fallback"), empty, "fallback"},
{"und", Key("hello", "fallback"), empty, "fallback"},
},
}, {
desc: "zero substitution", // work around limitation of fmt
cat: []entry{
{"en", "hello %s", "Hello!"},
{"en", "hi %s and %s", "Hello %[2]s!"},
},
test: []test{
{"en", "hello %s", joe, "Hello!"},
{"en", "hello %s", joeAndMary, "Hello!"},
{"en", "hi %s and %s", joeAndMary, "Hello Mary!"},
// The following tests resolve to the fallback string.
{"und", "hello", joeAndMary, "hello"},
{"und", "hello %%%%", joeAndMary, "hello %%"},
{"und", "hello %#%%4.2% ", joeAndMary, "hello %% "},
{"und", "hello %s", joeAndMary, "hello Joe%!(EXTRA string=Mary)"},
{"und", "hello %+%%s", joeAndMary, "hello %Joe%!(EXTRA string=Mary)"},
{"und", "hello %-42%%s ", joeAndMary, "hello %Joe %!(EXTRA string=Mary)"},
},
}, {
desc: "number formatting", // work around limitation of fmt
cat: []entry{
{"und", "files", "%d files left"},
{"und", "meters", "%.2f meters"},
{"de", "files", "%d Dateien übrig"},
},
test: []test{
{"en", "meters", args(3000.2), "3,000.20 meters"},
{"en-u-nu-gujr", "files", args(123456), "૧૨૩,૪૫૬ files left"},
{"de", "files", args(1234), "1.234 Dateien übrig"},
{"de-CH", "files", args(1234), "1234 Dateien übrig"},
{"de-CH-u-nu-mong", "files", args(1234), "᠑’᠒᠓᠔ Dateien übrig"},
},
}}
for _, tc := range testCases {
cat, _ := initCat(tc.cat)
for i, pt := range tc.test {
t.Run(fmt.Sprintf("%s:%d", tc.desc, i), func(t *testing.T) {
p := NewPrinter(language.MustParse(pt.tag), Catalog(cat))
if got := p.Sprintf(pt.key, pt.args...); got != pt.want {
t.Errorf("Sprintf(%q, %v) = %s; want %s",
pt.key, pt.args, got, pt.want)
return // Next error will likely be the same.
}
w := &bytes.Buffer{}
p.Fprintf(w, pt.key, pt.args...)
if got := w.String(); got != pt.want {
t.Errorf("Fprintf(%q, %v) = %s; want %s",
pt.key, pt.args, got, pt.want)
}
})
}
}
}
type entry struct{ tag, key, msg string }
func initCat(entries []entry) (*catalog.Catalog, []language.Tag) {
tags := []language.Tag{}
cat := catalog.New()
for _, e := range entries {
tag := language.MustParse(e.tag)
tags = append(tags, tag)
cat.SetString(tag, e.key, e.msg)
}
return cat, internal.UniqueTags(tags)
}

1194
vendor/golang.org/x/text/message/print.go generated vendored Normal file

File diff suppressed because it is too large Load diff