diff --git a/qse/include/qse/cmn/str.h b/qse/include/qse/cmn/str.h index f7763148..25360da0 100644 --- a/qse/include/qse/cmn/str.h +++ b/qse/include/qse/cmn/str.h @@ -1,5 +1,5 @@ /* - * $Id: str.h 90 2009-03-01 09:58:19Z hyunghwan.chung $ + * $Id: str.h 110 2009-03-24 05:52:27Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. @@ -420,37 +420,68 @@ void qse_str_setsizer ( ); /******/ -/* - * NAME: get capacity - * - * DESCRIPTION: +/****f* Common/qse_str_getcapa + * NAME + * qse_str_getcapa - get capacity + * DESCRIPTION * The qse_str_getcapa() function returns the current capacity. * You may use QSE_STR_CAPA(str) macro for performance sake. - * - * RETURNS: the current capacity in number of characters. + * RETURNS + * current capacity in number of characters. */ qse_size_t qse_str_getcapa ( - qse_str_t* str /* a dynamic string */ + qse_str_t* str ); +/******/ -/* - * NAME: set new capacity - * - * DESCRIPTION: - * The qse_str_setcapa() function set new capacity. If the new capacity +/****f* Common/qse_str_setcapa + * NAME + * qse_str_setcapa - set new capacity + * DESCRIPTION + * The qse_str_setcapa() function sets the new capacity. If the new capacity * is smaller than the old, the overflowing characters are removed from * from the buffer. - * - * RETURNS: -1 on failure, a new capacity on success + * RETURNS + * (qse_size_t)-1 on failure, new capacity on success */ qse_size_t qse_str_setcapa ( - qse_str_t* str /* a dynamic string */, - qse_size_t capa /* a new capacity */ + qse_str_t* str, + qse_size_t capa ); +/******/ +/****f* Common/qse_str_getlen + * NAME + * qse_str_getlen - get length + */ +qse_size_t qse_str_getlen ( + qse_str_t* str +); +/******/ + +/****f* Common/qse_str_setlen + * NAME + * qse_str_setlen - change length + * RETURNS + * (qse_size_t)-1 on failure, new length on success + */ +qse_size_t qse_str_setlen ( + qse_str_t* str, + qse_size_t len +); +/******/ + +/****f* Common/qse_str_clear + * NAME + * qse_str_clear - clear a string + * DESCRIPTION + * The qse_str_clear() funtion deletes all characters in a string and sets + * the length to 0. It doesn't resize the internal buffer. + */ void qse_str_clear ( qse_str_t* str ); +/******/ void qse_str_swap ( qse_str_t* str, diff --git a/qse/lib/cmn/str_dyn.c b/qse/lib/cmn/str_dyn.c index 36c34035..dbd254f2 100644 --- a/qse/lib/cmn/str_dyn.c +++ b/qse/lib/cmn/str_dyn.c @@ -1,5 +1,5 @@ /* - * $Id: str_dyn.c 109 2009-03-24 01:44:31Z hyunghwan.chung $ + * $Id: str_dyn.c 110 2009-03-24 05:52:27Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. @@ -159,6 +159,31 @@ qse_size_t qse_str_setcapa (qse_str_t* str, qse_size_t capa) return str->capa; } +qse_size_t qse_str_getlen (qse_str_t* str) +{ + return QSE_STR_LEN (str); +} + +qse_size_t qse_str_setlen (qse_str_t* str, qse_size_t len) +{ + if (len == str->len) return len; + if (len < str->len) + { + str->len = len; + str->ptr[len] = QSE_T('\0'); + return len; + } + + if (len > str->capa) + { + if (qse_str_setcapa (str, len) == (qse_size_t)-1) + return (qse_size_t)-1; + } + + while (str->len < len) str->ptr[str->len++] = QSE_T(' '); + return str->len; +} + void qse_str_clear (qse_str_t* str) { str->len = 0; diff --git a/qse/lib/utl/sed.c b/qse/lib/utl/sed.c index 4cb8d9f1..b94e4acd 100644 --- a/qse/lib/utl/sed.c +++ b/qse/lib/utl/sed.c @@ -580,11 +580,7 @@ static int get_file_name (qse_sed_t* sed, qse_sed_cmd_t* cmd) if (trailing_spaces > 0) { - qse_str_delete ( - t, - QSE_STR_LEN(t) - trailing_spaces, - trailing_spaces - ); + qse_str_setlen (t, QSE_STR_LEN(t) - trailing_spaces); } qse_str_yield (t, &cmd->u.filename, 0); diff --git a/qse/test/cmn/str.c b/qse/test/cmn/str.c index 467c0cf3..a1348702 100644 --- a/qse/test/cmn/str.c +++ b/qse/test/cmn/str.c @@ -28,7 +28,19 @@ static int test1 () qse_printf (QSE_T("LEN=%u CAPA=%u [%.*s]\n"), (unsigned)QSE_STR_LEN(s1), (unsigned)QSE_STR_CAPA(s1), (int)QSE_STR_LEN(s1), QSE_STR_PTR(s1)); - + + qse_printf (QSE_T("LEN=%u\n"), + (unsigned int)qse_str_setlen (s1, QSE_STR_LEN(s1)-1)); + qse_printf (QSE_T("LEN=%u CAPA=%u [%.*s]\n"), + (unsigned)QSE_STR_LEN(s1), (unsigned)QSE_STR_CAPA(s1), + (int)QSE_STR_LEN(s1), QSE_STR_PTR(s1)); + + qse_printf (QSE_T("LEN=%u\n"), + (unsigned int)qse_str_setlen (s1, QSE_STR_CAPA(s1) + 5)); + qse_printf (QSE_T("LEN=%u CAPA=%u [%.*s]\n"), + (unsigned)QSE_STR_LEN(s1), (unsigned)QSE_STR_CAPA(s1), + (int)QSE_STR_LEN(s1), QSE_STR_PTR(s1)); + qse_str_close (s1); return 0; } @@ -215,7 +227,11 @@ static int test6 (void) qse_size_t wlen = QSE_COUNTOF(buf); n = qse_mbstowcs (x[i], buf, &wlen); - qse_printf (QSE_T("[%S]=>"),x[i]); + #ifdef QSE_CHAR_IS_MCHAR + qse_printf (QSE_T("[%s]=>"), x[i]); + #else + qse_printf (QSE_T("[%S]=>"), x[i]); + #endif #ifdef QSE_CHAR_IS_MCHAR qse_printf (QSE_T("[%S] %d chars %d bytes\n"), buf, (int)wlen, (int)n);