constexpr std::size_t finder = ( std::size_t )0x0101010101010101ULL;
constexpr std::size_t masker = ( std::size_t )0x8080808080808080ULL;
constexpr std::size_t has_zero_7bit( const std::size_t n )
{
return ( n - finder ) & masker;
}
constexpr std::size_t has_zero_8bit( const std::size_t n )
{
return has_zero_7bit( n ) & ~n;
}
inline auto where_zero( const std::size_t* w )
{
if( ( *w & 0x00000000000000FF ) == 0 ) return (char*)w;
if( ( *w & 0x000000000000FF00 ) == 0 ) return (char*)w + 1;
if( ( *w & 0x0000000000FF0000 ) == 0 ) return (char*)w + 2;
if( sizeof finder == 8 )
{
if( ( *w & 0x00000000FF000000 ) == 0 ) return (char*)w + 3;
if( ( *w & 0x000000FF00000000 ) == 0 ) return (char*)w + 4;
if( ( *w & 0x0000FF0000000000 ) == 0 ) return (char*)w + 5;
if( ( *w & 0x00FF000000000000 ) == 0 ) return (char*)w + 6;
}
return (char*)w + ( sizeof finder - 1 );
}
template< int bits >
auto strlen_bit( const char* s )
{
const int step = sizeof finder == 4 ? 8 : 4;
const auto has_zero = bits == 7 ? has_zero_7bit : has_zero_8bit;
if( has_zero( *(std::size_t*)s ) )
return where_zero( (std::size_t*)s ) - s;
auto w = (std::size_t*)( (size_t)s & ~size_t( sizeof finder - 1 ) ) + 1;
while( 1 )
{
if( has_zero( w[ 0 ] ) ) return where_zero( w ) - s;
if( has_zero( w[ 1 ] ) ) return where_zero( w + 1 ) - s;
if( has_zero( w[ 2 ] ) ) return where_zero( w + 2 ) - s;
if( has_zero( w[ 3 ] ) ) return where_zero( w + 3 ) - s;
if( sizeof finder == 4 )
{
if( has_zero( w[ 4 ] ) ) return where_zero( w + 4 ) - s;
if( has_zero( w[ 5 ] ) ) return where_zero( w + 5 ) - s;
if( has_zero( w[ 6 ] ) ) return where_zero( w + 6 ) - s;
if( has_zero( w[ 7 ] ) ) return where_zero( w + 7 ) - s;
}
w += step;
}
}
auto strlen_fast( const char* s )
{
const size_t len = strlen_bit< 7 >( s );
return len + strlen_bit< 8 >( s + len );
}
다국어 처리 됩니다.
dhk
와
이거너님이짠거마즘?뭔가엉성한데
내가 짠거 맞고 안엉성함. ㅋㅋ
strlen 은 딱히 기교들어갈 여지가 많진 않지~
http://ideone.com/xyGEKr
코세형은 뭐하는 사람이에요?
진짜 학교보다 이 사람한테서 더 배울게 많네;
나는 이분을 보면 나의 목마름이 느껴진다.