Skip to content

smart_str: mention smart_str_extract() #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion Book/php7/internal_types/strings/smart_str.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ parameter you'll use::
/* Don't forget to release/free it */
smart_str_free(&my_str);

We can also use the embedded ``zend_string`` independently of the ``smart_str``::

smart_str my_str = {0};

smart_str_appends(&my_str, "Hello, you are using PHP version ");
smart_str_appends(&my_str, PHP_VERSION);

zend_string *str = smart_str_extract(my_str);
RETURN_STR(str);

/* We must not free my_str in this case */

``smart_str_extract()`` returns a pre-allocated empty string if ``smart_str.s``
is ``NULL``. Otherwise, it adds a trailing *NUL* byte and trims the allocated
memory to the string size.

We used here the simple API, the extended one ends with ``_ex()``, and allows you to tell if you want a persistent or
a request-bound allocation for the underlying ``zend_string``. Example::
Expand All @@ -92,7 +107,11 @@ smart_str specific tricks
* Never forget to finish your string with a call to ``smart_str_0()``. That puts a *NUL* char at the end of the embed
string and make it compatible with libc string functions.
* Never forget to free your string, with ``smart_str_free()``, once you're done with it.
* ``smart_str`` embeds a ``zend_string``, and then allows you to share that later elsewhere playing with its reference
* Use ``smart_str_extract()`` to get a standalone ``zend_string`` when you have
finished building the string. This takes care of calling ``smart_str_0()``,
and of optimizing allocations. In this case, calling ``smart_str_free()`` is
not necessary.
* You can share the standalone ``zend_string`` later elsewhere playing with its reference
counter. Please, visit the :doc:`zend_string dedicated chapter <zend_strings>` to know more about it.
* You can play with ``smart_str`` allocations. Look at ``smart_str_alloc()`` and friends.
* ``smart_str`` is heavily used into PHP's heart. For example, PHP's
Expand Down