added qse_str_del() and codede the D command in sed

This commit is contained in:
hyung-hwan 2009-05-18 03:35:16 +00:00
parent f0b916ce81
commit bd6a54d00d
5 changed files with 97 additions and 26 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: str.h 127 2009-05-07 13:15:04Z hyunghwan.chung $
* $Id: str.h 138 2009-05-17 09:35:16Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -539,6 +539,12 @@ qse_size_t qse_str_nccat (
qse_size_t len
);
qse_size_t qse_str_del (
qse_str_t* str,
qse_size_t index,
qse_size_t size
);
/****f* Common/qse_strspl
* NAME
* qse_strspl - split a string into fields

View File

@ -1,5 +1,5 @@
/*
* $Id: str_dyn.c 126 2009-05-05 02:12:38Z hyunghwan.chung $
* $Id: str_dyn.c 138 2009-05-17 09:35:16Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -311,3 +311,24 @@ qse_size_t qse_str_nccat (qse_str_t* str, qse_char_t c, qse_size_t len)
return str->len;
}
qse_size_t qse_str_del (qse_str_t* str, qse_size_t index, qse_size_t size)
{
if (str->ptr != QSE_NULL && index < str->len && size > 0)
{
qse_size_t nidx = index + size;
if (nidx >= str->len)
{
str->ptr[index] = QSE_T('\0');
str->len = index;
}
else
{
qse_strncpy (
&str->ptr[index], &str->ptr[nidx],
str->len - nidx);
str->len -= size;
}
}
return str->len;
}

View File

@ -246,6 +246,14 @@ static void free_command (qse_sed_t* sed, qse_sed_cmd_t* cmd)
QSE_MMGR_FREE (sed->mmgr, cmd->u.text.ptr);
break;
case QSE_SED_CMD_READ_FILE:
case QSE_SED_CMD_READ_FILELN:
case QSE_SED_CMD_WRITE_FILE:
case QSE_SED_CMD_WRITE_FILELN:
if (cmd->u.file.ptr != QSE_NULL)
QSE_MMGR_FREE (sed->mmgr, cmd->u.file.ptr);
break;
case QSE_SED_CMD_BRANCH:
case QSE_SED_CMD_BRANCH_COND:
if (cmd->u.branch.label.ptr != QSE_NULL)
@ -266,14 +274,6 @@ static void free_command (qse_sed_t* sed, qse_sed_cmd_t* cmd)
QSE_MMGR_FREE (sed->mmgr, cmd->u.transet.ptr);
break;
case QSE_SED_CMD_READ_FILE:
case QSE_SED_CMD_READ_FILELN:
case QSE_SED_CMD_WRITE_FILE:
case QSE_SED_CMD_WRITE_FILELN:
if (cmd->u.file.ptr != QSE_NULL)
QSE_MMGR_FREE (sed->mmgr, cmd->u.file.ptr);
break;
default:
break;
}
@ -1848,9 +1848,10 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd)
);
if (n <= -1) return -1;
}
sed->e.subst_done = 1;
}
sed->e.subst_done = 1;
return 0;
}
@ -2065,6 +2066,25 @@ static qse_sed_cmd_t* exec_cmd (qse_sed_t* sed, qse_sed_cmd_t* cmd)
break;
case QSE_SED_CMD_DELETE_FIRSTLN:
{
qse_char_t* nl;
/* delete the first line from the pattern space */
nl = qse_strxchr (
QSE_STR_PTR(&sed->e.in.line),
QSE_STR_LEN(&sed->e.in.line),
QSE_T('\n'));
if (nl != QSE_NULL)
{
/* if a new line is found */
qse_str_del (&sed->e.in.line, 0,
nl - QSE_STR_PTR(&sed->e.in.line) + 1);
break;
}
/* otherwise clear the entire pattern space below */
}
case QSE_SED_CMD_DELETE:
/* delete the pattern space */
qse_str_clear (&sed->e.in.line);
@ -2261,6 +2281,11 @@ static qse_sed_cmd_t* exec_cmd (qse_sed_t* sed, qse_sed_cmd_t* cmd)
jumpto = cmd->u.branch.target;
break;
case QSE_SED_CMD_SUBSTITUTE:
n = do_subst (sed, cmd);
if (n <= -1) return QSE_NULL;
break;
case QSE_SED_CMD_TRANSLATE:
{
qse_char_t* ptr = QSE_STR_PTR(&sed->e.in.line);
@ -2286,11 +2311,6 @@ static qse_sed_cmd_t* exec_cmd (qse_sed_t* sed, qse_sed_cmd_t* cmd)
}
break;
}
case QSE_SED_CMD_SUBSTITUTE:
n = do_subst (sed, cmd);
if (n <= -1) return QSE_NULL;
break;
}
if (jumpto == QSE_NULL) jumpto = cmd + 1;

