Share
Embed
Transcript
33 slides
-
自己紹介 • 福井 将之 (ふくい まさゆき) @msfukui • PHP を使った
Web アプリケーション開発に従事 • 平日は東京、週末は山梨、たまに香川 • 車とバイクに乗ることと猫とキーボードが好きです • PHP もとても好きです © 2026 CoDMON Inc. 2 -
三行で • PHP では 'Z' を ++ でインクリメントすると 'AA' •
でも 'AA' を -- でデクリメントしても 'AA' のまま • 不思議に思って少し調べてみた • というだけのお話をしてみます © 2026 CoDMON Inc. 3 -
経緯 • 過去とある Excel ファイルを出力する PHP のコードを書いていて • Excel の列番号は
'Z' の次は 'AA' • こいつ..動くぞ! PHP 8.3 から非推奨となったこともあって、実際は採用しませんでしたが © 2026 CoDMON Inc. 4 -
文字列のインクリメント $s = 'a'; $s++; // 'b' $s = 'z';
$s++; // 'aa' $s = 'Az'; $s++; // 'Ba' $s = 'Zz'; $s++; // 'AAa' $s = 'a9'; $s++; // 'b0' $s = ''; $s++; // '1' • 英小文字・英大文字・数字が、それぞれの範囲で繰り上がる • 桁があふれたら、先頭の文字の種類で 1 桁増える • 日本語や記号など英数字以外の文字があるとそこで繰り上がりが止まる • 空文字だけは文字列の 1 に © 2026 CoDMON Inc. 5 -
文字列のデクリメント $s = 'b'; $s--; // 'b' $s = 'aa';
$s--; // 'aa' $s = 'Ba'; $s--; // 'Ba' $s = 'AAa'; $s--; // 'AAa' $s = 'b0'; $s--; // 'b0' $s = ''; $s--; // -1 • 繰り下がりはなく、値は変わらない • 空文字だけは数値の -1 に © 2026 CoDMON Inc. 6 -
PHP での概観 • PHP/FI 2.0.1 ・ atol() で数値変換して +1、前置(++$a) は構文エラー
• PHP 3.0 - PHP 8.2 ・ 今回の繰り上がり処理が入り、そのまま継続 • PHP 8.3 ・ 空文字・英数字以外が含まれる文字列の ++ は deprecated に ・ str_increment(), str_decrement() が追加 • PHP 8.5 ・ 数値でない文字列の ++ はすべて deprecated に © 2026 CoDMON Inc. 9 -
PHP 8 での処理の流れ • Zend/zend_compile.c ・ $a++ を ZEND_POST_INC にコンパイル
• Zend/zend_vm_def.h の ZEND_POST_INC ハンドラ ・ 整数ならその場で +1、それ以外は zend_post_inc_helper へ • Zend/zend_operators.c の increment_function() を呼び出し © 2026 CoDMON Inc. 10 -
PHP 8.2: increment_function() Zend/zend_operators.c#L2499-L2524 (php-8.2.35) case IS_NULL: ZVAL_LONG(op1, 1); break;
case IS_STRING: { zend_long lval; double dval; switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) { case IS_LONG: ... break; case IS_DOUBLE: ... break; default: /* Perl style string increment */ increment_string(op1); break; 型ごとに switch、数値以外の文字列なら increment_string() を呼び出し © 2026 CoDMON Inc. 11 -
PHP 8.2: increment_string() 1/2 Zend/zend_operators.c#L2411-L2415 (php-8.2.35) if (Z_STRLEN_P(str) == 0)
{ zval_ptr_dtor_str(str); ZVAL_CHAR(str, '1'); return; } 空文字の場合は、文字列の '1' を返す © 2026 CoDMON Inc. 12 -
PHP 8.2: increment_string() 2/2 Zend/zend_operators.c#L2430-L2466 (php-8.2.35) do { 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') { ... } else if (ch >= '0' && ch <= '9') { ... } else { carry=0; break; } if (carry == 0) { break; } } while (pos-- > 0); 文字列末尾から 1 文字ずつ見て、文字種ごとに繰り上げている © 2026 CoDMON Inc. 13 -
ちなみに decrement_function() だと Zend/zend_operators.c#L2567-L2588 (php-8.2.35) case IS_STRING: /* Like perl
we only support string increment */ if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */ zval_ptr_dtor_str(op1); ZVAL_LONG(op1, -1); break; } switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) { case IS_LONG: ... break; case IS_DOUBLE: ... break; } break; default: © 2026 CoDMON Inc. がないので、数値でない文字列は何もせずそのまま 14 -
なお Perl だと $s = 'a'; $s++; # 'b' $s
= 'z'; $s++; # 'aa' $s = 'Az'; $s++; # 'Ba' $s = 'Zz'; $s++; # 'AAa' $s = 'a9'; $s++; # 'b0' $s = ''; $s++; # 1 $s = 'b'; $s--; # -1 $s = 'aa'; $s--; # -1 $s = 'Ba'; $s--; # -1 $s = 'AAa'; $s--; # -1 $s = 'b0'; $s--; # -1 $s = ''; $s--; # -1 • インクリメントの挙動に若干の違いがある(後述) ・ 空文字のインクリメントは文字列ではなく数値の 1 ・ '9z' は PHP では '10a'、Perl では 10 • デクリメントは全て -1 © 2026 CoDMON Inc. 15 -
遡ると Perl 2.0 (1988年) のマニュアルから: perl.man.1#L1037-L1052 (perl-2.0) The autoincrement operator
has a little extra built-in magic to it. ... The autodecrement is not magical. • 以後最新の Perl 5.44.0 (2026-07-15 リリース) まで挙動は変わらず ・ Perl 1.0 では異なる実装がされていますが一旦ここでは割愛します.. © 2026 CoDMON Inc. 16 -
Perl: sv.c の Perl_sv_inc_nomg() sv.c#L10219-L10286 (perl v5.44.0) (抜粋して一部を取り除いて再構成) d =
SvPVX(sv); while (isALPHA(*d)) d++; // /^[a-zA-Z]*[0-9]*$/ に while (isDIGIT(*d)) d++; // 合わなければ数値として +1 ... while (d >= SvPVX_const(sv)) { if (isDIGIT(*d)) { if (++*d <= '9') return; *(d--) = '0'; } else { ++*d; if (isALPHA(*d)) return; *(d--) -= 'z' - 'a' + 1; } } /* oh,oh, the number grew */ 微妙に PHP の実装と違う.. © 2026 CoDMON Inc. 17 -
PHP と Perl のアルゴリズムの違い • 書き方は違うけれど、ループの中は一緒 • 入力値の受け入れ方に違いがある ・ PHP
は数値でなければ文字列として扱う ・ Perl は /^[a-zA-Z]*[0-9]*$/ に合わなければ数値変換して +1 • '9z' の場合 ・ PHP では '10a', Perl では 9 + 1 → 10 • 'A9z' の場合 ・ PHP では 'B0a', Perl では 0 + 1 → 1 © 2026 CoDMON Inc. 18 -
PHP 8.3: increment_string() 1/2 Zend/zend_operators.c#L2523-L2532 (php-8.3.36) if (UNEXPECTED(Z_STRLEN_P(str) == 0))
{ zend_error(E_DEPRECATED, "Increment on non-alphanumeric string is deprecated"); if (EG(exception)) { return false; } /* A userland error handler can change the type from string to something else */ zval_ptr_dtor(str); ZVAL_CHAR(str, '1'); return true; } 空文字の場合は、警告を出して文字列の '1' を返す © 2026 CoDMON Inc. 20 -
PHP 8.3: increment_string() 2/2 Zend/zend_operators.c#L2534-L2544 (php-8.3.36) if (UNEXPECTED(!zend_string_only_has_ascii_alphanumeric(Z_STR_P(str)))) { zend_string
*zstr = Z_STR_P(str); zend_string_addref(zstr); zend_error(E_DEPRECATED, "Increment on non-alphanumeric string is deprecated"); if (EG(exception)) { zend_string_release(zstr); return false; } zval_ptr_dtor(str); ZVAL_STR(str, zstr); } 英数字以外を含む文字列の場合は、警告を出して処理は継続 後続の判定条件は PHP8.2 からほぼ変更なし © 2026 CoDMON Inc. 21 -
PHP 8.5 / 8.6: increment_string() Zend/zend_operators.c#L2622-L2628 (php-8.5.12) zend_string *zstr =
Z_STR_P(str); zend_string_addref(zstr); zend_error(E_DEPRECATED, "Increment on non-numeric string is deprecated, use str_increment() instead"); if (EG(exception)) { zend_string_release(zstr); return false; } 冒頭で無条件に警告を出して処理は継続 数値でない文字列は一律で警告対象に 後続の判定条件は PHP8.2, PHP8.3 からほぼ変更なし PHP 8.6 (8.6.0RC7) でも実装は変更なし © 2026 CoDMON Inc. 22 -
代替実装: str_increment() 1/2 既存の increment_string() とは別実装 ext/standard/string.c#L1283-L1290 (php-8.5.12) if (ZSTR_LEN(str)
== 0) { zend_argument_must_not_be_empty_error(1); RETURN_THROWS(); } if (!zend_string_only_has_ascii_alphanumeric(str)) { zend_argument_value_error(1, "must be composed only of alphanumeric ASCII characters"); RETURN_THROWS(); } 空文字を含めて英数字以外を ValueError にする厳密な実装 © 2026 CoDMON Inc. 23 -
代替実装: 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 -
おまけ: なぜ Excel の列は Z の次が AA ? • 起源は
1979年の表計算ソフト VisiCalc (らしい) 列は 63 (A〜BK)、行は 255 "We departed from common notation by numbering the rows and using letters for the columns. This was because we had only 63 columns and 255 rows." • この A1 方式が Lotus 1-2-3 を経て Excel の標準になった • 数学的には 0 のない「全単射 26 進法」(A=1 … Z=26) • ちなみに最後の列は VisiCalc BK (63) → Excel IV (256) → Excel 2007〜 XFD (16,384) • なお、PHP で 'A' から 16,383 回 ++ すると、ちゃんと 'XFD' になります © 2026 CoDMON Inc. 26 -
参考資料 1/4 • PHP RFC: Path to Saner Increment/Decrement operators
https://wiki.php.net/rfc/saner-inc-dec-operators • PHP RFC: Deprecations for PHP 8.5 https://wiki.php.net/rfc/deprecations_php_8_5 • PHP Manual: Incrementing/Decrementing Operators https://www.php.net/manual/en/language.operators.increment.php • php/php-src (GitHub) https://github.com/php/php-src © 2026 CoDMON Inc. 29 -
参考資料 2/4 • perlop: Auto-increment and Auto-decrement https://perldoc.perl.org/perlop#Auto-increment-and-Auto-decrement • Perl
2.0 (1988年) のマニュアル perl.man.1 https://github.com/Perl/perl5/blob/perl-2.0/perl.man.1 • Perl/perl5 (GitHub) https://github.com/Perl/perl5 • PHP Museum (PHP/FI 2.0.1, PHP 3.0 の配布物) https://museum.php.net/ • farwish/php2: PHP/FI 2.0.1 のソースのミラー (GitHub) https://github.com/farwish/php2 © 2026 CoDMON Inc. 30 -
参考資料 3/4 • Implementing VisiCalc (Bob Frankston, 2003) https://www.landley.net/history/mirror/apple2/implementingvisicalc.html •
Was VisiCalc the "first" spreadsheet? (Dan Bricklin) http://www.bricklin.com/firstspreadsheetquestion.htm • 表計算ソフト誕生の話 (Dan Bricklin, TEDxBeaconStreet 2016 / 青木靖 訳) https://aoky.net/articles/dan_bricklin/meet_the_inventor_of_the_electronic_spreadsheet.htm © 2026 CoDMON Inc. 31 -
参考資料 4/4 • Multiplan - Wikipedia https://en.wikipedia.org/wiki/Multiplan • Excel column
numbering (John D. Cook) https://www.johndcook.com/blog/2026/07/25/excel-column-numbering/ • Excel specifications and limits (Microsoft) https://support.microsoft.com/en-us/office/excel-specifications-and-limits-1672b34d-7043-467e8e27-269d656771c3 © 2026 CoDMON Inc. 32 -
メモ: PHP/FI 2.0.1: ++ が実行されるまで 1. src/lex.c: ++ を INC
トークンに (L1266-L1270) 2. src/parse.raw (yacc): expr INC の規則で CalcInc() を直接呼ぶ (L522-L525) 3. src/calc.c の CalcInc(): 文字列も数値と同じ分岐で +1 (L202-L208) | expr INC { if(GetCurrentState(NULL) || inCase || inElseIf) CalcInc(INC); } switch(s->type) { case LNUMBER: case STRING: switch(op) { case INC: s->intval++; sprintf(temp,"%ld",s->intval); • opcode へのコンパイルや VM はなく、構文解析しながらその場で実行 • intval は atol() で文字列から変換した値 (src/stack.c#L88) © 2026 CoDMON Inc. 33