mirror of
https://gitee.com/apaki/unicstl.git
synced 2026-05-29 07:04:20 +08:00
添加字符串比较
This commit is contained in:
parent
03f81cb8de
commit
6b4ef1d775
@ -30,9 +30,7 @@ struct _ustring
|
|||||||
{
|
{
|
||||||
// -------------------- private --------------------
|
// -------------------- private --------------------
|
||||||
arraylist_t _alist;
|
arraylist_t _alist;
|
||||||
|
|
||||||
bool is_view;
|
bool is_view;
|
||||||
|
|
||||||
bool _sorted;
|
bool _sorted;
|
||||||
|
|
||||||
struct _iterator _iter;
|
struct _iterator _iter;
|
||||||
@ -42,7 +40,7 @@ struct _ustring
|
|||||||
// char operations
|
// char operations
|
||||||
bool (*set)(struct _ustring *self, size_t index, const char c); // O(1)
|
bool (*set)(struct _ustring *self, size_t index, const char c); // O(1)
|
||||||
bool (*get)(struct _ustring *self, size_t index, char *c); // O(1)
|
bool (*get)(struct _ustring *self, size_t index, char *c); // O(1)
|
||||||
char (*at)(struct _ustring *self, size_t index); // O(1)
|
const char* (*at)(struct _ustring *self, size_t index); // O(1)
|
||||||
|
|
||||||
// base
|
// base
|
||||||
bool (*reserve)(struct _ustring *self, size_t capacity);
|
bool (*reserve)(struct _ustring *self, size_t capacity);
|
||||||
@ -108,13 +106,13 @@ struct _ustring
|
|||||||
bool (*format)(struct _ustring *self, const char *format);
|
bool (*format)(struct _ustring *self, const char *format);
|
||||||
|
|
||||||
// compare
|
// compare
|
||||||
int (*eq)(struct _ustring *self, struct _ustring *other);
|
int (*eq)(struct _ustring *self, uview_t v);
|
||||||
int (*ne)(struct _ustring *self, struct _ustring *other);
|
int (*ne)(struct _ustring *self, uview_t v);
|
||||||
int (*lt)(struct _ustring *self, struct _ustring *other);
|
int (*lt)(struct _ustring *self, uview_t v);
|
||||||
int (*le)(struct _ustring *self, struct _ustring *other);
|
int (*le)(struct _ustring *self, uview_t v);
|
||||||
int (*gt)(struct _ustring *self, struct _ustring *other);
|
int (*gt)(struct _ustring *self, uview_t v);
|
||||||
int (*ge)(struct _ustring *self, struct _ustring *other);
|
int (*ge)(struct _ustring *self, uview_t v);
|
||||||
int (*cmp)(struct _ustring *self, struct _ustring *other);
|
int (*cmp)(struct _ustring *self, uview_t v);
|
||||||
|
|
||||||
// iter
|
// iter
|
||||||
iterator_t (*iter)(struct _ustring *self, linear_order_t order);
|
iterator_t (*iter)(struct _ustring *self, linear_order_t order);
|
||||||
|
|||||||
@ -421,6 +421,7 @@ static bool darray_init(struct _darray *self, size_t obj_size, size_t capacity)
|
|||||||
self->search = darry_search;
|
self->search = darry_search;
|
||||||
|
|
||||||
// -------------------- default --------------------
|
// -------------------- default --------------------
|
||||||
|
self->compare = compare_char;
|
||||||
self->print_obj = default_print_obj;
|
self->print_obj = default_print_obj;
|
||||||
|
|
||||||
// -------------------- debug --------------------
|
// -------------------- debug --------------------
|
||||||
|
|||||||
@ -143,11 +143,11 @@ static bool ustring_get(struct _ustring *self, size_t index, char *c)
|
|||||||
return self->_alist->get(self->_alist, index, &c);
|
return self->_alist->get(self->_alist, index, &c);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char ustring_at(struct _ustring *self, size_t index)
|
static const char* ustring_at(struct _ustring *self, size_t index)
|
||||||
{
|
{
|
||||||
unicstl_assert(self != NULL);
|
unicstl_assert(self != NULL);
|
||||||
unicstl_assert(self->_alist != NULL);
|
unicstl_assert(self->_alist != NULL);
|
||||||
return *(char *)self->_alist->at(self->_alist, index);
|
return self->_alist->at(self->_alist, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ustring_iter_hasnext(struct _iterator *iter)
|
bool ustring_iter_hasnext(struct _iterator *iter)
|
||||||
@ -473,6 +473,60 @@ static bool ustring_strip_right(struct _ustring *self)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ustring_cmp(struct _ustring *self, uview_t v)
|
||||||
|
{
|
||||||
|
unicstl_assert(self != NULL);
|
||||||
|
// size_t min = self->len(self) < v.len ? self->len(self) : v.len;
|
||||||
|
// for (size_t i = 0; i < min; i++)
|
||||||
|
// {
|
||||||
|
// const char *a = (const char *)self->at(self, i);
|
||||||
|
// if (*a != v.str[i])
|
||||||
|
// {
|
||||||
|
// return *(char*)self->at(self, i) - v.str[i];
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
log_debug("self[%d]:%s, v.str[%d]:%s", self->len(self), self->at(self, 0), v.len, v.str);
|
||||||
|
|
||||||
|
size_t max = self->len(self) > v.len ? self->len(self) : v.len;
|
||||||
|
return strncmp(self->at(self, 0), v.str, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ustring_eq(struct _ustring *self, uview_t v)
|
||||||
|
{
|
||||||
|
unicstl_assert(self != NULL);
|
||||||
|
return self->cmp(self, v) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ustring_ne(struct _ustring *self, uview_t v)
|
||||||
|
{
|
||||||
|
unicstl_assert(self != NULL);
|
||||||
|
return self->cmp(self, v) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ustring_lt(struct _ustring *self, uview_t v)
|
||||||
|
{
|
||||||
|
unicstl_assert(self != NULL);
|
||||||
|
return self->cmp(self, v) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ustring_le(struct _ustring *self, uview_t v)
|
||||||
|
{
|
||||||
|
unicstl_assert(self != NULL);
|
||||||
|
return self->cmp(self, v) <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ustring_gt(struct _ustring *self, uview_t v)
|
||||||
|
{
|
||||||
|
unicstl_assert(self != NULL);
|
||||||
|
return self->cmp(self, v) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ustring_ge(struct _ustring *self, uview_t v)
|
||||||
|
{
|
||||||
|
unicstl_assert(self != NULL);
|
||||||
|
return self->cmp(self, v) >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
static bool ustring_init(struct _ustring *self, uview_t view, bool is_view)
|
static bool ustring_init(struct _ustring *self, uview_t view, bool is_view)
|
||||||
{
|
{
|
||||||
unicstl_assert(self != NULL);
|
unicstl_assert(self != NULL);
|
||||||
@ -534,6 +588,14 @@ static bool ustring_init(struct _ustring *self, uview_t view, bool is_view)
|
|||||||
self->strip_left = ustring_strip_left;
|
self->strip_left = ustring_strip_left;
|
||||||
self->strip_right = ustring_strip_right;
|
self->strip_right = ustring_strip_right;
|
||||||
|
|
||||||
|
self->eq = ustring_eq;
|
||||||
|
self->ne = ustring_ne;
|
||||||
|
self->lt = ustring_lt;
|
||||||
|
self->le = ustring_le;
|
||||||
|
self->gt = ustring_gt;
|
||||||
|
self->ge = ustring_ge;
|
||||||
|
self->cmp = ustring_cmp;
|
||||||
|
|
||||||
// -------------------- default --------------------
|
// -------------------- default --------------------
|
||||||
self->print_obj = uprint_char;
|
self->print_obj = uprint_char;
|
||||||
|
|
||||||
|
|||||||
@ -183,6 +183,24 @@ void test_ustring_append(void)
|
|||||||
ustring_free(&str2);
|
ustring_free(&str2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_ustring_cmp(void)
|
||||||
|
{
|
||||||
|
ustring_t str = ustring_new_fromcstr("unicstl");
|
||||||
|
TEST_ASSERT_TRUE(str->eq(str, uv("unicstl")));
|
||||||
|
TEST_ASSERT_TRUE(str->ne(str, uv("unicstl1")));
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(str->eq(str, uv("")));
|
||||||
|
TEST_ASSERT_TRUE(str->ne(str, uv("")));
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(str->eq(str, uv("unicstz")));
|
||||||
|
TEST_ASSERT_FALSE(str->ne(str, uv("unicstl")));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(str->le(str, uv("unicstz")));
|
||||||
|
TEST_ASSERT_TRUE(str->ge(str, uv("unicsta")));
|
||||||
|
TEST_ASSERT_TRUE(str->lt(str, uv("unicstz")));
|
||||||
|
TEST_ASSERT_TRUE(str->gt(str, uv("unicsta")));
|
||||||
|
ustring_free(&str);
|
||||||
|
}
|
||||||
|
|
||||||
void test_ustring(void)
|
void test_ustring(void)
|
||||||
{
|
{
|
||||||
@ -210,4 +228,6 @@ void test_ustring(void)
|
|||||||
RUN_TEST(test_ustring_strip);
|
RUN_TEST(test_ustring_strip);
|
||||||
|
|
||||||
RUN_TEST(test_ustring_append);
|
RUN_TEST(test_ustring_append);
|
||||||
|
|
||||||
|
RUN_TEST(test_ustring_cmp);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user