[Go] Implements a SharedStrings function (#5733)

* Adds the sharedstring implementation for go

* Reimplemented testcase according to request
This commit is contained in:
mustiikhalil
2020-01-24 20:57:34 +03:00
committed by Wouter van Oortmerssen
parent 7cdfc8475e
commit a593a11e59
2 changed files with 42 additions and 2 deletions

View File

@@ -17,6 +17,8 @@ type Builder struct {
head UOffsetT
nested bool
finished bool
sharedStrings map[string]UOffsetT
}
const fileIdentifierLength = 4
@@ -33,7 +35,6 @@ func NewBuilder(initialSize int) *Builder {
b.head = UOffsetT(initialSize)
b.minalign = 1
b.vtables = make([]UOffsetT, 0, 16) // sensible default capacity
return b
}
@@ -307,6 +308,20 @@ func (b *Builder) EndVector(vectorNumElems int) UOffsetT {
return b.Offset()
}
// CreateSharedString Checks if the string is already written
// to the buffer before calling CreateString
func (b *Builder) CreateSharedString(s string) UOffsetT {
if b.sharedStrings == nil {
b.sharedStrings = make(map[string]UOffsetT)
}
if v, ok := b.sharedStrings[s]; ok {
return v
}
off := b.CreateString(s)
b.sharedStrings[s] = off
return off
}
// CreateString writes a null-terminated string as a vector.
func (b *Builder) CreateString(s string) UOffsetT {
b.assertNotNested()