Skip to content
This repository was archived by the owner on Nov 21, 2023. It is now read-only.

Commit 541ddde

Browse files
Merge pull request #4 from goinsane/develop
v1.0.2
2 parents f430647 + 6b7ffaa commit 541ddde

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

erf.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (e *Erf) Unwrap() error {
3838

3939
// Format is implementation of fmt.Formatter.
4040
func (e *Erf) Format(f fmt.State, verb rune) {
41-
buf := bytes.NewBuffer(nil)
41+
buf := bytes.NewBuffer(make([]byte, 0, 4096))
4242
switch verb {
4343
case 's', 'v':
4444
if !f.Flag('+') {
@@ -95,10 +95,10 @@ func (e *Erf) Format(f fmt.State, verb rune) {
9595
}
9696
}
9797
buf.WriteRune('\n')
98+
default:
99+
return
98100
}
99-
if buf.Len() > 0 {
100-
_, _ = f.Write(buf.Bytes())
101-
}
101+
_, _ = f.Write(buf.Bytes())
102102
}
103103

104104
// Fmt returns the format argument of the formatting functions (Newf, Errorf or Wrap) that created Erf.

stack.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (c StackCaller) String() string {
1818

1919
// Format is implementation of fmt.Formatter.
2020
func (c StackCaller) Format(f fmt.State, verb rune) {
21-
buf := bytes.NewBuffer(nil)
21+
buf := bytes.NewBuffer(make([]byte, 0, 4096))
2222
switch verb {
2323
case 's', 'v':
2424
pad, wid, prec := byte('\t'), 0, 1
@@ -34,27 +34,32 @@ func (c StackCaller) Format(f fmt.State, verb rune) {
3434
}
3535
padding := bytes.Repeat([]byte{pad}, wid)
3636
indent := bytes.Repeat([]byte{pad}, prec)
37-
var str string
3837
buf.Write(padding)
39-
str = "???"
38+
fn := "???"
4039
if c.Function != "" {
41-
str = trimSrcPath(c.Function)
40+
fn = trimSrcPath(c.Function)
4241
}
43-
buf.WriteString(fmt.Sprintf("%s(%#x)", str, c.Entry))
42+
buf.WriteString(fmt.Sprintf("%s(%#x)", fn, c.Entry))
4443
if f.Flag('+') {
4544
buf.WriteRune('\n')
4645
buf.Write(padding)
4746
buf.Write(indent)
48-
str = trimSrcPath(c.File)
49-
if f.Flag('#') {
50-
str = trimDirs(str)
47+
file, line := "???", 0
48+
if c.File != "" {
49+
file = trimSrcPath(c.File)
50+
if f.Flag('#') {
51+
file = trimDirs(file)
52+
}
5153
}
52-
buf.WriteString(fmt.Sprintf("%s:%d +%#x", str, c.Line, c.PC-c.Entry))
54+
if c.Line > 0 {
55+
line = c.Line
56+
}
57+
buf.WriteString(fmt.Sprintf("%s:%d +%#x", file, line, c.PC-c.Entry))
5358
}
59+
default:
60+
return
5461
}
55-
if buf.Len() > 0 {
56-
_, _ = f.Write(buf.Bytes())
57-
}
62+
_, _ = f.Write(buf.Bytes())
5863
}
5964

6065
// StackTrace stores the information of stack trace.
@@ -107,7 +112,7 @@ func (t *StackTrace) String() string {
107112

108113
// Format is implementation of fmt.Formatter.
109114
func (t *StackTrace) Format(f fmt.State, verb rune) {
110-
buf := bytes.NewBuffer(nil)
115+
buf := bytes.NewBuffer(make([]byte, 0, 4096))
111116
switch verb {
112117
case 's', 'v':
113118
format := "%"
@@ -129,10 +134,10 @@ func (t *StackTrace) Format(f fmt.State, verb rune) {
129134
}
130135
buf.WriteString(fmt.Sprintf(format, c))
131136
}
137+
default:
138+
return
132139
}
133-
if buf.Len() > 0 {
134-
_, _ = f.Write(buf.Bytes())
135-
}
140+
_, _ = f.Write(buf.Bytes())
136141
}
137142

138143
// PC returns program counters.

0 commit comments

Comments
 (0)