MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / export_escaped_unquoted_to_binary_buffer

Function export_escaped_unquoted_to_binary_buffer

python/stringzilla.c:6645–6677  ·  view source on GitHub ↗

* @brief Exports a binary string to a buffer in Python bytes representation (b'\\x..'). * @param[in] data The binary data to export. * @param[in] data_length The length of the binary data. * @param[out] buffer The output buffer to write to. * @param[in] buffer_length The size of the output buffer. * @param[out] did_fit Populated with 1 if the data is fully exported, 0 if it didn't fit.

Source from the content-addressed store, hash-verified

6643 * @return Pointer to the end of the written data in the buffer.
6644 */
6645sz_cptr_t export_escaped_unquoted_to_binary_buffer(sz_cptr_t data, sz_size_t data_length, //
6646 sz_ptr_t buffer, sz_size_t buffer_length, //
6647 int *did_fit) {
6648 sz_ptr_t buffer_ptr = buffer;
6649 *did_fit = 1;
6650
6651 // First pass: calculate required buffer size
6652 // Format: b'\x00\x01...' -> 3 bytes prefix + 4 bytes per byte + 1 byte suffix
6653 sz_size_t required_bytes = 3 + (data_length * 4) + 1;
6654
6655 // Check if we have enough buffer space
6656 if (required_bytes > buffer_length) {
6657 *did_fit = 0;
6658 return buffer_ptr;
6659 }
6660
6661 // Second pass: write to buffer
6662 *(buffer_ptr++) = 'b';
6663 *(buffer_ptr++) = '\'';
6664
6665 // Export each byte as \x followed by two hex digits
6666 static const char hex_chars[] = "0123456789abcdef";
6667 for (sz_size_t i = 0; i < data_length; i++) {
6668 unsigned char byte = (unsigned char)data[i];
6669 *(buffer_ptr++) = '\\';
6670 *(buffer_ptr++) = 'x';
6671 *(buffer_ptr++) = hex_chars[byte >> 4];
6672 *(buffer_ptr++) = hex_chars[byte & 0x0f];
6673 }
6674
6675 *(buffer_ptr++) = '\'';
6676 return buffer_ptr;
6677}
6678
6679/**
6680 * @brief Formats an array of strings, similar to the `repr` method of Python lists.

Callers 2

Strs_reprFunction · 0.85
Strs_strFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…