Subroutine for writing strings or binary data. Returns the address of the string in the output (not the Value; the raw string itself), which can be used until the enoding is over. (Unless the string is written inline, or directly to file, in which case NULL is returned.)
| 318 | // output (not the Value; the raw string itself), which can be used until the enoding is over. |
| 319 | // (Unless the string is written inline, or directly to file, in which case NULL is returned.) |
| 320 | const void* Encoder::writeData(tags tag, slice s) { |
| 321 | byte *buf; |
| 322 | if (s.size < kNarrow) { |
| 323 | // Tiny data (0 or 1 byte) fits inline: |
| 324 | buf = placeValue<true>(tag, byte(s.size), 1 + s.size); |
| 325 | buf[1] = s.size > 0 ? s[0] : 0; |
| 326 | buf = nullptr; // this string is ephemeral |
| 327 | } else { |
| 328 | // Large data doesn't: |
| 329 | size_t bufLen = 1 + s.size; |
| 330 | if (s.size >= 0x0F) |
| 331 | bufLen += SizeOfVarInt(s.size); |
| 332 | buf = placeValue<false>(tag, 0, bufLen); |
| 333 | if (s.size < 0x0F) { |
| 334 | *buf++ |= byte(s.size); |
| 335 | } else { |
| 336 | *buf++ |= 0x0F; |
| 337 | buf += PutUVarInt(buf, s.size); |
| 338 | } |
| 339 | memcpy(buf, s.buf, s.size); |
| 340 | if (_out.outputFile()) |
| 341 | buf = nullptr; // ephemeral if writing to file |
| 342 | } |
| 343 | return buf; |
| 344 | } |
| 345 | |
| 346 | // Writes a string, or a pointer to an already-written copy of the same string. |
| 347 | // This is the main body of writeString() and writeKey(). |
nothing calls this directly
no test coverage detected