svn: /phpdoc/en/trunk/internals2/variables/ index.xml
| From: | Joe Watkins | Date: | Thu, 11 Apr 2013 09:37:58 +0000 |
| Subject: | svn: /phpdoc/en/trunk/internals2/variables/ index.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-11197@lists.php.net to get a copy of this message | ||
krakjoe Thu, 11 Apr 2013 09:37:58 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=330073
Log:
some hashtable traversal stuff
Changed paths:
U phpdoc/en/trunk/internals2/variables/index.xml
Modified: phpdoc/en/trunk/internals2/variables/index.xml
===================================================================
--- phpdoc/en/trunk/internals2/variables/index.xml 2013-04-11 09:29:12 UTC (rev 330072)
+++ phpdoc/en/trunk/internals2/variables/index.xml 2013-04-11 09:37:58 UTC (rev 330073)
@@ -622,6 +622,10 @@
<para>Unless otherwise stated, these functions all return <code>FAILURE</code>
if the requested opeation fails for any reason, otherwise <code>SUCCESS</code> is
returned.</para>
+ <note>
+ <para>It is important to remember that ordinarily <code>HashTable</code> API
functions expect key lengths to be the length of the null terminated string, including the null
termination, in other words they expect <code>strlen(key) + 1</code></para>
+ </note>
+
<table xml:id="internals2.variables.advrrays.api">
<title>HashTable API</title>
<tgroup cols="1">
@@ -685,8 +689,99 @@
</caution>
</section>
- <!-- traversal here -->
+ <para>Traversing the HashTable is often necessary, to do this you provide a pointer to a
<code>HashTable</code>, with an optional <code>HashPosition</code> - a
structure internal to the HashTable API that allows traversal not to affect the internal position of
HashTable. The following traversal functions are provided, and an example usage found
below.</para>
+ <table xml:id="internals2.variables.trarrays.api">
+ <title>HashTable Traversal API</title>
+ <tgroup cols="1">
+ <tbody>
+ <row>
+ <entry><code>int zend_hash_internal_pointer_reset(HashTable*
ht)</code></entry>
+ </row>
+ <row>
+ <entry>resets the internal pointer of <code>ht</code> to the
start</entry>
+ </row>
+ <row>
+ <entry><code>int zend_hash_internal_pointer_reset_ex(HashTable* ht, HashPosition
position)</code></entry>
+ </row>
+ <row>
+ <entry>sets <code>position</code> the the start of
<code>ht</code></entry>
+ </row>
+ <row>
+ <entry><code>int zend_hash_get_current_data(HashTable* ht, void*
data)</code></entry>
+ </row>
+ <row>
+ <entry>gets the data at the current position in <code>ht</code>,
<code>data</code> should be cast to <code>void**</code>, ie:
<code>(void**) &data</code></entry>
+ </row>
+ <row>
+ <entry><code>int zend_hash_get_current_data_ex(HashTable* ht, void* data,
HashPosition position)</code></entry>
+ </row>
+ <row>
+ <entry>sets <code>data</code> to the data at
<code>position</code> in <code>ht</code></entry>
+ </row>
+ <row>
+ <entry><code>int zend_hash_get_current_key(HashTable* ht, void* data, char**key,
uint klen, ulong index, zend_bool duplicate)</code></entry>
+ </row>
+ <row>
+ <entry>sets <code>key</code>, <code>klen</code>, and
<code>index</code> from the key information at the current position. The possible return
values HASH_KEY_IS_STRING and HASH_KEY_IS_LONG are indicative of the kind of key found at the
current posision.</entry>
+ </row>
+ <row>
+ <entry><code>int zend_hash_get_current_key_ex(HashTable* ht, void* data,
char**key, uint klen, ulong index, zend_bool duplicate, HashPosition
position)</code></entry>
+ </row>
+ <row>
+ <entry>sets <code>key</code>, <code>klen</code>, and
<code>index</code> from the key information at <code>position</code>. The
possible return values HASH_KEY_IS_STRING and HASH_KEY_IS_LONG are indicative of the kind of key
found at <code>position</code>.</entry>
+ </row>
+ <row>
+ <entry><code>int zend_hash_move_forward(HashTable* ht)</code></entry>
+ </row>
+ <row>
+ <entry>moves the internal pointer of <code>ht</code> to the next entry in
<code>ht</code></entry>
+ </row>
+ <row>
+ <entry><code>int zend_hash_move_forward_ex(HashTable* ht, HashPosition
position)</code></entry>
+ </row>
+ <row>
+ <entry>moves <code>position</code> to the next entry in
<code>ht</code> </entry>
+ </row>
+ </tbody>
+ </tgroup>
+ </table>
+
+ <para>The functions above allow traversal over a <code>HashTable</code> to look
much like a normal loop, which will look something like:</para>
+
+ <screen>
+<![CDATA[
+HashPosition position;
+zval **data = NULL;
+
+for (zend_hash_internal_pointer_reset_ex(ht, &position);
+ zend_hash_get_current_data_ex(ht, (void**) &data, &position) == SUCCESS;
+ zend_hash_move_forward_ex(ht, &position)) {
+
+ /* by now we have data set and can use Z_ macros for accessing type and variable data */
+
+ char *key = NULL;
+ uint klen;
+ ulong index;
+
+ if (zend_hash_get_current_key_ex(ht, &key, &klen, &index, 0, &position) ==
HASH_KEY_IS_STRING) {
+ /* the key is a string, key and klen will be set */
+ } else {
+ /* we assume the key to be long, index will be set */
+ }
+}
+]]>
+ </screen>
+
+ <note>
+ <para>If a <code>HashTable</code> has been passed into the engine, it is a good
idea to use the zend_hash_*_ex API to avoid unexpected behaviour in userland.</para>
+ </note>
+
+ <note>
+ <para>If a <code>duplicate</code> of the key is requested and
<code>HAS_KEY_IS_STRING</code> is returned the caller must
<code>efree</code> the duplicated key</para>
+ </note>
+
+
<!-- copying/merging/cleaning here -->
<section xml:id="internals2.variables.objects">