From d1142d3549731282ac9ed5f1dc7acdcff9290dca Mon Sep 17 00:00:00 2001 From: Vladimir Varankin Date: Tue, 7 Mar 2017 12:49:04 +0300 Subject: [PATCH] avoid data convertion during encodig --- gen/decoder.go | 4 ++-- jlexer/lexer.go | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/gen/decoder.go b/gen/decoder.go index 82558a7..80f8d2c 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -68,8 +68,8 @@ func (g *Generator) genTypeDecoder(t reflect.Type, out string, tags fieldTags, i unmarshalerIface = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() if reflect.PtrTo(t).Implements(unmarshalerIface) { - fmt.Fprintln(g.out, ws+"if data := in.UnsafeString(); in.Ok() {") - fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalText([]byte(data)) )") + fmt.Fprintln(g.out, ws+"if data := in.UnsafeBytes(); in.Ok() {") + fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalText(data) )") fmt.Fprintln(g.out, ws+"}") return nil } diff --git a/jlexer/lexer.go b/jlexer/lexer.go index ae8344e..d4c16f1 100644 --- a/jlexer/lexer.go +++ b/jlexer/lexer.go @@ -618,6 +618,12 @@ func (r *Lexer) UnsafeString() string { return ret } +// UnsafeBytes returns the byte slice if the token is a string literal. +func (r *Lexer) UnsafeBytes() []byte { + _, ret := r.unsafeString() + return ret +} + // String reads a string literal. func (r *Lexer) String() string { if r.token.kind == tokenUndef && r.Ok() {