Intern a string and return its index. Args: string: The string to intern Returns: int: The index of the string in the table
(self, string)
| 8 | self._string_to_index = {} |
| 9 | |
| 10 | def intern(self, string): |
| 11 | """Intern a string and return its index. |
| 12 | |
| 13 | Args: |
| 14 | string: The string to intern |
| 15 | |
| 16 | Returns: |
| 17 | int: The index of the string in the table |
| 18 | """ |
| 19 | if not isinstance(string, str): |
| 20 | string = str(string) |
| 21 | |
| 22 | if string in self._string_to_index: |
| 23 | return self._string_to_index[string] |
| 24 | |
| 25 | index = len(self._strings) |
| 26 | self._strings.append(string) |
| 27 | self._string_to_index[string] = index |
| 28 | return index |
| 29 | |
| 30 | def get_string(self, index): |
| 31 | """Get a string by its index. |