2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbx_c_string.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +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.
|
2007-12-30 17:41:49 +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.
|
2007-12-30 17:41:49 +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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_C_STRING_C
|
|
|
|
|
|
|
|
#include "gbx_info.h"
|
|
|
|
|
|
|
|
#ifndef GBX_INFO
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
2011-09-21 11:42:19 +02:00
|
|
|
#include "gb_common_case.h"
|
2015-07-12 00:44:09 +02:00
|
|
|
#include "gb_common_buffer.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include <wctype.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_table.h"
|
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gbx_exec.h"
|
2011-09-08 18:01:36 +02:00
|
|
|
#include "gbx_subr.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gbx_compare.h"
|
|
|
|
#include "gambas.h"
|
|
|
|
|
|
|
|
#include "gbx_c_string.h"
|
|
|
|
|
2014-10-30 17:13:10 +01:00
|
|
|
//#define DEBUG_CACHE
|
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
const char STRING_char_length[256] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 18:01:36 +02:00
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
|
|
|
|
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
/***************************************************************************/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
static int utf8_get_length(const char *sstr, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-04-29 02:24:14 +02:00
|
|
|
const uchar *str = (const uchar *)sstr;
|
2011-09-08 18:01:36 +02:00
|
|
|
int ulen;
|
2007-12-30 17:41:49 +01:00
|
|
|
int i;
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
ulen = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if ((str[i] & 0xC0) != 0x80)
|
|
|
|
ulen++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
return ulen;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
uint STRING_utf8_to_unicode(const char *sstr, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-04-29 02:24:14 +02:00
|
|
|
const uchar *str = (const uchar *)sstr;
|
2011-09-08 18:01:36 +02:00
|
|
|
uint unicode;
|
|
|
|
|
|
|
|
switch (len)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
unicode = (str[1] & 0x3F) + ((str[0] & 0x1F) << 6);
|
|
|
|
if (unicode < 0x80)
|
2015-07-12 00:44:09 +02:00
|
|
|
goto __INVALID;
|
2011-09-08 18:01:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
unicode = (str[2] & 0x3F) + ((str[1] & 0x3F) << 6) + ((str[0] & 0xF) << 12);
|
|
|
|
if (unicode < 0x800)
|
2015-07-12 00:44:09 +02:00
|
|
|
goto __INVALID;
|
2011-09-08 18:01:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
unicode = (str[3] & 0x3F) + ((str[2] & 0x3F) << 6) + ((str[1] & 0x3F) << 12) + ((str[0] & 0x7) << 18);
|
|
|
|
if (unicode < 0x10000)
|
2015-07-12 00:44:09 +02:00
|
|
|
goto __INVALID;
|
2011-09-08 18:01:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
unicode = (str[4] & 0x3F) + ((str[3] & 0x3F) << 6) + ((str[2] & 0x3F) << 12) + ((str[1] & 0x3F) << 18) + ((str[0] & 0x3) << 24);
|
|
|
|
if (unicode < 0x200000)
|
2015-07-12 00:44:09 +02:00
|
|
|
goto __INVALID;
|
2011-09-08 18:01:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
unicode = (str[5] & 0x3F) + ((str[4] & 0x3F) << 6) + ((str[3] & 0x3F) << 12) + ((str[2] & 0x3F) << 18) + ((str[1] & 0x3F) << 24) + ((str[0] & 0x1) << 30);
|
|
|
|
if (unicode < 0x4000000)
|
2015-07-12 00:44:09 +02:00
|
|
|
goto __INVALID;
|
2011-09-08 18:01:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
unicode = str[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return unicode;
|
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
__INVALID:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
return UNICODE_INVALID;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
void STRING_utf8_from_unicode(uint code, char *sstr)
|
2011-09-21 11:42:19 +02:00
|
|
|
{
|
2012-04-29 02:24:14 +02:00
|
|
|
uchar *str = (uchar *)sstr;
|
|
|
|
|
2011-09-21 11:42:19 +02:00
|
|
|
if (code < 0x80)
|
|
|
|
str[0] = code;
|
|
|
|
else if (code < 0x800)
|
|
|
|
{
|
|
|
|
str[0] = (code >> 6) | 0xC0;
|
|
|
|
str[1] = (code & 0x3F) | 0x80;
|
|
|
|
}
|
|
|
|
else if (code < 0x10000)
|
|
|
|
{
|
|
|
|
str[0] = (code >> 12) | 0xE0;
|
|
|
|
str[1] = ((code >> 6) & 0x3F) | 0x80;
|
|
|
|
str[2] = (code & 0x3F) | 0x80;
|
|
|
|
}
|
|
|
|
else if (code < 0x200000)
|
|
|
|
{
|
|
|
|
str[0] = (code >> 18) | 0xF0;
|
|
|
|
str[1] = ((code >> 12) & 0x3F) | 0x80;
|
|
|
|
str[2] = ((code >> 6) & 0x3F) | 0x80;
|
|
|
|
str[3] = (code & 0x3F) | 0x80;
|
|
|
|
}
|
|
|
|
else if (code < 0x4000000)
|
|
|
|
{
|
|
|
|
str[0] = (code >> 24) | 0xF8;
|
|
|
|
str[1] = ((code >> 18) & 0x3F) | 0x80;
|
|
|
|
str[2] = ((code >> 12) & 0x3F) | 0x80;
|
|
|
|
str[3] = ((code >> 6) & 0x3F) | 0x80;
|
|
|
|
str[4] = (code & 0x3F) | 0x80;
|
|
|
|
}
|
|
|
|
else if (code < 0x80000000)
|
|
|
|
{
|
|
|
|
str[0] = (code >> 31) | 0xFC;
|
|
|
|
str[1] = ((code >> 24) & 0x3F)| 0x80;
|
|
|
|
str[2] = ((code >> 18) & 0x3F) | 0x80;
|
|
|
|
str[3] = ((code >> 12) & 0x3F) | 0x80;
|
|
|
|
str[4] = ((code >> 6) & 0x3F) | 0x80;
|
|
|
|
str[5] = (code & 0x3F) | 0x80;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str[0] = 0;
|
|
|
|
}
|
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
int COMMON_get_unicode_char()
|
|
|
|
{
|
|
|
|
int lc;
|
|
|
|
uint uc;
|
|
|
|
|
|
|
|
lc = STRING_utf8_get_char_length(*COMMON_get_current());
|
|
|
|
if (COMMON_pos + lc > COMMON_len)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
uc = STRING_utf8_to_unicode(COMMON_get_current(), lc);
|
|
|
|
COMMON_pos += lc;
|
|
|
|
return (int)uc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
/***************************************************************************/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
char *STRING_utf8_current = NULL;
|
2014-10-30 17:13:10 +01:00
|
|
|
static const char *_utf8_current_start = NULL;
|
2018-04-20 18:26:52 +02:00
|
|
|
static int _utf8_current_len = 0;
|
2012-09-15 04:17:12 +02:00
|
|
|
|
|
|
|
#define UTF8_MAX_COUNT 256
|
|
|
|
#define UTF8_MAX_CACHE 64
|
|
|
|
|
|
|
|
struct {
|
|
|
|
ushort pos[UTF8_MAX_COUNT];
|
|
|
|
ushort last_pos;
|
|
|
|
ushort cnext;
|
|
|
|
int lpos;
|
|
|
|
int lindex;
|
|
|
|
int cindex[UTF8_MAX_CACHE];
|
|
|
|
int cpos[UTF8_MAX_CACHE];
|
|
|
|
}
|
|
|
|
_utf8 = { { 0 } };
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-10-30 17:13:10 +01:00
|
|
|
static int utf8_get_pos(const char *ref, const char *start, int len, int index)
|
2011-09-08 18:01:36 +02:00
|
|
|
{
|
2014-10-30 17:13:10 +01:00
|
|
|
const uchar *str;
|
2012-09-15 04:17:12 +02:00
|
|
|
int i, j, pos;
|
|
|
|
int min_index, min_i;
|
2011-09-08 18:01:36 +02:00
|
|
|
|
2014-10-30 17:13:10 +01:00
|
|
|
#ifdef DEBUG_CACHE
|
|
|
|
fprintf(stderr, "utf8_get_pos: [%p] %p %d %d\n", ref, start, len, index);
|
|
|
|
#endif
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
if (index <= 0)
|
|
|
|
return 0;
|
2014-10-30 17:13:10 +01:00
|
|
|
|
2018-04-20 18:26:52 +02:00
|
|
|
if (ref != STRING_utf8_current || start != _utf8_current_start || len != _utf8_current_len)
|
2011-09-08 18:01:36 +02:00
|
|
|
{
|
2012-04-29 02:24:14 +02:00
|
|
|
STRING_utf8_current = (char *)ref;
|
2014-10-30 17:13:10 +01:00
|
|
|
_utf8_current_start = start;
|
2018-04-20 18:26:52 +02:00
|
|
|
_utf8_current_len = len;
|
2014-10-30 17:13:10 +01:00
|
|
|
#ifdef DEBUG_CACHE
|
|
|
|
fprintf(stderr, "current -> %p / %ld\n", STRING_utf8_current, _utf8_current_start - STRING_utf8_current);
|
|
|
|
#endif
|
2012-09-15 04:17:12 +02:00
|
|
|
CLEAR(&_utf8);
|
2011-09-08 18:01:36 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 18:03:54 +02:00
|
|
|
str = (const uchar *)start;
|
2014-10-30 17:13:10 +01:00
|
|
|
|
2012-09-15 04:17:12 +02:00
|
|
|
if (index < UTF8_MAX_COUNT)
|
|
|
|
{
|
|
|
|
if (index <= _utf8.last_pos)
|
2014-10-30 17:13:10 +01:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_CACHE
|
|
|
|
fprintf(stderr, "cached -> %d\n", _utf8.pos[index]);
|
|
|
|
#endif
|
2015-05-18 18:03:54 +02:00
|
|
|
pos = _utf8.pos[index];
|
|
|
|
if (pos >= len)
|
|
|
|
pos = len;
|
|
|
|
return pos;
|
2014-10-30 17:13:10 +01:00
|
|
|
}
|
2012-09-15 04:17:12 +02:00
|
|
|
|
|
|
|
pos = _utf8.pos[_utf8.last_pos];
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (pos >= len)
|
|
|
|
return len;
|
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
pos += STRING_utf8_get_char_length(str[pos]);
|
2012-09-15 04:17:12 +02:00
|
|
|
_utf8.pos[++_utf8.last_pos] = pos;
|
|
|
|
|
|
|
|
if (index == _utf8.last_pos)
|
2014-10-30 17:13:10 +01:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_CACHE
|
|
|
|
fprintf(stderr, "search -> %d\n", pos);
|
|
|
|
#endif
|
2012-09-15 04:17:12 +02:00
|
|
|
return pos;
|
2014-10-30 17:13:10 +01:00
|
|
|
}
|
2012-09-15 04:17:12 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-08 18:01:36 +02:00
|
|
|
|
2014-10-30 17:13:10 +01:00
|
|
|
#ifdef DEBUG_CACHE
|
|
|
|
fprintf(stderr, "index = %d\n", index);
|
|
|
|
#endif
|
2011-09-08 18:01:36 +02:00
|
|
|
|
2012-09-15 04:17:12 +02:00
|
|
|
if (index == _utf8.lindex)
|
2015-06-26 15:18:39 +02:00
|
|
|
{
|
|
|
|
pos = _utf8.lpos;
|
|
|
|
if (pos >= len)
|
|
|
|
pos = len;
|
2015-06-29 17:30:00 +02:00
|
|
|
return pos;
|
2015-06-26 15:18:39 +02:00
|
|
|
}
|
2011-09-08 18:01:36 +02:00
|
|
|
|
2012-09-15 04:17:12 +02:00
|
|
|
min_index = 0;
|
|
|
|
min_i = -1;
|
|
|
|
|
|
|
|
for (j = 0; j < UTF8_MAX_CACHE; j++)
|
2011-09-08 18:01:36 +02:00
|
|
|
{
|
2012-09-15 04:17:12 +02:00
|
|
|
i = (_utf8.cnext + UTF8_MAX_CACHE - j - 1) % UTF8_MAX_CACHE;
|
|
|
|
|
|
|
|
if (_utf8.cindex[i] == 0)
|
|
|
|
break;
|
2012-09-16 16:54:10 +02:00
|
|
|
|
2012-09-15 04:17:12 +02:00
|
|
|
if ((index >= _utf8.cindex[i]) && (index < (_utf8.cindex[i] + 256)))
|
|
|
|
{
|
2014-10-30 17:13:10 +01:00
|
|
|
#ifdef DEBUG_CACHE
|
|
|
|
fprintf(stderr, "use cache %d (%d)\n", i, _utf8.cindex[i]);
|
|
|
|
#endif
|
2012-09-15 04:17:12 +02:00
|
|
|
pos = _utf8.cpos[i];
|
|
|
|
j = _utf8.cindex[i];
|
|
|
|
goto __CALC_POS;
|
|
|
|
}
|
|
|
|
else if (_utf8.cindex[i] > min_index && _utf8.cindex[i] < index)
|
|
|
|
{
|
|
|
|
min_index = _utf8.cindex[i];
|
|
|
|
min_i = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
j = index & ~0xFF;
|
2012-04-29 02:24:14 +02:00
|
|
|
|
2012-09-15 04:17:12 +02:00
|
|
|
if (min_i < 0)
|
|
|
|
{
|
|
|
|
pos = 0;
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = _utf8.cpos[min_i];
|
|
|
|
i = _utf8.cindex[min_i];
|
2011-09-08 18:01:36 +02:00
|
|
|
}
|
|
|
|
|
2014-10-30 17:13:10 +01:00
|
|
|
#ifdef DEBUG_CACHE
|
|
|
|
fprintf(stderr, "add cache %d: %d / %d\n", _utf8.cnext, j, index - i);
|
|
|
|
#endif
|
2012-09-15 04:17:12 +02:00
|
|
|
|
|
|
|
for (; i < j; i++)
|
2012-03-03 03:17:24 +01:00
|
|
|
{
|
|
|
|
if (pos >= len)
|
2012-09-15 04:17:12 +02:00
|
|
|
{
|
|
|
|
pos = len;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-12 00:44:09 +02:00
|
|
|
pos += STRING_utf8_get_char_length(str[pos]);
|
2012-09-15 04:17:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_utf8.cindex[_utf8.cnext] = j;
|
|
|
|
_utf8.cpos[_utf8.cnext] = pos;
|
|
|
|
_utf8.cnext = (_utf8.cnext + 1) % UTF8_MAX_CACHE;
|
2012-04-29 02:24:14 +02:00
|
|
|
|
2012-09-15 04:17:12 +02:00
|
|
|
__CALC_POS:
|
|
|
|
|
|
|
|
for (i = j; i < index; i++)
|
|
|
|
{
|
|
|
|
if (pos >= len)
|
|
|
|
{
|
|
|
|
pos = len;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-12 00:44:09 +02:00
|
|
|
pos += STRING_utf8_get_char_length(str[pos]);
|
2012-03-03 03:17:24 +01:00
|
|
|
}
|
2011-09-08 18:01:36 +02:00
|
|
|
|
2012-09-15 04:17:12 +02:00
|
|
|
_utf8.lindex = index;
|
|
|
|
_utf8.lpos = pos;
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
return pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-09-21 11:42:19 +02:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int byte_to_index(const char *str, int len, int byte)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 18:01:36 +02:00
|
|
|
if (byte <= 0)
|
|
|
|
return 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
if (byte > len)
|
|
|
|
byte = len;
|
|
|
|
|
|
|
|
return utf8_get_length(str, byte);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
static int index_to_byte(const char *ref, const char *str, int len, int index)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (index <= 0)
|
|
|
|
return 0;
|
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
return utf8_get_pos(ref, str, len, index - 1) + 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
BEGIN_METHOD(String_Pos, GB_STRING str; GB_INTEGER index)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
GB_ReturnInteger(index_to_byte(VARG(str).addr, STRING(str), LENGTH(str), VARG(index)));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
BEGIN_METHOD(String_Len, GB_STRING str)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_ReturnInteger(utf8_get_length(STRING(str), LENGTH(str)));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
BEGIN_METHOD(String_Index, GB_STRING str; GB_INTEGER pos)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_ReturnInteger(byte_to_index(STRING(str), LENGTH(str), VARG(pos)));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
static void String_Mid(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 18:01:36 +02:00
|
|
|
char *str;
|
2012-04-29 02:24:14 +02:00
|
|
|
char *ref;
|
2011-09-08 18:01:36 +02:00
|
|
|
int start, length;
|
2012-03-03 03:17:24 +01:00
|
|
|
int len, ulen, upos;
|
2011-09-08 18:01:36 +02:00
|
|
|
bool null;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
null = SUBR_check_string(PARAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
VALUE_conv_integer(&PARAM[1]);
|
|
|
|
start = PARAM[1]._integer.value - 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
if (start < 0)
|
|
|
|
THROW(E_ARG);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
if (null)
|
|
|
|
goto _SUBR_MID_FIN;
|
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
ref = PARAM->_string.addr;
|
|
|
|
str = ref + PARAM->_string.start;
|
2011-09-08 18:01:36 +02:00
|
|
|
len = PARAM->_string.len;
|
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
ulen = utf8_get_pos(ref, str, len, start);
|
2011-09-08 18:01:36 +02:00
|
|
|
if (ulen >= len)
|
|
|
|
{
|
|
|
|
VOID_STRING(PARAM);
|
|
|
|
goto _SUBR_MID_FIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
PARAM->_string.start += ulen;
|
|
|
|
//str += ulen;
|
|
|
|
//len -= ulen;
|
|
|
|
|
|
|
|
if (NPARAM == 2)
|
|
|
|
{
|
|
|
|
ulen = len - ulen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE_conv_integer(&PARAM[2]);
|
|
|
|
length = PARAM[2]._integer.value;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
if (length < 0)
|
|
|
|
length += utf8_get_length(str, len) - start;
|
|
|
|
|
|
|
|
if (length == 1)
|
2012-04-29 02:24:14 +02:00
|
|
|
{
|
2015-07-12 00:44:09 +02:00
|
|
|
ulen = STRING_utf8_get_char_length(str[ulen]);
|
2012-04-29 02:24:14 +02:00
|
|
|
}
|
2011-09-08 18:01:36 +02:00
|
|
|
else
|
2012-03-03 03:17:24 +01:00
|
|
|
{
|
2012-04-29 02:24:14 +02:00
|
|
|
upos = utf8_get_pos(ref, str, len, start + length);
|
2012-03-03 03:17:24 +01:00
|
|
|
if (upos > len)
|
|
|
|
upos = len;
|
|
|
|
ulen = upos - ulen;
|
|
|
|
}
|
2011-09-08 18:01:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ulen <= 0)
|
|
|
|
{
|
|
|
|
VOID_STRING(PARAM);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PARAM->_string.len = ulen;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
_SUBR_MID_FIN:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
SP -= NPARAM;
|
|
|
|
SP++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
static void String_Left(ushort code)
|
|
|
|
{
|
|
|
|
int val;
|
2012-04-29 02:24:14 +02:00
|
|
|
char *ref;
|
2011-09-08 18:01:36 +02:00
|
|
|
char *str;
|
|
|
|
int len, ulen;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
if (!SUBR_check_string(PARAM))
|
|
|
|
{
|
|
|
|
if (NPARAM == 1)
|
|
|
|
val = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE_conv_integer(&PARAM[1]);
|
|
|
|
val = PARAM[1]._integer.value;
|
|
|
|
}
|
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
ref = PARAM->_string.addr;
|
|
|
|
str = ref + PARAM->_string.start;
|
2011-09-08 18:01:36 +02:00
|
|
|
len = PARAM->_string.len;
|
|
|
|
|
|
|
|
if (val < 0)
|
|
|
|
val += utf8_get_length(str, len);
|
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
ulen = utf8_get_pos(ref, str, len, val);
|
2011-09-08 18:01:36 +02:00
|
|
|
PARAM->_string.len = ulen;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
SP -= NPARAM;
|
|
|
|
SP++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
static void String_Right(ushort code)
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
char *str;
|
2012-04-29 02:24:14 +02:00
|
|
|
char *ref;
|
2011-09-08 18:01:36 +02:00
|
|
|
int len, ulen;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
if (!SUBR_check_string(PARAM))
|
|
|
|
{
|
|
|
|
if (NPARAM == 1)
|
|
|
|
val = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE_conv_integer(&PARAM[1]);
|
|
|
|
val = PARAM[1]._integer.value;
|
|
|
|
}
|
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
ref = PARAM->_string.addr;
|
|
|
|
str = ref + PARAM->_string.start;
|
2011-09-08 18:01:36 +02:00
|
|
|
len = PARAM->_string.len;
|
|
|
|
|
|
|
|
if (val < 0)
|
|
|
|
val = (-val);
|
|
|
|
else
|
|
|
|
val = utf8_get_length(str, len) - val;
|
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
ulen = utf8_get_pos(ref, str, len, val);
|
2011-09-08 18:01:36 +02:00
|
|
|
|
|
|
|
PARAM->_string.start += ulen;
|
|
|
|
PARAM->_string.len -= ulen;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
SP -= NPARAM;
|
|
|
|
SP++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
bool STRING_convert_to_unicode(wchar_t **pwstr, int *pwlen, const char *str, int len)
|
|
|
|
{
|
|
|
|
char *result;
|
|
|
|
int wlen = utf8_get_length(str, len);
|
|
|
|
int i, lc;
|
|
|
|
wchar_t *wstr;
|
|
|
|
|
2012-05-15 03:22:45 +02:00
|
|
|
result = STRING_new_temp(NULL, (wlen + 1) * sizeof(wchar_t) - 1);
|
2011-09-26 03:46:48 +02:00
|
|
|
wstr = (wchar_t *)result;
|
|
|
|
|
|
|
|
for (i = 0; i < wlen; i++)
|
|
|
|
{
|
2015-07-12 00:44:09 +02:00
|
|
|
lc = STRING_utf8_get_char_length(*str);
|
|
|
|
wstr[i] = (wchar_t)STRING_utf8_to_unicode(str, lc);
|
2011-09-26 03:46:48 +02:00
|
|
|
if (wstr[i] == UNICODE_INVALID)
|
|
|
|
return TRUE;
|
|
|
|
str += lc;
|
|
|
|
}
|
|
|
|
|
2011-09-26 05:00:00 +02:00
|
|
|
wstr[wlen] = 0;
|
2011-09-26 03:46:48 +02:00
|
|
|
*pwstr = wstr;
|
|
|
|
*pwlen = wlen;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-02-22 18:59:07 +01:00
|
|
|
static bool convert_to_unicode(wchar_t **wstr, int *wlen, const char *str, int len, bool upper)
|
|
|
|
{
|
|
|
|
wchar_t *wtemp;
|
|
|
|
int i, l;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
*wstr = NULL;
|
|
|
|
*wlen = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
if (STRING_convert_to_unicode(&wtemp, &l, str, len))
|
2011-02-22 18:59:07 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (upper)
|
|
|
|
{
|
|
|
|
for (i = 0; i < l; i++)
|
|
|
|
wtemp[i] = towupper(wtemp[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < l; i++)
|
|
|
|
wtemp[i] = towlower(wtemp[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
*wstr = wtemp;
|
|
|
|
*wlen = l;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static void convert_string(char *str, int len, bool upper)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-21 11:42:19 +02:00
|
|
|
char *result;
|
|
|
|
char *p, *pe;
|
|
|
|
char c;
|
|
|
|
int lc;
|
|
|
|
wchar_t wc;
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
{
|
2011-10-24 16:52:20 +02:00
|
|
|
GB_ReturnVoidString();
|
2011-09-21 11:42:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = STRING_new_temp(str, len);
|
|
|
|
|
|
|
|
p = result;
|
|
|
|
pe = &result[len];
|
|
|
|
|
|
|
|
if (upper)
|
|
|
|
{
|
|
|
|
while (p < pe)
|
|
|
|
{
|
|
|
|
c = *p;
|
2015-07-12 00:44:09 +02:00
|
|
|
lc = STRING_utf8_get_char_length(c);
|
2011-09-21 11:42:19 +02:00
|
|
|
|
|
|
|
if (lc == 1)
|
|
|
|
{
|
|
|
|
*p = toupper(c);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-12 00:44:09 +02:00
|
|
|
wc = (wchar_t)STRING_utf8_to_unicode(p, lc);
|
2011-09-21 11:42:19 +02:00
|
|
|
wc = towupper(wc);
|
|
|
|
// We suppose that the conversion does not change the number of bytes!
|
2015-07-12 00:44:09 +02:00
|
|
|
STRING_utf8_from_unicode((uint)wc, p);
|
|
|
|
//if (STRING_utf8_get_char_length(*p) != lc)
|
2011-09-21 11:42:19 +02:00
|
|
|
// fprintf(stderr, "convert_string: not the same number of bytes!\n");
|
|
|
|
p += lc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (p < pe)
|
|
|
|
{
|
|
|
|
c = *p;
|
2015-07-12 00:44:09 +02:00
|
|
|
lc = STRING_utf8_get_char_length(c);
|
2011-09-21 11:42:19 +02:00
|
|
|
|
|
|
|
if (lc == 1)
|
|
|
|
{
|
|
|
|
*p = tolower(c);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-12 00:44:09 +02:00
|
|
|
wc = (wchar_t)STRING_utf8_to_unicode(p, lc);
|
2011-09-21 11:42:19 +02:00
|
|
|
wc = towlower(wc);
|
|
|
|
// We suppose that the conversion does not change the number of bytes!
|
2015-07-12 00:44:09 +02:00
|
|
|
STRING_utf8_from_unicode((uint)wc, p);
|
|
|
|
//if (STRING_utf8_get_char_length(*p) != lc)
|
2011-09-21 11:42:19 +02:00
|
|
|
// fprintf(stderr, "convert_string: not the same number of bytes!\n");
|
|
|
|
p += lc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_ReturnString(result);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
BEGIN_METHOD(String_Lower, GB_STRING str)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
convert_string(STRING(str), LENGTH(str), FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
BEGIN_METHOD(String_Upper, GB_STRING str)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
convert_string(STRING(str), LENGTH(str), TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2012-09-05 20:36:59 +02:00
|
|
|
BEGIN_METHOD(String_UCaseFirst, GB_STRING str)
|
|
|
|
|
|
|
|
char *str = STRING(str);
|
|
|
|
int len = LENGTH(str);
|
|
|
|
char *result;
|
|
|
|
int lc;
|
|
|
|
wchar_t wc;
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
{
|
|
|
|
GB_ReturnVoidString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = STRING_new_temp(str, len);
|
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
lc = STRING_utf8_get_char_length(result[0]);
|
2012-09-05 20:36:59 +02:00
|
|
|
|
|
|
|
if (lc == 1)
|
|
|
|
{
|
|
|
|
result[0] = toupper(result[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-12 00:44:09 +02:00
|
|
|
wc = (wchar_t)STRING_utf8_to_unicode(result, lc);
|
2012-09-05 20:36:59 +02:00
|
|
|
wc = towupper(wc);
|
|
|
|
// We suppose that the conversion does not change the number of bytes!
|
2015-07-12 00:44:09 +02:00
|
|
|
STRING_utf8_from_unicode((uint)wc, result);
|
2012-09-05 20:36:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GB_ReturnString(result);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
BEGIN_METHOD(String_Chr, GB_INTEGER code)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-21 11:42:19 +02:00
|
|
|
char temp[8];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
STRING_utf8_from_unicode(VARG(code), temp);
|
|
|
|
temp[STRING_utf8_get_char_length(temp[0])] = 0;
|
2011-09-21 11:42:19 +02:00
|
|
|
GB_ReturnNewZeroString(temp);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-09-21 11:42:19 +02:00
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
BEGIN_METHOD(String_Code, GB_STRING str; GB_INTEGER index)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
char *str;
|
|
|
|
int len, index, pos, lc;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
index = VARGOPT(index, 1);
|
|
|
|
if (index < 1)
|
|
|
|
{
|
|
|
|
GB_ReturnInteger(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
str = STRING(str);
|
|
|
|
len = LENGTH(str);
|
2016-03-31 21:06:03 +02:00
|
|
|
|
|
|
|
if (index > len)
|
|
|
|
{
|
|
|
|
GB_ReturnInteger(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
pos = utf8_get_pos(VARG(str).addr, str, len, index - 1);
|
2015-07-12 00:44:09 +02:00
|
|
|
lc = STRING_utf8_get_char_length(str[pos]);
|
2011-09-08 18:01:36 +02:00
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
GB_ReturnInteger(STRING_utf8_to_unicode(&str[pos], lc));
|
2011-09-08 18:01:36 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
END_METHOD
|
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
static void string_search(const char *str, const char *ref, int len, const char *pattern, int lenp, int start, bool right, bool nocase)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-01-17 22:39:26 +01:00
|
|
|
int pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-12-06 19:51:35 +01:00
|
|
|
if (lenp == 0)
|
|
|
|
goto __ERROR;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (start)
|
2012-04-29 02:24:14 +02:00
|
|
|
start = index_to_byte(ref, str, len, start);
|
2011-02-22 18:59:07 +01:00
|
|
|
|
|
|
|
if (!nocase)
|
|
|
|
{
|
|
|
|
pos = STRING_search(str, len, pattern, lenp, start, right, FALSE);
|
|
|
|
pos = byte_to_index(str, len, pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wchar_t *wstr;
|
|
|
|
int lstr;
|
|
|
|
wchar_t *wpattern;
|
|
|
|
int lpattern;
|
|
|
|
|
|
|
|
if (convert_to_unicode(&wstr, &lstr, str, len, TRUE))
|
|
|
|
goto __ERROR;
|
|
|
|
|
|
|
|
if (convert_to_unicode(&wpattern, &lpattern, pattern, lenp, TRUE))
|
|
|
|
goto __ERROR;
|
|
|
|
|
|
|
|
pos = STRING_search((char *)wstr, lstr * sizeof(wchar_t), (char *)wpattern, lpattern * sizeof(wchar_t), start * sizeof(wchar_t), right, FALSE);
|
|
|
|
if (pos)
|
|
|
|
pos = (pos - 1) / sizeof(wchar_t) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_ReturnInteger(pos);
|
|
|
|
return;
|
|
|
|
|
|
|
|
__ERROR:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-22 18:59:07 +01:00
|
|
|
GB_ReturnInteger(0);
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
BEGIN_METHOD(String_Instr, GB_STRING str; GB_STRING pattern; GB_INTEGER start; GB_INTEGER mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
string_search(STRING(str), VARG(str).addr, LENGTH(str), STRING(pattern), LENGTH(pattern), VARGOPT(start, 0), FALSE, VARGOPT(mode, GB_COMP_BINARY) == GB_COMP_NOCASE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
BEGIN_METHOD(String_RInstr, GB_STRING str; GB_STRING pattern; GB_INTEGER start; GB_INTEGER mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-04-29 02:24:14 +02:00
|
|
|
string_search(STRING(str), VARG(str).addr, LENGTH(str), STRING(pattern), LENGTH(pattern), VARGOPT(start, 0), TRUE, VARGOPT(mode, GB_COMP_BINARY) == GB_COMP_NOCASE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
BEGIN_METHOD(String_Comp, GB_STRING str1; GB_STRING str2; GB_INTEGER mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-02-08 00:10:41 +01:00
|
|
|
int mode = VARGOPT(mode, GB_COMP_BINARY) | GB_COMP_LANG;
|
|
|
|
bool nocase = (mode & GB_COMP_NOCASE) != 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-02-08 00:10:41 +01:00
|
|
|
if (mode & GB_COMP_NATURAL)
|
|
|
|
GB_ReturnInteger(COMPARE_string_natural(STRING(str1), LENGTH(str1), STRING(str2), LENGTH(str2), nocase));
|
|
|
|
else
|
|
|
|
GB_ReturnInteger(COMPARE_string_lang(STRING(str1), LENGTH(str1), STRING(str2), LENGTH(str2), nocase, TRUE));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-08-27 21:49:48 +02:00
|
|
|
#define IS_VALID(_char) \
|
2011-09-08 18:01:36 +02:00
|
|
|
((_char) < 0x110000 && \
|
|
|
|
(((_char) & 0xFFFFF800) != 0xD800) && \
|
|
|
|
((_char) < 0xFDD0 || (_char) > 0xFDEF) && \
|
|
|
|
((_char) & 0xFFFE) != 0xFFFE)
|
2011-08-27 21:49:48 +02:00
|
|
|
|
|
|
|
BEGIN_METHOD(String_IsValid, GB_STRING str)
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
const uchar *str;
|
|
|
|
int len, lc;
|
2011-08-27 21:49:48 +02:00
|
|
|
uint unicode;
|
|
|
|
bool valid = FALSE;
|
|
|
|
int i;
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
str = (const uchar *)STRING(str);
|
|
|
|
len = LENGTH(str);
|
|
|
|
|
|
|
|
while (len)
|
2011-08-27 21:49:48 +02:00
|
|
|
{
|
2015-07-12 00:44:09 +02:00
|
|
|
lc = STRING_utf8_get_char_length(*str);
|
2011-09-08 18:01:36 +02:00
|
|
|
len -= lc;
|
|
|
|
if (len < 0)
|
2011-08-27 21:49:48 +02:00
|
|
|
goto _INVALID;
|
|
|
|
|
|
|
|
//for (i = 0; i < len; i++)
|
|
|
|
// fprintf(stderr, "%02X ", str[i]);
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
for (i = 1; i < lc; i++)
|
2011-08-27 21:49:48 +02:00
|
|
|
{
|
|
|
|
if ((str[i] & 0xC0) != 0x80)
|
|
|
|
goto _INVALID;
|
|
|
|
}
|
|
|
|
|
2015-07-12 00:44:09 +02:00
|
|
|
unicode = STRING_utf8_to_unicode((char *)str, lc);
|
2011-09-08 18:01:36 +02:00
|
|
|
if (unicode == UNICODE_INVALID)
|
|
|
|
goto _INVALID;
|
2011-08-27 21:49:48 +02:00
|
|
|
if (!IS_VALID(unicode))
|
|
|
|
goto _INVALID;
|
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
str += lc;
|
2011-08-27 21:49:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
valid = TRUE;
|
|
|
|
|
|
|
|
_INVALID:
|
|
|
|
|
|
|
|
GB_ReturnBoolean(valid);
|
|
|
|
//fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
GB_DESC NATIVE_String[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2014-09-07 23:21:14 +02:00
|
|
|
GB_DECLARE_STATIC("String"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_STATIC_METHOD("Len", "i", String_Len, "(String)s"),
|
2011-08-27 21:49:48 +02:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_STATIC_FAST_METHOD("Mid", "s", String_Mid, "(String)s(Start)i[(Length)i]"),
|
|
|
|
GB_STATIC_FAST_METHOD("Mid$", "s", String_Mid, "(String)s(Start)i[(Length)i]"),
|
|
|
|
GB_STATIC_FAST_METHOD("Left", "s", String_Left, "(String)s[(Length)i]"),
|
|
|
|
GB_STATIC_FAST_METHOD("Left$", "s", String_Left, "(String)s[(Length)i]"),
|
|
|
|
GB_STATIC_FAST_METHOD("Right", "s", String_Right, "(String)s[(Length)i]"),
|
|
|
|
GB_STATIC_FAST_METHOD("Right$", "s", String_Right, "(String)s[(Length)i]"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_STATIC_METHOD("Upper", "s", String_Upper, "(String)s"),
|
|
|
|
GB_STATIC_METHOD("Upper$", "s", String_Upper, "(String)s"),
|
|
|
|
GB_STATIC_METHOD("UCase", "s", String_Upper, "(String)s"),
|
|
|
|
GB_STATIC_METHOD("UCase$", "s", String_Upper, "(String)s"),
|
2012-09-05 20:36:59 +02:00
|
|
|
GB_STATIC_METHOD("UCaseFirst", "s", String_UCaseFirst, "(String)s"),
|
|
|
|
GB_STATIC_METHOD("UCaseFirst$", "s", String_UCaseFirst, "(String)s"),
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_STATIC_METHOD("Lower", "s", String_Lower, "(String)s"),
|
|
|
|
GB_STATIC_METHOD("Lower$", "s", String_Lower, "(String)s"),
|
|
|
|
GB_STATIC_METHOD("LCase", "s", String_Lower, "(String)s"),
|
|
|
|
GB_STATIC_METHOD("LCase$", "s", String_Lower, "(String)s"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_STATIC_METHOD("InStr", "i", String_Instr, "(String)s(Pattern)s[(From)i(Mode)i]"),
|
|
|
|
GB_STATIC_METHOD("RInStr", "i", String_RInstr, "(String)s(Pattern)s[(From)i(Mode)i]"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_STATIC_METHOD("Comp", "i", String_Comp, "(String)s(String2)s[(Mode)i]"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_STATIC_METHOD("Byte", "i", String_Pos, "(String)s(Index)i"),
|
|
|
|
GB_STATIC_METHOD("Pos", "i", String_Pos, "(String)s(Index)i"),
|
|
|
|
GB_STATIC_METHOD("Index", "i", String_Index, "(String)s(Byte)i"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_STATIC_METHOD("Chr", "s", String_Chr, "(Unicode)i"),
|
|
|
|
GB_STATIC_METHOD("Chr$", "s", String_Chr, "(Unicode)i"),
|
|
|
|
GB_STATIC_METHOD("Code", "i", String_Code, "(String)s[(Index)i]"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_STATIC_METHOD("IsValid", "b", String_IsValid, "(String)s"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 18:01:36 +02:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|