Add string_len() (return length of string) and string_dup_cs() (create a

string from a null-terminated string).  Add test for string_trunc().
This commit is contained in:
Dag-Erling Smørgrav 2015-10-12 10:23:24 +00:00 committed by des
parent eb61dac230
commit e6049c1d6a
6 changed files with 64 additions and 0 deletions

View file

@ -31,7 +31,9 @@
#define CRYB_STRING_H_INCLUDED #define CRYB_STRING_H_INCLUDED
#define string_new cryb_string_new #define string_new cryb_string_new
#define string_len cryb_string_len
#define string_dup cryb_string_dup #define string_dup cryb_string_dup
#define string_dup_cs cryb_string_dup_cs
#define string_delete cryb_string_delete #define string_delete cryb_string_delete
#define string_expand cryb_string_expand #define string_expand cryb_string_expand
#define string_shrink cryb_string_shrink #define string_shrink cryb_string_shrink
@ -47,7 +49,9 @@
typedef struct cryb_string string; typedef struct cryb_string string;
string *string_new(void); string *string_new(void);
size_t string_len(const string *);
string *string_dup(const string *); string *string_dup(const string *);
string *string_dup_cs(const char *, size_t);
void string_delete(string *); void string_delete(string *);
int string_expand(string *, size_t); int string_expand(string *, size_t);
void string_shrink(string *); void string_shrink(string *);

View file

@ -31,7 +31,9 @@
#define CRYB_WSTRING_H_INCLUDED #define CRYB_WSTRING_H_INCLUDED
#define wstring_new cryb_wstring_new #define wstring_new cryb_wstring_new
#define wstring_len cryb_wstring_len
#define wstring_dup cryb_wstring_dup #define wstring_dup cryb_wstring_dup
#define wstring_dup_wcs cryb_wstring_dup_wcs
#define wstring_delete cryb_wstring_delete #define wstring_delete cryb_wstring_delete
#define wstring_expand cryb_wstring_expand #define wstring_expand cryb_wstring_expand
#define wstring_shrink cryb_wstring_shrink #define wstring_shrink cryb_wstring_shrink
@ -47,7 +49,9 @@
typedef struct cryb_wstring wstring; typedef struct cryb_wstring wstring;
wstring *wstring_new(void); wstring *wstring_new(void);
size_t wstring_len(const wstring *);
wstring *wstring_dup(const wstring *); wstring *wstring_dup(const wstring *);
wstring *wstring_dup_wcs(const wchar_t *, size_t);
void wstring_delete(wstring *); void wstring_delete(wstring *);
int wstring_expand(wstring *, size_t); int wstring_expand(wstring *, size_t);
void wstring_shrink(wstring *); void wstring_shrink(wstring *);

View file

@ -66,6 +66,16 @@ string_new(void)
return (str); return (str);
} }
/*
* Return the length of a string
*/
size_t
string_len(const string *str)
{
return (str->len);
}
/* /*
* Duplicate an existing string * Duplicate an existing string
*/ */
@ -85,6 +95,23 @@ string_dup(const string *str)
return (newstr); return (newstr);
} }
/*
* Duplicate an existing null-terminated string
*/
string *
string_dup_cs(const char_t *cs, size_t len)
{
string *newstr;
if ((newstr = string_new()) == NULL)
return (NULL);
if (string_append_cs(newstr, cs, len) < 0) {
string_delete(newstr);
return (NULL);
}
return (newstr);
}
/* /*
* Delete a string * Delete a string
*/ */

View file

@ -44,9 +44,11 @@
#define string wstring #define string wstring
#define string_new wstring_new #define string_new wstring_new
#define string_len wstring_len
#define string_expand wstring_expand #define string_expand wstring_expand
#define string_shrink wstring_shrink #define string_shrink wstring_shrink
#define string_dup wstring_dup #define string_dup wstring_dup
#define string_dup_cs wstring_dup_wcs
#define string_delete wstring_delete #define string_delete wstring_delete
#define string_trunc wstring_trunc #define string_trunc wstring_trunc
#define string_append_c wstring_append_wc #define string_append_c wstring_append_wc

View file

@ -135,6 +135,30 @@ t_equal_test(char **desc CRYB_UNUSED, void *arg)
return (ret); return (ret);
} }
/***************************************************************************
* Miscellaneous functions
*/
static int
t_trunc_test(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
string *s;
ssize_t len;
int ret;
if ((s = string_dup_cs(CS("magic ossifrage"), SIZE_MAX)) == NULL)
return (0);
if (!t_compare_sz(15, (len = string_len(s))))
return (0);
ret = t_compare_ssz(len, string_trunc(s, SIZE_MAX)) &
t_compare_ssz(len, string_trunc(s, len + 1)) &
t_compare_ssz(len, string_trunc(s, len)) &
t_compare_ssz(len - 1, string_trunc(s, len - 1));
string_delete(s);
return (ret);
}
/*************************************************************************** /***************************************************************************
* Boilerplate * Boilerplate
@ -155,6 +179,7 @@ t_prepare(int argc, char *argv[])
for (i = 0; i < sizeof t_compare_cases / sizeof *t_compare_cases; ++i) for (i = 0; i < sizeof t_compare_cases / sizeof *t_compare_cases; ++i)
t_add_test(t_equal_test, &t_compare_cases[i], t_add_test(t_equal_test, &t_compare_cases[i],
"%s(%s)", "string_equal", t_compare_cases[i].desc); "%s(%s)", "string_equal", t_compare_cases[i].desc);
t_add_test(t_trunc_test, NULL, "string_trunc");
return (0); return (0);
} }

View file

@ -47,9 +47,11 @@
#define string wstring #define string wstring
#define string_new wstring_new #define string_new wstring_new
#define string_len wstring_len
#define string_expand wstring_expand #define string_expand wstring_expand
#define string_shrink wstring_shrink #define string_shrink wstring_shrink
#define string_dup wstring_dup #define string_dup wstring_dup
#define string_dup_cs wstring_dup_wcs
#define string_delete wstring_delete #define string_delete wstring_delete
#define string_trunc wstring_trunc #define string_trunc wstring_trunc
#define string_append_c wstring_append_wc #define string_append_c wstring_append_wc