GNOME Bugzilla – Bug 622261
foreach support for strings
Last modified: 2018-05-22 13:38:26 UTC
"foreach (unichar c in my_string) { }" would be nice, since string already supports "unichar c = my_string[5]".
Created attachment 166918 [details] [review] Add foreach support for strings Demo: void main () { string s = "123 ١٢٣۰ ABC abc أبتةثجحخدذرزسشصضطظعغفقكلمنهوي"; foreach (unichar c in s) { stdout.printf ("%s\n", c.to_string ()); } }
Created attachment 166920 [details] [review] Add test for foreach for strings
Would you believe it... I was just about to post my patch for this! Pretty much identical to yours. One thing I was wondering though. Theoretically it should be possible to achieve this by just giving class string an iterator, as in: public Iterator iterator() { return Iterator (this); } public struct Iterator { private weak string _s; public Iterator (string s) { this._s = s; } public unichar get () { return _s.get_char(); } public bool next () { _s = _s.next_char(); char* ss = _s; return *ss != '\0'; } } Unfortunately, this does not work: the generated C code does not contain any of the definitions for the string.Iterator struct.
To clarify, I meant that one could add both method iterator() and inner class Iterator to class string in glib-2.0.vapi.
> Would you believe it... I was just about to post my patch for this! Pretty much > identical to yours. It's my first non-trivial patch to the core compiler, it was a low hanging fruit ;) > Unfortunately, this does not work: the generated C code does not contain any of > the definitions for the string.Iterator struct. Yes, the thing is, you can't add classes/structs with fields that don't exist in reality to .vapi bindings. Valac only adds custom methods to the generated code, not custom types.
Hmm, maybe it would be worth lifting that restriction? You would need an attribute to say that this class/struct is not defined in the library and that vala needs to generate the code for it. By the way, your generated C code will contain two invocations of g_utf8_get_char() per loop. This is not necessary; you could either save the resulting unichar as a temp, or your loop condition could simply be "*c_collection_it != '\0'". This would also allow you to process Modified UTF-8 (http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8) which can contain embedded null characters (although it sounds like this is mostly just a Java thing).
Created attachment 166935 [details] [review] Add foreach support for strings @carlo: Thank you! Updated patch, now with only one invocation of 'g_utf8_get_char'.
Now that string indices are byte based, would it be useful at all to have (byte based) foreach support for strings? Maybe for consistency reasons (since strings allow index access)? This already works with current Vala: foreach (uint8 b in "hello, world".data) { print ("%x\n", b); }
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/108.