User:Ans/firstChar
Appearance
< User:Ans
bugzilla:1701: special firstChar() routine for Korean characters
function c($code) {
# (0)
#$index = ($code - 0xac00 - (($code - 0xac00) % 0x024c)) / 0x024c;
#$index = $code - 0xac00;
# (1)
#$index = ($index - ($index % 0x024c)) / 0x024c; # $index = $index div 0x024c
# (2)
#$index = floor($index / 0x024c); # a bit slower than (2)
# (3)
#$index = floor(($code-0xac00) / 0x024c); # a bit faster than (1),(2)
# however, (0) is a bit faster than (1),(2),(3)
# (4)
#switch ($index)
# (5)
#switch (floor(($code-0xac00) / 0x024c)) # 10-15% faster than (4)
# (6) # 9-10% faster than (5), but 1.5-2% slower than if/else
switch ((($code - 0xac00) - (($code - 0xac00) % 0x024c)) / 0x024c) {
# (7) # slower than (6),(5), but faster than (4)
#switch (($index - ($index % 0x024c)) / 0x024c)
# 0xac00
case 0:
case 1: return "\xe3\x84\xb1";
# 0xb098
case 2: return "\xe3\x84\xb4";
# 0xb2e4
case 3:
case 4: return "\xe3\x84\xb7";
# 0xb77c
case 5: return "\xe3\x84\xb9";
# 0xb9c8
case 6: return "\xe3\x85\x81";
# 0xbc14
case 7:
case 8: return "\xe3\x85\x82";
# 0xc0ac
case 9:
case 10: return "\xe3\x85\x85";
# 0xc544
case 11: return "\xe3\x85\x87";
# 0xc790
case 12:
case 13: return "\xe3\x85\x88";
# 0xcc28
case 14: return "\xe3\x85\x8a";
# 0xce74
case 15: return "\xe3\x85\x8b";
# 0xd0c0
case 16: return "\xe3\x85\x8c";
# 0xd30c
case 17: return "\xe3\x85\x8d";
# 0xd558
case 18:
default: return "\xe3\x85\x8e";
}
}
function c2($code) {
if ( $code < 0xb098 ) {
return "\xe3\x84\xb1";
} elseif ( $code < 0xb2e4 ) {
return "\xe3\x84\xb4";
} elseif ( $code < 0xb77c ) {
return "\xe3\x84\xb7";
} elseif ( $code < 0xb9c8 ) {
return "\xe3\x84\xb9";
} elseif ( $code < 0xbc14 ) {
return "\xe3\x85\x81";
} elseif ( $code < 0xc0ac ) {
return "\xe3\x85\x82";
} elseif ( $code < 0xc544 ) {
return "\xe3\x85\x85";
} elseif ( $code < 0xc790 ) {
return "\xe3\x85\x87";
} elseif ( $code < 0xcc28 ) {
return "\xe3\x85\x88";
} elseif ( $code < 0xce74 ) {
return "\xe3\x85\x8a";
} elseif ( $code < 0xd0c0 ) {
return "\xe3\x85\x8b";
} elseif ( $code < 0xd30c ) {
return "\xe3\x85\x8c";
} elseif ( $code < 0xd558 ) {
return "\xe3\x85\x8d";
} else {
return "\xe3\x85\x8e";
}
}
#for ($j=0xac00; $j<0xffff; ++$j) { print c($j)."\n"; } # test case
for ($j=0; $j<100; ++$j) {
#for ($code=0xac00; $code<0xffff; ++$code) { c($code); }
for ($code=0xac00; $code<0xd7a4; ++$code) { c($code); }
}