2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_compare.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +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
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
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
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_COMPARE_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_case.h"
|
|
|
|
|
|
|
|
#include <wctype.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
|
|
|
|
#include "gbx_type.h"
|
|
|
|
#include "gbx_compare.h"
|
|
|
|
#include "gbx_date.h"
|
|
|
|
#include "gbx_object.h"
|
|
|
|
#include "gbx_class.h"
|
|
|
|
#include "gbx_exec.h"
|
2009-08-22 14:17:31 +02:00
|
|
|
#include "gbx_regexp.h"
|
2010-01-25 18:53:58 +01:00
|
|
|
#include "gbx_c_string.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static bool _descent = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
int compare_nothing(void *a, void *b)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare_integer(int *a, int *b)
|
|
|
|
{
|
|
|
|
bool comp;
|
|
|
|
|
|
|
|
if (*a < *b)
|
|
|
|
comp = -1;
|
|
|
|
else if (*a > *b)
|
|
|
|
comp = 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (_descent)
|
|
|
|
comp = -comp;
|
|
|
|
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare_short(short *a, short *b)
|
|
|
|
{
|
|
|
|
bool comp;
|
|
|
|
|
|
|
|
if (*a < *b)
|
|
|
|
comp = -1;
|
|
|
|
else if (*a > *b)
|
|
|
|
comp = 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (_descent)
|
|
|
|
comp = -comp;
|
|
|
|
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int compare_byte(unsigned char *a, unsigned char *b)
|
|
|
|
{
|
|
|
|
bool comp;
|
|
|
|
|
|
|
|
if (*a < *b)
|
|
|
|
comp = -1;
|
|
|
|
else if (*a > *b)
|
|
|
|
comp = 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (_descent)
|
|
|
|
comp = -comp;
|
|
|
|
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int compare_long(int64_t *a, int64_t *b)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
bool comp;
|
|
|
|
|
|
|
|
if (*a < *b)
|
|
|
|
comp = -1;
|
|
|
|
else if (*a > *b)
|
|
|
|
comp = 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (_descent)
|
|
|
|
comp = -comp;
|
|
|
|
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int compare_float(double *a, double *b)
|
|
|
|
{
|
|
|
|
bool comp;
|
|
|
|
|
|
|
|
if (*a < *b)
|
|
|
|
comp = -1;
|
|
|
|
else if (*a > *b)
|
|
|
|
comp = 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (_descent)
|
|
|
|
comp = -comp;
|
|
|
|
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int compare_single(float *a, float *b)
|
|
|
|
{
|
|
|
|
bool comp;
|
|
|
|
|
|
|
|
if (*a < *b)
|
|
|
|
comp = -1;
|
|
|
|
else if (*a > *b)
|
|
|
|
comp = 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (_descent)
|
|
|
|
comp = -comp;
|
|
|
|
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int compare_date(DATE *a, DATE *b)
|
|
|
|
{
|
|
|
|
bool comp;
|
|
|
|
|
|
|
|
comp = DATE_comp(a, b);
|
|
|
|
|
|
|
|
if (_descent)
|
|
|
|
comp = -comp;
|
|
|
|
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
2010-01-25 18:53:58 +01:00
|
|
|
int COMPARE_string_lang(const char *s1, int l1, const char *s2, int l2, bool nocase, bool throw)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
wchar_t *t1 = NULL;
|
|
|
|
wchar_t *t2 = NULL;
|
2008-10-05 01:46:15 +02:00
|
|
|
int i, cmp;
|
2008-10-21 11:28:34 +02:00
|
|
|
int lt1, lt2;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (l1 < 0)
|
|
|
|
l1 = s1 ? strlen(s1) : 0;
|
|
|
|
|
|
|
|
if (l2 < 0)
|
|
|
|
l2 = s2 ? strlen(s2) : 0;
|
|
|
|
|
|
|
|
if (l1 == 0)
|
|
|
|
{
|
|
|
|
if (l2 == 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
else if (l2 == 0)
|
|
|
|
return 1;
|
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
if (STRING_convert_to_unicode(&t1, <1, s1, l1)
|
|
|
|
|| STRING_convert_to_unicode(&t2, <2, s2, l2))
|
|
|
|
{
|
|
|
|
if (throw)
|
|
|
|
THROW(E_CONV);
|
|
|
|
else
|
|
|
|
goto __FAILED;
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (nocase)
|
|
|
|
{
|
2008-10-21 11:28:34 +02:00
|
|
|
for (i = 0; i < lt1; i++)
|
2008-10-05 01:46:15 +02:00
|
|
|
t1[i] = towlower(t1[i]);
|
2008-10-21 11:28:34 +02:00
|
|
|
for (i = 0; i < lt2; i++)
|
2008-10-05 01:46:15 +02:00
|
|
|
t2[i] = towlower(t2[i]);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-10-05 01:46:15 +02:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
cmp = wcscoll(t1, t2);
|
|
|
|
if (!errno)
|
2010-01-25 18:53:58 +01:00
|
|
|
return (cmp < 0) ? - 1 : (cmp > 0) ? 1 : 0;
|
2008-10-05 01:46:15 +02:00
|
|
|
|
|
|
|
__FAILED:
|
|
|
|
|
|
|
|
return nocase ? TABLE_compare_ignore_case(s1, l1, s2, l2) : TABLE_compare(s1, l1, s2, l2);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-01-25 18:53:58 +01:00
|
|
|
/*
|
|
|
|
Natural sort order.
|
|
|
|
Based on the algorithm made by Martin Pol (http://sourcefrog.net/projects/natsort/)
|
2010-12-18 05:27:59 +01:00
|
|
|
|
|
|
|
This software is copyright by Martin Pool, and made available under the same
|
|
|
|
licence as zlib:
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use
|
|
|
|
of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software in a
|
|
|
|
product, an acknowledgment in the product documentation would be appreciated but
|
|
|
|
is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
2010-01-25 18:53:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int strnatcmp_compare_right(const char *a, int la, const char *b, int lb)
|
|
|
|
{
|
|
|
|
int bias = 0;
|
|
|
|
unsigned char ca, cb;
|
|
|
|
|
|
|
|
/* The longest run of digits wins. That aside, the greatest
|
|
|
|
value wins, but we can't know that it will until we've scanned
|
|
|
|
both numbers to know that they have the same magnitude, so we
|
|
|
|
remember it in BIAS. */
|
|
|
|
|
|
|
|
for (;; a++, b++, la--, lb--)
|
|
|
|
{
|
|
|
|
ca = (la > 0) ? *a : 0;
|
|
|
|
cb = (lb > 0) ? *b : 0;
|
|
|
|
|
|
|
|
if (!isdigit(ca) && !isdigit(cb))
|
|
|
|
return bias;
|
|
|
|
else if (!isdigit(ca))
|
|
|
|
return -1;
|
|
|
|
else if (!isdigit(cb))
|
|
|
|
return +1;
|
|
|
|
else if (ca < cb)
|
|
|
|
{
|
|
|
|
if (!bias)
|
|
|
|
bias = -1;
|
|
|
|
}
|
|
|
|
else if (ca > cb)
|
|
|
|
{
|
|
|
|
if (!bias)
|
|
|
|
bias = +1;
|
|
|
|
}
|
2010-12-05 21:17:24 +01:00
|
|
|
else if (!ca) // && !cb)
|
2010-01-25 18:53:58 +01:00
|
|
|
return bias;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int strnatcmp_compare_left(const char *a, int la, const char *b, int lb)
|
|
|
|
{
|
|
|
|
unsigned char ca, cb;
|
|
|
|
|
|
|
|
/* Compare two left-aligned numbers: the first to have a
|
|
|
|
different value wins. */
|
|
|
|
for (;; a++, b++, la--, lb--)
|
|
|
|
{
|
|
|
|
ca = (la > 0) ? *a : 0;
|
|
|
|
cb = (lb > 0) ? *b : 0;
|
|
|
|
|
|
|
|
if (!isdigit(ca) && !isdigit(cb))
|
|
|
|
return 0;
|
|
|
|
else if (!isdigit(ca))
|
|
|
|
return -1;
|
|
|
|
else if (!isdigit(cb))
|
|
|
|
return +1;
|
|
|
|
else if (ca < cb)
|
|
|
|
return -1;
|
|
|
|
else if (ca > cb)
|
|
|
|
return +1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-27 04:28:06 +01:00
|
|
|
int COMPARE_string_natural(const char *a, int la, const char *b, int lb, bool nocase)
|
2010-01-25 18:53:58 +01:00
|
|
|
{
|
|
|
|
int ai, bi, lca, lcb;
|
|
|
|
unsigned char ca, cb;
|
|
|
|
int fractional, result;
|
|
|
|
|
|
|
|
ai = bi = 0;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (ai >= la)
|
|
|
|
{
|
|
|
|
ca = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ca = a[ai];
|
2011-09-21 14:42:56 +02:00
|
|
|
if (ca > ' ')
|
2010-01-25 18:53:58 +01:00
|
|
|
break;
|
|
|
|
ai++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (bi >= lb)
|
|
|
|
{
|
|
|
|
cb = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cb = b[bi];
|
2011-09-21 14:42:56 +02:00
|
|
|
if (cb > ' ')
|
2010-01-25 18:53:58 +01:00
|
|
|
break;
|
|
|
|
bi++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* process run of digits */
|
2011-09-23 22:17:57 +02:00
|
|
|
if (ca >= '0' && ca <= '9' && cb >= '0' && cb <= '9')
|
2010-01-25 18:53:58 +01:00
|
|
|
{
|
|
|
|
fractional = (ca == '0' || cb == '0');
|
|
|
|
|
|
|
|
if (fractional)
|
|
|
|
{
|
|
|
|
if ((result = strnatcmp_compare_left(a+ai, la-ai, b+bi, lb-bi)) != 0)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((result = strnatcmp_compare_right(a+ai, la-ai, b+bi, lb-bi)) != 0)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 21:11:20 +01:00
|
|
|
if (!ca)
|
2010-01-25 18:53:58 +01:00
|
|
|
{
|
2012-11-06 21:11:20 +01:00
|
|
|
if (!cb)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
2010-01-25 18:53:58 +01:00
|
|
|
}
|
2012-11-06 21:11:20 +01:00
|
|
|
else if (!cb)
|
|
|
|
return 1;
|
2010-01-25 18:53:58 +01:00
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
lca = STRING_utf8_get_char_length(ca);
|
|
|
|
lcb = STRING_utf8_get_char_length(cb);
|
2010-01-27 04:28:06 +01:00
|
|
|
if (lca > 1 || lcb > 1)
|
2010-01-25 18:53:58 +01:00
|
|
|
{
|
2010-01-27 04:28:06 +01:00
|
|
|
if ((result = COMPARE_string_lang(&a[ai], lca, &b[bi], lcb, nocase, FALSE)))
|
|
|
|
return result;
|
|
|
|
ai += lca;
|
|
|
|
bi += lcb;
|
2010-01-25 18:53:58 +01:00
|
|
|
}
|
2010-01-27 04:28:06 +01:00
|
|
|
else
|
2010-01-25 18:53:58 +01:00
|
|
|
{
|
2010-01-27 04:28:06 +01:00
|
|
|
if (nocase)
|
|
|
|
{
|
|
|
|
ca = toupper(ca);
|
|
|
|
cb = toupper(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ca < cb)
|
|
|
|
return -1;
|
|
|
|
else if (ca > cb)
|
|
|
|
return +1;
|
|
|
|
++ai; ++bi;
|
2010-01-25 18:53:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define IMPLEMENT_COMPARE_STRING(_name, _func) \
|
|
|
|
int compare_string_##_name(char **pa, char **pb) \
|
|
|
|
{ \
|
|
|
|
char *a; \
|
|
|
|
char *b; \
|
|
|
|
int comp; \
|
|
|
|
\
|
|
|
|
a = *pa; \
|
|
|
|
if (!a) \
|
|
|
|
a = ""; \
|
|
|
|
\
|
|
|
|
b = *pb; \
|
|
|
|
if (!b) \
|
|
|
|
b = ""; \
|
|
|
|
\
|
|
|
|
comp = _func(a, b); \
|
|
|
|
if (_descent) \
|
|
|
|
comp = -comp; \
|
|
|
|
return comp; \
|
|
|
|
}
|
|
|
|
|
|
|
|
IMPLEMENT_COMPARE_STRING(binary, strcmp)
|
|
|
|
IMPLEMENT_COMPARE_STRING(case, strcasecmp)
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static int compare_string_lang(char **pa, char **pb)
|
|
|
|
{
|
|
|
|
int diff = COMPARE_string_lang(*pa, -1, *pb, -1, FALSE, TRUE);
|
|
|
|
return _descent ? (-diff) : diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compare_string_lang_case(char **pa, char **pb)
|
|
|
|
{
|
|
|
|
int diff = COMPARE_string_lang(*pa, -1, *pb, -1, TRUE, TRUE);
|
|
|
|
return _descent ? (-diff) : diff;
|
|
|
|
}
|
|
|
|
|
2010-01-25 18:53:58 +01:00
|
|
|
int COMPARE_string_like(const char *s1, int l1, const char *s2, int l2)
|
|
|
|
{
|
2010-01-27 04:28:06 +01:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (REGEXP_match(s2, l2, s1, l1))
|
|
|
|
return 0;
|
|
|
|
result = TABLE_compare_ignore_case(s1, l1, s2, l2);
|
|
|
|
return (result < 0) ? -1 : (result > 0) ? 1 : 0;
|
2010-01-25 18:53:58 +01:00
|
|
|
}
|
|
|
|
|
2009-08-22 14:17:31 +02:00
|
|
|
static int compare_string_like(char **pa, char **pb)
|
|
|
|
{
|
|
|
|
int la = *pa ? strlen(*pa) : 0;
|
|
|
|
int lb = *pb ? strlen(*pb) : 0;
|
2010-08-03 13:00:42 +02:00
|
|
|
int diff = COMPARE_string_like(*pa, la, *pb, lb);
|
|
|
|
if (_descent)
|
|
|
|
return (-diff);
|
|
|
|
else
|
|
|
|
return diff;
|
|
|
|
//return REGEXP_match(*pb, lb, *pa, la) ? 0 : TABLE_compare_ignore_case(*pa, la, *pb, lb);
|
2009-08-22 14:17:31 +02:00
|
|
|
}
|
|
|
|
|
2010-01-27 04:28:06 +01:00
|
|
|
#define IMPLEMENT_COMPARE_STRING_NATURAL(_name, _nocase) \
|
2010-01-25 18:53:58 +01:00
|
|
|
static int compare_string_##_name(char **pa, char **pb) \
|
|
|
|
{ \
|
|
|
|
int la = *pa ? strlen(*pa) : 0; \
|
|
|
|
int lb = *pb ? strlen(*pb) : 0; \
|
2010-08-03 13:00:42 +02:00
|
|
|
int diff = COMPARE_string_natural(*pa, la, *pb, lb, _nocase); \
|
|
|
|
if (_descent) \
|
|
|
|
return (-diff); \
|
|
|
|
else \
|
|
|
|
return diff; \
|
2010-01-25 18:53:58 +01:00
|
|
|
}
|
|
|
|
|
2010-01-27 04:28:06 +01:00
|
|
|
IMPLEMENT_COMPARE_STRING_NATURAL(natural, FALSE)
|
|
|
|
IMPLEMENT_COMPARE_STRING_NATURAL(natural_case, TRUE)
|
2010-01-25 18:53:58 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
int COMPARE_object(void **a, void **b)
|
|
|
|
{
|
|
|
|
bool comp;
|
2009-01-13 01:42:55 +01:00
|
|
|
CLASS *ca, *cb;
|
|
|
|
|
2011-09-01 16:19:34 +02:00
|
|
|
/*{
|
|
|
|
STACK_BACKTRACE *bt = STACK_get_backtrace();
|
|
|
|
fprintf(stderr, "COMPARE_object\n");
|
|
|
|
DEBUG_print_backtrace(bt);
|
|
|
|
STACK_free_backtrace(&bt);
|
|
|
|
}*/
|
|
|
|
|
2012-07-10 21:57:43 +02:00
|
|
|
ca = OBJECT_class_null(*a);
|
|
|
|
cb = OBJECT_class_null(*b);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-09-03 23:11:55 +02:00
|
|
|
if (ca && cb)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-09-03 23:11:55 +02:00
|
|
|
if (ca->has_operators && CLASS_has_operator(ca, CO_COMP) && ca == cb)
|
|
|
|
{
|
|
|
|
void *func = ca->operators[CO_COMP];
|
|
|
|
comp = (*(int (*)(void *, void *))func)(*a, *b);
|
|
|
|
goto __RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ca->special[SPEC_COMPARE] != NO_SYMBOL)
|
|
|
|
{
|
|
|
|
STACK_check(1);
|
|
|
|
SP->_object.class = cb;
|
|
|
|
SP->_object.object = *b;
|
|
|
|
OBJECT_REF(*b);
|
|
|
|
SP++;
|
|
|
|
EXEC_special(SPEC_COMPARE, ca, *a, 1, FALSE);
|
|
|
|
VALUE_conv_integer(&SP[-1]);
|
|
|
|
SP--;
|
|
|
|
comp = SP->_integer.value;
|
|
|
|
goto __RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cb->special[SPEC_COMPARE] != NO_SYMBOL)
|
|
|
|
{
|
|
|
|
STACK_check(1);
|
|
|
|
SP->_object.class = ca;
|
|
|
|
SP->_object.object = *a;
|
|
|
|
OBJECT_REF(*a);
|
|
|
|
SP++;
|
|
|
|
EXEC_special(SPEC_COMPARE, cb, *b, 1, FALSE);
|
|
|
|
VALUE_conv_integer(&SP[-1]);
|
|
|
|
SP--;
|
|
|
|
comp = (- SP->_integer.value);
|
|
|
|
goto __RETURN;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2013-09-03 23:11:55 +02:00
|
|
|
comp = (*a == *b) ? 0 : (*a > *b) ? 1 : -1;
|
|
|
|
|
|
|
|
__RETURN:
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
return _descent ? (-comp) : comp;
|
|
|
|
}
|
|
|
|
|
2012-08-29 23:02:09 +02:00
|
|
|
int COMPARE_variant(VARIANT *a, VARIANT *b)
|
2012-08-29 22:56:13 +02:00
|
|
|
{
|
|
|
|
TYPE type;
|
|
|
|
VALUE value;
|
|
|
|
int comp;
|
|
|
|
|
|
|
|
if (a->type == T_NULL)
|
|
|
|
return b->type == T_NULL ? 0 : -1;
|
|
|
|
else if (b->type == T_NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (TYPE_is_object(a->type))
|
|
|
|
{
|
|
|
|
if (TYPE_is_object(b->type))
|
|
|
|
return COMPARE_object(&a->value._object, &b->value._object);
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (TYPE_is_object(b->type))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (a->type == b->type)
|
|
|
|
return (*COMPARE_get(a->type, 0))(&a->value, &b->value);
|
|
|
|
|
|
|
|
type = Max(a->type, b->type);
|
|
|
|
|
|
|
|
if (b->type == type)
|
|
|
|
{
|
|
|
|
VARIANT *c;
|
|
|
|
|
|
|
|
c = a;
|
|
|
|
a = b;
|
|
|
|
b = c;
|
|
|
|
_descent = !_descent;
|
|
|
|
}
|
|
|
|
|
|
|
|
value.type = T_VARIANT;
|
|
|
|
value._variant.vtype = b->type;
|
|
|
|
value._variant.value.data = b->value.data;
|
|
|
|
|
|
|
|
BORROW(&value);
|
|
|
|
VALUE_conv(&value, type);
|
|
|
|
VALUE_conv_variant(&value);
|
|
|
|
comp = (*COMPARE_get(type, 0))(&a->value, &value._variant.value);
|
|
|
|
RELEASE(&value);
|
|
|
|
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
COMPARE_FUNC COMPARE_get(TYPE type, int mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
_descent = (mode & GB_COMP_DESCENT) != 0;
|
|
|
|
mode &= GB_COMP_TYPE_MASK;
|
|
|
|
|
2008-09-01 01:45:47 +02:00
|
|
|
if (type >= T_OBJECT)
|
|
|
|
return (COMPARE_FUNC)COMPARE_object;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case T_INTEGER:
|
|
|
|
return (COMPARE_FUNC)compare_integer;
|
|
|
|
|
|
|
|
case T_SHORT:
|
|
|
|
return (COMPARE_FUNC)compare_short;
|
|
|
|
|
|
|
|
case T_BYTE:
|
|
|
|
case T_BOOLEAN:
|
|
|
|
return (COMPARE_FUNC)compare_byte;
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
return (COMPARE_FUNC)compare_long;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
return (COMPARE_FUNC)compare_float;
|
|
|
|
|
|
|
|
case T_SINGLE:
|
|
|
|
return (COMPARE_FUNC)compare_single;
|
|
|
|
|
|
|
|
case T_DATE:
|
|
|
|
return (COMPARE_FUNC)compare_date;
|
|
|
|
|
|
|
|
case T_STRING:
|
2010-01-27 04:28:06 +01:00
|
|
|
|
|
|
|
if (mode & GB_COMP_NATURAL)
|
2010-02-08 00:10:41 +01:00
|
|
|
return (COMPARE_FUNC)((mode & GB_COMP_NOCASE) ? compare_string_natural_case : compare_string_natural);
|
2010-01-27 04:28:06 +01:00
|
|
|
else if (mode & GB_COMP_LIKE)
|
|
|
|
return (COMPARE_FUNC)compare_string_like;
|
|
|
|
else if (mode & GB_COMP_LANG)
|
2010-02-08 00:10:41 +01:00
|
|
|
return (COMPARE_FUNC)((mode & GB_COMP_NOCASE) ? compare_string_lang_case : compare_string_lang);
|
2010-01-27 04:28:06 +01:00
|
|
|
else
|
2010-02-08 00:10:41 +01:00
|
|
|
return (COMPARE_FUNC)((mode & GB_COMP_NOCASE) ? compare_string_case : compare_string_binary);
|
2012-08-29 22:56:13 +02:00
|
|
|
|
|
|
|
case T_POINTER:
|
|
|
|
#ifdef OS_64BITS
|
|
|
|
return (COMPARE_FUNC)compare_long;
|
|
|
|
#else
|
|
|
|
return (COMPARE_FUNC)compare_integer;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case T_VARIANT:
|
2012-08-29 23:02:09 +02:00
|
|
|
return (COMPARE_FUNC)COMPARE_variant;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
return (COMPARE_FUNC)compare_nothing;
|
|
|
|
}
|
|
|
|
}
|