Slide 24
Slide 24 text
代替実装: str_increment() 2/2
ext/standard/string.c#L1292-L1310 (php-8.5.12)
zend_string *incremented = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), /* persistent */ false);
size_t position = ZSTR_LEN(str)-1;
bool carry = false;
do {
char c = ZSTR_VAL(incremented)[position];
/* We know c is in ['a', 'z'], ['A', 'Z'], or ['0', '9'] range from zend_string_only_has_ascii_alphanumeric() */
if (EXPECTED( c != 'z' && c != 'Z' && c != '9' )) {
carry = false;
ZSTR_VAL(incremented)[position]++;
} else { /* if 'z', 'Z', or '9' */
carry = true;
if (c == '9') {
ZSTR_VAL(incremented)[position] = '0';
} else {
ZSTR_VAL(incremented)[position] -= 25;
}
}
} while (carry && position-- > 0);
ループの挙動は同じだが、入力が厳密になった分、書き方はすっきりしている
© 2026 CoDMON Inc.
24