google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library

Home Page:http://google.github.io/flatbuffers/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Python] Missing CreateSharedString()

CTVjweiss opened this issue · 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

Feel free to submit a PR for this.

ezoic increase your site revenue