After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 622261 - foreach support for strings
foreach support for strings
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: Basic Types
unspecified
Other All
: Normal enhancement
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2010-06-21 06:56 UTC by Frederik Zipp
Modified: 2018-05-22 13:38 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Add foreach support for strings (3.86 KB, patch)
2010-08-01 11:03 UTC, Frederik Zipp
none Details | Review
Add test for foreach for strings (825 bytes, patch)
2010-08-01 11:26 UTC, Frederik Zipp
none Details | Review
Add foreach support for strings (4.58 KB, patch)
2010-08-01 16:36 UTC, Frederik Zipp
none Details | Review

Description Frederik Zipp 2010-06-21 06:56:35 UTC
"foreach (unichar c in my_string) { }" would be nice, since string already supports "unichar c = my_string[5]".
Comment 1 Frederik Zipp 2010-08-01 11:03:03 UTC
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 ());
	}
}
Comment 2 Frederik Zipp 2010-08-01 11:26:41 UTC
Created attachment 166920 [details] [review]
Add test for foreach for strings
Comment 3 carlo.teubner 2010-08-01 13:59:13 UTC
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.
Comment 4 carlo.teubner 2010-08-01 14:00:43 UTC
To clarify, I meant that one could add both method iterator() and inner class Iterator to class string in glib-2.0.vapi.
Comment 5 Frederik Zipp 2010-08-01 14:12:03 UTC
> 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.
Comment 6 carlo.teubner 2010-08-01 15:16:42 UTC
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).
Comment 7 Frederik Zipp 2010-08-01 16:36:17 UTC
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'.
Comment 8 Frederik Zipp 2010-10-23 09:35:09 UTC
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);
}
Comment 9 GNOME Infrastructure Team 2018-05-22 13:38:26 UTC
-- 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.