MCPcopy Create free account
hub / github.com/couchbase/fleece / _writeString

Method _writeString

Fleece/Core/Encoder.cc:349–386  ·  view source on GitHub ↗

Writes a string, or a pointer to an already-written copy of the same string. This is the main body of writeString() and writeKey(). Returns the address where s got written to, if possible, just like writeData above.

Source from the content-addressed store, hash-verified

347 // This is the main body of writeString() and writeKey().
348 // Returns the address where s got written to, if possible, just like writeData above.
349 const void* Encoder::_writeString(slice s) {
350 if (!_usuallyTrue(_uniqueStrings && s.size >= kNarrow && s.size <= kMaxSharedStringSize)) {
351 // Not uniquing this string, so just write it:
352 return writeData(kStringTag, s);
353 }
354
355 // Check whether this string's already been written:
356 StringTable::entry_t *entry;
357 bool isNew;
358 std::tie(entry, isNew) = _strings.insert(s, 0);
359 if (!isNew) {
360 // String exists: Write pointer to it, as long as the offset's not too large:
361 ssize_t offset = entry->second - _base.size;
362 if (_items->wide || nextWritePos() - offset <= Pointer::kMaxNarrowOffset - 32) {
363 writePointer(offset);
364 if (offset < 0) {
365 const void *stringVal = &_base[_base.size + offset];
366 if (stringVal < _baseMinUsed)
367 _baseMinUsed = stringVal;
368 }
369#ifndef NDEBUG
370 _numSavedStrings++;
371#endif
372 return entry->first.buf; // done!
373 }
374 }
375
376 // Write the string to the output:
377 auto offset = _base.size + nextWritePos();
378 throwIf(offset > 1u<<31, MemoryError, "encoded data too large");
379 writeData(kStringTag, s);
380
381 // Store a copy of the string, since _out won't necessarily keep it around (if it's
382 // writing to a file, or if the caller calls snip()), and the offset:
383 const void* writtenStr = _stringStorage.write(s);
384 *entry = {{writtenStr, s.size}, (uint32_t)offset};
385 return writtenStr;
386 }
387
388 // Adds a preexisting string to the cache
389 void Encoder::cacheString(slice s, size_t offsetInBase) {

Callers

nothing calls this directly

Calls 2

insertMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected