- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
xash_force_inline size_t Q_strncat( char *dst, const char *src, size_t size )
{
register const char *pchr;
register const unsigned int *plongword;
register unsigned int *pdst;
register unsigned int len;
const unsigned int himagic = 0x80808080, lomagic = 0x01010101;
if( !dst || !src || !size )
return 0;
len = Q_strlen( dst );
dst += len;
if( ( unsigned long int )src & ( sizeof( int ) - 1) != ( unsigned long int )dst & ( sizeof( int ) - 1) )
return len + Q_strncpy_unaligned( dst, src, size - len );
// first, copy all unaligned bytes
for( pchr = src; ( ( unsigned long int )pchr & ( sizeof( int ) - 1) ) != 0; pchr++, len++, dst++ )
{
*dst = *pchr;
if( len >= size )
{
*dst = '\0';
return len;
}
if( *pchr == '\0' )
return len;
}
plongword = ( const unsigned int * ) pchr;
pdst = ( unsigned int * ) dst;
// plongword is aligned now, copy by 4 bytes
while( true )
{
register unsigned int longword = *plongword++;
// if magic check failed
if( ( ( longword - lomagic ) & himagic ) != 0 || (size - len < 4) )
{
const char *pchar = ( const char * )( plongword - 1 );
char *pchdst = ( char * )( pdst );
int i;
for( i = 0; i < 4; i++ )
{
pchdst[i] = pchar[i];
if( len + i >= size )
{
pchdst[i] = '\0';
return len + i;
}
if( pchar[i] == 0 )
return len + i;
}
}
len += sizeof( longword );
*pdst++ = longword;
}
return 0;
}
/*
* sanity checker
xash_force_inline size_t Q_strncat( char *dst, const char *src, size_t size )
{
char buf1[100000], buf2[100000], r1, r2;
if( !dst || !src || !size )
return 0;
if( size > 99999 )
size = 99999;
strncpy( buf1, dst, size );
strncpy( buf2, dst, size );
buf1[99999] = buf2[99999] = 0;
r1 = Q_strncat_( buf1, src, size );
r2 = Q_strncat2( buf2, src, size );
if( r1 != r2 )
printf("DIFFERENT RESULT %d %d %d %d %s\n%s\n", r1, r2, aligned, counter, buf1, src);
if( strcmp( buf1, buf2 ) )
{
printf("DIFFERENT DATA %s %d %d %d %4s\n", src, size, aligned, counter, last);
printf("1: %s\n", buf1);
printf("2: %s\n", buf2);
}
//strncpy( dst, buf1, size );
return Q_strncat2(dst, src, size);
}
*/
Оптимизация удалась, однако при виде этого кода становится не по себе. Что можно с этим сделать?