mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 07:11:38 +00:00
[Go] Implements a SharedStrings function (#5733)
* Adds the sharedstring implementation for go * Reimplemented testcase according to request
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
7cdfc8475e
commit
a593a11e59
@@ -17,6 +17,8 @@ type Builder struct {
|
|||||||
head UOffsetT
|
head UOffsetT
|
||||||
nested bool
|
nested bool
|
||||||
finished bool
|
finished bool
|
||||||
|
|
||||||
|
sharedStrings map[string]UOffsetT
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileIdentifierLength = 4
|
const fileIdentifierLength = 4
|
||||||
@@ -33,7 +35,6 @@ func NewBuilder(initialSize int) *Builder {
|
|||||||
b.head = UOffsetT(initialSize)
|
b.head = UOffsetT(initialSize)
|
||||||
b.minalign = 1
|
b.minalign = 1
|
||||||
b.vtables = make([]UOffsetT, 0, 16) // sensible default capacity
|
b.vtables = make([]UOffsetT, 0, 16) // sensible default capacity
|
||||||
|
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,6 +308,20 @@ func (b *Builder) EndVector(vectorNumElems int) UOffsetT {
|
|||||||
return b.Offset()
|
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.
|
// CreateString writes a null-terminated string as a vector.
|
||||||
func (b *Builder) CreateString(s string) UOffsetT {
|
func (b *Builder) CreateString(s string) UOffsetT {
|
||||||
b.assertNotNested()
|
b.assertNotNested()
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
"testing/quick"
|
||||||
|
|
||||||
flatbuffers "github.com/google/flatbuffers/go"
|
flatbuffers "github.com/google/flatbuffers/go"
|
||||||
)
|
)
|
||||||
@@ -77,6 +78,7 @@ func TestAll(t *testing.T) {
|
|||||||
CheckByteStringIsNestedError(t.Fatalf)
|
CheckByteStringIsNestedError(t.Fatalf)
|
||||||
CheckStructIsNotInlineError(t.Fatalf)
|
CheckStructIsNotInlineError(t.Fatalf)
|
||||||
CheckFinishedBytesError(t.Fatalf)
|
CheckFinishedBytesError(t.Fatalf)
|
||||||
|
CheckSharedStrings(t.Fatalf)
|
||||||
|
|
||||||
// Verify that GetRootAs works for non-root tables
|
// Verify that GetRootAs works for non-root tables
|
||||||
CheckGetRootAsForNonRootTable(t.Fatalf)
|
CheckGetRootAsForNonRootTable(t.Fatalf)
|
||||||
@@ -1375,6 +1377,29 @@ func CheckStringIsNestedError(fail func(string, ...interface{})) {
|
|||||||
b.CreateString("foo")
|
b.CreateString("foo")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CheckSharedStrings(fail func(string, ...interface{})) {
|
||||||
|
f := func(strings []string) bool {
|
||||||
|
b := flatbuffers.NewBuilder(0)
|
||||||
|
for _, s1 := range strings {
|
||||||
|
for _, s2 := range strings {
|
||||||
|
off1 := b.CreateSharedString(s1)
|
||||||
|
off2 := b.CreateSharedString(s2)
|
||||||
|
|
||||||
|
if (s1 == s2) && (off1 != off2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (s1 != s2) && (off1 == off2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if err := quick.Check(f, nil); err != nil {
|
||||||
|
fail("expected same offset")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CheckByteStringIsNestedError verifies that a bytestring can not be created
|
// CheckByteStringIsNestedError verifies that a bytestring can not be created
|
||||||
// inside another object.
|
// inside another object.
|
||||||
func CheckByteStringIsNestedError(fail func(string, ...interface{})) {
|
func CheckByteStringIsNestedError(fail func(string, ...interface{})) {
|
||||||
|
|||||||
Reference in New Issue
Block a user