Where in the PHP source is the code for increasing lines?

PHP has a function in which you can use incrementing operators line by line. It behaves similarly to the odometer, where when you reach the end of the range, it rolls around.

<?php
$str = 'zy';
$str++;
echo "$str\n"; // zz
$str++;
echo "$str\n"; // aaa

Just curious where in the PHP source code this is. I often look in the source code of a function / extension, but something like this, I have no idea where to look.

A link to a file using web SVN would be awesome.

+5
source share
1 answer

The implementation of this operator is conveniently located in zend_operators.cthe function, which is even more conveniently called increment_string():

static void increment_string(zval *str) /* {{{ */
{
    int carry=0;
    int pos=Z_STRLEN_P(str)-1;
    char *s=Z_STRVAL_P(str);
    char *t;
    int last=0; /* Shut up the compiler warning */
    int ch;

    if (Z_STRLEN_P(str) == 0) {
        STR_FREE(Z_STRVAL_P(str));
        Z_STRVAL_P(str) = estrndup("1", sizeof("1")-1);
        Z_STRLEN_P(str) = 1;
        return;
    }

    if (IS_INTERNED(s)) {
        s = (char*) emalloc(Z_STRLEN_P(str) + 1);
        memcpy(s, Z_STRVAL_P(str), Z_STRLEN_P(str) + 1);
        Z_STRVAL_P(str) = s;
    }

    while (pos >= 0) {
        ch = s[pos];
        if (ch >= 'a' && ch <= 'z') {
            if (ch == 'z') {
                s[pos] = 'a';
                carry=1;
            } else {
                s[pos]++;
                carry=0;
            }
            last=LOWER_CASE;
        } else if (ch >= 'A' && ch <= 'Z') {
            if (ch == 'Z') {
                s[pos] = 'A';
                carry=1;
            } else {
                s[pos]++;
                carry=0;
            }
            last=UPPER_CASE;
        } else if (ch >= '0' && ch <= '9') {
            if (ch == '9') {
                s[pos] = '0';
                carry=1;
            } else {
                s[pos]++;
                carry=0;
            }
            last = NUMERIC;
        } else {
            carry=0;
            break;
        }
        if (carry == 0) {
            break;
        }
        pos--;
    }

    if (carry) {
        t = (char *) emalloc(Z_STRLEN_P(str)+1+1);
        memcpy(t+1, Z_STRVAL_P(str), Z_STRLEN_P(str));
        Z_STRLEN_P(str)++;
        t[Z_STRLEN_P(str)] = '\0';
        switch (last) {
            case NUMERIC:
                t[0] = '1';
                break;
            case UPPER_CASE:
                t[0] = 'A';
                break;
            case LOWER_CASE:
                t[0] = 'a';
                break;
        }
        STR_FREE(Z_STRVAL_P(str));
        Z_STRVAL_P(str) = t;
    }
}
/* }}} */
+10
source

All Articles