From e8d5a6f479876a5dcc9dca1ccd9282abb8459003 Mon Sep 17 00:00:00 2001 From: huzedong1 Date: Wed, 15 Mar 2023 14:52:19 +0800 Subject: [PATCH] perf: use strings.Builder in GoString --- errors.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/errors.go b/errors.go index deaded8..193e89b 100644 --- a/errors.go +++ b/errors.go @@ -98,16 +98,23 @@ func (ve *ValidationError) Error() string { return fmt.Sprintf("jsonschema: %s does not validate with %s: %s", quote(leaf.InstanceLocation), u+"#"+leaf.KeywordLocation, leaf.Message) } -func (ve *ValidationError) GoString() string { +func (ve *ValidationError) goStringTo(tgt *strings.Builder, indent int) { sloc := ve.AbsoluteKeywordLocation sloc = sloc[strings.IndexByte(sloc, '#')+1:] - msg := fmt.Sprintf("[I#%s] [S#%s] %s", ve.InstanceLocation, sloc, ve.Message) + for i := 0; i < indent; i++ { + tgt.WriteByte(' ') + } + tgt.WriteString(fmt.Sprintf("[I#%s] [S#%s] %s", ve.InstanceLocation, sloc, ve.Message)) for _, c := range ve.Causes { - for _, line := range strings.Split(c.GoString(), "\n") { - msg += "\n " + line - } + tgt.WriteByte('\n') + c.goStringTo(tgt, indent+2) } - return msg +} + +func (ve *ValidationError) GoString() string { + var msgBuf strings.Builder + ve.goStringTo(&msgBuf, 0) + return msgBuf.String() } func joinPtr(ptr1, ptr2 string) string {