renamed ASE_STR_XXX macros and added the ase_xstr_t type

This commit is contained in:
hyung-hwan 2008-09-24 03:51:24 +00:00
parent a1348d4ca5
commit 2230fbd445
9 changed files with 120 additions and 112 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: str.h 371 2008-09-23 09:36:30Z baconevi $
* $Id: str.h 372 2008-09-23 09:51:24Z baconevi $
*
* {License}
*/
@ -10,12 +10,10 @@
#include <ase/types.h>
#include <ase/macros.h>
#define ASE_STR_LEN(x) ((x)->size)
#define ASE_STR_PTR(x) ((x)->buf)
#define ASE_STR_SIZE(x) ((x)->size + 1)
#define ASE_STR_LEN(x) ((x)->len)
#define ASE_STR_PTR(x) ((x)->ptr)
#define ASE_STR_CAPA(x) ((x)->capa)
#define ASE_STR_BUF(x) ((x)->buf)
#define ASE_STR_CHAR(x,idx) ((x)->buf[idx])
#define ASE_STR_CHAR(x,idx) ((x)->ptr[idx])
typedef struct ase_str_t ase_str_t;
@ -24,8 +22,8 @@ struct ase_str_t
ase_mmgr_t* mmgr;
ase_sizer_t sizer;
ase_char_t* buf;
ase_size_t size;
ase_char_t* ptr;
ase_size_t len;
ase_size_t capa;
};
@ -208,14 +206,14 @@ void ase_str_fini (
*
* DESCRIPTION:
* The ase_str_yield() function assigns the buffer to an variable of the
* ase_cstr_t type and recreate a new buffer of the new_capa capacity.
* ase_xstr_t type and recreate a new buffer of the new_capa capacity.
* The function fails if it fails to allocate a new buffer.
*
* RETURNS: 0 on success, -1 on failure.
*/
int ase_str_yield (
ase_str_t* str /* a dynamic string */,
ase_cstr_t* buf /* the pointer to a ase_cstr_t variable */,
ase_xstr_t* buf /* the pointer to a ase_xstr_t variable */,
int new_capa /* new capacity in number of characters */
);
@ -239,6 +237,8 @@ ase_sizer_t ase_str_getsizer (
* With no sizer specified, the dynamic string doubles the current buffer
* when it needs to increase its size. The sizer function is passed a dynamic
* string and the minimum capacity required to hold data after resizing.
* The string is truncated if the sizer function returns a smaller number
* than the hint passed.
*/
void ase_str_setsizer (
ase_str_t* str /* a dynamic string */,
@ -250,6 +250,7 @@ void ase_str_setsizer (
*
* DESCRIPTION:
* The ase_str_getcapa() function returns the current capacity.
* You may use ASE_STR_CAPA(str) macro for performance sake.
*
* RETURNS: the current capacity in number of characters.
*/

View File

@ -1,5 +1,5 @@
/*
* $Id: types.h 369 2008-09-22 11:21:08Z baconevi $
* $Id: types.h 372 2008-09-23 09:51:24Z baconevi $
*
* {License}
*/
@ -320,6 +320,7 @@ typedef int ase_mcint_t;
#endif
#endif
typedef struct ase_xstr_t ase_xstr_t;
typedef struct ase_cstr_t ase_cstr_t;
typedef struct ase_mmgr_t ase_mmgr_t;
typedef struct ase_ccls_t ase_ccls_t;
@ -331,9 +332,15 @@ typedef void (*ase_free_t) (void* data, void* ptr);
typedef ase_bool_t (*ase_isccls_t) (void* data, ase_cint_t c);
typedef ase_cint_t (*ase_toccls_t) (void* data, ase_cint_t c);
struct ase_xstr_t
{
ase_char_t* ptr; /* this is not a const pointer */
ase_size_t len;
};
struct ase_cstr_t
{
const ase_char_t* ptr;
const ase_char_t* ptr; /* this is a const pointer */
ase_size_t len;
};

View File

@ -1,5 +1,5 @@
/*
* $Id: extio.c 337 2008-08-20 09:17:25Z baconevi $
* $Id: extio.c 372 2008-09-23 09:51:24Z baconevi $
*
* {License}
*/
@ -263,7 +263,7 @@ int ase_awk_readextio (
n = ASE_AWK_MATCHREX (
run->awk, run->global.rs,
((run->global.ignorecase)? ASE_REX_IGNORECASE: 0),
ASE_STR_BUF(buf), ASE_STR_LEN(buf),
ASE_STR_PTR(buf), ASE_STR_LEN(buf),
&match_ptr, &match_len, &run->errnum);
if (n == -1)
{
@ -276,7 +276,7 @@ int ase_awk_readextio (
/* the match should be found at the end of
* the current buffer */
ASE_ASSERT (
ASE_STR_BUF(buf) + ASE_STR_LEN(buf) ==
ASE_STR_PTR(buf) + ASE_STR_LEN(buf) ==
match_ptr + match_len);
ASE_STR_LEN(buf) -= match_len;
@ -351,7 +351,7 @@ int ase_awk_readextio (
n = ASE_AWK_MATCHREX (
run->awk, run->global.rs,
((run->global.ignorecase)? ASE_REX_IGNORECASE: 0),
ASE_STR_BUF(buf), ASE_STR_LEN(buf),
ASE_STR_PTR(buf), ASE_STR_LEN(buf),
&match_ptr, &match_len, &run->errnum);
if (n == -1)
{
@ -365,7 +365,7 @@ int ase_awk_readextio (
/* the match should be found at the end of
* the current buffer */
ASE_ASSERT (
ASE_STR_BUF(buf) + ASE_STR_LEN(buf) ==
ASE_STR_PTR(buf) + ASE_STR_LEN(buf) ==
match_ptr + match_len);
ASE_STR_LEN(buf) -= match_len;

View File

@ -1,5 +1,5 @@
/*
* $Id: func.c 363 2008-09-04 10:58:08Z baconevi $
* $Id: func.c 372 2008-09-23 09:51:24Z baconevi $
*
* {License}
*/
@ -992,7 +992,7 @@ static int __substitute (ase_awk_run_t* run, ase_long_t max_count)
if (a2 == ASE_NULL)
{
/* is this correct? any needs to use inrec.d0? */
a2_ptr = ASE_STR_BUF(&run->inrec.line);
a2_ptr = ASE_STR_PTR(&run->inrec.line);
a2_len = ASE_STR_LEN(&run->inrec.line);
}
else if (((ase_awk_val_ref_t*)a2)->id == ASE_AWK_VAL_REF_POS)
@ -1002,7 +1002,7 @@ static int __substitute (ase_awk_run_t* run, ase_long_t max_count)
idx = (ase_size_t)((ase_awk_val_ref_t*)a2)->adr;
if (idx == 0)
{
a2_ptr = ASE_STR_BUF(&run->inrec.line);
a2_ptr = ASE_STR_PTR(&run->inrec.line);
a2_len = ASE_STR_LEN(&run->inrec.line);
}
else if (idx <= run->inrec.nflds)
@ -1149,7 +1149,7 @@ static int __substitute (ase_awk_run_t* run, ase_long_t max_count)
if (a2 == ASE_NULL)
{
if (ase_awk_setrec (run, 0,
ASE_STR_BUF(&new), ASE_STR_LEN(&new)) == -1)
ASE_STR_PTR(&new), ASE_STR_LEN(&new)) == -1)
{
ase_str_close (&new);
FREE_A_PTRS (run->awk);
@ -1162,7 +1162,7 @@ static int __substitute (ase_awk_run_t* run, ase_long_t max_count)
n = ase_awk_setrec (
run, (ase_size_t)((ase_awk_val_ref_t*)a2)->adr,
ASE_STR_BUF(&new), ASE_STR_LEN(&new));
ASE_STR_PTR(&new), ASE_STR_LEN(&new));
if (n == -1)
{
@ -1174,7 +1174,7 @@ static int __substitute (ase_awk_run_t* run, ase_long_t max_count)
else
{
v = ase_awk_makestrval (run,
ASE_STR_BUF(&new), ASE_STR_LEN(&new));
ASE_STR_PTR(&new), ASE_STR_LEN(&new));
if (v == ASE_NULL)
{
ase_str_close (&new);

View File

@ -1,5 +1,5 @@
/*
* $Id: parse.c 363 2008-09-04 10:58:08Z baconevi $
* $Id: parse.c 372 2008-09-23 09:51:24Z baconevi $
*
* {License}
*/
@ -376,7 +376,7 @@ static global_t gtab[] =
do { \
ase_cstr_t errarg; \
errarg.len = ASE_STR_LEN(&(awk)->token.name); \
errarg.ptr = ASE_STR_BUF(&(awk)->token.name); \
errarg.ptr = ASE_STR_PTR(&(awk)->token.name); \
if (MATCH(awk,TOKEN_EOF)) \
ase_awk_seterror (awk, code, (awk)->token.prev.line, &errarg, 1); \
else \
@ -841,7 +841,7 @@ static ase_awk_nde_t* parse_function (ase_awk_t* awk)
return ASE_NULL;
}
name = ASE_STR_BUF(&awk->token.name);
name = ASE_STR_PTR(&awk->token.name);
name_len = ASE_STR_LEN(&awk->token.name);
/* check if it is a builtin function */
@ -930,7 +930,7 @@ static ase_awk_nde_t* parse_function (ase_awk_t* awk)
return ASE_NULL;
}
param = ASE_STR_BUF(&awk->token.name);
param = ASE_STR_PTR(&awk->token.name);
param_len = ASE_STR_LEN(&awk->token.name);
/* NOTE: the following is not a conflict.
@ -1601,7 +1601,7 @@ static ase_awk_t* collect_globals (ase_awk_t* awk)
if (add_global (
awk,
ASE_STR_BUF(&awk->token.name),
ASE_STR_PTR(&awk->token.name),
ASE_STR_LEN(&awk->token.name),
awk->token.line, 0) == -1) return ASE_NULL;
@ -1639,7 +1639,7 @@ static ase_awk_t* collect_locals (
return ASE_NULL;
}
local = ASE_STR_BUF(&awk->token.name);
local = ASE_STR_PTR(&awk->token.name);
local_len = ASE_STR_LEN(&awk->token.name);
#if 0
@ -2758,10 +2758,10 @@ static ase_awk_nde_t* parse_primary (ase_awk_t* awk, ase_size_t line)
nde->line = line;
nde->next = ASE_NULL;
nde->val = ase_awk_strxtolong (awk,
ASE_STR_BUF(&awk->token.name),
ASE_STR_PTR(&awk->token.name),
ASE_STR_LEN(&awk->token.name), 0, ASE_NULL);
nde->str = ASE_AWK_STRXDUP (awk,
ASE_STR_BUF(&awk->token.name),
ASE_STR_PTR(&awk->token.name),
ASE_STR_LEN(&awk->token.name));
if (nde->str == ASE_NULL)
{
@ -2772,7 +2772,7 @@ static ase_awk_nde_t* parse_primary (ase_awk_t* awk, ase_size_t line)
ASE_ASSERT (
ASE_STR_LEN(&awk->token.name) ==
ase_strlen(ASE_STR_BUF(&awk->token.name)));
ase_strlen(ASE_STR_PTR(&awk->token.name)));
if (get_token(awk) == -1)
{
@ -2799,10 +2799,10 @@ static ase_awk_nde_t* parse_primary (ase_awk_t* awk, ase_size_t line)
nde->line = line;
nde->next = ASE_NULL;
nde->val = ase_awk_strxtoreal (awk,
ASE_STR_BUF(&awk->token.name),
ASE_STR_PTR(&awk->token.name),
ASE_STR_LEN(&awk->token.name), ASE_NULL);
nde->str = ASE_AWK_STRXDUP (awk,
ASE_STR_BUF(&awk->token.name),
ASE_STR_PTR(&awk->token.name),
ASE_STR_LEN(&awk->token.name));
if (nde->str == ASE_NULL)
{
@ -2813,7 +2813,7 @@ static ase_awk_nde_t* parse_primary (ase_awk_t* awk, ase_size_t line)
ASE_ASSERT (
ASE_STR_LEN(&awk->token.name) ==
ase_strlen(ASE_STR_BUF(&awk->token.name)));
ase_strlen(ASE_STR_PTR(&awk->token.name)));
if (get_token(awk) == -1)
{
@ -2841,7 +2841,7 @@ static ase_awk_nde_t* parse_primary (ase_awk_t* awk, ase_size_t line)
nde->next = ASE_NULL;
nde->len = ASE_STR_LEN(&awk->token.name);
nde->buf = ASE_AWK_STRXDUP (awk,
ASE_STR_BUF(&awk->token.name), nde->len);
ASE_STR_PTR(&awk->token.name), nde->len);
if (nde->buf == ASE_NULL)
{
ASE_AWK_FREE (awk, nde);
@ -2885,7 +2885,7 @@ static ase_awk_nde_t* parse_primary (ase_awk_t* awk, ase_size_t line)
nde->len = ASE_STR_LEN(&awk->token.name);
nde->buf = ASE_AWK_STRXDUP (awk,
ASE_STR_BUF(&awk->token.name),
ASE_STR_PTR(&awk->token.name),
ASE_STR_LEN(&awk->token.name));
if (nde->buf == ASE_NULL)
{
@ -2895,7 +2895,7 @@ static ase_awk_nde_t* parse_primary (ase_awk_t* awk, ase_size_t line)
}
nde->code = ASE_AWK_BUILDREX (awk,
ASE_STR_BUF(&awk->token.name),
ASE_STR_PTR(&awk->token.name),
ASE_STR_LEN(&awk->token.name),
&errnum);
if (nde->code == ASE_NULL)
@ -3109,7 +3109,7 @@ static ase_awk_nde_t* parse_primary_ident (ase_awk_t* awk, ase_size_t line)
ASE_ASSERT (MATCH(awk,TOKEN_IDENT));
name_dup = ASE_AWK_STRXDUP (awk,
ASE_STR_BUF(&awk->token.name),
ASE_STR_PTR(&awk->token.name),
ASE_STR_LEN(&awk->token.name));
if (name_dup == ASE_NULL)
{
@ -4589,7 +4589,7 @@ static int get_token (ase_awk_t* awk)
c == ASE_T('_') || ASE_AWK_ISDIGIT(awk,c));
type = classify_ident (awk,
ASE_STR_BUF(&awk->token.name),
ASE_STR_PTR(&awk->token.name),
ASE_STR_LEN(&awk->token.name));
SET_TOKEN_TYPE (awk, type);
}

View File

@ -1,5 +1,5 @@
/*
* $Id: rec.c 337 2008-08-20 09:17:25Z baconevi $
* $Id: rec.c 372 2008-09-23 09:51:24Z baconevi $
*
* {License}
*/
@ -19,7 +19,7 @@ int ase_awk_setrec (
if (idx == 0)
{
if (str == ASE_STR_BUF(&run->inrec.line) &&
if (str == ASE_STR_PTR(&run->inrec.line) &&
len == ASE_STR_LEN(&run->inrec.line))
{
if (ase_awk_clrrec (run, ASE_TRUE) == -1) return -1;
@ -66,7 +66,7 @@ int ase_awk_setrec (
/* recompose $0 */
v = ase_awk_makestrval (run,
ASE_STR_BUF(&run->inrec.line),
ASE_STR_PTR(&run->inrec.line),
ASE_STR_LEN(&run->inrec.line));
if (v == ASE_NULL)
{
@ -117,7 +117,7 @@ static int split_record (ase_awk_run_t* run)
}
/* scan the input record to count the fields */
p = ASE_STR_BUF(&run->inrec.line);
p = ASE_STR_PTR(&run->inrec.line);
len = ASE_STR_LEN(&run->inrec.line);
nflds = 0;
@ -153,7 +153,7 @@ static int split_record (ase_awk_run_t* run)
nflds++;
len = ASE_STR_LEN(&run->inrec.line) -
(p - ASE_STR_BUF(&run->inrec.line));
(p - ASE_STR_PTR(&run->inrec.line));
}
/* allocate space */
@ -175,7 +175,7 @@ static int split_record (ase_awk_run_t* run)
}
/* scan again and split it */
p = ASE_STR_BUF(&run->inrec.line);
p = ASE_STR_PTR(&run->inrec.line);
len = ASE_STR_LEN(&run->inrec.line);
while (p != ASE_NULL)
@ -215,7 +215,7 @@ static int split_record (ase_awk_run_t* run)
run->inrec.nflds++;
len = ASE_STR_LEN(&run->inrec.line) -
(p - ASE_STR_BUF(&run->inrec.line));
(p - ASE_STR_PTR(&run->inrec.line));
}
if (fs_free != ASE_NULL) ASE_AWK_FREE (run->awk, fs_free);
@ -354,7 +354,7 @@ static int recomp_record_fields (
ase_awk_val_t* tmp;
run->inrec.flds[i].ptr =
ASE_STR_BUF(&run->inrec.line) +
ASE_STR_PTR(&run->inrec.line) +
ASE_STR_LEN(&run->inrec.line);
run->inrec.flds[i].len = len;
@ -379,7 +379,7 @@ static int recomp_record_fields (
else if (i >= nflds)
{
run->inrec.flds[i].ptr =
ASE_STR_BUF(&run->inrec.line) +
ASE_STR_PTR(&run->inrec.line) +
ASE_STR_LEN(&run->inrec.line);
run->inrec.flds[i].len = 0;
@ -406,7 +406,7 @@ static int recomp_record_fields (
tmp = (ase_awk_val_str_t*)run->inrec.flds[i].val;
run->inrec.flds[i].ptr =
ASE_STR_BUF(&run->inrec.line) +
ASE_STR_PTR(&run->inrec.line) +
ASE_STR_LEN(&run->inrec.line);
run->inrec.flds[i].len = tmp->len;

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c 363 2008-09-04 10:58:08Z baconevi $
* $Id: run.c 372 2008-09-23 09:51:24Z baconevi $
*
* {License}
*/
@ -1758,7 +1758,7 @@ static int run_block0 (ase_awk_run_t* run, ase_awk_nde_blk_t* nde)
n = ase_awk_writeextio_str (run,
ASE_AWK_OUT_CONSOLE, ASE_T(""),
ASE_STR_BUF(&run->inrec.line),
ASE_STR_PTR(&run->inrec.line),
ASE_STR_LEN(&run->inrec.line));
if (n == -1)
{
@ -2813,7 +2813,7 @@ static int run_print (ase_awk_run_t* run, ase_awk_nde_print_t* nde)
* input record */
n = ase_awk_writeextio_str (
run, nde->out_type, dst,
ASE_STR_BUF(&run->inrec.line),
ASE_STR_PTR(&run->inrec.line),
ASE_STR_LEN(&run->inrec.line));
if (n <= -1 /*&& run->errnum != ASE_AWK_EIOIMPL*/)
{
@ -6230,7 +6230,7 @@ static ase_awk_val_t* eval_getline (ase_awk_run_t* run, ase_awk_nde_t* nde)
{
/* set $0 with the input value */
if (ase_awk_setrec (run, 0,
ASE_STR_BUF(&buf),
ASE_STR_PTR(&buf),
ASE_STR_LEN(&buf)) == -1)
{
ase_str_close (&buf);
@ -6244,7 +6244,7 @@ static ase_awk_val_t* eval_getline (ase_awk_run_t* run, ase_awk_nde_t* nde)
ase_awk_val_t* v;
v = ase_awk_makestrval (run,
ASE_STR_BUF(&buf), ASE_STR_LEN(&buf));
ASE_STR_PTR(&buf), ASE_STR_LEN(&buf));
ase_str_close (&buf);
if (v == ASE_NULL)
{
@ -6335,7 +6335,7 @@ static int read_record (ase_awk_run_t* run)
ase_dprintf (ASE_T("record len = %d str=[%.*s]\n"),
(int)ASE_STR_LEN(&run->inrec.line),
(int)ASE_STR_LEN(&run->inrec.line),
ASE_STR_BUF(&run->inrec.line));
ASE_STR_PTR(&run->inrec.line));
#endif
if (n == 0)
{
@ -6344,7 +6344,7 @@ static int read_record (ase_awk_run_t* run)
}
if (ase_awk_setrec (run, 0,
ASE_STR_BUF(&run->inrec.line),
ASE_STR_PTR(&run->inrec.line),
ASE_STR_LEN(&run->inrec.line)) == -1) return -1;
return 1;
@ -6421,7 +6421,7 @@ static int shorten_record (ase_awk_run_t* run, ase_size_t nflds)
if (nflds > 1) ase_awk_refdownval (run, v);
v = (ase_awk_val_t*) ase_awk_makestrval (
run, ASE_STR_BUF(&tmp), ASE_STR_LEN(&tmp));
run, ASE_STR_PTR(&tmp), ASE_STR_LEN(&tmp));
if (v == ASE_NULL) return -1;
ase_awk_refdownval (run, run->inrec.d0);
@ -6897,7 +6897,7 @@ ase_char_t* ase_awk_format (
run->awk->prmfns->data,
run->format.tmp.ptr,
run->format.tmp.len,
ASE_STR_BUF(fbu),
ASE_STR_PTR(fbu),
#if ASE_SIZEOF_LONG_LONG > 0
(long long)l
#elif ASE_SIZEOF___INT64 > 0
@ -6985,7 +6985,7 @@ ase_char_t* ase_awk_format (
run->awk->prmfns->data,
run->format.tmp.ptr,
run->format.tmp.len,
ASE_STR_BUF(fbu),
ASE_STR_PTR(fbu),
(long double)r);
if (n == -1)
@ -7251,6 +7251,6 @@ ase_char_t* ase_awk_format (
OUT_CHAR (ASE_STR_CHAR(fbu,j));
*len = ASE_STR_LEN(out);
return ASE_STR_BUF(out);
return ASE_STR_PTR(out);
}

View File

@ -1,5 +1,5 @@
/*
* $Id: val.c 363 2008-09-04 10:58:08Z baconevi $
* $Id: val.c 372 2008-09-23 09:51:24Z baconevi $
*
* {License}
*/
@ -772,7 +772,7 @@ static ase_char_t* str_to_str (
}
if (len != ASE_NULL) *len = ASE_STR_LEN(buf);
return ASE_STR_BUF(buf);
return ASE_STR_PTR(buf);
}
}
@ -833,7 +833,7 @@ static ase_char_t* val_int_to_str (
}
if (len != ASE_NULL) *len = ASE_STR_LEN(buf);
return ASE_STR_BUF(buf);
return ASE_STR_PTR(buf);
}
}
@ -876,7 +876,7 @@ static ase_char_t* val_int_to_str (
/* clear the buffer */
if (opt & ASE_AWK_VALTOSTR_CLEAR) ase_str_clear (buf);
tmp = ASE_STR_BUF(buf) + ASE_STR_LEN(buf);
tmp = ASE_STR_PTR(buf) + ASE_STR_LEN(buf);
/* extend the buffer */
if (ase_str_nccat (
@ -901,7 +901,7 @@ static ase_char_t* val_int_to_str (
if (buf != ASE_NULL && !(opt & ASE_AWK_VALTOSTR_FIXED))
{
tmp = ASE_STR_BUF(buf);
tmp = ASE_STR_PTR(buf);
if (len != ASE_NULL) *len = ASE_STR_LEN(buf);
}
@ -992,7 +992,7 @@ static ase_char_t* val_real_to_str (
return ASE_NULL;
}
tmp = ASE_STR_BUF(buf);
tmp = ASE_STR_PTR(buf);
if (len != ASE_NULL) *len = ASE_STR_LEN(buf);
ase_str_fini (&fbu);

View File

@ -1,5 +1,5 @@
/*
* $Id: str_dyn.c 369 2008-09-22 11:21:08Z baconevi $
* $Id: str_dyn.c 372 2008-09-23 09:51:24Z baconevi $
*
* {License}
*/
@ -36,16 +36,16 @@ ase_str_t* ase_str_init (ase_str_t* str, ase_mmgr_t* mmgr, ase_size_t capa)
str->mmgr = mmgr;
str->sizer = ASE_NULL;
if (capa == 0) str->buf = ASE_NULL;
if (capa == 0) str->ptr = ASE_NULL;
else
{
str->buf = (ase_char_t*) ASE_MMGR_ALLOC (
str->ptr = (ase_char_t*) ASE_MMGR_ALLOC (
mmgr, ASE_SIZEOF(ase_char_t) * (capa + 1));
if (str->buf == ASE_NULL) return ASE_NULL;
str->buf[0] = ASE_T('\0');
if (str->ptr == ASE_NULL) return ASE_NULL;
str->ptr[0] = ASE_T('\0');
}
str->size = 0;
str->len = 0;
str->capa = capa;
return str;
@ -53,10 +53,10 @@ ase_str_t* ase_str_init (ase_str_t* str, ase_mmgr_t* mmgr, ase_size_t capa)
void ase_str_fini (ase_str_t* str)
{
if (str->buf != ASE_NULL) ASE_MMGR_FREE (str->mmgr, str->buf);
if (str->ptr != ASE_NULL) ASE_MMGR_FREE (str->mmgr, str->ptr);
}
int ase_str_yield (ase_str_t* str, ase_cstr_t* buf, int new_capa)
int ase_str_yield (ase_str_t* str, ase_xstr_t* buf, int new_capa)
{
ase_char_t* tmp;
@ -71,12 +71,12 @@ int ase_str_yield (ase_str_t* str, ase_cstr_t* buf, int new_capa)
if (buf != ASE_NULL)
{
buf->ptr = str->buf;
buf->len = str->size;
buf->ptr = str->ptr;
buf->len = str->len;
}
str->buf = tmp;
str->size = 0;
str->ptr = tmp;
str->len = 0;
str->capa = new_capa;
return 0;
@ -101,10 +101,10 @@ ase_size_t ase_str_setcapa (ase_str_t* str, ase_size_t capa)
{
ase_char_t* tmp;
if (str->mmgr->realloc != ASE_NULL && str->buf != ASE_NULL)
if (str->mmgr->realloc != ASE_NULL && str->ptr != ASE_NULL)
{
tmp = (ase_char_t*) ASE_MMGR_REALLOC (
str->mmgr, str->buf,
str->mmgr, str->ptr,
ASE_SIZEOF(ase_char_t)*(capa+1));
if (tmp == ASE_NULL) return (ase_size_t)-1;
}
@ -114,49 +114,49 @@ ase_size_t ase_str_setcapa (ase_str_t* str, ase_size_t capa)
str->mmgr, ASE_SIZEOF(ase_char_t)*(capa+1));
if (tmp == ASE_NULL) return (ase_size_t)-1;
if (str->buf != ASE_NULL)
if (str->ptr != ASE_NULL)
{
ase_size_t ncopy = (str->size <= capa)? str->size: capa;
ASE_MEMCPY (tmp, str->buf,
ase_size_t ncopy = (str->len <= capa)? str->len: capa;
ASE_MEMCPY (tmp, str->ptr,
ASE_SIZEOF(ase_char_t)*(ncopy+1));
ASE_MMGR_FREE (str->mmgr, str->buf);
ASE_MMGR_FREE (str->mmgr, str->ptr);
}
}
if (capa < str->size)
if (capa < str->len)
{
str->size = capa;
str->len = capa;
tmp[capa] = ASE_T('\0');
}
str->capa = capa;
str->buf = tmp;
str->ptr = tmp;
return str->capa;
}
void ase_str_clear (ase_str_t* str)
{
str->size = 0;
str->buf[0] = ASE_T('\0');
str->len = 0;
str->ptr[0] = ASE_T('\0');
}
void ase_str_swap (ase_str_t* str, ase_str_t* str1)
{
ase_str_t tmp;
tmp.buf = str->buf;
tmp.size = str->size;
tmp.ptr = str->ptr;
tmp.len = str->len;
tmp.capa = str->capa;
tmp.mmgr = str->mmgr;
str->buf = str1->buf;
str->size = str1->size;
str->ptr = str1->ptr;
str->len = str1->len;
str->capa = str1->capa;
str->mmgr = str1->mmgr;
str1->buf = tmp.buf;
str1->size = tmp.size;
str1->ptr = tmp.ptr;
str1->len = tmp.len;
str1->capa = tmp.capa;
str1->mmgr = tmp.mmgr;
}
@ -171,20 +171,20 @@ ase_size_t ase_str_ncpy (ase_str_t* str, const ase_char_t* s, ase_size_t len)
{
ase_char_t* buf;
if (len > str->capa || str->buf == ASE_NULL)
if (len > str->capa || str->ptr == ASE_NULL)
{
buf = (ase_char_t*) ASE_MMGR_ALLOC (
str->mmgr, ASE_SIZEOF(ase_char_t) * (len + 1));
if (buf == ASE_NULL) return (ase_size_t)-1;
if (str->buf != ASE_NULL) ASE_MMGR_FREE (str->mmgr, str->buf);
if (str->ptr != ASE_NULL) ASE_MMGR_FREE (str->mmgr, str->ptr);
str->capa = len;
str->buf = buf;
str->ptr = buf;
}
str->size = ase_strncpy (str->buf, s, len);
str->buf[str->size] = ASE_T('\0');
return str->size;
str->len = ase_strncpy (str->ptr, s, len);
str->ptr[str->len] = ASE_T('\0');
return str->len;
}
ase_size_t ase_str_cat (ase_str_t* str, const ase_char_t* s)
@ -196,14 +196,14 @@ ase_size_t ase_str_cat (ase_str_t* str, const ase_char_t* s)
ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len)
{
if (len > str->capa - str->size)
if (len > str->capa - str->len)
{
ase_size_t ncapa;
if (str->sizer == ASE_NULL)
{
/* increase the capacity by the length to add */
ncapa = str->size + len;
ncapa = str->len + len;
/* if the new capacity is less than the double,
* just double it */
if (ncapa < str->capa * 2) ncapa = str->capa * 2;
@ -212,7 +212,7 @@ ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len)
{
/* let the user determine the new capacity.
* pass the minimum capacity required as a hint */
ncapa = str->sizer (str, str->size + len);
ncapa = str->sizer (str, str->len + len);
}
if (ase_str_setcapa (str, ncapa) == (ase_size_t)-1)
@ -221,19 +221,19 @@ ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len)
}
}
if (len > str->capa - str->size)
if (len > str->capa - str->len)
{
/* copy as many characters as the number of cells available */
len = str->capa - str->size;
len = str->capa - str->len;
}
if (len > 0)
{
ASE_MEMCPY (&str->buf[str->size], s, len*ASE_SIZEOF(*s));
str->size += len;
str->buf[str->size] = ASE_T('\0');
ASE_MEMCPY (&str->ptr[str->len], s, len*ASE_SIZEOF(*s));
str->len += len;
str->ptr[str->len] = ASE_T('\0');
}
return str->size;
return str->len;
}
ase_size_t ase_str_ccat (ase_str_t* str, ase_char_t c)
@ -252,6 +252,6 @@ ase_size_t ase_str_nccat (ase_str_t* str, ase_char_t c, ase_size_t len)
len--;
}
return str->size;
return str->len;
}