flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

[Python] Missing CreateSharedString()

Open CTVjweiss opened this issue 3 years ago • 1 comments

The Python builder does not have a CreateSharedString() method as is supported in other languages. For parity with other languages I believe this functionality should be added.

An implementation could look like:

def CreateSharedString(self, s, encoding='utf-8', errors='strict'):
    """CreateString, except if a string with the exact same contents has been serialized before,
    simply returns the offset of the existing string."""
    if not isinstance(s, compat.string_types):
        return TypeError("Only strings can be passed to CreateSharedString")

    if self.sharedStringMap == None:
        self.sharedStringMap = {}
        
    if s in self.sharedStringMap:
        return self.sharedStringMap[s]

    offset = self.CreateString(s)
    self.sharedStringMap[s] = offset
    return offset

CTVjweiss avatar May 03 '22 15:05 CTVjweiss

Feel free to submit a PR for this.

dbaileychess avatar May 11 '22 16:05 dbaileychess