qse/lib/cmn/str-dyn.h

546 lines
12 KiB
C
Raw Normal View History

2008-03-18 22:09:04 +00:00
/*
2012-08-16 03:47:55 +00:00
* $Id$
2008-03-18 22:09:04 +00:00
*
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
2014-11-19 14:42:24 +00:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
2014-11-19 14:42:24 +00:00
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2008-03-18 22:09:04 +00:00
*/
2008-12-21 21:35:07 +00:00
#include <qse/cmn/str.h>
2016-04-29 03:55:42 +00:00
#include "mem-prv.h"
#include "fmt-prv.h"
2013-10-30 05:53:54 +00:00
#include <stdarg.h>
2008-03-18 22:09:04 +00:00
2013-10-29 14:14:43 +00:00
str_t* str_open (qse_mmgr_t* mmgr, qse_size_t xtnsize, qse_size_t capa)
2008-03-18 22:09:04 +00:00
{
2013-10-29 14:14:43 +00:00
str_t* str;
2008-03-18 22:09:04 +00:00
2013-10-29 14:14:43 +00:00
str = (str_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(str_t) + xtnsize);
if (str)
2008-03-18 22:09:04 +00:00
{
2013-10-29 14:14:43 +00:00
if (str_init (str, mmgr, capa) <= -1)
{
QSE_MMGR_FREE (mmgr, str);
str = QSE_NULL;
}
else
{
2014-07-11 14:17:00 +00:00
QSE_MEMSET (QSE_XTN(str), 0, xtnsize);
}
2008-03-18 22:09:04 +00:00
}
return str;
}
2013-10-29 14:14:43 +00:00
void str_close (str_t* str)
{
2013-10-29 14:14:43 +00:00
str_fini (str);
2008-12-21 21:35:07 +00:00
QSE_MMGR_FREE (str->mmgr, str);
}
2013-10-29 14:14:43 +00:00
int str_init (str_t* str, qse_mmgr_t* mmgr, qse_size_t capa)
{
2013-10-29 14:14:43 +00:00
QSE_MEMSET (str, 0, QSE_SIZEOF(str_t));
2008-09-05 04:58:08 +00:00
str->mmgr = mmgr;
2008-12-21 21:35:07 +00:00
str->sizer = QSE_NULL;
2011-05-19 08:36:40 +00:00
if (capa == 0) str->val.ptr = QSE_NULL;
else
{
2013-10-29 14:14:43 +00:00
str->val.ptr = (char_t*) QSE_MMGR_ALLOC (
mmgr, QSE_SIZEOF(char_t) * (capa + 1));
2011-09-01 09:43:46 +00:00
if (str->val.ptr == QSE_NULL) return -1;
2013-10-29 14:14:43 +00:00
str->val.ptr[0] = T('\0');
}
2011-05-19 08:36:40 +00:00
str->val.len = 0;
str->capa = capa;
2008-03-18 22:09:04 +00:00
2011-09-01 09:43:46 +00:00
return 0;
2008-03-18 22:09:04 +00:00
}
2013-10-29 14:14:43 +00:00
void str_fini (str_t* str)
2008-03-18 22:09:04 +00:00
{
2013-10-30 05:53:54 +00:00
if (str->val.ptr) QSE_MMGR_FREE (str->mmgr, str->val.ptr);
2008-03-18 22:09:04 +00:00
}
2013-10-29 14:14:43 +00:00
qse_mmgr_t* str_getmmgr (str_t* mbs)
{
return mbs->mmgr;
}
2013-10-29 14:14:43 +00:00
void* str_getxtn (str_t* mbs)
{
return QSE_XTN (mbs);
}
2014-07-08 14:30:42 +00:00
int str_yield (str_t* str, cstr_t* buf, qse_size_t newcapa)
2008-03-18 22:09:04 +00:00
{
2013-10-29 14:14:43 +00:00
char_t* tmp;
2011-06-21 08:56:40 +00:00
if (newcapa == 0) tmp = QSE_NULL;
else
{
2013-10-29 14:14:43 +00:00
tmp = (char_t*) QSE_MMGR_ALLOC (
str->mmgr, QSE_SIZEOF(char_t) * (newcapa + 1));
2008-12-21 21:35:07 +00:00
if (tmp == QSE_NULL) return -1;
2013-10-29 14:14:43 +00:00
tmp[0] = T('\0');
}
2019-11-17 16:23:50 +00:00
if (buf) *buf = str->val;
2011-05-19 08:36:40 +00:00
str->val.ptr = tmp;
str->val.len = 0;
2011-06-21 08:56:40 +00:00
str->capa = newcapa;
return 0;
2008-03-18 22:09:04 +00:00
}
2013-10-29 14:14:43 +00:00
char_t* str_yieldptr (str_t* str, qse_size_t newcapa)
2011-06-21 08:56:40 +00:00
{
2014-07-08 14:30:42 +00:00
cstr_t mx;
2013-10-29 14:14:43 +00:00
if (str_yield (str, &mx, newcapa) <= -1) return QSE_NULL;
2011-06-21 08:56:40 +00:00
return mx.ptr;
}
2013-10-29 14:14:43 +00:00
str_sizer_t str_getsizer (str_t* str)
2008-09-05 04:58:08 +00:00
{
return str->sizer;
}
2013-10-29 14:14:43 +00:00
void str_setsizer (str_t* str, str_sizer_t sizer)
2008-09-05 04:58:08 +00:00
{
str->sizer = sizer;
}
2013-10-29 14:14:43 +00:00
qse_size_t str_getcapa (str_t* str)
2008-09-05 04:58:08 +00:00
{
return str->capa;
}
2013-10-29 14:14:43 +00:00
qse_size_t str_setcapa (str_t* str, qse_size_t capa)
2008-09-05 04:58:08 +00:00
{
2013-10-29 14:14:43 +00:00
char_t* tmp;
2008-09-05 04:58:08 +00:00
2009-10-09 07:09:19 +00:00
if (capa == str->capa) return capa;
2011-05-19 08:36:40 +00:00
if (str->mmgr->realloc != QSE_NULL && str->val.ptr != QSE_NULL)
2008-09-05 04:58:08 +00:00
{
2013-10-29 14:14:43 +00:00
tmp = (char_t*) QSE_MMGR_REALLOC (
2011-05-19 08:36:40 +00:00
str->mmgr, str->val.ptr,
2013-10-29 14:14:43 +00:00
QSE_SIZEOF(char_t)*(capa+1));
2008-12-21 21:35:07 +00:00
if (tmp == QSE_NULL) return (qse_size_t)-1;
2008-09-05 04:58:08 +00:00
}
else
{
2013-10-29 14:14:43 +00:00
tmp = (char_t*) QSE_MMGR_ALLOC (
str->mmgr, QSE_SIZEOF(char_t)*(capa+1));
2008-12-21 21:35:07 +00:00
if (tmp == QSE_NULL) return (qse_size_t)-1;
2008-09-05 04:58:08 +00:00
if (str->val.ptr)
2008-09-05 04:58:08 +00:00
{
2011-05-19 08:36:40 +00:00
qse_size_t ncopy = (str->val.len <= capa)? str->val.len: capa;
QSE_MEMCPY (tmp, str->val.ptr,
2013-10-29 14:14:43 +00:00
QSE_SIZEOF(char_t)*(ncopy+1));
2011-05-19 08:36:40 +00:00
QSE_MMGR_FREE (str->mmgr, str->val.ptr);
2008-09-05 04:58:08 +00:00
}
}
2011-05-19 08:36:40 +00:00
if (capa < str->val.len)
2008-09-05 04:58:08 +00:00
{
2011-05-19 08:36:40 +00:00
str->val.len = capa;
2013-10-29 14:14:43 +00:00
tmp[capa] = T('\0');
2008-09-05 04:58:08 +00:00
}
str->capa = capa;
2011-05-19 08:36:40 +00:00
str->val.ptr = tmp;
2008-09-05 04:58:08 +00:00
return str->capa;
}
2013-10-29 14:14:43 +00:00
qse_size_t str_getlen (str_t* str)
{
2019-11-17 16:23:50 +00:00
return STR_LEN(str);
}
2013-10-29 14:14:43 +00:00
qse_size_t str_setlen (str_t* str, qse_size_t len)
{
2011-05-19 08:36:40 +00:00
if (len == str->val.len) return len;
if (len < str->val.len)
{
2011-05-19 08:36:40 +00:00
str->val.len = len;
str->val.ptr[len] = T('\0');
return len;
}
if (len > str->capa)
{
2019-11-17 16:23:50 +00:00
if (str_setcapa(str, len) == (qse_size_t)-1) return (qse_size_t)-1;
}
2013-10-29 14:14:43 +00:00
while (str->val.len < len) str->val.ptr[str->val.len++] = T(' ');
str->val.ptr[str->val.len] = T('\0');
2011-05-19 08:36:40 +00:00
return str->val.len;
}
2013-10-29 14:14:43 +00:00
void str_clear (str_t* str)
2008-03-18 22:09:04 +00:00
{
2011-05-19 08:36:40 +00:00
str->val.len = 0;
if (str->val.ptr)
2009-05-03 06:16:24 +00:00
{
QSE_ASSERT (str->capa >= 1);
2013-10-29 14:14:43 +00:00
str->val.ptr[0] = T('\0');
2009-05-03 06:16:24 +00:00
}
2008-03-18 22:09:04 +00:00
}
2013-10-29 14:14:43 +00:00
void str_swap (str_t* str, str_t* str1)
2008-03-18 22:09:04 +00:00
{
2013-10-29 14:14:43 +00:00
str_t tmp;
2008-03-18 22:09:04 +00:00
2011-05-19 08:36:40 +00:00
tmp.val.ptr = str->val.ptr;
tmp.val.len = str->val.len;
2008-03-18 22:09:04 +00:00
tmp.capa = str->capa;
tmp.mmgr = str->mmgr;
2011-05-19 08:36:40 +00:00
str->val.ptr = str1->val.ptr;
str->val.len = str1->val.len;
2008-03-18 22:09:04 +00:00
str->capa = str1->capa;
str->mmgr = str1->mmgr;
2011-05-19 08:36:40 +00:00
str1->val.ptr = tmp.val.ptr;
str1->val.len = tmp.val.len;
2008-03-18 22:09:04 +00:00
str1->capa = tmp.capa;
str1->mmgr = tmp.mmgr;
}
2013-10-29 14:14:43 +00:00
qse_size_t str_cpy (str_t* str, const char_t* s)
2008-03-18 22:09:04 +00:00
{
/* TODO: improve it */
2013-10-29 14:14:43 +00:00
return str_ncpy (str, s, strlen(s));
2008-03-18 22:09:04 +00:00
}
2013-10-29 14:14:43 +00:00
qse_size_t str_ncpy (str_t* str, const char_t* s, qse_size_t len)
2008-03-18 22:09:04 +00:00
{
if (len > str->capa || str->capa <= 0)
2008-03-18 22:09:04 +00:00
{
qse_size_t tmp;
/* if the current capacity is 0 and the string len to copy is 0
* we can't simply pass 'len' as the new capapcity.
2013-10-29 14:14:43 +00:00
* str_setcapa() won't do anything the current capacity of 0
* is the same as new capacity required. note that when str->capa
* is 0, str->val.ptr is QSE_NULL. However, this is copying operation.
* Copying a zero-length string may indicate that str->val.ptr must
* not be QSE_NULL. so I simply pass 1 as the new capacity */
2013-10-29 14:14:43 +00:00
tmp = str_setcapa (
str, ((str->capa <= 0 && len <= 0)? 1: len)
);
if (tmp == (qse_size_t)-1) return (qse_size_t)-1;
2008-03-18 22:09:04 +00:00
}
QSE_MEMCPY (&str->val.ptr[0], s, len*QSE_SIZEOF(*s));
2013-10-29 14:14:43 +00:00
str->val.ptr[len] = T('\0');
str->val.len = len;
return len;
#if 0
2013-10-29 14:14:43 +00:00
str->val.len = strncpy (str->val.ptr, s, len);
/*str->val.ptr[str->val.len] = T('\0'); -> mbsncpy does this*/
2011-05-19 08:36:40 +00:00
return str->val.len;
#endif
2008-03-18 22:09:04 +00:00
}
2013-10-29 14:14:43 +00:00
qse_size_t str_cat (str_t* str, const char_t* s)
2008-03-18 22:09:04 +00:00
{
/* TODO: improve it */
2013-10-29 14:14:43 +00:00
return str_ncat (str, s, strlen(s));
2008-03-18 22:09:04 +00:00
}
2013-10-29 14:14:43 +00:00
static int resize_for_ncat (str_t* str, qse_size_t len)
{
if (len > str->capa - str->val.len)
{
qse_size_t ncapa, mincapa;
/* let the minimum capacity be as large as
* to fit in the new substring */
mincapa = str->val.len + len;
if (str->sizer == QSE_NULL)
{
/* increase the capacity by the length to add */
ncapa = mincapa;
/* if the new capacity is less than the double,
* just double it */
if (ncapa < str->capa * 2) ncapa = str->capa * 2;
}
else
{
/* let the user determine the new capacity.
* pass the minimum capacity required as a hint */
ncapa = str->sizer(str, mincapa);
/* if no change in capacity, return current length */
if (ncapa == str->capa) return 0;
}
/* change the capacity */
do
{
if (str_setcapa(str, ncapa) != (qse_size_t)-1) break;
if (ncapa <= mincapa) return -1;
ncapa--;
}
while (1);
}
else if (str->capa <= 0 && len <= 0)
{
QSE_ASSERT (str->val.ptr == QSE_NULL);
QSE_ASSERT (str->val.len <= 0);
if (str_setcapa(str, 1) == (qse_size_t)-1) return -1;
}
return 1;
}
2013-10-29 14:14:43 +00:00
qse_size_t str_ncat (str_t* str, const char_t* s, qse_size_t len)
2008-03-18 22:09:04 +00:00
{
int n;
2013-10-29 14:14:43 +00:00
qse_size_t i, j;
n = resize_for_ncat(str, len);
if (n <= -1) return (qse_size_t)-1;
if (n == 0) return str->val.len;
2008-03-18 22:09:04 +00:00
2011-05-19 08:36:40 +00:00
if (len > str->capa - str->val.len)
2008-09-05 04:58:08 +00:00
{
2008-09-24 05:27:24 +00:00
/* copy as many characters as the number of cells available.
* if the capacity has been decreased, len is adjusted here */
2011-05-19 08:36:40 +00:00
len = str->capa - str->val.len;
2008-09-05 04:58:08 +00:00
}
2013-10-29 14:14:43 +00:00
/*
QSE_MEMCPY (&str->val.ptr[str->val.len], s, len*QSE_SIZEOF(*s));
str->val.len += len;
str->val.ptr[str->val.len] = T('\0');
*/
for (i = 0, j = str->val.len ; i < len; j++, i++) str->val.ptr[j] = s[i];
str->val.ptr[j] = T('\0');
str->val.len = j;
2008-09-24 05:27:24 +00:00
2011-05-19 08:36:40 +00:00
return str->val.len;
2008-03-18 22:09:04 +00:00
}
2013-10-29 14:14:43 +00:00
qse_size_t str_nrcat (str_t* str, const char_t* s, qse_size_t len)
{
int n;
qse_size_t i, j;
n = resize_for_ncat(str, len);
if (n <= -1) return (qse_size_t)-1;
if (n == 0) return str->val.len;
if (len > str->capa - str->val.len) len = str->capa - str->val.len;
for (i = len, j = str->val.len ; i > 0; j++) str->val.ptr[j] = s[--i];
2013-10-29 14:14:43 +00:00
str->val.ptr[j] = T('\0');
str->val.len = j;
return str->val.len;
}
2013-10-29 14:14:43 +00:00
qse_size_t str_ccat (str_t* str, char_t c)
2008-03-18 22:09:04 +00:00
{
2013-10-29 14:14:43 +00:00
return str_ncat (str, &c, 1);
2008-03-18 22:09:04 +00:00
}
2013-10-29 14:14:43 +00:00
qse_size_t str_nccat (str_t* str, char_t c, qse_size_t len)
2008-03-18 22:09:04 +00:00
{
while (len > 0)
{
2013-10-29 14:14:43 +00:00
if (str_ncat (str, &c, 1) == (qse_size_t)-1)
2008-03-18 22:09:04 +00:00
{
2008-12-21 21:35:07 +00:00
return (qse_size_t)-1;
2008-03-18 22:09:04 +00:00
}
len--;
}
2011-05-19 08:36:40 +00:00
return str->val.len;
2008-03-18 22:09:04 +00:00
}
2013-10-29 14:14:43 +00:00
qse_size_t str_del (str_t* str, qse_size_t index, qse_size_t size)
{
if (str->val.ptr && index < str->val.len && size > 0)
{
qse_size_t nidx = index + size;
2011-05-19 08:36:40 +00:00
if (nidx >= str->val.len)
{
2013-10-29 14:14:43 +00:00
str->val.ptr[index] = T('\0');
2011-05-19 08:36:40 +00:00
str->val.len = index;
}
else
{
2013-10-29 14:14:43 +00:00
strncpy (
2011-05-19 08:36:40 +00:00
&str->val.ptr[index], &str->val.ptr[nidx],
str->val.len - nidx);
str->val.len -= size;
}
}
2011-05-19 08:36:40 +00:00
return str->val.len;
}
2009-10-07 07:47:16 +00:00
qse_size_t str_amend (str_t* str, qse_size_t pos, qse_size_t len, const char_t* repl)
{
qse_size_t max_len;
qse_size_t repl_len = strlen(repl);
if (pos >= str->val.len) pos = str->val.len;
max_len = str->val.len - pos;
if (len > max_len) len = max_len;
if (len > repl_len)
{
str_del (str, pos, len - repl_len);
}
else if (len < repl_len)
{
qse_size_t old_str_len = str->val.len;
if (str_setlen (str, str->val.len + repl_len - len) == (qse_size_t)-1) return (qse_size_t)-1;
QSE_MEMMOVE (&str->val.ptr[pos + repl_len], &str->val.ptr[pos + len], QSE_SIZEOF(*repl) * (old_str_len - (pos + len)));
}
if (repl_len > 0) QSE_MEMMOVE (&str->val.ptr[pos], repl, QSE_SIZEOF(*repl) * repl_len);
return str->val.len;
}
2013-10-29 14:14:43 +00:00
qse_size_t str_trm (str_t* str)
2009-10-07 07:47:16 +00:00
{
if (str->val.ptr)
2009-10-07 07:47:16 +00:00
{
2013-10-29 14:14:43 +00:00
str->val.len = strxtrm (str->val.ptr, str->val.len);
2009-10-07 07:47:16 +00:00
}
2011-05-19 08:36:40 +00:00
return str->val.len;
2009-10-07 07:47:16 +00:00
}
2013-10-29 14:14:43 +00:00
qse_size_t str_pac (str_t* str)
2009-10-07 07:47:16 +00:00
{
if (str->val.ptr)
2009-10-07 07:47:16 +00:00
{
2013-10-29 14:14:43 +00:00
str->val.len = strxpac (str->val.ptr, str->val.len);
2009-10-07 07:47:16 +00:00
}
2011-05-19 08:36:40 +00:00
return str->val.len;
2009-10-07 07:47:16 +00:00
}
2013-10-30 13:50:21 +00:00
qse_size_t str_vfcat (str_t* str, const char_t* fmt, va_list ap)
2013-10-30 05:53:54 +00:00
{
2013-10-30 13:50:21 +00:00
va_list orgap;
fmtout_t fo;
int x;
qse_size_t old_len;
old_len = str->val.len;
fo.limit = QSE_TYPE_MAX(qse_size_t) - 1;
fo.ctx = str;
fo.put = str->val.ptr? put_char_check: put_char_null;
fo.conv = conv_char;
va_copy (orgap, ap);
x = fmtout (fmt, &fo, ap);
if (x <= -1)
{
str->val.len = old_len;
return (qse_size_t)-1;
}
if (str->val.ptr == QSE_NULL || str->val.len - old_len < fo.count)
{
str->val.len = old_len;
/* resizing is required */
x = resize_for_ncat(str, fo.count);
2013-10-30 13:50:21 +00:00
if (x <= -1) return (qse_size_t)-1;
if (x >= 1)
{
fo.put = put_char_nocheck;
x = fmtout (fmt, &fo, orgap);
}
}
str->val.ptr[str->val.len] = T('\0');
return str->val.len;
2013-10-30 05:53:54 +00:00
}
qse_size_t str_fcat (str_t* str, const char_t* fmt, ...)
{
2013-10-30 13:50:21 +00:00
qse_size_t x;
va_list ap;
va_start (ap, fmt);
x = str_vfcat (str, fmt, ap);
va_end (ap);
return x;
}
qse_size_t str_vfmt (str_t* str, const char_t* fmt, va_list ap)
{
int x;
fmtout_t fo;
va_list orgap;
fo.limit = QSE_TYPE_MAX(qse_size_t) - 1;
fo.ctx = str;
fo.put = put_char_null;
fo.conv = conv_char;
va_copy (orgap, ap);
if (fmtout (fmt, &fo, ap) <= -1) return (qse_size_t)-1;
str_clear (str);
x = resize_for_ncat(str, fo.count);
2013-10-30 13:50:21 +00:00
if (x <= -1) return (qse_size_t)-1;
if (x >= 1)
{
fo.put = put_char_nocheck;
x = fmtout (fmt, &fo, orgap);
}
str->val.ptr[str->val.len] = T('\0');
return str->val.len;
}
qse_size_t str_fmt (str_t* str, const char_t* fmt, ...)
{
qse_size_t x;
va_list ap;
va_start (ap, fmt);
x = str_vfmt (str, fmt, ap);
va_end (ap);
return x;
2013-10-30 05:53:54 +00:00
}