remove
and take
methods
=> 08d97e4bfec42162b7953efe80cc2b410b63fdcd
[1mdiff --git a/CHANGES.md b/CHANGES.md[m [1mindex ba891bd..59b43d4 100644[m [1m--- a/CHANGES.md[m [1m+++ b/CHANGES.md[m [36m@@ -2,6 +2,7 @@[m [m ## 1.7.1[m * String: Fixed issue with the `mid` method when starting position is at or past the end.[m [32m+[m[32m* StringList: Implemented missing iterator `remove` and `take` methods.[m * TlsRequest: TLS 1.2 is the minimum accepted protocol version.[m [m ## 1.7[m [1mdiff --git a/src/stringlist.c b/src/stringlist.c[m [1mindex 70ae6ec..e16d63e 100644[m [1m--- a/src/stringlist.c[m [1m+++ b/src/stringlist.c[m [36m@@ -32,7 +32,7 @@[m [mSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.[m #include[m #include [m [m [31m-#define iStringListMaxStringsPerNode 1024[m [32m+[m[32m#define iStringListMaxStringsPerNode 512 /* TODO: could be a per-list dynamic setting */[m [m iDeclareType(StringListNode)[m [m [36m@@ -101,6 +101,7 @@[m [mvoid clear_StringList(iStringList *d) {[m static iStringListNode *locateNode_StringList_(const iStringList *d, size_t pos, size_t *start_out) {[m iAssert(pos < d->size);[m if (isEmpty_List(&d->list)) {[m [32m+[m[32m *start_out = iInvalidPos;[m return NULL;[m }[m const iBool forwards = (pos < d->size / 2);[m [36m@@ -330,6 +331,11 @@[m [mvoid init_StringListIterator(iStringListIterator *d, iStringList *list) {[m void next_StringListIterator(iStringListIterator *d) {[m d->pos++;[m d->nodePos++;[m [32m+[m[32m if (!d->node || d->pos >= d->list->size) {[m [32m+[m[32m d->value = NULL;[m [32m+[m[32m return;[m [32m+[m[32m }[m [32m+[m[32m iAssert(d->nodePos <= size_StringListNode_(d->node));[m if (d->nodePos == size_StringListNode_(d->node)) {[m d->nodePos = 0;[m d->node = next_StringListNode_(d->node);[m [36m@@ -341,6 +347,25 @@[m [mvoid next_StringListIterator(iStringListIterator *d) {[m updateValue_StringListIterator(d);[m }[m [m [32m+[m[32mvoid remove_StringListIterator(iStringListIterator *d) {[m [32m+[m[32m delete_String(take_StringListIterator(d));[m [32m+[m[32m}[m [32m+[m [32m+[m[32miString *take_StringListIterator(iStringListIterator *d) {[m [32m+[m[32m iString *taken = take_StringList(d->list, d->pos);[m [32m+[m[32m if (d->list->size == 0) {[m [32m+[m[32m d->node = NULL;[m [32m+[m[32m }[m [32m+[m[32m else {[m [32m+[m[32m size_t start;[m [32m+[m[32m d->pos--;[m [32m+[m[32m d->node = locateNode_StringList_(d->list, d->pos < size_StringList(d->list) ? d->pos : 0,[m [32m+[m[32m &start);[m [32m+[m[32m d->nodePos = d->pos - start;[m [32m+[m[32m }[m [32m+[m[32m return taken;[m [32m+[m[32m}[m [32m+[m void init_StringListConstIterator(iStringListConstIterator *d, const iStringList *list) {[m d->node = front_List(&list->list);[m d->list = list;[m [1mdiff --git a/tests/t_string.c b/tests/t_string.c[m [1mindex e95b196..1e17811 100644[m [1m--- a/tests/t_string.c[m [1m+++ b/tests/t_string.c[m [36m@@ -42,12 +42,12 @@[m [mint main(int argc, char *argv[]) {[m const iString str = iStringLiteral("Ääkkönén");[m iString *upper = collect_String(upper_String(&str));[m iString *lower = collect_String(lower_String(&str));[m [31m- printf("Original: %s Upper: %s Lower: %s\n", [m [32m+[m[32m printf("Original: %s Upper: %s Lower: %s\n",[m cstrLocal_String(&str), cstrLocal_String(upper), cstrLocal_String(lower));[m }[m /* Test Unicode strings. */ {[m iString *s = collect_String(newCStr_String("A_Äö\U0001f698a"));[m [31m- printf("String: %s length: %zu size: %zu\n", [m [32m+[m[32m printf("String: %s length: %zu size: %zu\n",[m cstrLocal_String(s), length_String(s), size_String(s)); {[m iConstForEach(String, i, s) {[m printf(" char: %06x [%s]\n", i.value, cstrLocal_Char(i.value));[m [36m@@ -60,7 +60,7 @@[m [mint main(int argc, char *argv[]) {[m }[m printf("Starts with: %i %i\n", startsWith_String(s, "a"), startsWithCase_String(s, "a"));[m printf("Ends with: %i %i\n", endsWith_String(s, "a"), endsWithCase_String(s, "A"));[m [31m- printf("Mid: %s\n", cstrLocal_String(collect_String(mid_String(s, 3, 1)))); [m [32m+[m[32m printf("Mid: %s\n", cstrLocal_String(collect_String(mid_String(s, 3, 1))));[m printf("%s is at: %zu %zu\n", cstrLocal_Char(u'ö'), indexOfCStr_String(s, "ö"), indexOf_String(s, U'ö'));[m truncate_String(s, 3);[m printf("Truncated: %s\n", cstrLocal_String(s));[m [36m@@ -105,6 +105,34 @@[m [mint main(int argc, char *argv[]) {[m }[m iRelease(file);[m }[m [32m+[m[32m /* String list iteration. */ {[m [32m+[m[32m iStringList *list = new_StringList();[m [32m+[m[32m pushBackCStr_StringList(list, "first");[m [32m+[m[32m pushBackCStr_StringList(list, "second");[m [32m+[m[32m /* Remove it with the iterator. */[m [32m+[m[32m iForEach(StringList, j, list) {[m [32m+[m[32m if (!cmp_String(j.value, "first")) {[m [32m+[m[32m remove_StringListIterator(&j);[m [32m+[m[32m }[m [32m+[m[32m }[m [32m+[m[32m clear_StringList(list);[m [32m+[m[32m iAssert(size_StringList(list) == 0);[m [32m+[m[32m for (size_t i = 0; i < 514; i++) {[m [32m+[m[32m iString *s = newFormat_String("str%zu", i);[m [32m+[m[32m pushBack_StringList(list, s);[m [32m+[m[32m delete_String(s);[m [32m+[m[32m }[m [32m+[m[32m int numRemoved = 2;[m [32m+[m[32m printf("Iterating %zu strings, removing some:\n", size_StringList(list));[m [32m+[m[32m iForEach(StringList, i, list) {[m [32m+[m[32m if (i.pos == 256 && numRemoved-- > 0) {[m [32m+[m[32m remove_StringListIterator(&i);[m [32m+[m[32m continue;[m [32m+[m[32m }[m [32m+[m[32m printf("%s\n", cstr_String(i.value));[m [32m+[m[32m }[m [32m+[m[32m iRelease(list);[m [32m+[m[32m }[m /* Splitting a string. */ {[m const iString *str = &iStringLiteral("/usr/local/bin");[m const iRangecc rng = range_String(str);[m
text/gemini; charset=utf-8
This content has been proxied by September (ba2dc).