Skip to content

/doc/core/: document zend_string APIs - #21642

Open
LamentXU123 wants to merge 7 commits into
php:masterfrom
LamentXU123:doc-1
Open

LamentXU123 wants to merge 7 commits into
php:masterfrom
LamentXU123:doc-1

Conversation

@LamentXU123

@LamentXU123 LamentXU123 commented Apr 5, 2026 •

Copy link
Copy Markdown
Member

This PR added (and also fixed) some internal APIs concerning zend_string, cc @iluuu1994 as we privately talk about the doc work before.

@LamentXU123 LamentXU123 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just commenting for easier reviewing

- - Function/Macro [#persistent]_
- Description

- - ``ZSTR_INIT(s, p)``

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the original doc is wrong. This should be ZSTR_INIT_LITERAL. Is it?

- Same as ``zend_string_concat2``, but for three character buffers.

- - ``ZSTR_EMPTY_ALLOC()``
- Gets an immutable, empty string. This does not allocate memory.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return would be better in case of Get in functions

- Decreases the reference count and frees the string if it goes to 0.

- - ``zend_string_dup(s, p)``
- Creates a true copy of the string in a new allocation, except if the string is interned.

@LamentXU123 LamentXU123 Apr 5, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this function related to reference counting? Shouldn't it be in the "create/allocate" section?


- - ``ZSTR_HASH``
- ``Z_STRHASH[_P]``
- Computes the string has if it hasn't already been, and returns it.

@LamentXU123 LamentXU123 Apr 5, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this fix is right, but the original one is wrong.

API
*****

The string API is defined in ``Zend/zend_string.h``. It provides a number of functions for creating

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It do provide other functions except just for creating new strings?

hashing, and interning helpers.

.. list-table:: ``zend_string`` creation
.. list-table:: Creation and allocation APIs

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make things more accurate here

Comment thread docs/source/core/data-structures/zend_string.rst

@iluuu1994 iluuu1994 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I wonder of how much use listing all functions and macros is. I think the docs should mainly focus on explaining concepts and core functions. Once you grasp that, you'll understand all the variations.

If we want to document these functions, maybe we should see if we can document them in-source instead and export the synopses somehow?

Anyway, I don't object but I think the wording could be improved.

Also: Yes, I started reviewing this in April. 😄

The string API is defined in ``Zend/zend_string.h``. It provides a number of functions for creating
new strings.
The string API is defined in ``Zend/zend_string.h``. It contains creation, resizing, comparison,
hashing, and interning helpers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd avoid the term helper, it's not clearly defined. Please keep the term function. E.g.:

It provides functions for creating, resizing, comparing, hashing and interning strings.

- Allocates a new string of length ``l`` without initializing its contents.

- - ``zend_string_safe_alloc(n, m, l, p)``
- Allocates ``n * m + l`` bytes of payload with overflow checks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Payload seems like the wrong term, it implies data rather than storage. "A string of length n * m + l" might work better.

memory.

- - ``ZSTR_MAX_OVERHEAD``
- Maximum allocator/header overhead used by ``zend_string``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to document this macro? It's barely used and the description isn't very good. Alternatively:

Maximum number of bytes added to some raw string buffer to form for a zend_string allocation.


- Changes the size of the string. If the string has a reference count greater than 1 or if
the string is interned, a new string is created. You must always use the return value of
this function, as the original array may have been moved to a new location in memory.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this function, as the original array may have been moved to a new location in memory.
this function, as the original string may have been moved to a new location in memory.

Comment on lines +126 to +127
- Resizes a string to ``n * m + l`` bytes with overflow checks. Allocates a string of length
``n * m + l``. This function is commonly useful for encoding changes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is odd wording, with two conflicting sentences.

also returns the original string. This makes code more readable in practice.

- - ``zend_string_delref(s)``
- Decrements the reference count of a non-interned string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should clarify to only use this when you're certain that the refcount >1, which is rare.

- Creates a true copy of the string in a new allocation, except if the string is interned.
- - ``zend_string_release_ex(s, p)``
- Like ``zend_string_release()``, but allows you to specify whether the passed string is
persistent or non-persistent. If it is persistent, ``p`` should be ``0``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
persistent or non-persistent. If it is persistent, ``p`` should be ``0``.
persistent or non-persistent. If it is persistent, ``p`` should be ``false``.


- - ``zend_string_realloc(s, l, p)``
- Frees a non-interned string directly. The caller must ensure it is no longer shared.
Requires refcount 1 or immutable.You should avoid using these functions, as it is easy to

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Requires refcount 1 or immutable.You should avoid using these functions, as it is easy to
Requires refcount 1 or immutable. You should avoid using these functions, as it is easy to

- Frees a non-interned string directly. The caller must ensure it is no longer shared.
Requires refcount 1 or immutable.You should avoid using these functions, as it is easy to
introduce critical bugs when some API changes from returning new strings to reusing
existing ones.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awkward wording.

- - ``zend_string_equals(s1, s2)``
- Full equality check for two ``zend_string`` values.
- - ``zend_string_equal_content(s1, s2)``
- Full equality check assuming both arguments are ``zend_string`` pointers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not explain the difference to zend_string_equals(). zend_string_equals() includes a fast pointer equality test.

@LamentXU123

Copy link
Copy Markdown
Member Author

Thanks for reviewing! I am far from a native English user so yeah some wording might be bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants