Shlomi Fish avatar Shlomi Fish committed 1d3157f

->parse_string now accepts a reference to a string.

This is to avoid unnecessary copying.

Comments (0)

Files changed (4)

 Revision history for Perl extension XML::LibXML
 
+    - Make sure parse_string() and load_xml() also accept references to
+    strings (to avoid unnecessary copying).
+        - See: https://rt.cpan.org/Ticket/Display.html?id=64051
+
 1.97            Mon Apr 30 20:31:24 IDT 2012
     - Apply a test and a fix to correct keep_blanks having no effect on
     parse_balanced_chunk.
                 directory = NULL;
             }
         }
+        /* If string is a reference to a string - dereference it.
+         * See: https://rt.cpan.org/Ticket/Display.html?id=64051 . */
+        if (SvROK(string)) {
+            string = SvRV(string);
+        }
         ptr = SvPV(string, len);
         if (len <= 0) {
             croak("Empty string\n");
                                 <funcsynopsisinfo>$doc = $parser-&gt;parse_string( $xmlstring);</funcsynopsisinfo>
                             </funcsynopsis>
 
-                            <para>This function is similar to parse_fh(), but it parses an XML document that is available as a single string in memory. Again,
+                            <para>This function is similar to parse_fh(), but it parses an XML document that is available as a single string in memory, or alternatively as a reference to a scalar containing a string. Again,
                             you can pass an optional base URI to the function.</para>
 
-                            <programlisting>my $doc = $parser-&gt;parse_string( $xmlstring, $baseuri );</programlisting>
+                            <programlisting>my $doc = $parser-&gt;parse_string( $xmlstring, $baseuri );
+my $doc = $parser-&gt;parse_string(\$xmlstring, $baseuri);
+                            </programlisting>
                         </listitem>
                     </varlistentry>
 
 use warnings;
 
 # Should be 168.
-use Test::More tests => 167;
+use Test::More tests => 180;
 
 use XML::LibXML;
 use XML::LibXML::Common qw(:libxml);
 =end taken_out
 
 =cut
+    # TEST:$count=2;
+    # Also test that we can parse from scalar references:
+    # See RT #64051 ( https://rt.cpan.org/Ticket/Display.html?id=64051 )
+        foreach my $input ( $string5, \$string5 )
         {
-            my $doc2 = $parser2->parse_string($string5);
-            # TEST
+            my $doc2 = $parser2->parse_string($input);
+            # TEST*$count
             _count_tag_name($doc2, 'C:A', 1, q{3 C:As});
-            # TEST
+            # TEST*$count
             _count_tag_name($doc2, 'A', 3, q{3 As});
-            # TEST
+            # TEST*$count
             _count_elements_by_name_ns($doc2, ["*", "A"], 4,
                 q{4 Elements of A of any namespace}
             );
-            # TEST
+            # TEST*$count
             _count_elements_by_name_ns($doc2, ['*', '*'], 5,
                 q{4 Elements of any namespace},
             );
-            # TEST
+            # TEST*$count
             _count_elements_by_name_ns( $doc2, ["xml://D", "*" ], 2,
                 q{2 elements of any name in D}
             );
 
             my $A = $doc2->getDocumentElement;
-            # TEST
+            # TEST*$count
             _count_children_by_name($A, 'A', 1, q{1 A});
-            # TEST
+            # TEST*$count
             _count_children_by_name($A, 'C:A', 1, q{C:A});
-            # TEST
+            # TEST*$count
             _count_children_by_name($A, 'C:B', 0, q{No C:B children});
-            # TEST
+            # TEST*$count
             _count_children_by_name($A, "*", 2, q{2 Childern in $A in total});
-            # TEST
+            # TEST*$count
             _count_children_by_name_ns($A, ['*', 'A'], 2, 
                 q{2 As of any namespace});
-            # TEST
+            # TEST*$count
             _count_children_by_name_ns($A, [ "xml://D", "*" ], 1,
                 q{1 Child of D},
             );
-            # TEST
+            # TEST*$count
             _count_children_by_name_ns($A, [ "*", "*" ], 2,
                 q{2 Children in total},
             );
-            # TEST
+            # TEST*$count
             _count_children_by_local_name($A, 'A', 2, q{2 As});
         }
     }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.