Add string_buf(), string_compare_cs(), string_equal_cs().

This commit is contained in:
Dag-Erling Smørgrav 2016-09-17 21:37:18 +02:00
parent cb6743bace
commit 8396c4193b
4 changed files with 52 additions and 0 deletions

View file

@ -36,6 +36,7 @@
#define string_new cryb_string_new #define string_new cryb_string_new
#define string_len cryb_string_len #define string_len cryb_string_len
#define string_buf cryb_string_buf
#define string_dup cryb_string_dup #define string_dup cryb_string_dup
#define string_dup_cs cryb_string_dup_cs #define string_dup_cs cryb_string_dup_cs
#define string_delete cryb_string_delete #define string_delete cryb_string_delete
@ -48,12 +49,15 @@
#define string_printf cryb_string_printf #define string_printf cryb_string_printf
#define string_vprintf cryb_string_vprintf #define string_vprintf cryb_string_vprintf
#define string_compare cryb_string_compare #define string_compare cryb_string_compare
#define string_compare_cs cryb_string_compare_cs
#define string_equal cryb_string_equal #define string_equal cryb_string_equal
#define string_equal_cs cryb_string_equal_cs
typedef struct cryb_string string; typedef struct cryb_string string;
string *string_new(void); string *string_new(void);
size_t string_len(const string *); size_t string_len(const string *);
const char *string_buf(const string *);
string *string_dup(const string *); string *string_dup(const string *);
string *string_dup_cs(const char *, size_t); string *string_dup_cs(const char *, size_t);
void string_delete(string *); void string_delete(string *);
@ -68,6 +72,8 @@ ssize_t string_printf(string *, const char *, ...);
ssize_t string_vprintf(string *, const char *, va_list); ssize_t string_vprintf(string *, const char *, va_list);
#endif #endif
int string_compare(const string *, const string *); 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(const string *, const string *);
int string_equal_cs(const string *, const char *, size_t);
#endif #endif

View file

@ -36,6 +36,7 @@
#define wstring_new cryb_wstring_new #define wstring_new cryb_wstring_new
#define wstring_len cryb_wstring_len #define wstring_len cryb_wstring_len
#define wstring_buf cryb_wstring_buf
#define wstring_dup cryb_wstring_dup #define wstring_dup cryb_wstring_dup
#define wstring_dup_wcs cryb_wstring_dup_wcs #define wstring_dup_wcs cryb_wstring_dup_wcs
#define wstring_delete cryb_wstring_delete #define wstring_delete cryb_wstring_delete
@ -48,12 +49,15 @@
#define wstring_printf cryb_wstring_printf #define wstring_printf cryb_wstring_printf
#define wstring_vprintf cryb_wstring_vprintf #define wstring_vprintf cryb_wstring_vprintf
#define wstring_compare cryb_wstring_compare #define wstring_compare cryb_wstring_compare
#define wstring_compare_wcs cryb_wstring_compare_wcs
#define wstring_equal cryb_wstring_equal #define wstring_equal cryb_wstring_equal
#define wstring_equal_wcs cryb_wstring_equal_wcs
typedef struct cryb_wstring wstring; typedef struct cryb_wstring wstring;
wstring *wstring_new(void); wstring *wstring_new(void);
size_t wstring_len(const wstring *); size_t wstring_len(const wstring *);
const wchar_t *wstring_buf(const wstring *);
wstring *wstring_dup(const wstring *); wstring *wstring_dup(const wstring *);
wstring *wstring_dup_wcs(const wchar_t *, size_t); wstring *wstring_dup_wcs(const wchar_t *, size_t);
void wstring_delete(wstring *); 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); ssize_t wstring_vprintf(wstring *, const wchar_t *, va_list);
#endif #endif
int wstring_compare(const wstring *, const wstring *); 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(const wstring *, const wstring *);
int wstring_equal_wcs(const wstring *, const wchar_t *, size_t);
#endif #endif

View file

@ -78,6 +78,17 @@ string_len(const string *str)
return (str->len); 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 * Duplicate an existing string
*/ */
@ -328,6 +339,17 @@ string_compare(const string *s1, const string *s2)
return (*p1 ? 1 : *p2 ? -1 : 0); 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 * Compare two strings, returning true (non-zero) if they are equal and
* false (zero) if they are not. * false (zero) if they are not.
@ -342,3 +364,18 @@ string_equal(const string *s1, const string *s2)
return (0); return (0);
return (*p1 || *p2 ? 0 : 1); 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);
}

View file

@ -45,6 +45,7 @@
#define string_new wstring_new #define string_new wstring_new
#define string_len wstring_len #define string_len wstring_len
#define string_buf wstring_buf
#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
@ -57,6 +58,8 @@
#define string_printf wstring_printf #define string_printf wstring_printf
#define string_vprintf wstring_vprintf #define string_vprintf wstring_vprintf
#define string_compare wstring_compare #define string_compare wstring_compare
#define string_compare_cs wstring_compare_wcs
#define string_equal wstring_equal #define string_equal wstring_equal
#define string_equal_cs wstring_equal_wcs
#include "cryb_string_impl.c" #include "cryb_string_impl.c"