Module bytes_like

Module bytes_like 

Source
Expand description

Bytes-like (Bytes, String) implementation for the storage traits.

§Storage Layout

Bytes-like types use Solidity-compatible: Short strings (≤31 bytes) are stored inline in a single slot:

  • Bytes 0..len: UTF-8 string data (left-aligned)
  • Byte 31 (LSB): length * 2 (bit 0 = 0 indicates short string)

Long strings (≥32 bytes) use keccak256-based storage:

  • Base slot: stores length * 2 + 1 (bit 0 = 1 indicates long string)
  • Data slots: stored at keccak256(main_slot) + i for each 32-byte chunk

Functions§

calc_chunks 🔒
Compute the number of 32-byte chunks needed to store a byte string.
calc_data_slot 🔒
Compute the storage slot where long string data begins.
calc_string_length 🔒
Extract the string length from a storage slot value.
delete_bytes_like 🔒
Generic delete implementation for byte-like types (String, Bytes) using Solidity’s encoding.
encode_long_string_length 🔒
Encode the length metadata for a long string (≥32 bytes).
encode_short_string 🔒
Encode a short string (≤31 bytes) into a U256 for inline storage.
is_long_string 🔒
Check if a storage slot value represents a long string.
load_bytes_like 🔒
Generic load implementation for string-like types (String, Bytes) using Solidity’s encoding.
store_bytes_like 🔒
Generic store implementation for byte-like types (String, Bytes) using Solidity’s encoding.