optimize case-insensitive string ops

This commit is contained in:
Glenn Maynard
2003-10-20 03:57:02 +00:00
parent d7cb5aaa22
commit b34bc5108d
2 changed files with 34 additions and 12 deletions
+20
View File
@@ -1002,3 +1002,23 @@ CString Capitalize( CString s )
CString s2 = s.Right( s.GetLength()-1 );
return s1+s2;
}
char char_traits_char_nocase::uptab[256] =
{
'\x00','\x01','\x02','\x03','\x04','\x05','\x06','\x07','\x08','\x09','\x0A','\x0B','\x0C','\x0D','\x0E','\x0F',
'\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1A','\x1B','\x1C','\x1D','\x1E','\x1F',
'\x20','\x21','\x22','\x23','\x24','\x25','\x26','\x27','\x28','\x29','\x2A','\x2B','\x2C','\x2D','\x2E','\x2F',
'\x30','\x31','\x32','\x33','\x34','\x35','\x36','\x37','\x38','\x39','\x3A','\x3B','\x3C','\x3D','\x3E','\x3F',
'\x40','\x41','\x42','\x43','\x44','\x45','\x46','\x47','\x48','\x49','\x4A','\x4B','\x4C','\x4D','\x4E','\x4F',
'\x50','\x51','\x52','\x53','\x54','\x55','\x56','\x57','\x58','\x59','\x5A','\x5B','\x5C','\x5D','\x5E','\x5F',
'\x60','\x41','\x42','\x43','\x44','\x45','\x46','\x47','\x48','\x49','\x4A','\x4B','\x4C','\x4D','\x4E','\x4F',
'\x50','\x51','\x52','\x53','\x54','\x55','\x56','\x57','\x58','\x59','\x5A','\x7B','\x7C','\x7D','\x7E','\x7F',
'\x80','\x81','\x82','\x83','\x84','\x85','\x86','\x87','\x88','\x89','\x8A','\x8B','\x8C','\x8D','\x8E','\x8F',
'\x90','\x91','\x92','\x93','\x94','\x95','\x96','\x97','\x98','\x99','\x9A','\x9B','\x9C','\x9D','\x9E','\x9F',
'\xA0','\xA1','\xA2','\xA3','\xA4','\xA5','\xA6','\xA7','\xA8','\xA9','\xAA','\xAB','\xAC','\xAD','\xAE','\xAF',
'\xB0','\xB1','\xB2','\xB3','\xB4','\xB5','\xB6','\xB7','\xB8','\xB9','\xBA','\xBB','\xBC','\xBD','\xBE','\xBF',
'\xC0','\xC1','\xC2','\xC3','\xC4','\xC5','\xC6','\xC7','\xC8','\xC9','\xCA','\xCB','\xCC','\xCD','\xCE','\xCF',
'\xD0','\xD1','\xD2','\xD3','\xD4','\xD5','\xD6','\xD7','\xD8','\xD9','\xDA','\xDB','\xDC','\xDD','\xDE','\xDF',
'\xE0','\xE1','\xE2','\xE3','\xE4','\xE5','\xE6','\xE7','\xE8','\xE9','\xEA','\xEB','\xEC','\xED','\xEE','\xEF',
'\xF0','\xF1','\xF2','\xF3','\xF4','\xF5','\xF6','\xF7','\xF8','\xF9','\xFA','\xFB','\xFC','\xFD','\xFE','\xFF',
};
+14 -12
View File
@@ -255,25 +255,27 @@ CString Capitalize( CString s );
/* ASCII-only case insensitivity. */
struct char_traits_char_nocase: public char_traits<char>
{
static bool eq( char c1, char c2 )
{ return toupper(c1) == toupper(c2); }
static char uptab[256];
static bool ne( char c1, char c2 )
{ return toupper(c1) != toupper(c2); }
static inline bool eq( char c1, char c2 )
{ return uptab[c1] == uptab[c2]; }
static bool lt( char c1, char c2 )
{ return toupper(c1) < toupper(c2); }
static inline bool ne( char c1, char c2 )
{ return uptab[c1] != uptab[c2]; }
static inline bool lt( char c1, char c2 )
{ return uptab[c1] < uptab[c2]; }
static int compare( const char* s1, const char* s2, size_t n )
{
for(size_t i = 0; i < n; ++i)
int ret = 0;
while( n-- )
{
const char c1 = (char) tolower(s1[i]);
const char c2 = (char) tolower(s2[i]);
if(c1 < c2) return -1;
if(c1 > c2) return 1;
ret = fasttoupper(*s1++) - fasttoupper(*s2++);
if( ret != 0 )
break;
}
return 0;
return ret;
}
static inline char fasttoupper(char a)