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 514186 - Array length for null-terminated arrays is always -1
Array length for null-terminated arrays is always -1
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Arrays
0.3.x
Other All
: Urgent major
: ---
Assigned To: Jürg Billeter
Vala maintainers
: 562391 (view as bug list)
Depends on: 532486
Blocks: 547135
 
 
Reported: 2008-02-03 22:56 UTC by ebichete
Modified: 2009-01-04 16:15 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
g_strv_length patch (534 bytes, patch)
2008-05-02 21:13 UTC, rainwoodman
reviewed Details | Review

Description ebichete 2008-02-03 22:56:58 UTC
Please describe the problem:
Accessing the '.length' member for a null-terminated array, such as that returned by 'string.split()', always gives the value of -1.

This is especially confusing because other arrays work just fine. Without a good knowledge of the Gtk docs it is hard to tell if the array you are dealing with is null-terminated or not.


Steps to reproduce:
// Here is some sample code that illustrates the problem
using GLib;

public class StringSplit {
	static int main (string[] args) {     
		string st = "a b c d . e";
		string[] starr = st.split(" ");
		
		stdout.printf("string st of len(%d): %s\n", st.len(), st);
		// should print "starr.length: 6" !!
		stdout.printf("starr.length: %d\n", starr.length);
		stdout.printf("starr[%d]: %s\n", 0, starr[0]);
		stdout.printf("starr[%d]: %s\n", 1, starr[1]);
		stdout.printf("starr[%d]: %s\n", 2, starr[2]);

		return 0;
	}
}


Actual results:


Expected results:


Does this happen every time?
Yes. For null-terminated arrays.

Other information:
Comment 1 Jürg Billeter 2008-02-04 20:03:48 UTC
Confirming.
Comment 2 rainwoodman 2008-05-01 20:42:04 UTC
If someone invent a syntax for the attribute, I'll be glad to make a patch.

can it be something like this?

NoArrayLength ( length = "g_strv_length" )
Comment 3 rainwoodman 2008-05-02 21:13:03 UTC
Created attachment 110288 [details] [review]
g_strv_length patch

Is it possible to expose g_strv_length as a temparory solution?
Comment 4 Jürg Billeter 2008-05-10 13:17:22 UTC
[CCode (array_null_terminated = true)] would make sense, in my opinion. We can bind g_strv_length, however, I'm not sure whether string.lengthv is the right method name, maybe just add it as a namespace method GLib.strv_length?
Comment 5 rainwoodman 2008-05-10 22:51:45 UTC
Yes, a namespace method will be better than a static class member.

BTW: array_null_terminated sounds perfect to replace NoArrayLength.
However, to specify in the CCode modifier which argument and or return value is  null_terminated is also neccesary.


Comment 6 Jürg Billeter 2008-05-11 06:50:02 UTC
Vala supports per-parameter attributes, you can just add [CCode (array_null_terminated = true)] in front of the parameter.
Comment 7 Jürg Billeter 2008-05-11 06:51:12 UTC
2008-05-11  Jürg Billeter  <j@bitron.ch>

	* vapi/glib-2.0.vapi: add g_strv_length binding

Added g_str_v_length binding in r1361.
Comment 8 rainwoodman 2008-08-22 06:39:53 UTC
Bug 548897 can ultimately solve this bug
Comment 9 Geert Jan Alsem 2008-11-12 19:08:40 UTC
After reading this bug report I understand that I can get the length of a null-terminated string array using strv_length.

But I think the fact that I have to use that function is very confusing, and it especially will be for beginners. Using array.length always worked for me, but after using string.split(), all of a sudden I have an array with length -1.

Couldn't the Vala compiler create C code in which g_strv_length() is used automatically to determine the length of the array of a string.split()?

For example:

  string[] s = x.split (" - ");

Compiles to:

  s = (_tmp0 = g_strsplit (x, " - ", 0), s_length1 = -1, _tmp0);

Is it possible to make it compile to:

  s = (_tmp0 = g_strsplit (x, " - ", 0), s_length1 = g_strv_length (_temp0), _tmp0);

Or would that cause other problems?
Comment 10 Jürg Billeter 2008-11-12 19:15:05 UTC
That's exactly the way we want to solve the bug.
Comment 11 Jürg Billeter 2008-12-18 00:08:01 UTC
*** Bug 562391 has been marked as a duplicate of this bug. ***
Comment 12 Jürg Billeter 2009-01-04 16:15:56 UTC
2009-01-04  Jürg Billeter  <j@bitron.ch>

	* vala/valadelegate.vala:
	* vala/valafield.vala:
	* vala/valaformalparameter.vala:
	* vala/valamethod.vala:
	* gobject/valaccodearraymodule.vala:
	* gobject/valaccodemethodcallmodule.vala:

	Support [CCode (array_null_terminated = true)] attribute to fix
	length handling of null terminated arrays, fixes bug 514186

	* vapi/glib-2.0.vapi:

	Fix g_strsplit binding

Fixed in r2267.