develooper Front page | perl.perl5.porters | Postings from October 2016

Re: Encode.xs and sv_force_normal

Thread Previous | Thread Next
From:
Father Chrysostomos
Date:
October 23, 2016 18:36
Subject:
Re: Encode.xs and sv_force_normal
Message ID:
3EE630C7-DCB0-45C8-9DFA-1CBE10A24352@cpan.org

On Oct 22, 2016, at 4:16 AM, pali@cpan.org wrote:

> On Monday 17 October 2016 23:31:31 pali@cpan.org wrote:
> 
> Encode::_utf8_on(sv)
> 
>> @@ -972,12 +982,11 @@ SV *	sv
>> CODE:
>> {
>>     if (SvPOK(sv)) {
>> -    SV *rsv = newSViv(SvUTF8(sv));
>> -    RETVAL = rsv;
>> -    if (SvIsCOW(sv)) sv_force_normal(sv);
>> -    SvUTF8_on(sv);
>> +        if (SvTHINKFIRST(sv)) sv_force_normal(sv);
>> +        RETVAL = newSViv(SvUTF8(sv));
>> +        SvUTF8_on(sv);
>>     } else {
>> -    RETVAL = &PL_sv_undef;
>> +        RETVAL = &PL_sv_undef;
>>     }
>> }
>> OUTPUT:
> 
> Encode::_utf8_off(sv)
> 
>> @@ -989,12 +998,11 @@ SV *	sv
>> CODE:
>> {
>>     if (SvPOK(sv)) {
>> -    SV *rsv = newSViv(SvUTF8(sv));
>> -    RETVAL = rsv;
>> -    if (SvIsCOW(sv)) sv_force_normal(sv);
>> -    SvUTF8_off(sv);
>> +        if (SvTHINKFIRST(sv)) sv_force_normal(sv);
>> +        RETVAL = newSViv(SvUTF8(sv));
>> +        SvUTF8_off(sv);
>>     } else {
>> -    RETVAL = &PL_sv_undef;
>> +        RETVAL = &PL_sv_undef;
>>     }
>> }
>> OUTPUT:
> 
> I'm not sure if these two functions shouldn't call also SvGETMAGIC(sv)
> before SvPOK(sv) and also SvSETMAGIC(sv) after SvUTF8_off/on(sv) calls.

It depends on how they are intended to be used.  These are low-level functions for tinkering with the internal encoding of a string (certainly not for general use; in fact, I would argue that their main purpose is to work around bugs in XS code that does not handle utf8 properly).

I am not sure it makes sense to use these on tied variables; after all, the user has to know what it is in the SV to begin with for any use of Encode::_utf8_off to make sense, but a FETCH call (SvGETMAGIC) might actually change whether the string is flagged as utf8.

That said, if someone does it on $/ then I would expect it just to work, so SvGETMAGIC/SvSETMAGIC seem appropriate.

It may be worth adding a caveat to the documentation that they do not make much sense on tied variables, unless you think that the existing caveats are sufficient.



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About