@brief Store a string in the buffer, which can contain any binary data. If a string with this exact contents has already been serialized before, instead simply returns the offset of the existing string. This uses a map stored on the heap, but only stores the numerical offsets. @param[in] str A const char pointer to the data to be stored as a string. @param[in] len The number of bytes that should b
| 1681 | /// @param[in] len The number of bytes that should be stored from `str`. |
| 1682 | /// @return Returns the offset in the buffer where the string starts. |
| 1683 | Offset<String> CreateSharedString(const char *str, size_t len) { |
| 1684 | FLATBUFFERS_ASSERT(FLATBUFFERS_GENERAL_HEAP_ALLOC_OK); |
| 1685 | if (!string_pool) |
| 1686 | string_pool = new StringOffsetMap(StringOffsetCompare(buf_)); |
| 1687 | auto size_before_string = buf_.size(); |
| 1688 | // Must first serialize the string, since the set is all offsets into |
| 1689 | // buffer. |
| 1690 | auto off = CreateString(str, len); |
| 1691 | auto it = string_pool->find(off); |
| 1692 | // If it exists we reuse existing serialized data! |
| 1693 | if (it != string_pool->end()) { |
| 1694 | // We can remove the string we serialized. |
| 1695 | buf_.pop(buf_.size() - size_before_string); |
| 1696 | return *it; |
| 1697 | } |
| 1698 | // Record this string for future use. |
| 1699 | string_pool->insert(off); |
| 1700 | return off; |
| 1701 | } |
| 1702 | |
| 1703 | #ifdef FLATBUFFERS_HAS_STRING_VIEW |
| 1704 | /// @brief Store a string in the buffer, which can contain any binary data. |
nothing calls this directly
no test coverage detected