/doc/core/: document zend_string APIs - #21642
LamentXU123 wants to merge 7 commits into
Conversation
LamentXU123
left a comment
There was a problem hiding this comment.
Just commenting for easier reviewing
| - - Function/Macro [#persistent]_ | ||
| - Description | ||
|
|
||
| - - ``ZSTR_INIT(s, p)`` |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Just to make things more accurate here
iluuu1994
left a comment
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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``. |
There was a problem hiding this comment.
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_stringallocation.
|
|
||
| - 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. |
There was a problem hiding this comment.
| 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. |
| - 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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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``. |
There was a problem hiding this comment.
| 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 |
There was a problem hiding this comment.
| 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. |
| - - ``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. |
There was a problem hiding this comment.
This does not explain the difference to zend_string_equals(). zend_string_equals() includes a fast pointer equality test.
|
Thanks for reviewing! I am far from a native English user so yeah some wording might be bad. |
This PR added (and also fixed) some internal APIs concerning zend_string, cc @iluuu1994 as we privately talk about the doc work before.