I really like the ideas shared here. It's a thing of consideration that array-functions should also work with strings. Maybe this would be the way to go, but I'm more excited about the OOP implementation of TextIterator and ByteIterator, which solves the whole problem at once (and is easier to implement, as mentioned by Stas). As Jonathan said, Database results with a certain encoding could get iterated, too. The only way to workaround the Text/Byte problem would be, offsetting >EVERY< string with 1-2 byte "string-type" information or an additional type flag in the zval-strcuture. Handling everything with zval's instead of objects would have the advantage, that database-layers like mysqlnd could write the database-encoding directly into the zval and the user had no need to decide what encoding is used.
A new casting operator (binary) could then cast the string to a 1-byte array. But this is syntactical sugar over OOP-implementations - I don't know which one is the better choice. For example: $utf8_string = "Jägermeister"; // information of utf8 ist stored in the zval foreach ($utf8_string as $k => $v) // would iterate in byte mode foreach ((binary)$utf8_string as $k => $v) // would iterate in text mode over this: $utf8_obj = new ByteIterator("Jägermeister"); foreach ($utf8_obj as $k => $v) foreach ($utf8_obj->toText() as $k => $v) I think the first one is easier and would be nicer to average developers (and lazy programmers like me ;o) ) Todd, I don't like neither str_split() nor text_string_to_array(). Sure, str_split could be optimized to return a different more optimized result inside of foreach() but I would use rather one of the implementations, mentioned above. 2011/6/20 Todd Ruth <tr...@proposaltech.com> > On Mon, 2011-06-20 at 10:06 -0700, Stas Malyshev wrote: > > Hi! > > > > On 6/20/11 9:57 AM, Todd Ruth wrote: > > > Iterators are nice. Having a "text_string_to_array" function > > > would also be fine. For example: > > > > > > $s = 'hello'; > > > foreach (text_string_to_array($s) as $x) { > > > var_dump($x); > > > } > > > > > > text_to_array($s) == str_split($s, 1) > > Does that have approximately the same performance as marking the > string as being OK to use as an array? For example, > > $s = file_get_contents($big_file); > foreach (str_split($s, 1) as $x) { > f($x); > } > > Are there performance issues with the above compared to: > > $s = file_get_contents($big_file); > foreach (text_string_to_array($s) as $x) { > f($x); > } > > assuming text_string_to_array could be implemented as marking > the string OK to use as an array. > > Again, I don't know enough about the internals. I'm just imagining > a significant difference for very long strings between: > $a1 = text_to_array('hello'); > and > $a2 = array('h','e','l','l','o'); > > $a1 and $a2 could act identically until a set occurred. For example, > "$a1['key'] = 5;" would first trigger $a1 becoming just like $a2 so > that the set could take place. > > Any string that has not been hit with text_string_to_array would lead > to all the usual error messages some of us know and love and > any string that has been hit with text_string_to_array would allow all > the fancy features some people are seeking. I'm trying to find a > way to please the people that want strings to act like arrays without > ruining the day for those of us who are glad strings don't act like > arrays. > > - Todd > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >