Re: Unicode support for *printf()

From: Date: Fri, 15 Dec 2006 19:21:23 +0000
Subject: Re: Unicode support for *printf()
Groups: php.internals 
Request: Send a blank email to internals+get-26977@lists.php.net to get a copy of this message
I know we had discussion about *fprintf() on IRC, but I'm still not clear on some stuff. Sara, could you please explain again why UG(unicode) should not be used as the selection for php_formatted_print/php_u_formatted_print? I bet a few of us are hazy on the streams Unicode/binary details, so let's please clarify this and make sure we're on the same page. UG(unicode) only determines what kind of data the runtime should give back to the script. But we're not giving any data to the scipt here, we're giving data to a stream. Whether this stream expects unicode or binary data has nothing to do with the setting of u.s, I could quite easily run the following in non-unicode semantics mode:
$fp = fopen("somefile.txt", "w"); stream_encoding($fp, "utf8"); fwrite($fp, u"\C{SNOWMAN}"); Or this in unicode.semantics mode: $fp = fopen("somefile.bin", "wb"); fwrite($fp, b"\xFF\xFE"); Let's look at how fwrite() works (maxchars logic stripped out for simplicity): if (Z_TYPE_P(zstring) == IS_UNICODE) { ret = php_stream_write_unicode(stream, Z_USTRVAL_P(zstring), write_len); } else { convert_to_string(zstring); ret = php_stream_write(stream, Z_STRVAL_P(zstring), write_len); } Here, we rely on the user to know what kind of data they should be pushing at the stream. If they push unicode, it's written as unicode, if they push binary, it's written as binary. Sending the wrong type is dealt with by the streams layer, potentially raising an error. What I was proposing for (v)fprintf(), since they involve multiple parameters, was to use the format specifier as the type hinter. If that arg is unicode, then generate the string as a whole as unicode, if that arg is binary, then generate the string as a whole as binary. Of course , it'd be even more comprehensive to do smaller writes as the string is processed (dispatching to write() or write_unicode() as determined by the arg), but that's going I bit far in my oppinion. This keeps the responsibility (and the flexibility) of generating and sending the proper types on the script author, where it belongs. -Sara

« previous php.internals (#26977) next »