From 017fa6495eafd6cc45b0b53a79c5dd88cf185e04 Mon Sep 17 00:00:00 2001 From: pajas Date: Thu, 24 Sep 2009 16:29:20 +0000 Subject: [PATCH] address #46942: encodeToUTF8 and decodeFromUTF8 now accept empty strings and undef (returning the same) --- LibXML.xs | 14 +++++++++++--- t/42common.t | 11 ++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/LibXML.xs b/LibXML.xs index dc72e51..5c80d9f 100644 --- a/LibXML.xs +++ b/LibXML.xs @@ -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 ); */ @@ -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 ) { diff --git a/t/42common.t b/t/42common.t index 92734df..e53cc54 100644 --- a/t/42common.t +++ b/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"; @@ -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 :(