| 188 | } |
| 189 | |
| 190 | void MarisaDict::LoadFromMappedBuffer() { |
| 191 | try { |
| 192 | internal->marisa->map(internal->mappedBuffer.data(), |
| 193 | internal->mappedBuffer.size()); |
| 194 | } catch (const std::exception& e) { |
| 195 | throw InvalidFormat(std::string("Invalid OpenCC Marisa dictionary: ") + |
| 196 | e.what()); |
| 197 | } |
| 198 | |
| 199 | const size_t trieSize = internal->marisa->io_size(); |
| 200 | if (trieSize > internal->mappedBuffer.size()) { |
| 201 | throw InvalidFormat( |
| 202 | "Invalid OpenCC Marisa dictionary (trie exceeds file size)"); |
| 203 | } |
| 204 | |
| 205 | size_t valuesBytesRead = 0; |
| 206 | std::shared_ptr<SerializedValues> serialized_values = |
| 207 | SerializedValues::NewFromBuffer( |
| 208 | internal->mappedBuffer.data() + trieSize, |
| 209 | internal->mappedBuffer.size() - trieSize, &valuesBytesRead); |
| 210 | valuesLexicon = serialized_values->GetLexicon(); |
| 211 | // Validate key count consistency |
| 212 | size_t numKeys = internal->marisa->num_keys(); |
| 213 | if (numKeys != valuesLexicon->Length()) { |
| 214 | throw InvalidFormat( |
| 215 | "Invalid OpenCC Marisa dictionary (key count mismatch)"); |
| 216 | } |
| 217 | maxLength = 128; |
| 218 | lexiconReconstructed.store(false, std::memory_order_release); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | MarisaDictPtr MarisaDict::NewFromDict(const Dict& thatDict) { |