在PHP源代码中是用于增加字符串的代码


Where in the PHP source is the code for incrementing strings?

PHP有一个特性,可以在字符串上使用自增运算符。它的行为类似于里程表,一旦你到达一个范围的尽头,它就会"翻转"。

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

只是好奇这在PHP源代码的什么地方。我经常在源代码中查看函数/扩展,但是像这样的东西我不知道去哪里看。

使用他们的基于web的SVN链接到文件将是非常棒的。

这个运算符的实现方便地位于zend_operators.c中,在一个更方便地称为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;
    }
}
/* }}} */