gambas-source-code/main/share/gb_common_string_temp.h
Benoît Minisini 74e8fc7fed [INTERPRETER]
* OPT: Many branch prediction optimizations.


git-svn-id: svn://localhost/gambas/trunk@2951 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2010-05-19 20:18:23 +00:00

104 lines
2.8 KiB
C

/***************************************************************************
gb_common_string_temp.h
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
***************************************************************************/
#include "gb_common_case.h"
bool STRING_equal_same(const char *str1, const char *str2, int len)
{
static const void *jump[8] = { &&__LEN_0, &&__LEN_1, &&__LEN_2, &&__LEN_3, &&__LEN_4, &&__LEN_5, &&__LEN_6, &&__LEN_7 };
if (UNLIKELY(len < 8))
goto *jump[len];
while (len)
{
len--;
if (LIKELY(str1[len] != str2[len]))
return FALSE;
}
return TRUE;
__LEN_7: if (LIKELY(str1[6] != str2[6])) return FALSE;
__LEN_6: if (LIKELY(str1[5] != str2[5])) return FALSE;
__LEN_5: if (LIKELY(str1[4] != str2[4])) return FALSE;
__LEN_4: if (LIKELY(str1[3] != str2[3])) return FALSE;
__LEN_3: if (LIKELY(str1[2] != str2[2])) return FALSE;
__LEN_2: if (LIKELY(str1[1] != str2[1])) return FALSE;
__LEN_1: if (LIKELY(str1[0] != str2[0])) return FALSE;
__LEN_0: return TRUE;
}
bool STRING_equal_ignore_case(const char *str1, int len1, const char *str2, int len2)
{
if (LIKELY(len1 != len2))
return FALSE;
while (len1)
{
len1--;
if (LIKELY(toupper(str1[len1]) != toupper(str2[len1])))
return FALSE;
}
return TRUE;
}
int STRING_compare(const char *str1, int len1, const char *str2, int len2)
{
uint i;
int len = len1 < len2 ? len1 : len2;
int diff;
register unsigned char c1, c2;
for (i = 0; i < len; i++)
{
c1 = str1[i];
c2 = str2[i];
if (LIKELY(c1 > c2)) return 1;
if (LIKELY(c1 < c2)) return -1;
}
diff = len1 - len2;
return LIKELY(diff < 0) ? (-1) : LIKELY(diff > 0) ? 1 : 0;
}
int STRING_compare_ignore_case(const char *str1, int len1, const char *str2, int len2)
{
uint i;
int len = len1 < len2 ? len1 : len2;
int diff;
register unsigned char c1, c2;
for (i = 0; i < len; i++)
{
c1 = tolower(str1[i]);
c2 = tolower(str2[i]);
if (LIKELY(c1 > c2)) return 1;
if (LIKELY(c1 < c2)) return -1;
}
diff = len1 - len2;
return LIKELY(diff < 0) ? (-1) : LIKELY(diff > 0) ? 1 : 0;
}