ds: string: implement b_iterable interface
This commit is contained in:
32
ds/string.c
32
ds/string.c
@@ -48,7 +48,6 @@ struct b_string_iterator_p {
|
|||||||
const char **_d;
|
const char **_d;
|
||||||
size_t _nd, _ds;
|
size_t _nd, _ds;
|
||||||
|
|
||||||
b_status status;
|
|
||||||
size_t iteration_index;
|
size_t iteration_index;
|
||||||
size_t byte_index;
|
size_t byte_index;
|
||||||
size_t codepoint_index;
|
size_t codepoint_index;
|
||||||
@@ -1412,7 +1411,7 @@ static b_iterator *iterator_begin(b_object *obj)
|
|||||||
struct b_string_p *p = b_object_get_private(obj, B_TYPE_STRING);
|
struct b_string_p *p = b_object_get_private(obj, B_TYPE_STRING);
|
||||||
|
|
||||||
if (!p->s_len) {
|
if (!p->s_len) {
|
||||||
it->status = B_ERR_NO_DATA;
|
b_iterator_set_status(it_obj, B_ERR_NO_DATA);
|
||||||
return it_obj;
|
return it_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1422,12 +1421,17 @@ static b_iterator *iterator_begin(b_object *obj)
|
|||||||
it->char_value = b_wchar_utf8_codepoint_decode(s);
|
it->char_value = b_wchar_utf8_codepoint_decode(s);
|
||||||
|
|
||||||
if (it->char_value == B_WCHAR_INVALID) {
|
if (it->char_value == B_WCHAR_INVALID) {
|
||||||
it->status = B_ERR_BAD_FORMAT;
|
b_iterator_set_status(it_obj, B_ERR_BAD_FORMAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
return it_obj;
|
return it_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const b_iterator *iterator_cbegin(const b_object *obj)
|
||||||
|
{
|
||||||
|
return iterator_begin((b_object *)obj);
|
||||||
|
}
|
||||||
|
|
||||||
static enum b_status chars_iterator_move_next(struct b_string_iterator_p *it)
|
static enum b_status chars_iterator_move_next(struct b_string_iterator_p *it)
|
||||||
{
|
{
|
||||||
if (!it->_s_p) {
|
if (!it->_s_p) {
|
||||||
@@ -1513,6 +1517,21 @@ static b_iterator_value iterator_get_value(b_iterator *obj)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const b_iterator_value iterator_get_cvalue(const b_iterator *obj)
|
||||||
|
{
|
||||||
|
struct b_string_iterator_p *it
|
||||||
|
= b_object_get_private(obj, B_TYPE_STRING_ITERATOR);
|
||||||
|
|
||||||
|
switch (it->_m) {
|
||||||
|
case ITERATOR_MODE_CHARS:
|
||||||
|
return chars_iterator_get_value(it);
|
||||||
|
case ITERATOR_MODE_TOKENS:
|
||||||
|
return tokens_iterator_get_value(it);
|
||||||
|
default:
|
||||||
|
return B_ITERATOR_VALUE_NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|
||||||
// ---- b_string DEFINITION
|
// ---- b_string DEFINITION
|
||||||
@@ -1520,10 +1539,16 @@ B_TYPE_CLASS_DEFINITION_BEGIN(b_string)
|
|||||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
||||||
B_INTERFACE_ENTRY(to_string) = string_to_string;
|
B_INTERFACE_ENTRY(to_string) = string_to_string;
|
||||||
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
||||||
|
|
||||||
|
B_TYPE_CLASS_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE)
|
||||||
|
B_INTERFACE_ENTRY(it_begin) = iterator_begin;
|
||||||
|
B_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
|
||||||
|
B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE)
|
||||||
B_TYPE_CLASS_DEFINITION_END(b_string)
|
B_TYPE_CLASS_DEFINITION_END(b_string)
|
||||||
|
|
||||||
B_TYPE_DEFINITION_BEGIN(b_string)
|
B_TYPE_DEFINITION_BEGIN(b_string)
|
||||||
B_TYPE_ID(0x200194f6, 0x0327, 0x4a82, 0xb9c9, 0xb62ddd038c33);
|
B_TYPE_ID(0x200194f6, 0x0327, 0x4a82, 0xb9c9, 0xb62ddd038c33);
|
||||||
|
B_TYPE_IMPLEMENTS(B_TYPE_ITERABLE);
|
||||||
B_TYPE_CLASS(b_string_class);
|
B_TYPE_CLASS(b_string_class);
|
||||||
B_TYPE_INSTANCE_PRIVATE(struct b_string_p);
|
B_TYPE_INSTANCE_PRIVATE(struct b_string_p);
|
||||||
B_TYPE_INSTANCE_INIT(string_init);
|
B_TYPE_INSTANCE_INIT(string_init);
|
||||||
@@ -1540,6 +1565,7 @@ B_TYPE_CLASS_DEFINITION_BEGIN(b_string_iterator)
|
|||||||
B_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
B_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||||
B_INTERFACE_ENTRY(it_erase) = NULL;
|
B_INTERFACE_ENTRY(it_erase) = NULL;
|
||||||
B_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
B_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||||
|
B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
||||||
B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR)
|
B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR)
|
||||||
B_TYPE_CLASS_DEFINITION_END(b_string_iterator)
|
B_TYPE_CLASS_DEFINITION_END(b_string_iterator)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user