Skip to content

Commit 3c57b60

Browse files
author
Julien Pauli
committed
refactored strings for smart_str
1 parent 6cb5d8b commit 3c57b60

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

Book/php7/internal_types.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Contents:
1111
:maxdepth: 2
1212

1313
internal_types/zvals.rst
14-
internal_types/zend_strings.rst
14+
internal_types/strings.rst
1515
internal_types/zend_resources.rst
1616

1717
..

Book/php7/internal_types/strings.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Strings management
2+
==================
3+
4+
Any program needs to manage strings. Managing is like allocating, searching, concatenating, extending, shrinking etc..
5+
6+
Many operations are needed with strings. Although the C standard library provides many functions for such a goal,
7+
C classical strings, aka ``char *`` (or ``char []``) are usually a little bit weak to use as-is in a strong program
8+
like PHP is.
9+
10+
Thus, PHP designed a layer on top of C strings : ``zend_strings``. Also, anoter API exists that implements common string
11+
operations both for C classical strings, or for ``zend_strings``: ``smart_str`` API.
12+
13+
.. toctree::
14+
:maxdepth: 2
15+
16+
strings/zend_strings.rst
17+
strings/smart_str.rst
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
smart_str API
2+
=============
3+
4+

Book/php7/internal_types/zend_strings.rst renamed to Book/php7/internal_types/strings/zend_strings.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ Here is the simple ``zend_string`` structure explosed::
2222
};
2323

2424
Like you can see, the structure embeds a ``zend_refcounted_h`` header. This is done for memory management and reference
25-
counting, as you may have learnt by reading the :doc:`./zvals/memory_management` chapter.
2625
As the string is very likely to be used as the key of a HashTable probe, it embeds its hash in the ``h`` field. This is
2726
an unsigned long ``zend_ulong``. This number is only used when the ``zend_string`` needs to be hashed, aka especially
28-
when used together with :doc:`./hashtables`; this is very likely though.
27+
when used together with :doc:`../hashtables`; this is very likely though.
2928

3029
As you know, the string knows its length as the ``len`` field, to support "binary strings". Binary strings are
3130
strings that embed one or several ``NUL`` characters (\\0). When passed to libc functions, those strings will get
@@ -44,7 +43,7 @@ This struct hack must be remembered, as the memory layout looks like with the C
4443
structure, and may be felt/seen when using a C debugger (or when debugging strings). This hack is entirely managed by
4544
the API you'll use when manipulating ``zend_string`` structures.
4645

47-
.. image:: ./zend_strings/images/zend_string_memory_layout.png
46+
.. image:: images/zend_string_memory_layout.png
4847
:align: center
4948

5049
Using zend_string API
@@ -53,7 +52,7 @@ Using zend_string API
5352
Simple use case
5453
***************
5554

56-
Like with :doc:`zvals`, you dont manipulate the ``zend_string`` internals fields by hand, but always use macros
55+
Like with :doc:`../zvals`, you dont manipulate the ``zend_string`` internals fields by hand, but always use macros
5756
for that. There also exists macros to trigger actions on strings. Those are not functions but macros, all stored into
5857
the required `Zend/zend_string.h <https://github.com/php/php-src/blob/PHP-7.0/Zend/zend_string.h>`_ header::
5958

@@ -216,7 +215,7 @@ zend_string access with zvals
216215
Now that you know how to manage and manipulate ``zend_string``, let's see the interaction they got with the ``zval``
217216
container.
218217

219-
.. note:: You need to be familiar with zvals, if not, read the :doc:`./zvals` dedicated chapter.
218+
.. note:: You need to be familiar with zvals, if not, read the :doc:`../zvals` dedicated chapter.
220219

221220
The macros will allow you to store a ``zend_string`` into a ``zval``, or to read the ``zend_string`` from a ``zval``::
222221

@@ -333,7 +332,7 @@ Example::
333332
/* At the end of the process, PHP will purge its interned
334333
string buffer, and thus free() our "foo" string itself */
335334

336-
It's all about garbage collection you've learnt about in the :doc:`./zvals/memory_management` chapter.
335+
It's all about garbage collection you've learnt about in the :doc:`../zvals/memory_management` chapter.
337336

338337
When a string is interned, its GC flags are changed to add the ``IS_STR_INTERNED`` flag, whatever the memory allocation
339338
class they use (permanent or request based).

0 commit comments

Comments
 (0)