2008-01-27 15:00:04 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gb_common_string_temp.h
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
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.
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
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.
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2008-01-27 15:00:04 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#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 };
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
//return len == 0 || __builtin_memcmp(str1, str2, len) == 0;
|
|
|
|
|
2011-09-07 14:38:29 +02:00
|
|
|
#if defined(ARCH_X86_64) || defined(ARCH_X86)
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-26 04:05:50 +02:00
|
|
|
for(;;)
|
2011-09-07 14:38:29 +02:00
|
|
|
{
|
2011-09-26 04:05:50 +02:00
|
|
|
if (len < 8) break;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
if (*((int64_t *)str1) ^ *((int64_t *)str2))
|
2011-09-26 04:05:50 +02:00
|
|
|
return FALSE;
|
|
|
|
str1 += 8;
|
|
|
|
str2 += 8;
|
|
|
|
len -= 8;
|
|
|
|
|
|
|
|
if (len < 8) break;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
if (*((int64_t *)str1) ^ *((int64_t *)str2))
|
2011-09-26 04:05:50 +02:00
|
|
|
return FALSE;
|
|
|
|
str1 += 8;
|
|
|
|
str2 += 8;
|
|
|
|
len -= 8;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-26 04:05:50 +02:00
|
|
|
if (len < 8) break;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
if (*((int64_t *)str1) ^ *((int64_t *)str2))
|
2011-09-26 04:05:50 +02:00
|
|
|
return FALSE;
|
|
|
|
str1 += 8;
|
|
|
|
str2 += 8;
|
|
|
|
len -= 8;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-26 04:05:50 +02:00
|
|
|
if (len < 8) break;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
if (*((int64_t *)str1) ^ *((int64_t *)str2))
|
2011-09-07 14:38:29 +02:00
|
|
|
return FALSE;
|
2010-06-05 01:48:53 +02:00
|
|
|
str1 += 8;
|
|
|
|
str2 += 8;
|
2011-09-07 14:38:29 +02:00
|
|
|
len -= 8;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-07 14:38:29 +02:00
|
|
|
#else
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-07 14:38:29 +02:00
|
|
|
while (len >= 8)
|
|
|
|
{
|
2015-11-02 04:24:44 +01:00
|
|
|
if (str1[0] ^ str2[0])
|
2011-09-07 14:38:29 +02:00
|
|
|
return FALSE;
|
2015-11-02 04:24:44 +01:00
|
|
|
if (str1[1] ^ str2[1])
|
2011-09-07 14:38:29 +02:00
|
|
|
return FALSE;
|
2015-11-02 04:24:44 +01:00
|
|
|
if (str1[2] ^ str2[2])
|
2011-09-07 14:38:29 +02:00
|
|
|
return FALSE;
|
2015-11-02 04:24:44 +01:00
|
|
|
if (str1[3] ^ str2[3])
|
2011-09-07 14:38:29 +02:00
|
|
|
return FALSE;
|
2015-11-02 04:24:44 +01:00
|
|
|
if (str1[4] ^ str2[4])
|
2011-09-07 14:38:29 +02:00
|
|
|
return FALSE;
|
2015-11-02 04:24:44 +01:00
|
|
|
if (str1[5] ^ str2[5])
|
2011-09-07 14:38:29 +02:00
|
|
|
return FALSE;
|
2015-11-02 04:24:44 +01:00
|
|
|
if (str1[6] ^ str2[6])
|
2011-09-07 14:38:29 +02:00
|
|
|
return FALSE;
|
2015-11-02 04:24:44 +01:00
|
|
|
if (str1[7] ^ str2[7])
|
2011-09-07 14:38:29 +02:00
|
|
|
return FALSE;
|
|
|
|
str1 += 8;
|
|
|
|
str2 += 8;
|
|
|
|
len -= 8;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-07 14:38:29 +02:00
|
|
|
#endif
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-07 14:38:29 +02:00
|
|
|
goto *jump[len];
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
__LEN_7: if (str1[6] ^ str2[6]) return FALSE;
|
|
|
|
__LEN_6: if (str1[5] ^ str2[5]) return FALSE;
|
|
|
|
__LEN_5: if (str1[4] ^ str2[4]) return FALSE;
|
|
|
|
__LEN_4: if (str1[3] ^ str2[3]) return FALSE;
|
|
|
|
__LEN_3: if (str1[2] ^ str2[2]) return FALSE;
|
|
|
|
__LEN_2: if (str1[1] ^ str2[1]) return FALSE;
|
|
|
|
__LEN_1: if (str1[0] ^ str2[0]) return FALSE;
|
2008-01-27 15:00:04 +01:00
|
|
|
__LEN_0: return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
bool STRING_equal_ignore_case_same(const char *str1, const char *str2, int len)
|
2008-01-27 15:00:04 +01:00
|
|
|
{
|
2010-06-05 01:48:53 +02:00
|
|
|
static const void *jump[8] = { &&__LEN_0, &&__LEN_1, &&__LEN_2, &&__LEN_3, &&__LEN_4, &&__LEN_5, &&__LEN_6, &&__LEN_7 };
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-07 14:38:29 +02:00
|
|
|
#if defined(ARCH_X86_64) || defined(ARCH_X86)
|
2011-09-08 22:23:15 +02:00
|
|
|
while (len >= 8)
|
2011-09-07 14:38:29 +02:00
|
|
|
{
|
2015-11-02 04:24:44 +01:00
|
|
|
if (*((int64_t *)str1) ^ *((int64_t *)str2))
|
2011-09-07 14:38:29 +02:00
|
|
|
{
|
2015-11-02 04:24:44 +01:00
|
|
|
if (toupper(str1[0]) ^ toupper(str2[0])) return FALSE;
|
|
|
|
if (toupper(str1[1]) ^ toupper(str2[1])) return FALSE;
|
|
|
|
if (toupper(str1[2]) ^ toupper(str2[2])) return FALSE;
|
|
|
|
if (toupper(str1[3]) ^ toupper(str2[3])) return FALSE;
|
|
|
|
if (toupper(str1[4]) ^ toupper(str2[4])) return FALSE;
|
|
|
|
if (toupper(str1[5]) ^ toupper(str2[5])) return FALSE;
|
|
|
|
if (toupper(str1[6]) ^ toupper(str2[6])) return FALSE;
|
|
|
|
if (toupper(str1[7]) ^ toupper(str2[7])) return FALSE;
|
2011-09-07 14:38:29 +02:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-07 14:38:29 +02:00
|
|
|
str1 += 8;
|
|
|
|
str2 += 8;
|
2011-09-08 22:23:15 +02:00
|
|
|
len -= 8;
|
2011-09-07 14:38:29 +02:00
|
|
|
}
|
|
|
|
#else
|
2011-09-08 22:23:15 +02:00
|
|
|
while (len >= 8)
|
2011-09-07 14:38:29 +02:00
|
|
|
{
|
2015-11-02 04:24:44 +01:00
|
|
|
if (str1[0] ^ str2[0] && toupper(str1[0]) ^ toupper(str2[0])) return FALSE;
|
|
|
|
if (str1[1] ^ str2[1] && toupper(str1[1]) ^ toupper(str2[1])) return FALSE;
|
|
|
|
if (str1[2] ^ str2[2] && toupper(str1[2]) ^ toupper(str2[2])) return FALSE;
|
|
|
|
if (str1[3] ^ str2[3] && toupper(str1[3]) ^ toupper(str2[3])) return FALSE;
|
|
|
|
if (str1[4] ^ str2[4] && toupper(str1[4]) ^ toupper(str2[4])) return FALSE;
|
|
|
|
if (str1[5] ^ str2[5] && toupper(str1[5]) ^ toupper(str2[5])) return FALSE;
|
|
|
|
if (str1[6] ^ str2[6] && toupper(str1[6]) ^ toupper(str2[6])) return FALSE;
|
|
|
|
if (str1[7] ^ str2[7] && toupper(str1[7]) ^ toupper(str2[7])) return FALSE;
|
2010-06-05 01:48:53 +02:00
|
|
|
str1 += 8;
|
|
|
|
str2 += 8;
|
2011-09-08 22:23:15 +02:00
|
|
|
len -= 8;
|
2011-09-07 14:38:29 +02:00
|
|
|
}
|
|
|
|
#endif
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
goto *jump[len];
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
__LEN_7: if (str1[6] ^ str2[6] && toupper(str1[6]) ^ toupper(str2[6])) return FALSE;
|
|
|
|
__LEN_6: if (str1[5] ^ str2[5] && toupper(str1[5]) ^ toupper(str2[5])) return FALSE;
|
|
|
|
__LEN_5: if (str1[4] ^ str2[4] && toupper(str1[4]) ^ toupper(str2[4])) return FALSE;
|
|
|
|
__LEN_4: if (str1[3] ^ str2[3] && toupper(str1[3]) ^ toupper(str2[3])) return FALSE;
|
|
|
|
__LEN_3: if (str1[2] ^ str2[2] && toupper(str1[2]) ^ toupper(str2[2])) return FALSE;
|
|
|
|
__LEN_2: if (str1[1] ^ str2[1] && toupper(str1[1]) ^ toupper(str2[1])) return FALSE;
|
|
|
|
__LEN_1: if (str1[0] ^ str2[0] && toupper(str1[0]) ^ toupper(str2[0])) return FALSE;
|
2010-06-05 01:48:53 +02:00
|
|
|
__LEN_0: return TRUE;
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int STRING_compare(const char *str1, int len1, const char *str2, int len2)
|
|
|
|
{
|
2011-09-07 14:38:29 +02:00
|
|
|
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];
|
2013-08-17 00:35:28 +02:00
|
|
|
if (c1 > c2) return 1;
|
|
|
|
if (c1 < c2) return -1;
|
2011-09-07 14:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
diff = len1 - len2;
|
2013-08-17 00:35:28 +02:00
|
|
|
return (diff < 0) ? (-1) : (diff > 0) ? 1 : 0;
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int STRING_compare_ignore_case(const char *str1, int len1, const char *str2, int len2)
|
|
|
|
{
|
2011-09-07 14:38:29 +02:00
|
|
|
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]);
|
2013-08-17 00:35:28 +02:00
|
|
|
if (c1 > c2) return 1;
|
|
|
|
if (c1 < c2) return -1;
|
2011-09-07 14:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
diff = len1 - len2;
|
2013-08-17 00:35:28 +02:00
|
|
|
return (diff < 0) ? (-1) : (diff > 0) ? 1 : 0;
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|