2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
subr_string.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
The String management subroutines
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
(c) 2000-2007 Benoit Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +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 1, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +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
|
|
|
|
2009-01-02 01:05:38 +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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
#include "gb_common_case.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <regex.h>
|
|
|
|
|
|
|
|
#include "gb_pcode.h"
|
|
|
|
#include "gbx_value.h"
|
|
|
|
#include "gbx_subr.h"
|
|
|
|
#include "gbx_regexp.h"
|
|
|
|
#include "gbx_class.h"
|
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_c_array.h"
|
|
|
|
#include "gbx_local.h"
|
|
|
|
#include "gbx_compare.h"
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_cat(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
int i;
|
|
|
|
int len, len_cat;
|
|
|
|
char *str, *ptr;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
len_cat = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
for (i = 0; i < NPARAM; i++)
|
|
|
|
{
|
|
|
|
VALUE_conv_string(&PARAM[i]);
|
|
|
|
/*BORROW(&PARAM[i]);*/
|
|
|
|
len_cat += PARAM[i]._string.len;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
STRING_new_temp(&str, NULL, len_cat);
|
|
|
|
ptr = str;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
for (i = 0; i < NPARAM; i++)
|
|
|
|
{
|
|
|
|
len = PARAM[i]._string.len;
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
/*printf("add %p ", PARAM[i]._string.addr + PARAM[i]._string.start); fflush(NULL);
|
|
|
|
printf("%.*s\n", (int)len, PARAM[i]._string.addr + PARAM[i]._string.start);*/
|
|
|
|
memcpy(ptr, PARAM[i]._string.addr + PARAM[i]._string.start, len);
|
|
|
|
ptr += len;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
/*printf("\n");*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RETURN->type = T_STRING;
|
|
|
|
RETURN->_string.addr = str;
|
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = len_cat;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_file(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
int i;
|
|
|
|
int length;
|
|
|
|
char *addr;
|
|
|
|
int len;
|
|
|
|
char *str, *ptr;
|
|
|
|
boolean slash;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
length = 0;
|
|
|
|
slash = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
for (i = 0; i < NPARAM; i++)
|
|
|
|
{
|
|
|
|
/*VALUE_conv(&PARAM[i], T_STRING);*/
|
|
|
|
SUBR_get_string_len(&PARAM[i], &addr, &len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
if (!slash && (addr[0] != '/'))
|
|
|
|
length++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
slash = addr[len - 1] == '/';
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
length += len;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
STRING_new_temp(&str, NULL, length);
|
|
|
|
ptr = str;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
for (i = 0; i < NPARAM; i++)
|
|
|
|
{
|
|
|
|
VALUE_get_string(&PARAM[i], &addr, &len);
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
if ((ptr > str) && (ptr[-1] != '/') && (*addr != '/'))
|
|
|
|
*ptr++ = '/';
|
|
|
|
memcpy(ptr, addr, len);
|
|
|
|
ptr += len;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RETURN->type = T_STRING;
|
|
|
|
RETURN->_string.addr = str;
|
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = length;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-27 15:00:04 +01:00
|
|
|
#if 0
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_left(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
int val;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (SUBR_check_string(PARAM))
|
|
|
|
goto _FIN;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (NPARAM == 1)
|
|
|
|
val = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE_conv(&PARAM[1], T_INTEGER);
|
|
|
|
val = PARAM[1]._integer.value;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (val < 0)
|
|
|
|
val += PARAM->_string.len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
PARAM->_string.len = MinMax(val, 0, PARAM->_string.len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
_FIN:
|
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SP -= NPARAM;
|
|
|
|
SP++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_right(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
int val;
|
|
|
|
int new_len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (SUBR_check_string(PARAM))
|
|
|
|
goto _FIN;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (NPARAM == 1)
|
|
|
|
val = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE_conv(&PARAM[1], T_INTEGER);
|
|
|
|
val = PARAM[1]._integer.value;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (val < 0)
|
|
|
|
val += PARAM->_string.len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
new_len = MinMax(val, 0, PARAM->_string.len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
PARAM->_string.start += PARAM->_string.len - new_len;
|
|
|
|
PARAM->_string.len = new_len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
_FIN:
|
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SP -= NPARAM;
|
|
|
|
SP++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_mid(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
int start;
|
|
|
|
int len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (SUBR_check_string(PARAM))
|
|
|
|
goto FIN;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
VALUE_conv(&PARAM[1], T_INTEGER);
|
|
|
|
start = PARAM[1]._integer.value - 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (start < 0)
|
|
|
|
THROW(E_ARG);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (start >= PARAM->_string.len)
|
|
|
|
{
|
|
|
|
RELEASE(PARAM);
|
|
|
|
STRING_void_value(PARAM);
|
|
|
|
goto FIN;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (NPARAM == 2)
|
|
|
|
len = PARAM->_string.len;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE_conv(&PARAM[2], T_INTEGER);
|
|
|
|
len = PARAM[2]._integer.value;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (len < 0)
|
|
|
|
len = Max(0, PARAM->_string.len - start + len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
len = MinMax(len, 0, PARAM->_string.len - start);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
RELEASE(PARAM);
|
|
|
|
PARAM->_string.addr = NULL;
|
|
|
|
PARAM->_string.start = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PARAM->_string.start += start;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
PARAM->_string.len = len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
FIN:
|
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SP -= NPARAM;
|
|
|
|
SP++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_len(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
int len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_GET_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (SUBR_check_string(PARAM))
|
|
|
|
len = 0;
|
|
|
|
else
|
|
|
|
len = PARAM->_string.len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RELEASE(PARAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
PARAM->type = T_INTEGER;
|
|
|
|
PARAM->_integer.value = len;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-01-27 15:00:04 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_space(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
int len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_check_integer(PARAM);
|
|
|
|
len = PARAM->_integer.value;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (len < 0)
|
|
|
|
THROW(E_ARG);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
STRING_void_value(RETURN);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
STRING_new_temp_value(RETURN, NULL, len);
|
|
|
|
memset(RETURN->_string.addr, ' ', len);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_string(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
int i;
|
|
|
|
char *d;
|
|
|
|
char *s;
|
|
|
|
int ld, ls;
|
|
|
|
|
|
|
|
SUBR_ENTER_PARAM(2);
|
|
|
|
|
|
|
|
SUBR_check_integer(PARAM);
|
|
|
|
SUBR_get_string_len(&PARAM[1], &s, &ls);
|
|
|
|
|
|
|
|
ld = PARAM->_integer.value * ls;
|
|
|
|
if (ld < 0)
|
|
|
|
THROW(E_ARG);
|
|
|
|
|
|
|
|
if (ld == 0)
|
|
|
|
{
|
|
|
|
STRING_void_value(RETURN);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
STRING_new_temp_value(RETURN, NULL, ld);
|
|
|
|
d = RETURN->_string.addr;
|
|
|
|
|
|
|
|
for (i = 0; i < PARAM->_integer.value; i++)
|
|
|
|
{
|
|
|
|
memcpy(d, s, ls);
|
|
|
|
d += ls;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
*d = 0;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_trim(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
unsigned char *str;
|
|
|
|
bool left, right;
|
|
|
|
int code;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_GET_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (SUBR_check_string(PARAM))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
code = EXEC_code & 0x1F;
|
|
|
|
left = (code == 0 || code == 1);
|
|
|
|
right = (code == 0 || code == 2);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
/* if (!(left || right))
|
|
|
|
THROW(E_ILLEGAL);*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (PARAM->_string.len > 0)
|
|
|
|
{
|
|
|
|
str = (uchar *)&PARAM->_string.addr[PARAM->_string.start];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (left)
|
|
|
|
{
|
|
|
|
while (PARAM->_string.len > 0 && *str <= ' ')
|
|
|
|
{
|
|
|
|
PARAM->_string.start++;
|
|
|
|
PARAM->_string.len--;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (right)
|
|
|
|
{
|
|
|
|
while (PARAM->_string.len > 0 && str[PARAM->_string.len - 1] <= ' ')
|
|
|
|
{
|
|
|
|
PARAM->_string.len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define STRING_APPLY(_func) \
|
2009-01-02 01:05:38 +01:00
|
|
|
char *str; \
|
|
|
|
int len, i; \
|
|
|
|
\
|
|
|
|
SUBR_ENTER_PARAM(1); \
|
|
|
|
\
|
|
|
|
if (SUBR_check_string(PARAM)) \
|
|
|
|
STRING_void_value(RETURN); \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
len = PARAM->_string.len; \
|
|
|
|
if (len > 0) \
|
|
|
|
{ \
|
|
|
|
STRING_new_temp(&str, &PARAM->_string.addr[PARAM->_string.start], PARAM->_string.len); \
|
|
|
|
\
|
|
|
|
for (i = 0; i < len; i++) \
|
|
|
|
str[i] = _func(str[i]); \
|
|
|
|
\
|
|
|
|
RETURN->type = T_STRING; \
|
|
|
|
RETURN->_string.addr = str; \
|
|
|
|
RETURN->_string.start = 0; \
|
|
|
|
RETURN->_string.len = len; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_upper(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
STRING_APPLY(toupper);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_lower(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
STRING_APPLY(tolower);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_chr(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
int car;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_GET_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
VALUE_conv(PARAM, T_INTEGER);
|
|
|
|
/*SUBR_check_integer(PARAM);*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
car = PARAM->_integer.value;
|
|
|
|
if (car < 0 || car > 255)
|
|
|
|
THROW(E_ARG);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
STRING_char_value(PARAM, car);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_asc(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
int pos = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (!SUBR_check_string(PARAM))
|
|
|
|
{
|
|
|
|
pos = 1;
|
|
|
|
if (NPARAM == 2)
|
|
|
|
{
|
|
|
|
SUBR_check_integer(&PARAM[1]);
|
|
|
|
pos = PARAM[1]._integer.value;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (pos < 1 || pos > PARAM->_string.len)
|
|
|
|
pos = 0;
|
|
|
|
else
|
|
|
|
pos = (unsigned char)PARAM->_string.addr[PARAM->_string.start + pos - 1];
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RETURN->type = T_INTEGER;
|
|
|
|
RETURN->_integer.value = pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_instr(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
boolean right, nocase = FALSE;
|
|
|
|
int is, pos;
|
|
|
|
char *ps, *pp;
|
|
|
|
int ls, lp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
/* Knuth Morris Pratt one day maybe ? */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
pos = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (SUBR_check_string(PARAM))
|
|
|
|
goto __FOUND;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (SUBR_check_string(&PARAM[1]))
|
|
|
|
goto __FOUND;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
lp = PARAM[1]._string.len;
|
|
|
|
ls = PARAM->_string.len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
right = ((EXEC_code >> 8) == CODE_RINSTR);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (lp > ls) goto __FOUND;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
is = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (NPARAM >= 3)
|
|
|
|
is = SUBR_get_integer(&PARAM[2]);
|
|
|
|
|
|
|
|
if (NPARAM == 4)
|
|
|
|
nocase = SUBR_get_integer(&PARAM[3]) == GB_COMP_TEXT;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
ps = PARAM->_string.addr + PARAM->_string.start;
|
|
|
|
pp = PARAM[1]._string.addr + PARAM[1]._string.start;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
pos = STRING_search(ps, ls, pp, lp, is, right, nocase);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
__FOUND:
|
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RETURN->type = T_INTEGER;
|
|
|
|
RETURN->_integer.value = pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_like(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
char *pattern;
|
|
|
|
char *string;
|
|
|
|
int len_pattern, len_string;
|
|
|
|
boolean ret;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER_PARAM(2);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_get_string_len(&PARAM[0], &string, &len_string);
|
|
|
|
SUBR_get_string_len(&PARAM[1], &pattern, &len_pattern);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
ret = REGEXP_match(pattern, len_pattern, string, len_string) ? -1 : 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RETURN->type = T_BOOLEAN;
|
|
|
|
RETURN->_boolean.value = ret;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int subst_nparam;
|
|
|
|
static VALUE *subst_param;
|
|
|
|
|
|
|
|
static void get_subst(int np, char **str, int *len)
|
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
if (np > 0 && np < subst_nparam)
|
|
|
|
VALUE_get_string(&subst_param[np], str, len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*str = NULL;
|
|
|
|
*len = 0;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_subst(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
char *string;
|
|
|
|
int len;
|
|
|
|
int np;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_get_string_len(&PARAM[0], &string, &len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
for (np = 1; np < NPARAM; np++)
|
|
|
|
VALUE_conv_string(&PARAM[np]);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
subst_param = PARAM;
|
|
|
|
subst_nparam = NPARAM;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
string = STRING_subst(string, len, get_subst);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
/*for (np = 0; np < NPARAM; np++)
|
|
|
|
RELEASE_STRING(&PARAM[np]);*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RETURN->type = T_STRING;
|
|
|
|
RETURN->_string.addr = (char *)string;
|
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = STRING_length(string);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_replace(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
char *ps;
|
|
|
|
char *pp;
|
|
|
|
char *pr;
|
|
|
|
int ls, lp, lr;
|
|
|
|
int is, pos;
|
|
|
|
bool nocase = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_get_string_len(&PARAM[0], &ps, &ls);
|
|
|
|
SUBR_get_string_len(&PARAM[1], &pp, &lp);
|
|
|
|
SUBR_get_string_len(&PARAM[2], &pr, &lr);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (NPARAM == 4)
|
|
|
|
nocase = SUBR_get_integer(&PARAM[3]) == GB_COMP_TEXT;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-11-10 01:18:00 +01:00
|
|
|
STRING_start_len(ls);
|
2009-01-02 01:05:38 +01:00
|
|
|
|
|
|
|
if (ls > 0 && lp > 0)
|
|
|
|
{
|
2008-11-07 02:16:13 +01:00
|
|
|
is = 0;
|
|
|
|
|
2008-11-09 00:42:08 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
pos = STRING_search(ps, ls, pp, lp, 1, FALSE, nocase);
|
|
|
|
if (pos == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pos--;
|
|
|
|
|
|
|
|
if (pos > 0)
|
2008-11-10 01:18:00 +01:00
|
|
|
STRING_make(ps, pos);
|
2008-11-09 00:42:08 +01:00
|
|
|
|
2008-11-10 01:18:00 +01:00
|
|
|
STRING_make(pr, lr);
|
2008-11-09 00:42:08 +01:00
|
|
|
|
|
|
|
pos += lp;
|
|
|
|
|
|
|
|
ps += pos;
|
|
|
|
ls -= pos;
|
|
|
|
|
|
|
|
if (ls <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-10 01:18:00 +01:00
|
|
|
STRING_make(ps, ls);
|
2009-01-02 01:05:38 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RETURN->type = T_STRING;
|
|
|
|
RETURN->_string.addr = STRING_end_temp();
|
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = STRING_length(RETURN->_string.addr);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_split(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
CARRAY *array;
|
|
|
|
char *str;
|
|
|
|
int lstr;
|
|
|
|
char *sep = "";
|
|
|
|
char *esc = "";
|
|
|
|
bool no_void = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_get_string_len(&PARAM[0], &str, &lstr);
|
|
|
|
|
|
|
|
if (NPARAM >= 2)
|
|
|
|
{
|
|
|
|
sep = SUBR_get_string(&PARAM[1]);
|
|
|
|
if (NPARAM >= 3)
|
|
|
|
{
|
|
|
|
esc = SUBR_get_string(&PARAM[2]);
|
|
|
|
if (NPARAM == 4)
|
|
|
|
{
|
|
|
|
VALUE_conv(&PARAM[3], T_BOOLEAN);
|
|
|
|
no_void = PARAM[3]._boolean.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
OBJECT_create((void **)(void *)&array, CLASS_StringArray, NULL, NULL, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (lstr)
|
|
|
|
{
|
|
|
|
if (*sep) STRING_ref(sep);
|
|
|
|
if (*esc) STRING_ref(esc);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
CARRAY_split(array, str, lstr, sep, esc, no_void);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (*sep) STRING_unref(&sep);
|
|
|
|
if (*esc) STRING_unref(&esc);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RETURN->_object.class = CLASS_StringArray;
|
|
|
|
RETURN->_object.object = array;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_scan(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
CARRAY *array;
|
|
|
|
char *str;
|
|
|
|
int len_str;
|
|
|
|
char *pat;
|
|
|
|
int len_pat;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER_PARAM(2);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_get_string_len(&PARAM[0], &str, &len_str);
|
|
|
|
SUBR_get_string_len(&PARAM[1], &pat, &len_pat);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
OBJECT_create((void **)(void *)&array, CLASS_StringArray, NULL, NULL, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (len_str && len_pat)
|
|
|
|
REGEXP_scan(array, pat, len_pat, str, len_str);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RETURN->_object.class = CLASS_StringArray;
|
|
|
|
RETURN->_object.object = array;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_iconv(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
char *str;
|
|
|
|
const char *src;
|
|
|
|
const char *dst;
|
|
|
|
char *result;
|
|
|
|
int len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER_PARAM(3);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
str = SUBR_get_string(&PARAM[0]);
|
|
|
|
len = PARAM[0]._string.len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
src = SUBR_get_string(&PARAM[1]);
|
|
|
|
dst = SUBR_get_string(&PARAM[2]);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
STRING_conv(&result, str, len, src, dst, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (!result)
|
|
|
|
RETURN->type = T_NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RETURN->type = T_STRING;
|
|
|
|
RETURN->_string.addr = result;
|
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = STRING_length(result);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_sconv(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
char *str;
|
|
|
|
const char *src;
|
|
|
|
const char *dst;
|
|
|
|
char *result;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
SUBR_ENTER_PARAM(1);
|
|
|
|
|
|
|
|
if (LOCAL_is_UTF8)
|
|
|
|
return;
|
|
|
|
|
|
|
|
str = SUBR_get_string(&PARAM[0]);
|
|
|
|
len = PARAM[0]._string.len;
|
|
|
|
|
|
|
|
if (EXEC_code & 0xF)
|
|
|
|
{
|
|
|
|
src = LOCAL_encoding;
|
|
|
|
dst = "UTF-8";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
src = "UTF-8";
|
|
|
|
dst = LOCAL_encoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
STRING_conv(&result, str, len, src, dst, TRUE);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
RETURN->type = T_NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RETURN->type = T_STRING;
|
|
|
|
RETURN->_string.addr = result;
|
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = STRING_length(result);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _is_ascii(int c)
|
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
return (c & ~0x7F) == 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _is_letter(int c)
|
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _is_lower(int c)
|
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
return (c >= 'a' && c <= 'z');
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _is_upper(int c)
|
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
return (c >= 'A' && c <= 'Z');
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _is_digit(int c)
|
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
return (c >= '0' && c <= '9');
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _is_hexa(int c)
|
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _is_space(int c)
|
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
return strchr(" \n\r\t\f\v", c) != NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _is_blank(int c)
|
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
return (c == 32 || c == '\t');
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _is_punct(int c)
|
|
|
|
{
|
|
|
|
return ((c > 32) && (c < 128) && !(_is_letter(c) || _is_digit(c)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_is_chr(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-02 01:05:38 +01:00
|
|
|
static void *jump[] =
|
|
|
|
{
|
|
|
|
NULL, _is_ascii, _is_letter, _is_lower, _is_upper, _is_digit, _is_hexa, _is_space, _is_blank, _is_punct
|
|
|
|
};
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
char *addr;
|
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
int (*func)(int);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
VALUE_conv_string(PARAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_get_string_len(PARAM, &addr, &len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
func = jump[EXEC_code & 0x3F];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (!(*func)(addr[i]))
|
|
|
|
break;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
RETURN->type = T_BOOLEAN;
|
|
|
|
RETURN->_boolean.value = (len > 0 && i >= len) ? -1 : 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
void SUBR_tr(void)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
|
|
|
|
2009-01-05 11:31:11 +01:00
|
|
|
VALUE_conv_string(&PARAM[0]);
|
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
if (SUBR_check_string(PARAM))
|
|
|
|
STRING_void_value(RETURN);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
STRING_new_temp(&str, &PARAM->_string.addr[PARAM->_string.start], PARAM->_string.len);
|
2009-01-01 14:42:17 +01:00
|
|
|
|
|
|
|
RETURN->type = T_CSTRING;
|
|
|
|
RETURN->_string.addr = (char *)LOCAL_gettext(str);
|
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = strlen(RETURN->_string.addr);
|
2009-01-02 01:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SUBR_quote(void)
|
|
|
|
{
|
|
|
|
static void *jump[4] = { &&__QUOTE, &&__SHELL, &&__HTML, &&__QUOTE };
|
|
|
|
char *str;
|
|
|
|
int lstr;
|
|
|
|
int i;
|
|
|
|
unsigned char c;
|
|
|
|
char buf[8];
|
|
|
|
|
|
|
|
SUBR_ENTER_PARAM(1);
|
|
|
|
|
2009-01-05 11:31:11 +01:00
|
|
|
VALUE_conv_string(&PARAM[0]);
|
|
|
|
|
|
|
|
str = PARAM->_string.addr + PARAM->_string.start;
|
|
|
|
lstr = PARAM->_string.len;
|
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
STRING_start_len(lstr);
|
|
|
|
|
|
|
|
goto *jump[EXEC_code & 0x3];
|
|
|
|
|
|
|
|
__QUOTE:
|
|
|
|
|
|
|
|
STRING_make_char('"');
|
|
|
|
|
|
|
|
for (i = 0; i < lstr; i++)
|
|
|
|
{
|
|
|
|
c = str[i];
|
|
|
|
if (c >= ' ' && c <= 126 && c != '\\' && c != '"')
|
|
|
|
STRING_make_char(c);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
STRING_make_char('\\');
|
|
|
|
if (c == '\n')
|
|
|
|
STRING_make_char('n');
|
|
|
|
else if (c == '\r')
|
|
|
|
STRING_make_char('r');
|
|
|
|
else if (c == '\t')
|
|
|
|
STRING_make_char('t');
|
|
|
|
else if (c == '"')
|
|
|
|
STRING_make_char('"');
|
|
|
|
else if (c == '\\')
|
|
|
|
STRING_make_char('\\');
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf), "x%02X", c);
|
|
|
|
STRING_make(buf, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STRING_make_char('"');
|
|
|
|
goto __END;
|
|
|
|
|
|
|
|
__SHELL:
|
|
|
|
|
|
|
|
if (!LOCAL_is_UTF8)
|
|
|
|
{
|
|
|
|
char *conv;
|
|
|
|
STRING_conv(&conv, str, lstr, "UTF-8", LOCAL_encoding, FALSE);
|
|
|
|
str = conv;
|
|
|
|
lstr = str ? strlen(str) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < lstr; i++)
|
|
|
|
{
|
|
|
|
c = str[i];
|
|
|
|
if (c == '\n')
|
|
|
|
STRING_make("$'\\n'", 5);
|
|
|
|
else if (c == '\r')
|
|
|
|
STRING_make("$'\\r'", 5);
|
|
|
|
else if (c == '\t')
|
|
|
|
STRING_make("$'\\t'", 5);
|
|
|
|
else if (c < ' ') //|| (c > 126 && !LOCAL_is_UTF8))
|
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf), "$'\\x%02X'", c);
|
|
|
|
STRING_make(buf, 7);
|
|
|
|
}
|
|
|
|
else if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || index(".-/_~", c) || c > 126)
|
|
|
|
STRING_make_char(c);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
STRING_make_char('\\');
|
|
|
|
STRING_make_char(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
goto __END;
|
|
|
|
|
|
|
|
__HTML:
|
|
|
|
|
|
|
|
for (i = 0; i < lstr; i++)
|
|
|
|
{
|
|
|
|
c = str[i];
|
|
|
|
if (c == '&')
|
|
|
|
STRING_make("&", 5);
|
|
|
|
else if (c == '<')
|
|
|
|
STRING_make("<", 4);
|
|
|
|
else if (c == '>')
|
|
|
|
STRING_make(">", 4);
|
|
|
|
else if (c == '"')
|
|
|
|
STRING_make(""", 6);
|
|
|
|
else
|
|
|
|
STRING_make_char(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto __END;
|
|
|
|
|
|
|
|
__END:
|
|
|
|
|
|
|
|
RETURN->type = T_STRING;
|
|
|
|
RETURN->_string.addr = STRING_end_temp();
|
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = STRING_length(RETURN->_string.addr);
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_hex_digit(unsigned char c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return (c - '0');
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
return (c - 'A' + 10);
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
return (c - 'a' + 10);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SUBR_unquote(void)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
int lstr;
|
|
|
|
int i;
|
|
|
|
unsigned char c;
|
|
|
|
|
|
|
|
SUBR_ENTER_PARAM(1);
|
|
|
|
|
2009-01-05 11:25:50 +01:00
|
|
|
VALUE_conv_string(&PARAM[0]);
|
|
|
|
|
|
|
|
str = PARAM->_string.addr + PARAM->_string.start;
|
|
|
|
lstr = PARAM->_string.len;
|
|
|
|
|
2009-01-02 01:05:38 +01:00
|
|
|
STRING_start_len(lstr);
|
|
|
|
|
|
|
|
if (lstr >= 2 && str[0] == '"' && str[lstr - 1] == '"')
|
|
|
|
{
|
|
|
|
str++;
|
|
|
|
lstr -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < lstr; i++)
|
|
|
|
{
|
|
|
|
c = str[i];
|
|
|
|
if (c == '\\')
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
if (i >= lstr)
|
|
|
|
break;
|
|
|
|
c = str[i];
|
|
|
|
|
|
|
|
if (c == 'n')
|
|
|
|
c = '\n';
|
|
|
|
else if (c == 't')
|
|
|
|
c = '\t';
|
|
|
|
else if (c == 'r')
|
|
|
|
c = '\r';
|
|
|
|
else if (c == 'x')
|
|
|
|
{
|
|
|
|
if (i >= (lstr - 2))
|
|
|
|
break;
|
|
|
|
|
|
|
|
c = (read_hex_digit(str[i + 1]) << 4) + read_hex_digit(str[i + 2]);
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STRING_make_char(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN->type = T_STRING;
|
|
|
|
RETURN->_string.addr = STRING_end_temp();
|
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = STRING_length(RETURN->_string.addr);
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|