From 8396c4193b25b896bc3629c0199178074a70bab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Sat, 17 Sep 2016 21:37:18 +0200 Subject: [PATCH] Add string_buf(), string_compare_cs(), string_equal_cs(). --- include/cryb/string.h | 6 ++++++ include/cryb/wstring.h | 6 ++++++ lib/core/cryb_string_impl.c | 37 +++++++++++++++++++++++++++++++++++++ lib/core/cryb_wstring.c | 3 +++ 4 files changed, 52 insertions(+) diff --git a/include/cryb/string.h b/include/cryb/string.h index bf26256..97dcc8f 100644 --- a/include/cryb/string.h +++ b/include/cryb/string.h @@ -36,6 +36,7 @@ #define string_new cryb_string_new #define string_len cryb_string_len +#define string_buf cryb_string_buf #define string_dup cryb_string_dup #define string_dup_cs cryb_string_dup_cs #define string_delete cryb_string_delete @@ -48,12 +49,15 @@ #define string_printf cryb_string_printf #define string_vprintf cryb_string_vprintf #define string_compare cryb_string_compare +#define string_compare_cs cryb_string_compare_cs #define string_equal cryb_string_equal +#define string_equal_cs cryb_string_equal_cs typedef struct cryb_string string; string *string_new(void); size_t string_len(const string *); +const char *string_buf(const string *); string *string_dup(const string *); string *string_dup_cs(const char *, size_t); void string_delete(string *); @@ -68,6 +72,8 @@ ssize_t string_printf(string *, const char *, ...); ssize_t string_vprintf(string *, const char *, va_list); #endif int string_compare(const string *, const string *); +int string_compare_cs(const string *, const char *, size_t); int string_equal(const string *, const string *); +int string_equal_cs(const string *, const char *, size_t); #endif diff --git a/include/cryb/wstring.h b/include/cryb/wstring.h index 477b986..d3d15cc 100644 --- a/include/cryb/wstring.h +++ b/include/cryb/wstring.h @@ -36,6 +36,7 @@ #define wstring_new cryb_wstring_new #define wstring_len cryb_wstring_len +#define wstring_buf cryb_wstring_buf #define wstring_dup cryb_wstring_dup #define wstring_dup_wcs cryb_wstring_dup_wcs #define wstring_delete cryb_wstring_delete @@ -48,12 +49,15 @@ #define wstring_printf cryb_wstring_printf #define wstring_vprintf cryb_wstring_vprintf #define wstring_compare cryb_wstring_compare +#define wstring_compare_wcs cryb_wstring_compare_wcs #define wstring_equal cryb_wstring_equal +#define wstring_equal_wcs cryb_wstring_equal_wcs typedef struct cryb_wstring wstring; wstring *wstring_new(void); size_t wstring_len(const wstring *); +const wchar_t *wstring_buf(const wstring *); wstring *wstring_dup(const wstring *); wstring *wstring_dup_wcs(const wchar_t *, size_t); void wstring_delete(wstring *); @@ -68,6 +72,8 @@ ssize_t wstring_printf(wstring *, const wchar_t *, ...); ssize_t wstring_vprintf(wstring *, const wchar_t *, va_list); #endif int wstring_compare(const wstring *, const wstring *); +int wstring_compare_wcs(const wstring *, const wchar_t *, size_t); int wstring_equal(const wstring *, const wstring *); +int wstring_equal_wcs(const wstring *, const wchar_t *, size_t); #endif diff --git a/lib/core/cryb_string_impl.c b/lib/core/cryb_string_impl.c index d8ce8cd..f251b72 100644 --- a/lib/core/cryb_string_impl.c +++ b/lib/core/cryb_string_impl.c @@ -78,6 +78,17 @@ string_len(const string *str) return (str->len); } +/* + * Return a pointer to the current string buffer, valid only until the + * next operation that modifies the string + */ +const char_t * +string_buf(const string *str) +{ + + return (str->buf); +} + /* * Duplicate an existing string */ @@ -328,6 +339,17 @@ string_compare(const string *s1, const string *s2) return (*p1 ? 1 : *p2 ? -1 : 0); } +int +string_compare_cs(const string *s1, const char_t *s2, size_t len) +{ + const char_t *p1, *p2; + + for (p1 = s1->buf, p2 = s2; *p1 && *p2 && len--; ++p1, ++p2) + if (*p1 != *p2) + return (*p1 < *p2 ? -1 : 1); + return (*p1 ? 1 : *p2 ? -1 : 0); +} + /* * Compare two strings, returning true (non-zero) if they are equal and * false (zero) if they are not. @@ -342,3 +364,18 @@ string_equal(const string *s1, const string *s2) return (0); return (*p1 || *p2 ? 0 : 1); } + +/* + * Compare two strings, returning true (non-zero) if they are equal and + * false (zero) if they are not. + */ +int +string_equal_cs(const string *s1, const char_t *s2, size_t len) +{ + const char_t *p1, *p2; + + for (p1 = s1->buf, p2 = s2; *p1 && *p2 && len--; ++p1, ++p2) + if (*p1 != *p2) + return (0); + return (*p1 || *p2 ? 0 : 1); +} diff --git a/lib/core/cryb_wstring.c b/lib/core/cryb_wstring.c index fa946ea..94d7db1 100644 --- a/lib/core/cryb_wstring.c +++ b/lib/core/cryb_wstring.c @@ -45,6 +45,7 @@ #define string_new wstring_new #define string_len wstring_len +#define string_buf wstring_buf #define string_expand wstring_expand #define string_shrink wstring_shrink #define string_dup wstring_dup @@ -57,6 +58,8 @@ #define string_printf wstring_printf #define string_vprintf wstring_vprintf #define string_compare wstring_compare +#define string_compare_cs wstring_compare_wcs #define string_equal wstring_equal +#define string_equal_cs wstring_equal_wcs #include "cryb_string_impl.c"