zephir icon indicating copy to clipboard operation
zephir copied to clipboard

For loop, key on traversing string

Open mruz opened this issue 9 years ago • 4 comments

It's not possible to move key during the loop on the string variable;

    public function filter(string str)
    {
        string output = "";
        char c;
        int i;

        for i, c in str {
            if c == '\n' {
                let i++;
            }
            let output .= c;
        }
    }
for (_0 = 0; _0 < Z_STRLEN_P(str); _0++) {
    i = _0; 
    c = ZEPHIR_STRING_OFFSET(str, _0);

is it possible to get access to _0 variable?

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/7049207-for-loop-key-on-traversing-string?utm_campaign=plugin&utm_content=tracker%2F280146&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F280146&utm_medium=issues&utm_source=github).

mruz avatar Dec 16 '14 10:12 mruz

You might want to use a 'while' instead of a 'for'

phalcon avatar Dec 26 '14 21:12 phalcon

I'm doing that now..

mruz avatar Dec 27 '14 08:12 mruz

Yes, you can always convert your for loop into equivalent while loop. Check out my tool: https://github.com/jimthunderbird/php-to-c-extension#example-04

jimthunderbird avatar Feb 08 '15 17:02 jimthunderbird

I think this is a bug. It should use i in the loop:

for (i = 0; i < Z_STRLEN_P(str); i++) {

or replace inside loop:

let i++;

to

_0++;

because now i++ is useless. @ovr @nkt what do you think?

mruz avatar May 14 '15 09:05 mruz