Skip to content

Commit

Permalink
address #46942: encodeToUTF8 and decodeFromUTF8 now accept empty stri…
Browse files Browse the repository at this point in the history
…ngs and undef (returning the same)
  • Loading branch information
pajas committed Sep 24, 2009
1 parent bdd07f7 commit 017fa64
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
14 changes: 11 additions & 3 deletions LibXML.xs
Expand Up @@ -9127,6 +9127,11 @@ encodeToUTF8( encoding, string )
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 ); */
Expand Down Expand Up @@ -9210,10 +9215,13 @@ decodeFromUTF8( encoding, string )
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 ) {
Expand Down
11 changes: 10 additions & 1 deletion t/42common.t
@@ -1,7 +1,7 @@
#########################

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";
Expand Down Expand Up @@ -47,6 +47,15 @@ eval {
};
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 :(

Expand Down

0 comments on commit 017fa64

Please sign in to comment.