Exemple #1
0
 public state(fmt.State State = default, bytes.Buffer buf = default, bool printDetail = default, bool inDetail = default, bool needColon = default, bool needNewline = default)
 {
     this.State       = State;
     this.buf         = buf;
     this.printDetail = printDetail;
     this.inDetail    = inDetail;
     this.needColon   = needColon;
     this.needNewline = needNewline;
 }
Exemple #2
0
                        // FormatError calls the FormatError method of f with an errors.Printer
                        // configured according to s and verb, and writes the result to s.
                        public static void FormatError(Formatter f, fmt.State s, int verb)
                        {
                            // Assuming this function is only called from the Format method, and given
                            // that FormatError takes precedence over Format, it cannot be called from
                            // any package that supports errors.Formatter. It is therefore safe to
                            // disregard that State may be a specific printer implementation and use one
                            // of our choice instead.

                            // limitations: does not support printing error as Go struct.

                            @string sep = " ";            ptr <state> p = addr(new state(State: s));            var direct = true;

                            error err = error.As(f) !;

                            switch (verb)
                            {
                            // Note that this switch must match the preference order
                            // for ordinary string printing (%#v before %+v, and so on).

                            case 'v':
                                if (s.Flag('#'))
                                {
                                    {
                                        fmt.GoStringer(stringer, ok) = err._ <fmt.GoStringer>();

                                        if (ok)
                                        {
                                            io.WriteString(_addr_p.buf, stringer.GoString());
                                            goto exit;
                                        }
                                    }
                                    // proceed as if it were %v
                                }
                                else if (s.Flag('+'))
                                {
                                    p.printDetail = true;
                                    sep           = "\n  - ";
                                }
                                break;

                            case 's':
                                break;

                            case 'q':
                            // Use an intermediate buffer in the rare cases that precision,
                            // truncation, or one of the alternative verbs (q, x, and X) are
                            // specified.

                            case 'x':
                            // Use an intermediate buffer in the rare cases that precision,
                            // truncation, or one of the alternative verbs (q, x, and X) are
                            // specified.

                            case 'X':
                                // Use an intermediate buffer in the rare cases that precision,
                                // truncation, or one of the alternative verbs (q, x, and X) are
                                // specified.
                                direct = false;
                                break;

                            default:
                                p.buf.WriteString("%!");
                                p.buf.WriteRune(verb);
                                p.buf.WriteByte('(');

                                if (err != null)
                                {
                                    p.buf.WriteString(reflect.TypeOf(f).String());
                                }
                                else
                                {
                                    p.buf.WriteString("<nil>");
                                }
                                p.buf.WriteByte(')');
                                io.Copy(s, _addr_p.buf);
                                return;

                                break;
                            }

loop:

                            while (true)
                            {
                                switch (err.type())
                                {
                                case Formatter v:
                                    err = error.As(v.FormatError((printer.val)(p))) !;
                                    break;

                                case fmt.Formatter v:
                                    v.Format(p, 'v');
                                    _breakloop = true;
                                    break;
                                    break;

                                default:
                                {
                                    var v = err.type();
                                    io.WriteString(_addr_p.buf, v.Error());
                                    _breakloop = true;
                                    break;
                                    break;
                                }
                                }
                                if (err == null)
                                {
                                    break;
                                }
                                if (p.needColon || !p.printDetail)
                                {
                                    p.buf.WriteByte(':');
                                    p.needColon = false;
                                }
                                p.buf.WriteString(sep);
                                p.inDetail    = false;
                                p.needNewline = false;
                            }
exit:
                            var(width, okW) = s.Width();
                            var(prec, okP)  = s.Precision();

                            if (!direct || (okW && width > 0L) || okP)
                            {
                                // Construct format string from State s.
                                byte format = new slice <byte>(new byte[] { '%' });
                                if (s.Flag('-'))
                                {
                                    format = append(format, '-');
                                }
                                if (s.Flag('+'))
                                {
                                    format = append(format, '+');
                                }
                                if (s.Flag(' '))
                                {
                                    format = append(format, ' ');
                                }
                                if (okW)
                                {
                                    format = strconv.AppendInt(format, int64(width), 10L);
                                }
                                if (okP)
                                {
                                    format = append(format, '.');
                                    format = strconv.AppendInt(format, int64(prec), 10L);
                                }
                                format = append(format, string(verb));
                                fmt.Fprintf(s, string(format), p.buf.String());
                            }
                            else
                            {
                                io.Copy(s, _addr_p.buf);
                            }
                        }