주제: UTF8::lessenAsByte 메소드의 더 빠른 버전을 소개합니다.
반갑습니다, Cyrus입니다(구 Ikaris C. Faust).
현재 Textcube에서 사용하는 UTF8 클래스의 lessenAsByte의 더 빠른 버전을 소개합니다.
개인적으로 쓰려고 만든 것이지만, 그냥 이곳에 한 번 던져 봅니다.
이 메소드와 lessenAsByte 메소드의 속도 비교는 http://cyrush.com/4db/library/test.php 에서 보실 수 있습니다.
public static function Slice($siclTextStr,$siclLengthInt=255) {
if(strlen($siclTextStr)<=$siclLengthInt)
return $siclTextStr;
$siclVerifyInt=ord($siclTextStr[$siclLengthInt-1])>>6;
if(($siclVerifyInt>>1)===0) // 1byte
$siclLocationSubtInt=0;
elseif($siclVerifyInt===3) // Head byte of multi-bytes characters.
$siclLocationSubtInt=1;
elseif($siclVerifyInt===2) { // Middle of multi-bytes character.
if(isset($siclTextStr[$siclLengthInt])===false) { // End of the string
for($siclLoopInt=2;true;$siclLoopInt++) { // Seeking for head byte.
if((ord($siclTextStr[$siclLengthInt-$siclLoopInt])>>6)===2)
continue;
else
break;
}
$siclVerifyLengthInt=ord($siclTextStr[$siclLengthInt-$siclLoopInt])>>4;
if($siclVerifyLengthInt>=0 && $siclVerifyLengthInt<=7) // Broken byte.
$siclLocationSubtInt=$siclLoopInt-1;
else {
switch($siclVerifyLengthInt) { // Identify the length of current character.
case 12:
case 13:
$siclVerifiedLengthInt=2;
break;
case 14:
$siclVerifiedLengthInt=3;
break;
case 15:
$siclVerifiedLengthInt=4;
break;
}
if($siclLoopInt!==$siclVerifiedLengthInt) // We're in the middle of the character.
$siclLocationSubtInt=$siclLoopInt;
else // The byte we're verifying is the last byte of the character.
$siclLocationSubtInt=0;
}
unset($siclLoopInt,$siclVerifiedLengthInt);
} elseif((ord($siclTextStr[$siclLengthInt])>>6)!==2) // Last byte of the character.
$siclLocationSubtInt=0;
else {
for($siclLoopInt=2;true;$siclLoopInt++) { // Seeking for head byte.
if((ord($siclTextStr[$siclLengthInt-$siclLoopInt])>>6)===2)
continue;
else
break;
}
$siclLocationSubtInt=$siclLoopInt;
unset($siclLoopInt);
}
}
$siclSlicedStr=substr($siclTextStr,0,$siclLengthInt-$siclLocationSubtInt);
unset($siclTextStr,$siclLengthInt,$siclLocationSubtInt);
return $siclSlicedStr;
}