GNOME Bugzilla – Bug 514186
Array length for null-terminated arrays is always -1
Last modified: 2009-01-04 16:15:56 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:
Confirming.
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" )
Created attachment 110288 [details] [review] g_strv_length patch Is it possible to expose g_strv_length as a temparory solution?
[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?
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.
Vala supports per-parameter attributes, you can just add [CCode (array_null_terminated = true)] in front of the parameter.
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.
Bug 548897 can ultimately solve this bug
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?
That's exactly the way we want to solve the bug.
*** Bug 562391 has been marked as a duplicate of this bug. ***
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.