Add CreateSharedString to python builder (#7608)

Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Michael Le
2022-10-28 17:28:35 -07:00
committed by GitHub
parent 728c033ad6
commit 051afd8825
2 changed files with 66 additions and 1 deletions

View File

@@ -112,7 +112,8 @@ class Builder(object):
## @cond FLATBUFFERS_INTENRAL
__slots__ = ("Bytes", "current_vtable", "head", "minalign", "objectEnd",
"vtables", "nested", "forceDefaults", "finished", "vectorNumElems")
"vtables", "nested", "forceDefaults", "finished", "vectorNumElems",
"sharedStrings")
"""Maximum buffer size constant, in bytes.
@@ -141,6 +142,7 @@ class Builder(object):
self.vtables = {}
self.nested = False
self.forceDefaults = False
self.sharedStrings = {}
## @endcond
self.finished = False
@@ -405,6 +407,20 @@ class Builder(object):
self.vectorNumElems = None
return self.Offset()
def CreateSharedString(self, s, encoding='utf-8', errors='strict'):
"""
CreateSharedString checks if the string is already written to the buffer
before calling CreateString.
"""
if s in self.sharedStrings:
return self.sharedStrings[s]
off = self.CreateString(s, encoding, errors)
self.sharedStrings[s] = off
return off
def CreateString(self, s, encoding='utf-8', errors='strict'):
"""CreateString writes a null-terminated byte string as a vector."""