Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

operators

php

PHP has a feature where you can use increment operators on strings. It behaves similarly to an odometer, where once you reach the end of a range, it "rolls over".

<?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 at functions/extensions, but something like this I have no idea where to look.

A link to the file using their web based SVN would be awesome.

like image 525
goat Avatar asked Dec 24 '11 17:12

goat


1 Answers

The implementation for this operator is conveniently located in zend_operators.c, in a function that's 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;
    }
}
/* }}} */
like image 137
BoltClock Avatar answered Nov 15 '22 00:11

BoltClock