View File

@ -48,15 +48,15 @@ struct qse_sed_cmd_t
{
enum
{
QSE_SED_CMD_QUIT = QSE_T('q'),
QSE_SED_CMD_QUIT_QUIET = QSE_T('Q'),
QSE_SED_CMD_QUIT = QSE_T('q'),
QSE_SED_CMD_QUIT_QUIET = QSE_T('Q'),
QSE_SED_CMD_APPEND = QSE_T('a'),
QSE_SED_CMD_INSERT = QSE_T('i'),
QSE_SED_CMD_CHANGE = QSE_T('c'),
QSE_SED_CMD_APPEND = QSE_T('a'),
QSE_SED_CMD_INSERT = QSE_T('i'),
QSE_SED_CMD_CHANGE = QSE_T('c'),
QSE_SED_CMD_DELETE = QSE_T('d'),
QSE_SED_CMD_DD = QSE_T('D'),
QSE_SED_CMD_DELETE = QSE_T('d'),
QSE_SED_CMD_DELETE_FIRSTLN = QSE_T('D'),
QSE_SED_CMD_PRINT_LNNUM = QSE_T('='),
QSE_SED_CMD_PRINT = QSE_T('p'),
@ -72,14 +72,14 @@ struct qse_sed_cmd_t
QSE_SED_CMD_NEXT = QSE_T('n'),
QSE_SED_CMD_NEXT_APPEND = QSE_T('N'),
QSE_SED_CMD_BRANCH = QSE_T('b'),
QSE_SED_CMD_BRANCH_COND = QSE_T('t'),
QSE_SED_CMD_READ_FILE = QSE_T('r'),
QSE_SED_CMD_READ_FILELN = QSE_T('R'),
QSE_SED_CMD_WRITE_FILE = QSE_T('w'),
QSE_SED_CMD_WRITE_FILELN = QSE_T('W'),
QSE_SED_CMD_BRANCH = QSE_T('b'),
QSE_SED_CMD_BRANCH_COND = QSE_T('t'),
QSE_SED_CMD_SUBSTITUTE = QSE_T('s'),
QSE_SED_CMD_TRANSLATE = QSE_T('y')

View File

@ -527,6 +527,29 @@ static int test12 (void)
return 0;
}
static int test13 (void)
{
qse_char_t a1[] = QSE_T("abcdefghijklmnopqrstuvwxyz");
qse_str_t x;
int i;
qse_str_init (&x, QSE_MMGR_GETDFL(), 5);
for (i = 1; i < 20; i++)
{
qse_str_cpy (&x, a1);
qse_str_del (&x, 10, i);
qse_printf (QSE_T("deleleted %d from 10 => %llu [%s]\n"),
i,
(unsigned long long)QSE_STR_LEN(&x),
QSE_STR_PTR(&x));
}
qse_str_fini (&x);
return 0;
}
int main ()
{
setlocale (LC_ALL, "");
@ -547,6 +570,7 @@ int main ()
R (test10);
R (test11);
R (test12);
R (test13);
return 0;
}