Commits

Anonymous committed f1e349a

address #46942: encodeToUTF8 and decodeFromUTF8 now accept empty strings and undef (returning the same)

  • Participants
  • Parent commits ef7ec82

Comments (0)

Files changed (2)

         xmlCharEncodingHandlerPtr coder = NULL;
 	PREINIT_SAVED_ERROR
     CODE:
+        if (!SvOK(string)) {
+            XSRETURN_UNDEF;
+        } else if (!SvCUR(string)) {
+            XSRETURN_PV("");
+        }
         realstring = (xmlChar*) SvPV(string, len);
         if ( realstring != NULL ) {
             /* warn("encode %s", realstring ); */
 	PREINIT_SAVED_ERROR
     CODE: 
 #ifdef HAVE_UTF8
-        if ( !SvUTF8(string) ) {
+        if ( !SvOK(string) ) {
+            XSRETURN_UNDEF;
+        } else if (!SvCUR(string)) {
+            XSRETURN_PV("");
+        } else if ( !SvUTF8(string) ) {
             croak("string is not utf8!!");
-        }
-        else {
+        } else {
 #endif
             realstring = (xmlChar*) SvPV(string, len);
             if ( realstring != NULL ) {

File t/42common.t

 #########################
 
 use Test;
-BEGIN { plan tests => 8 };
+BEGIN { plan tests => 12 };
 use XML::LibXML::Common qw( :libxml :encoding );
 
 use constant TEST_STRING_GER => "H�nsel und Gretel";
 };
 ok( length( $@ ) );
 
+ok((eval { encodeToUTF8( 'UTF-16' , '' ) eq '' ? 1 : 0 }) ==1);
+print $@ if $@;
+ok(eval { defined(encodeToUTF8( 'UTF-16' , undef )) ? 0 : 1 }==1);
+
+ok(eval { decodeFromUTF8( 'UTF-16' , '' ) eq '' ? 1 : 0 }==1);
+print $@ if $@;
+ok(eval { defined(decodeFromUTF8( 'UTF-16' , undef )) ? 0 : 1 }==1);
+print $@ if $@;
+
 # here should be a test to test badly encoded strings. but for some
 # reasons i am unable to create an apropriate test :(