|
|
|
| 2008/10/07 11:45||▲
|
|
|
<ctype.h>のところで、書くべきことは書いてしまったので、おさらいになりますが、一応to〜系の関数についても触れておきます。
通常、to〜系関数は、
int tolower(int c) { return isupper(c) ? c - 'A' + 'a' : c; }
のように実装されるのですが、この方法には問題があります。英文字の連続性については、文字コードを特定することで回避できますが、"C"ロケール以外をにらんだ場合、例えばドイツ語のß(エスツェット)は小文字しかないため、対応する大文字が存在しません。日本語の半角カナも類似の問題があります。
そこで、to〜関数も表引きの手法をとることにします。to〜系関数はEOFを受け入れる必要はないのですが、念のためis〜系関数と同様に、-1〜255までの257要素の配列にしたいと思います。
/* tolower.c */ const unsigned char __tolower_C[257] = { ... }; const unsigned char *__tolower = __tolower_C + 1;
/* toupper.c */ const unsigned char __toupper_C[257] = { ... }; const unsigned char *__toupper = __tolower_C + 1;
ヘッダ側は、
#ifdef __cplusplus extern "C" { #endif
extern const unsigned char __tolower_C[]; extern const unsigned char __toupper_C[];
extern const unsigned char *__tolower; extern const unsigned char *__toupper;
#ifdef __cplusplus } #endif
とします。そして、各関数はインライン関数として、
static __inline__ int tolower(int c) { #ifdef __ONLY_C_LOCALE return isupper(c) ? c - 'A' + 'a' : c; #else return __tolower[c]; #endif }
static __inline__ int toupper(int c) { #ifdef __ONLY_C_LOCALE return islower(c) ? c - 'a' + 'A' : c; #else return __toupper[c]; #endif }
と定義することができます。 もちろん、各関数は外部関数としても同様の定義が必要になります。
|
| 2006/02/11 02:03|文字種別|TB:0|CM:0|▲
|
|
|
コメント
|
|
コメントの投稿
|
|
|
|
|
トラックバック
|
トラックバックURLはこちら
http://libc.blog47.fc2.com/tb.php/27-2b7645a9
|
|
|
|
|
ホーム
全記事一覧
<< 前の記事
次の記事 >>
|