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.
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;
}
}
/* }}} */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With