wcsmbs系関数とsetlocale()

ここで触れられているphp_mblen()は元はといえば私のコミットしたコードなので、なんでこんなんになってたんだっけ? といろいろ思い出しつつ調べてみた。

結論から言うと、mblen() や mbrlen() をポータブルに利用するには、setlocale(LC_ALL, ""); をプログラムの実行開始時に呼んでやらにゃいかんかったらしい。コミットしたときに、ふーん、そうなんだと思ってもう4年くらい経過していてすっかり忘れていたのでした。

A C program inherits its locale environment variables when it starts up. This happens automatically. However, these variables do not automatically control the locale used by the library functions, because ISO C says that all programs start by default in the standard C locale. To use the locales specified by the environment, you must call setlocale. Call it as follows:

setlocale (LC_ALL, "");

glibc ドキュメント