diff options
Diffstat (limited to 'vendor/golang.org/x/text/message/format.go')
-rw-r--r-- | vendor/golang.org/x/text/message/format.go | 150 |
1 files changed, 64 insertions, 86 deletions
diff --git a/vendor/golang.org/x/text/message/format.go b/vendor/golang.org/x/text/message/format.go index d3340d194..a47d17dd4 100644 --- a/vendor/golang.org/x/text/message/format.go +++ b/vendor/golang.org/x/text/message/format.go @@ -8,6 +8,8 @@ import ( "bytes" "strconv" "unicode/utf8" + + "golang.org/x/text/internal/format" ) const ( @@ -20,45 +22,21 @@ const ( 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 + format.Parser // 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.ClearFlags() f.buf = buf - f.clearflags() } // writePadding generates n bytes of padding. @@ -69,7 +47,7 @@ func (f *formatInfo) writePadding(n int) { f.buf.Grow(n) // Decide which byte the padding should be filled with. padByte := byte(' ') - if f.zero { + if f.Zero { padByte = byte('0') } // Fill padding with padByte. @@ -80,12 +58,12 @@ func (f *formatInfo) writePadding(n int) { // 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 { + if !f.WidthPresent || f.Width == 0 { f.buf.Write(b) return } - width := f.wid - utf8.RuneCount(b) - if !f.minus { + width := f.Width - utf8.RuneCount(b) + if !f.Minus { // left padding f.writePadding(width) f.buf.Write(b) @@ -98,12 +76,12 @@ func (f *formatInfo) pad(b []byte) { // 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 { + if !f.WidthPresent || f.Width == 0 { f.buf.WriteString(s) return } - width := f.wid - utf8.RuneCountInString(s) - if !f.minus { + width := f.Width - utf8.RuneCountInString(s) + if !f.Minus { // left padding f.writePadding(width) f.buf.WriteString(s) @@ -131,8 +109,8 @@ func (f *formatInfo) fmt_unicode(u uint64) { // 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 + 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) { @@ -144,7 +122,7 @@ func (f *formatInfo) fmt_unicode(u uint64) { 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)) { + if f.Sharp && u <= utf8.MaxRune && strconv.IsPrint(rune(u)) { i-- buf[i] = '\'' i -= utf8.RuneLen(rune(u)) @@ -176,10 +154,10 @@ func (f *formatInfo) fmt_unicode(u uint64) { i-- buf[i] = 'U' - oldZero := f.zero - f.zero = false + oldZero := f.Zero + f.Zero = false f.pad(buf[i:]) - f.zero = oldZero + f.Zero = oldZero } // fmt_integer formats signed and unsigned integers. @@ -192,9 +170,9 @@ func (f *formatInfo) fmt_integer(u uint64, base int, isSigned bool, digits strin 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 { + if f.WidthPresent || 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. + width := 3 + f.Width + f.Prec // wid and prec are always positive. if width > len(buf) { // We're going to need a bigger boat. buf = make([]byte, width) @@ -205,19 +183,19 @@ func (f *formatInfo) fmt_integer(u uint64, base int, isSigned bool, digits strin // 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 + 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 + oldZero := f.Zero + f.Zero = false + f.writePadding(f.Width) + f.Zero = oldZero return } - } else if f.zero && f.widPresent { - prec = f.wid - if negative || f.plus || f.space { + } else if f.Zero && f.WidthPresent { + prec = f.Width + if negative || f.Plus || f.Space { prec-- // leave room for sign } } @@ -265,7 +243,7 @@ func (f *formatInfo) fmt_integer(u uint64, base int, isSigned bool, digits strin } // Various prefixes: 0x, -, etc. - if f.sharp { + if f.Sharp { switch base { case 8: if buf[i] != '0' { @@ -284,26 +262,26 @@ func (f *formatInfo) fmt_integer(u uint64, base int, isSigned bool, digits strin if negative { i-- buf[i] = '-' - } else if f.plus { + } else if f.Plus { i-- buf[i] = '+' - } else if f.space { + } 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 + oldZero := f.Zero + f.Zero = false f.pad(buf[i:]) - f.zero = oldZero + 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 + if f.PrecPresent { + n := f.Prec for i := range s { n-- if n < 0 { @@ -328,46 +306,46 @@ func (f *formatInfo) fmt_sbx(s string, b []byte, digits string) { length = len(s) } // Set length to not process more bytes than the precision demands. - if f.precPresent && f.prec < length { - length = f.prec + 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 { + if f.Space { // Each element encoded by two hexadecimals will get a leading 0x or 0X. - if f.sharp { + if f.Sharp { width *= 2 } // Elements will be separated by a space. width += length - 1 - } else if f.sharp { + } 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) + if f.WidthPresent { + f.writePadding(f.Width) } return } // Handle padding to the left. - if f.widPresent && f.wid > width && !f.minus { - f.writePadding(f.wid - width) + if f.WidthPresent && f.Width > width && !f.Minus { + f.writePadding(f.Width - width) } // Write the encoding directly into the output buffer. buf := f.buf - if f.sharp { + 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 { + if f.Space && i > 0 { // Separate elements with a space. buf.WriteByte(' ') - if f.sharp { + if f.Sharp { // Add leading 0x or 0X for each element. buf.WriteByte('0') buf.WriteByte(digits[16]) @@ -383,8 +361,8 @@ func (f *formatInfo) fmt_sbx(s string, b []byte, digits string) { buf.WriteByte(digits[c&0xF]) } // Handle padding to the right. - if f.widPresent && f.wid > width && f.minus { - f.writePadding(f.wid - width) + if f.WidthPresent && f.Width > width && f.Minus { + f.writePadding(f.Width - width) } } @@ -403,12 +381,12 @@ func (f *formatInfo) fmt_bx(b []byte, digits string) { // 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) { + if f.Sharp && strconv.CanBackquote(s) { f.padString("`" + s + "`") return } buf := f.intbuf[:0] - if f.plus { + if f.Plus { f.pad(strconv.AppendQuoteToASCII(buf, s)) } else { f.pad(strconv.AppendQuote(buf, s)) @@ -435,7 +413,7 @@ func (f *formatInfo) fmt_qc(c uint64) { r = utf8.RuneError } buf := f.intbuf[:0] - if f.plus { + if f.Plus { f.pad(strconv.AppendQuoteRuneToASCII(buf, r)) } else { f.pad(strconv.AppendQuoteRune(buf, r)) @@ -446,8 +424,8 @@ func (f *formatInfo) fmt_qc(c uint64) { // 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 + 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) @@ -458,25 +436,25 @@ func (f *formatInfo) fmt_float(v float64, size int, verb rune, prec int) { } // 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 { + 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 + oldZero := f.Zero + f.Zero = false // Remove sign before NaN if not asked for. - if num[1] == 'N' && !f.space && !f.plus { + if num[1] == 'N' && !f.Space && !f.Plus { num = num[1:] } f.pad(num) - f.zero = oldZero + 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' { + if f.Sharp && verb != 'b' { digits := 0 switch verb { case 'v', 'g', 'G': @@ -515,12 +493,12 @@ func (f *formatInfo) fmt_float(v float64, size int, verb rune, prec int) { num = append(num, tail...) } // We want a sign if asked for and if the sign is not positive. - if f.plus || num[0] != '+' { + 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) { + if f.Zero && f.WidthPresent && f.Width > len(num) { f.buf.WriteByte(num[0]) - f.writePadding(f.wid - len(num)) + f.writePadding(f.Width - len(num)) f.buf.Write(num[1:]) return } |