From 4b375b390bb10a94098b4753e6ea65140617db2d Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Mon, 31 Aug 2020 17:11:04 -0700 Subject: [PATCH] fix(Dgraph): Replace fmt.Sprintf with strconv.FormatUint int outputrdf.go (#6310) --- query/outputrdf.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/query/outputrdf.go b/query/outputrdf.go index 6fc364aaeec..9f3b84c60aa 100644 --- a/query/outputrdf.go +++ b/query/outputrdf.go @@ -113,7 +113,7 @@ func (b *rdfBuilder) rdfForSubgraph(sg *SubGraph) error { func (b *rdfBuilder) writeRDF(subject uint64, predicate []byte, object []byte) { // add subject - b.writeTriple([]byte(fmt.Sprintf("%#x", subject))) + x.Check2(b.buf.Write(toHex(subject))) x.Check(b.buf.WriteByte(' ')) // add predicate b.writeTriple(predicate) @@ -149,19 +149,14 @@ func (b *rdfBuilder) rdfForUIDList(subject uint64, list *pb.List, sg *SubGraph) continue } // Build object. - b.writeRDF( - subject, - []byte(sg.fieldName()), - buildTriple([]byte(fmt.Sprintf("%#x", destUID)))) + b.writeRDF(subject, []byte(sg.fieldName()), toHex(destUID)) } } // rdfForValueList returns rdf for the value list. func (b *rdfBuilder) rdfForValueList(subject uint64, valueList *pb.ValueList, attr string) { if attr == "uid" { - b.writeRDF(subject, - []byte(attr), - buildTriple([]byte(fmt.Sprintf("%#x", subject)))) + b.writeRDF(subject, []byte(attr), toHex(subject)) return } for _, destValue := range valueList.Values { @@ -234,3 +229,16 @@ func quotedNumber(val []byte) []byte { tmpVal = append(tmpVal, '"') return tmpVal } + +func toHex(uid uint64) []byte { + var buf [16]byte + tmp := strconv.AppendUint(buf[:0], uid, 16) + + out := make([]byte, len(tmp)+3+1) + out[0] = '<' + out[1] = '0' + out[2] = 'x' + n := copy(out[3:], tmp) + out[3+n] = '>' + return out +}