PCRE2_SERIALIZE_ENCODE(3)

πŸŽ€ NAME πŸŽ€

pcre2_serialize_encode β€” okay bestie, this is PCRE2 - Perl-compatible regular expressions (revised API), straight out of the Library Functions Manual. It’s the function that turns a list of compiled patterns into a byte stream you can save. Iconic storage behavior, fr fr. πŸ’–


✨ SYNOPSIS ✨

#include <pcre2.h>

int32_t pcre2_serialize_encode(const pcre2_code **codes,
  int32_t number_of_codes, uint8_t **serialized_bytes,
  PCRE2_SIZE *serialized_size, pcre2_general_context *gcontext);

πŸ’… DESCRIPTION πŸ’…

Okay so this function encodes a list of compiled patterns into a byte stream that can be saved on disc or elsewhere. Cute for storage, but read this next part twice: this is not an abstract format like Java or .NET. Conversion of the byte stream back into usable compiled patterns can only happen on a host that is running the same version of PCRE2, with the same code unit width, and the host must also have the same endianness, pointer width and PCRE2_SIZE type. No cap. 😭

The arguments for pcre2_serialize_encode() are lowkey the whole plot:

  • codes β€” pointer to a vector containing the list.
  • number_of_codes β€” number of slots in the vector.
  • serialized_bytes β€” set to point to the serialized byte stream.
  • serialized_size β€” set to the number of bytes in the byte stream.
  • gcontext β€” pointer to a general context or NULL.

The context argument is used to obtain memory for the byte stream. When the serialized data is no longer needed, it must be freed by calling pcre2_serialize_free(). The yield of the function is the number of serialized patterns, or one of these negative error codes, which are giving β€œsomething went wrong” energy:

  • PCRE2_ERROR_BADDATA β€” number_of_codes is zero or less.
  • PCRE2_ERROR_BADMAGIC β€” mismatch of id bytes in one of the patterns.
  • PCRE2_ERROR_MEMORY β€” memory allocation failed.
  • PCRE2_ERROR_MIXEDTABLES β€” the patterns do not all use the same tables.
  • PCRE2_ERROR_NULL β€” an argument other than gcontext is NULL.

PCRE2_ERROR_BADMAGIC means either that a pattern’s code has been corrupted, or that a slot in the vector does not point to a compiled pattern. So like, check your pointers and your vibes. πŸ’€

There is a complete description of the PCRE2 native API in the pcre2api page and a description of the serialization functions in the pcre2serialize page. Go read those for the full lore. πŸ¦‹


πŸ’– That’s the tea from PCRE2 10.47, dated 13 August 2018. Periodt. πŸŽ€

πŸ“¦ where this page comes from