# TextIterator changes

- Thread: https://externals.io/message/24200
- Original archive: https://news-web.php.net/php.internals/24200
- Started by Andrei Zmievski on 2006-06-23T21:00:57+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #24200 — Andrei Zmievski — 2006-06-23T21:00:57+00:00

URL: https://externals.io/message/24200#24200

I am working on implementing BreakIterator API [1]. I considered two 
approaches: making a separate class or merging the API into the 
existing TextIterator. Having a separate class would be a bit cleaner, 
but I can see people wanting to use it in foreach(), and since 
TextIterator already provides a lot of BreakIterator's functionality, I 
decided that merging would be the best option. However, there is an 
overlap between the BreakIterator API and the current TextIterator one, 
so there will have to be some changes.

1. TextIterator::current() signature will change from:

    mixed `current()`

to:

    mixed current(integer &$offset)

in order to support BreakIterator's functionality of returning offset 
in `current()`.

2. TextIterator::next() will return the offset of the next boundary 
instead of returning nothing.

3. TextIterator::rewind() will be renamed to TextIterator::first() to 
conform to BreakIterator's first()/last() API.

So this is heads up. Let me know if you have a problem with this.

-A

[1] http://icu.sourceforge.net/apiref/icu4c/ubrk_8h.html

---

## #24201 — Michael Wallner — 2006-06-23T21:16:10+00:00

URL: https://externals.io/message/24200#24201 · In reply to: #24200

Andrei Zmievski wrote:
> I am working on implementing BreakIterator API [1]. I considered two 
> approaches: making a separate class or merging the API into the existing 
> TextIterator. Having a separate class would be a bit cleaner, but I can 
> see people wanting to use it in foreach(), and since TextIterator 
> already provides a lot of BreakIterator's functionality, I decided that 
> merging would be the best option. However, there is an overlap between 
> the BreakIterator API and the current TextIterator one, so there will 
> have to be some changes.
> 
> 1. TextIterator::current() signature will change from:
> 
>    mixed `current()`
> 
> to:
> 
>    mixed current(integer &$offset)

This will raise the same issue as we have/had with the reflection API:
mike@honeybadger:~/build/php-unicode-debug$ cli -r 'interface i{function f();} class c implements i{function f($a){}}'

Fatal error: Declaration of c::f() must be compatible with that of i::f() in Command line code on line 1


Nah, don't look at me--I don't like that either.


> 3. TextIterator::rewind() will be renamed to TextIterator::first() to 
> conform to BreakIterator's first()/last() API.
> 
> So this is heads up. Let me know if you have a problem with this.

Huh? Rename or alias? It can't implement Iterator if there's no `rewind()` method.

Regards,
-- 
Michael

---

## #24202 — Andrei Zmievski — 2006-06-23T21:20:28+00:00

URL: https://externals.io/message/24200#24202 · In reply to: #24201

TextIterator does not implement Iterator interface, only Traversable. 
It just happens to have functions of the same name as Iterator.

-Andrei



> Andrei Zmievski wrote:
> > […]
>
> This will raise the same issue as we have/had with the reflection API:
> mike@honeybadger:~/build/php-unicode-debug$ cli -r 'interface 
> i{function f();} class c implements i{function f($a){}}'
>
> Fatal error: Declaration of c::f() must be compatible with that of 
> i::f() in Command line code on line 1
>
>
> Nah, don't look at me--I don't like that either.
>
>
> > […]
>
> Huh? Rename or alias? It can't implement Iterator if there's no 
> `rewind()` method.
>
> Regards,
> -- 
> Michael

---

## #24203 — Michael Wallner — 2006-06-23T21:27:10+00:00

URL: https://externals.io/message/24200#24203 · In reply to: #24202

Andrei Zmievski wrote:
> TextIterator does not implement Iterator interface, only Traversable. It 
> just happens to have functions of the same name as Iterator.

Ah, okay.
That leaves the picky OO strictness I personally don't like.
I think we should give PHP some freedom back in this area.


Regards,
-- 
Michael

---

## #24204 — Andrei Zmievski — 2006-06-23T21:57:18+00:00

URL: https://externals.io/message/24200#24204 · In reply to: #24200

Sean (on IRC) convinced me that something called *Iterator had better 
implement Iterator interface (which TextIterator currently does not). 
So changing method signatures is out of the question. Towards that, the 
current functions will stay as they are, but I'll have to add 
current_offset() (for getting the offset of the current element as 
opposed to the element itself), and alias `rewind()` to first().

-Andrei



> I am working on implementing BreakIterator API [1]. I considered two 
> approaches: making a separate class or merging the API into the 
> existing TextIterator. Having a separate class would be a bit cleaner, 
> but I can see people wanting to use it in foreach(), and since 
> TextIterator already provides a lot of BreakIterator's functionality, 
> I decided that merging would be the best option. However, there is an 
> overlap between the BreakIterator API and the current TextIterator 
> one, so there will have to be some changes.
>
> 1. TextIterator::current() signature will change from:
>
>    mixed `current()`
>
> to:
>
>    mixed current(integer &$offset)
>
> in order to support BreakIterator's functionality of returning offset 
> in `current()`.
>
> 2. TextIterator::next() will return the offset of the next boundary 
> instead of returning nothing.
>
> 3. TextIterator::rewind() will be renamed to TextIterator::first() to 
> conform to BreakIterator's first()/last() API.
>
> So this is heads up. Let me know if you have a problem with this.
>
> -A
>
> [1] http://icu.sourceforge.net/apiref/icu4c/ubrk_8h.html

---

## #24206 — Marcus Boerger — 2006-06-24T09:35:17+00:00

URL: https://externals.io/message/24200#24206 · In reply to: #24202

Hello Andrei,

  you should reconsider giving it full iterator interface. Adding more
functionality is of course quite easy and no problem at all. Sharing an
interface also makes the whole thing feel much better as in no conflicts
as in consistent. Actually that's what interfaces are for.

Friday, June 23, 2006, 11:20:28 PM, you wrote:

> TextIterator does not implement Iterator interface, only Traversable. 
> It just happens to have functions of the same name as Iterator.

> -Andrei

> 

> > […]

Best regards,
 Marcus

---

## #24208 — Andrei Zmievski — 2006-06-24T17:24:30+00:00

URL: https://externals.io/message/24200#24208 · In reply to: #24206

Thanks, I know what they are for. I remember discussing Iterator vs.  
Traversable with you on IRC, and for some reason we settled on  
Traversable. Anyway, I'm changing it now.

-Andrei




> Hello Andrei,
>
>   you should reconsider giving it full iterator interface. Adding more
> functionality is of course quite easy and no problem at all.  
> Sharing an
> interface also makes the whole thing feel much better as in no  
> conflicts
> as in consistent. Actually that's what interfaces are for.
>
> Friday, June 23, 2006, 11:20:28 PM, you wrote:
>
> > […]
>
> > […]
>
> > […]
>
> > […]
>
> Best regards,
>  Marcus
