From e6049c1d6aa7a9479c2e7f7cdc690d7aa25bc44a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 12 Oct 2015 10:23:24 +0000 Subject: [PATCH] Add string_len() (return length of string) and string_dup_cs() (create a string from a null-terminated string). Add test for string_trunc(). --- include/cryb/string.h | 4 ++++ include/cryb/wstring.h | 4 ++++ lib/core/_string.c | 27 +++++++++++++++++++++++++++ lib/core/wstring.c | 2 ++ t/t__string.c | 25 +++++++++++++++++++++++++ t/t_wstring.c | 2 ++ 6 files changed, 64 insertions(+) diff --git a/include/cryb/string.h b/include/cryb/string.h index a9cea83..408eb5b 100644 --- a/include/cryb/string.h +++ b/include/cryb/string.h @@ -31,7 +31,9 @@ #define CRYB_STRING_H_INCLUDED #define string_new cryb_string_new +#define string_len cryb_string_len #define string_dup cryb_string_dup +#define string_dup_cs cryb_string_dup_cs #define string_delete cryb_string_delete #define string_expand cryb_string_expand #define string_shrink cryb_string_shrink @@ -47,7 +49,9 @@ typedef struct cryb_string string; string *string_new(void); +size_t string_len(const string *); string *string_dup(const string *); +string *string_dup_cs(const char *, size_t); void string_delete(string *); int string_expand(string *, size_t); void string_shrink(string *); diff --git a/include/cryb/wstring.h b/include/cryb/wstring.h index 2ff74ee..81867f9 100644 --- a/include/cryb/wstring.h +++ b/include/cryb/wstring.h @@ -31,7 +31,9 @@ #define CRYB_WSTRING_H_INCLUDED #define wstring_new cryb_wstring_new +#define wstring_len cryb_wstring_len #define wstring_dup cryb_wstring_dup +#define wstring_dup_wcs cryb_wstring_dup_wcs #define wstring_delete cryb_wstring_delete #define wstring_expand cryb_wstring_expand #define wstring_shrink cryb_wstring_shrink @@ -47,7 +49,9 @@ typedef struct cryb_wstring wstring; wstring *wstring_new(void); +size_t wstring_len(const wstring *); wstring *wstring_dup(const wstring *); +wstring *wstring_dup_wcs(const wchar_t *, size_t); void wstring_delete(wstring *); int wstring_expand(wstring *, size_t); void wstring_shrink(wstring *); diff --git a/lib/core/_string.c b/lib/core/_string.c index c27169a..57c0ea7 100644 --- a/lib/core/_string.c +++ b/lib/core/_string.c @@ -66,6 +66,16 @@ string_new(void) return (str); } +/* + * Return the length of a string + */ +size_t +string_len(const string *str) +{ + + return (str->len); +} + /* * Duplicate an existing string */ @@ -85,6 +95,23 @@ string_dup(const string *str) 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 */ diff --git a/lib/core/wstring.c b/lib/core/wstring.c index cd9f7e1..59d140e 100644 --- a/lib/core/wstring.c +++ b/lib/core/wstring.c @@ -44,9 +44,11 @@ #define string wstring #define string_new wstring_new +#define string_len wstring_len #define string_expand wstring_expand #define string_shrink wstring_shrink #define string_dup wstring_dup +#define string_dup_cs wstring_dup_wcs #define string_delete wstring_delete #define string_trunc wstring_trunc #define string_append_c wstring_append_wc diff --git a/t/t__string.c b/t/t__string.c index 0110e21..ebf083b 100644 --- a/t/t__string.c +++ b/t/t__string.c @@ -135,6 +135,30 @@ t_equal_test(char **desc CRYB_UNUSED, void *arg) 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 @@ -155,6 +179,7 @@ t_prepare(int argc, char *argv[]) for (i = 0; i < sizeof t_compare_cases / sizeof *t_compare_cases; ++i) t_add_test(t_equal_test, &t_compare_cases[i], "%s(%s)", "string_equal", t_compare_cases[i].desc); + t_add_test(t_trunc_test, NULL, "string_trunc"); return (0); } diff --git a/t/t_wstring.c b/t/t_wstring.c index ebe015d..e2cbdaa 100644 --- a/t/t_wstring.c +++ b/t/t_wstring.c @@ -47,9 +47,11 @@ #define string wstring #define string_new wstring_new +#define string_len wstring_len #define string_expand wstring_expand #define string_shrink wstring_shrink #define string_dup wstring_dup +#define string_dup_cs wstring_dup_wcs #define string_delete wstring_delete #define string_trunc wstring_trunc #define string_append_c wstring_append_wc